GPLIB++
mtubandpass.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <fstream>
3 #include <string>
4 #include <numeric>
5 #include "Util.h"
6 #include "TimeSeriesData.h"
7 #include "statutils.h"
8 #include "FilterFunc.h"
9 
10 using namespace std;
11 using namespace gplib;
12 
13 string version = "$Id: mtubandpass.cpp 1816 2009-09-07 11:28:35Z mmoorkamp $";
14 
15 /*!
16  * \addtogroup UtilProgs Utility Programs
17  *@{
18  * \file
19  * Apply a band pass filter to each component of the MT time series. The program
20  * asks for the corner frequencies in Hz and the number of passes for the filter.
21  * The output filename is determined by appending .fil to the input filename.
22  */
23 
24 int main(int argc, char *argv[])
25  {
26  string infilename;
27  cout << "This is mtufilter: Apply a band pass filter to MT time series"
28  << endl << endl;
29  cout << " Usage: mtufilter infilename " << endl;
30  cout << " Ending '.fil' will be automatically assigned to outfilename"
31  << endl << endl;
32  cout << " This is Version: " << version << endl << endl;
33  if (argc == 2)
34  {
35  infilename = argv[1];
36  }
37  else
38  {
39 
40  infilename = AskFilename(" Mtu-Filename: ");
41  }
42 
43  TimeSeriesData Data;
44  Data.GetData(infilename);
45  // get the lower corner frequency
46  double lowfreq;
47  cout << "Lower Corner frequency [Hz]: ";
48  cin >> lowfreq;
49 
50  // get the lower corner frequency
51  double upfreq;
52  cout << "Upper Corner frequency [Hz]: ";
53  cin >> upfreq;
54 
55  // calculate the dimensionless corner frequency for the filtering class
56  const double lowfilfreq = lowfreq * Data.GetData().GetEx().GetSamplerate();
57  const double upfilfreq = upfreq * Data.GetData().GetEx().GetSamplerate();
58  // the more passes we do with this filter, the stronger the attenuation
59  size_t npass = 1;
60  cout << "Number of passes: ";
61  cin >> npass;
62 
63  // make sure the all components have zero mean
64  SubMean(Data.GetData().GetEx().GetData().begin(),
65  Data.GetData().GetEx().GetData().end());
66  SubMean(Data.GetData().GetEy().GetData().begin(),
67  Data.GetData().GetEy().GetData().end());
68  SubMean(Data.GetData().GetHx().GetData().begin(),
69  Data.GetData().GetHx().GetData().end());
70  SubMean(Data.GetData().GetHy().GetData().begin(),
71  Data.GetData().GetHy().GetData().end());
72  SubMean(Data.GetData().GetHz().GetData().begin(),
73  Data.GetData().GetHz().GetData().end());
74 
75  //we can do several passes of the filter to get strong attenuation of high frequencies
76  for (size_t i = 0; i < npass; ++i)
77  {
78  // apply the filter to one component and store the result in that component
79  transform(Data.GetData().GetEx().GetData().begin(),
80  Data.GetData().GetEx().GetData().end(),
81  Data.GetData().GetEx().GetData().begin(), SimpleBp(lowfilfreq,upfilfreq));
82  transform(Data.GetData().GetEy().GetData().begin(),
83  Data.GetData().GetEy().GetData().end(),
84  Data.GetData().GetEy().GetData().begin(), SimpleBp(lowfilfreq,upfilfreq));
85  transform(Data.GetData().GetHx().GetData().begin(),
86  Data.GetData().GetHx().GetData().end(),
87  Data.GetData().GetHx().GetData().begin(), SimpleBp(lowfilfreq,upfilfreq));
88  transform(Data.GetData().GetHy().GetData().begin(),
89  Data.GetData().GetHy().GetData().end(),
90  Data.GetData().GetHy().GetData().begin(), SimpleBp(lowfilfreq,upfilfreq));
91  transform(Data.GetData().GetHz().GetData().begin(),
92  Data.GetData().GetHz().GetData().end(),
93  Data.GetData().GetHz().GetData().begin(), SimpleBp(lowfilfreq,upfilfreq));
94  }
95  // write data in the same format that we read it in
96  Data.WriteBack(infilename + ".fil");
97  }
98 /*@}*/
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
void SubMean(InputIterator begin, InputIterator end, typename std::iterator_traits< InputIterator >::value_type mean)
Substract the mean from each element in the container, mean is passed as a parameter.
Definition: statutils.h:85
TimeSeriesComponent & GetHy()
Definition: TimeSeries.h:39
int main()
Definition: angleavg.cpp:12
void WriteBack(std::string filename_base)
Write in the format it was originally read in.
string version
Definition: mtubandpass.cpp:13
TimeSeriesData stores a pointer to the different components of magnetotelluric data and provides func...
double GetSamplerate() const
Return samplerate in Hz.
TimeSeriesComponent & GetEy()
Definition: TimeSeries.h:51
TimeSeriesComponent & GetHz()
Definition: TimeSeries.h:43
TimeSeriesComponent & GetHx()
Access function for Hx, returns reference for efficiency.
Definition: TimeSeries.h:35