addnoise.cpp

Go to the documentation of this file.
00001 /*! \file
00002  * This program adds gaussian random noise to the impedance estimates taken
00003  * from infilename and writes a new ".mtt" file with the noisy data and updated error
00004  * information. The output file has the same name as the input file with ".ns" appended.
00005  * The noise level is the relative noise for real and imaginary parts of the impedance at
00006  * each frequency.
00007  */
00008 
00009 #include <iostream>
00010 #include <complex>
00011 #include <ctime>
00012 #include <cmath>
00013 #include "MTStation.h"
00014 #include "Util.h"
00015 #include <boost/random/lagged_fibonacci.hpp>
00016 #include <boost/random/normal_distribution.hpp>
00017 #include <boost/random/variate_generator.hpp>
00018 
00019 using namespace std;
00020 using namespace gplib;
00021 
00022 string version = "$Id: addnoise.cpp 1844 2010-04-12 11:34:25Z mmoorkamp $";
00023 
00024 void AddNoise(std::complex<double> &impelement, double &noiseest,
00025     const double relativenoiselevel, const double absolutenoiselevel,
00026     boost::lagged_fibonacci607 &generator)
00027   {
00028     //save the original impedance element
00029     complex<double> oldimp(impelement);
00030     //determine the noise level for the real part
00031     double realnoiselevel = max(std::abs(impelement.real() * relativenoiselevel),
00032         absolutenoiselevel);
00033     //and the imaginary part
00034     double imagnoiselevel = max(std::abs(impelement.imag() * relativenoiselevel),
00035         absolutenoiselevel);
00036     //draw a sample from a normal distribution for the real part
00037     impelement = boost::variate_generator<boost::lagged_fibonacci607&,
00038     boost::normal_distribution<> >(generator, boost::normal_distribution<>(
00039         impelement.real(), realnoiselevel))();
00040     //and for the imaginary part
00041     impelement += complex<double> (0.0, 1.0)
00042         * boost::variate_generator<boost::lagged_fibonacci607&,
00043         boost::normal_distribution<> >(generator, boost::normal_distribution<>(
00044             oldimp.imag(), imagnoiselevel))();
00045 
00046     noiseest = max(relativenoiselevel * abs(oldimp), absolutenoiselevel);
00047   }
00048 
00049 int main(int argc, char *argv[])
00050   {
00051     string infilename, outfilename;
00052     double relativenoiselevel, absolutenoiselevel;
00053     MTStation Data;
00054     boost::lagged_fibonacci607 generator(
00055         static_cast<unsigned int> (std::time(0)));
00056     complex<double> I(0.0, 1.0);
00057     cout << "This is addnoise: Add random noise to MT impedance estimates."
00058         << endl << endl;
00059     cout << " Usage: addnoise  infilename  noiselevel" << endl;
00060     cout
00061         << " The output files will have the same name as the input files + .ns "
00062         << endl << endl;
00063     cout << " This is Version: " << version << endl << endl;
00064     if (argc > 4)
00065       {
00066         infilename = argv[1];
00067         outfilename = argv[2];
00068         relativenoiselevel = atof(argv[3]);
00069         absolutenoiselevel = atof(argv[4]);
00070       }
00071     else
00072       {
00073 
00074         infilename = AskFilename("Infilename: ");
00075         cout << "Relative Noiselevel:";
00076         cin >> relativenoiselevel;
00077         cout << "Absolute noiselevel:";
00078         cin >> absolutenoiselevel;
00079         outfilename = infilename + ".ns";
00080       }
00081 
00082     Data.GetData(infilename);
00083     double zxxerr, zxyerr, zyxerr, zyyerr;
00084     for (unsigned int i = 0; i < Data.GetMTData().size(); ++i)
00085       {
00086         AddNoise(Data.SetMTData().at(i).SetZxx(), zxxerr, relativenoiselevel,
00087             absolutenoiselevel, generator);
00088         AddNoise(Data.SetMTData().at(i).SetZxy(), zxyerr, relativenoiselevel,
00089             absolutenoiselevel, generator);
00090         AddNoise(Data.SetMTData().at(i).SetZyx(), zyxerr, relativenoiselevel,
00091             absolutenoiselevel, generator);
00092         AddNoise(Data.SetMTData().at(i).SetZyy(), zyyerr, relativenoiselevel,
00093             absolutenoiselevel, generator);
00094         Data.SetMTData().at(i).SetErrors(zxxerr, zxyerr, zyxerr, zyyerr);
00095       }
00096     Data.WriteAsMtt(outfilename);
00097   }

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