GPLIB++
shiftmt.cpp
Go to the documentation of this file.
1 /*! \file
2  * This program adds gaussian random noise to the impedance estimates taken
3  * from infilename and writes a new ".mtt" file with the noisy data and updated error
4  * information. The output file has the same name as the input file with ".ns" appended.
5  * The noise level is the relative noise for real and imaginary parts of the impedance at
6  * each frequency.
7  */
8 
9 #include <iostream>
10 #include <complex>
11 #include <ctime>
12 #include <cmath>
13 #include "MTStation.h"
14 #include "Util.h"
15 #include <boost/random/lagged_fibonacci.hpp>
16 #include <boost/random/normal_distribution.hpp>
17 #include <boost/random/variate_generator.hpp>
18 
19 using namespace std;
20 using namespace gplib;
21 
22 string version = "$Id: addnoise.cpp 1844 2010-04-12 11:34:25Z mmoorkamp $";
23 
24 void AddNoise(std::complex<double> &impelement, double &noiseest,
25  const double relativenoiselevel, const double absolutenoiselevel,
26  boost::lagged_fibonacci607 &generator)
27  {
28  //save the original impedance element
29  complex<double> oldimp(impelement);
30  //determine the noise level for the real part
31  double realnoiselevel = max(std::abs(impelement.real() * relativenoiselevel),
32  absolutenoiselevel);
33  //and the imaginary part
34  double imagnoiselevel = max(std::abs(impelement.imag() * relativenoiselevel),
35  absolutenoiselevel);
36  //draw a sample from a normal distribution for the real part
37  impelement = boost::variate_generator<boost::lagged_fibonacci607&,
38  boost::normal_distribution<> >(generator,
39  boost::normal_distribution<>(impelement.real(), realnoiselevel))();
40  //and for the imaginary part
41  impelement += complex<double>(0.0, 1.0)
42  * boost::variate_generator<boost::lagged_fibonacci607&,
43  boost::normal_distribution<> >(generator,
44  boost::normal_distribution<>(oldimp.imag(), imagnoiselevel))();
45 
46  noiseest = max(relativenoiselevel * abs(oldimp), absolutenoiselevel);
47  }
48 
49 int main(int argc, char *argv[])
50  {
51  string infilename, outfilename;
52  MTStation Data;
53  boost::lagged_fibonacci607 generator(static_cast<unsigned int>(std::time(0)));
54 
55  cout << "This is addnoise: Add random noise to MT impedance estimates." << endl
56  << endl;
57  cout << " Usage: addnoise infilename noiselevel" << endl;
58  cout << " The output files will have the same name as the input files + .ns " << endl
59  << endl;
60  cout << " This is Version: " << version << endl << endl;
61 
62  infilename = AskFilename("Infilename: ");
63  double minperiod;
64  cout << "Minimum period:";
65  cin >> minperiod;
66  double shiftfactor;
67  cout << "Shiftfactor:";
68  cin >> shiftfactor;
69  outfilename = infilename + ".rhons";
70 
71  Data.GetData(infilename);
72  for (unsigned int i = 0; i < Data.GetMTData().size(); ++i)
73  {
74  if (Data.GetFrequencies().at(i) < 1.0 / minperiod)
75  {
76  Data.SetMTData().at(i).SetZxx() *= shiftfactor;
77  Data.SetMTData().at(i).SetZxy() *= shiftfactor;
78  Data.SetMTData().at(i).SetZyx() *= shiftfactor;
79  Data.SetMTData().at(i).SetZyy() *= shiftfactor;
80  }
81  }
82  Data.WriteAsMtt(outfilename);
83  Data.GetData(infilename);
84  outfilename = infilename + ".phins";
85  for (unsigned int i = 0; i < Data.GetMTData().size(); ++i)
86  {
87  if (Data.GetFrequencies().at(i) < 1.0 / minperiod)
88  {
89  dcomp Zxx = Data.SetMTData().at(i).SetZxx();
90  dcomp Zxy = Data.SetMTData().at(i).SetZxy();
91  dcomp Zyx = Data.SetMTData().at(i).SetZyx();
92  dcomp Zyy = Data.SetMTData().at(i).SetZyy();
93  Data.SetMTData().at(i).SetZxx() = abs(Zxx)
94  * dcomp(cos(shiftfactor), sin(shiftfactor));
95  Data.SetMTData().at(i).SetZxy() = abs(Zxy)
96  * dcomp(cos(shiftfactor), sin(shiftfactor));
97  Data.SetMTData().at(i).SetZyx() = abs(Zyx)
98  * dcomp(cos(shiftfactor), sin(shiftfactor));
99  Data.SetMTData().at(i).SetZyy() = abs(Zyy)
100  * dcomp(cos(shiftfactor), sin(shiftfactor));
101 
102  }
103  }
104  Data.WriteAsMtt(outfilename);
105  }
int main(int argc, char *argv[])
Definition: shiftmt.cpp:49
virtual void GetData(const std::string filename)
read in data from file, determines format by ending
Definition: MTStation.cpp:597
void AddNoise(std::complex< double > &impelement, double &noiseest, const double relativenoiselevel, const double absolutenoiselevel, boost::lagged_fibonacci607 &generator)
Definition: shiftmt.cpp:24
The class MTStation is used to store the transfer functions and related information for a MT-site...
Definition: MTStation.h:17
void WriteAsMtt(const std::string filename)
Write data in goettingen .mtt format.
Definition: MTStation.cpp:681
const std::vector< MTTensor > & GetMTData() const
Get the full vector of Tensor elements read only.
Definition: MTStation.h:119
string version
Definition: shiftmt.cpp:22
std::vector< MTTensor > & SetMTData()
Get the full vector of Tensor elements for reading and writing.
Definition: MTStation.h:124
trealdata GetFrequencies() const
return the available frequencies in a single vector
Definition: MTStation.cpp:136