GPLIB++
addnoise.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, boost::normal_distribution<>(
39  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, boost::normal_distribution<>(
44  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  double relativenoiselevel, absolutenoiselevel;
53  MTStation Data;
54  boost::lagged_fibonacci607 generator(
55  static_cast<unsigned int> (std::time(0)));
56  complex<double> I(0.0, 1.0);
57  cout << "This is addnoise: Add random noise to MT impedance estimates."
58  << endl << endl;
59  cout << " Usage: addnoise infilename noiselevel" << endl;
60  cout
61  << " The output files will have the same name as the input files + .ns "
62  << endl << endl;
63  cout << " This is Version: " << version << endl << endl;
64  if (argc > 4)
65  {
66  infilename = argv[1];
67  outfilename = argv[2];
68  relativenoiselevel = atof(argv[3]);
69  absolutenoiselevel = atof(argv[4]);
70  }
71  else
72  {
73 
74  infilename = AskFilename("Infilename: ");
75  cout << "Relative Noiselevel:";
76  cin >> relativenoiselevel;
77  cout << "Absolute noiselevel:";
78  cin >> absolutenoiselevel;
79  outfilename = infilename + ".ns";
80  }
81 
82  Data.GetData(infilename);
83  double zxxerr, zxyerr, zyxerr, zyyerr;
84  for (unsigned int i = 0; i < Data.GetMTData().size(); ++i)
85  {
86  AddNoise(Data.SetMTData().at(i).SetZxx(), zxxerr, relativenoiselevel,
87  absolutenoiselevel, generator);
88  AddNoise(Data.SetMTData().at(i).SetZxy(), zxyerr, relativenoiselevel,
89  absolutenoiselevel, generator);
90  AddNoise(Data.SetMTData().at(i).SetZyx(), zyxerr, relativenoiselevel,
91  absolutenoiselevel, generator);
92  AddNoise(Data.SetMTData().at(i).SetZyy(), zyyerr, relativenoiselevel,
93  absolutenoiselevel, generator);
94  Data.SetMTData().at(i).SetErrors(zxxerr, zxyerr, zyxerr, zyyerr);
95  }
96  Data.WriteAsMtt(outfilename);
97  }
virtual void GetData(const std::string filename)
read in data from file, determines format by ending
Definition: MTStation.cpp:597
int main(int argc, char *argv[])
Definition: addnoise.cpp:49
The class MTStation is used to store the transfer functions and related information for a MT-site...
Definition: MTStation.h:17
string version
Definition: addnoise.cpp:22
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
std::vector< MTTensor > & SetMTData()
Get the full vector of Tensor elements for reading and writing.
Definition: MTStation.h:124
void AddNoise(std::complex< double > &impelement, double &noiseest, const double relativenoiselevel, const double absolutenoiselevel, boost::lagged_fibonacci607 &generator)
Definition: addnoise.cpp:24