mtupspec.cpp

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <string>
00003 #include <fstream>
00004 #include <vector>
00005 #include "StackedSpectrum.h"
00006 #include "WFunc.h"
00007 #include "TimeSeriesData.h"
00008 #include "Util.h"
00009 
00010 using namespace std;
00011 using namespace gplib;
00012 
00013 string version = "$Id: mtupspec.cpp 1816 2009-09-07 11:28:35Z mmoorkamp $";
00014 
00015 /*!
00016  * \addtogroup UtilProgs Utility Programs
00017  *@{
00018  * \file
00019  * Calculate the power spectra for the 4 horizontal components in a MT time-series.
00020  * Output is in ascii format for plotting with xmgrace etc.
00021  */
00022 
00023 //! Calculate the power spectrum and write it to an ascii file
00024 void CalcPSpecAndWrite(ttsdata Data, const double samplerate,
00025     const int seglength, const string name)
00026   {
00027     vector<complex<double> > Spectrum(seglength / 2 + 1);
00028     StackedSpectrum(Data.begin(), Data.end(), Spectrum.begin(), seglength,
00029         Hanning());
00030     ofstream outfile(name.c_str());
00031     for (unsigned int i = 1; i < Spectrum.size(); ++i) //we do not output the static contribution (0 frequency)
00032       outfile << i * samplerate / seglength << " " << abs(Spectrum.at(i))
00033           << endl;
00034   }
00035 
00036 int main(int argc, char *argv[])
00037   {
00038     TimeSeriesData TsData;
00039 
00040     string infilename;
00041     int seglength = 2400;
00042     cout
00043         << "This is mtupspec: Calculate power spectra from  Phoenix time series"
00044         << endl << endl;
00045     cout
00046         << " Usage      mtupspec filename     if no filename given, the program will ask for one"
00047         << endl;
00048     cout << " Output will be 4 ascii file with ending _specex, _specey etc."
00049         << endl << endl;
00050     cout << " This is Version: " << version << endl << endl;
00051 
00052     //get the filename information
00053     if (argc == 2)
00054       {
00055         infilename = argv[1];
00056       }
00057     else
00058       {
00059         infilename = AskFilename("Infilename: ");
00060       }
00061 
00062     //get the length of th individual segments for the fouries transform
00063     cout << "Length for each segment: ";
00064     cin >> seglength;
00065     // we need at least 2 points in each segment
00066     if (seglength < 2)
00067       {
00068         std::cerr << "Segment must be longer than 2 !";
00069         return 100;
00070       }
00071     // the segment length cannot be longer than the time series
00072     if (seglength > TsData.GetData().GetEx().GetData().size())
00073       {
00074         std::cerr << "Segment must shorter than the time series !";
00075         return 200;
00076       }
00077     TsData.GetData(infilename);
00078 
00079     const double samplerate = TsData.GetData().GetSamplerate();
00080     //calculate the power spectra for each component and write them to a file
00081     CalcPSpecAndWrite(TsData.GetData().GetEx().GetData(), samplerate,
00082         seglength, (infilename + "_specex"));
00083     CalcPSpecAndWrite(TsData.GetData().GetEy().GetData(), samplerate,
00084         seglength, (infilename + "_specey"));
00085     CalcPSpecAndWrite(TsData.GetData().GetHx().GetData(), samplerate,
00086         seglength, (infilename + "_spechx"));
00087     CalcPSpecAndWrite(TsData.GetData().GetHy().GetData(), samplerate,
00088         seglength, (infilename + "_spechy"));
00089     CalcPSpecAndWrite(TsData.GetData().GetHz().GetData(), samplerate,
00090         seglength, (infilename + "_spechz"));
00091 
00092   }
00093 /*@}*/

Generated on Tue May 4 16:52:14 2010 for GPLIB++ by  doxygen 1.5.8