mtutimefrequency.cpp
Go to the documentation of this file.00001 #include <iostream>
00002 #include <string>
00003 #include <fstream>
00004 #include <vector>
00005 #include "TimeFrequency.h"
00006 #include "WFunc.h"
00007 #include "TimeSeriesData.h"
00008 #include "netcdfcpp.h"
00009 #include "VecMat.h"
00010 #include "Util.h"
00011
00012 using namespace std;
00013 using namespace gplib;
00014
00015 string version =
00016 "$Id: mtutimefrequency.cpp 1816 2009-09-07 11:28:35Z mmoorkamp $";
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 void CalcTfAndWrite(ttsdata Data, const double samplerate, const int seglength,
00027 const int timeavg, const string name)
00028 {
00029
00030 gplib::cmat result = TimeFrequency(Data.begin(), Data.end(), seglength,
00031 Steep());
00032 const size_t ylength = result.size2() - timeavg;
00033 NcFile combrescdf(name.c_str(), NcFile::Replace);
00034 NcDim* xd = combrescdf.add_dim("x", result.size1());
00035 NcDim* yd = combrescdf.add_dim("y", ylength);
00036 NcVar* x = combrescdf.add_var("x", ncFloat, xd);
00037 NcVar* y = combrescdf.add_var("y", ncFloat, yd);
00038 NcVar* z = combrescdf.add_var("z", ncFloat, xd, yd);
00039 float *xvals = new float[xd->size()];
00040 float *yvals = new float[yd->size()];
00041 float *zvals = new float[xd->size() * yd->size()];
00042 float avg = 0.0;
00043 for (int i = 0; i < result.size1(); ++i)
00044 {
00045 xvals[i] = i;
00046
00047 for (int j = 0; j < ylength; ++j)
00048 {
00049 avg = 0.0;
00050 for (int k = 0; k < timeavg; ++k)
00051 {
00052 avg += abs(result(i, j + k));
00053 }
00054 zvals[i * ylength + j] = log10(avg);
00055 }
00056
00057 }
00058 for (int j = 0; j < ylength; ++j)
00059 yvals[j] = j * samplerate / seglength;
00060 x->put(xvals, xd->size());
00061 y->put(yvals, yd->size());
00062 z->put(zvals, z->edges());
00063 }
00064
00065 int main(int argc, char *argv[])
00066 {
00067 TimeSeriesData TsData;
00068
00069 string infilename;
00070 size_t timeavg = 5;
00071 size_t seglength = 2400;
00072 cout
00073 << "This is mtutf: Calculate time frequency matrix from Phoenix time series"
00074 << endl << endl;
00075 cout << " Usage: mtutf filename" << endl;
00076 cout << "if no filename given, the program will ask for one" << endl;
00077 cout
00078 << " Output will be 4 netcdf files with ending _tfex.nc, _tfey.nc etc."
00079 << endl << endl;
00080 cout << " This is Version: " << version << endl << endl;
00081
00082 if (argc == 2)
00083 {
00084 infilename = argv[1];
00085 }
00086 else
00087 {
00088 cout << "Infilename: ";
00089 cin >> infilename;
00090 }
00091 TsData.GetData(infilename);
00092 cout << "Length of individual segments in points: ";
00093 cin >> seglength;
00094 cout << "Number of segments for time averaging: ";
00095 cin >> timeavg;
00096 const double samplerate = TsData.GetData().GetSamplerate();
00097 CalcTfAndWrite(TsData.GetData().GetEx().GetData(), samplerate, seglength,
00098 timeavg, (infilename + "_tfex.nc"));
00099 CalcTfAndWrite(TsData.GetData().GetEy().GetData(), samplerate, seglength,
00100 timeavg, (infilename + "_tfey.nc"));
00101 CalcTfAndWrite(TsData.GetData().GetHx().GetData(), samplerate, seglength,
00102 timeavg, (infilename + "_tfhx.nc"));
00103 CalcTfAndWrite(TsData.GetData().GetHy().GetData(), samplerate, seglength,
00104 timeavg, (infilename + "_tfhy.nc"));
00105
00106 }
00107