GPLIB++
mtupspec.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <string>
3 #include <fstream>
4 #include <vector>
5 #include "StackedSpectrum.h"
6 #include "WFunc.h"
7 #include "TimeSeriesData.h"
8 #include "Util.h"
9 
10 using namespace std;
11 using namespace gplib;
12 
13 string version = "$Id: mtupspec.cpp 1857 2010-11-03 14:55:32Z mmoorkamp $";
14 
15 /*!
16  * \addtogroup UtilProgs Utility Programs
17  *@{
18  * \file
19  * Calculate the power spectra for the 4 horizontal components in a MT time-series.
20  * Output is in ascii format for plotting with xmgrace etc.
21  */
22 
23 //! Calculate the power spectrum and write it to an ascii file
24 void CalcPSpecAndWrite(ttsdata Data, const double samplerate,
25  const int seglength, const string name)
26  {
27  vector<complex<double> > Spectrum(seglength / 2 + 1);
28  StackedSpectrum(Data.begin(), Data.end(), Spectrum.begin(), seglength,
29  Hanning());
30  ofstream outfile(name.c_str());
31  for (unsigned int i = 1; i < Spectrum.size(); ++i) //we do not output the static contribution (0 frequency)
32  outfile << i * samplerate / seglength << " " << abs(Spectrum.at(i))
33  << endl;
34  }
35 
36 int main(int argc, char *argv[])
37  {
38  TimeSeriesData TsData;
39 
40  string infilename;
41  size_t seglength = 2400;
42  cout
43  << "This is mtupspec: Calculate power spectra from Phoenix time series"
44  << endl << endl;
45  cout
46  << " Usage mtupspec filename if no filename given, the program will ask for one"
47  << endl;
48  cout << " Output will be 4 ascii file with ending _specex, _specey etc."
49  << endl << endl;
50  cout << " This is Version: " << version << endl << endl;
51 
52  //get the filename information
53  if (argc == 2)
54  {
55  infilename = argv[1];
56  }
57  else
58  {
59  infilename = AskFilename("Infilename: ");
60  }
61 
62  //get the length of th individual segments for the fouries transform
63  cout << "Length for each segment: ";
64  cin >> seglength;
65  // we need at least 2 points in each segment
66  if (seglength < 2)
67  {
68  std::cerr << "Segment must be longer than 2 !";
69  return 100;
70  }
71  // the segment length cannot be longer than the time series
72  if (seglength > TsData.GetData().GetEx().GetData().size())
73  {
74  std::cerr << "Segment must shorter than the time series !";
75  return 200;
76  }
77  TsData.GetData(infilename);
78 
79  const double samplerate = TsData.GetData().GetSamplerate();
80  //calculate the power spectra for each component and write them to a file
81  CalcPSpecAndWrite(TsData.GetData().GetEx().GetData(), samplerate,
82  seglength, (infilename + "_specex"));
83  CalcPSpecAndWrite(TsData.GetData().GetEy().GetData(), samplerate,
84  seglength, (infilename + "_specey"));
85  CalcPSpecAndWrite(TsData.GetData().GetHx().GetData(), samplerate,
86  seglength, (infilename + "_spechx"));
87  CalcPSpecAndWrite(TsData.GetData().GetHy().GetData(), samplerate,
88  seglength, (infilename + "_spechy"));
89  CalcPSpecAndWrite(TsData.GetData().GetHz().GetData(), samplerate,
90  seglength, (infilename + "_spechz"));
91 
92  }
93 /*@}*/
std::vector< double > & GetData()
Access for data vector, for ease of use and efficiency we return a reference.
TimeSeries & GetData()
return a reference to the actual object stored in the pointer
TimeSeriesComponent & GetEx()
Definition: TimeSeries.h:47
string version
Definition: mtupspec.cpp:13
double GetSamplerate()
The samplerate is stored in each component, we just return the samplerate of Hx assuming they are all...
Definition: TimeSeries.h:71
TimeSeriesComponent & GetHy()
Definition: TimeSeries.h:39
int main()
Definition: angleavg.cpp:12
TimeSeriesData stores a pointer to the different components of magnetotelluric data and provides func...
void CalcPSpecAndWrite(ttsdata Data, const double samplerate, const int seglength, const string name)
Calculate the power spectrum and write it to an ascii file.
Definition: mtupspec.cpp:24
This functor returns the weighting factor for the Hanning window, given the relative position (0...
Definition: WFunc.h:31
TimeSeriesComponent & GetEy()
Definition: TimeSeries.h:51
TimeSeriesComponent & GetHz()
Definition: TimeSeries.h:43
void StackedSpectrum(InputIterator tsbegin, InputIterator tsend, OutputIterator freqbegin, const size_t seglength, WindowFunctype WFunc)
This template is used to calculate stacked spectra for example for power spectrum estimation...
TimeSeriesComponent & GetHx()
Access function for Hx, returns reference for efficiency.
Definition: TimeSeries.h:35