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 "MTStation.h"
00013 #include "Util.h"
00014 #include <boost/random/lagged_fibonacci.hpp>
00015 #include <boost/random/normal_distribution.hpp>
00016 #include <boost/random/variate_generator.hpp>
00017 
00018 using namespace std;
00019 using namespace gplib;
00020 
00021 string version = "$Id: addnoise.cpp 1816 2009-09-07 11:28:35Z mmoorkamp $";
00022 
00023 void AddNoise(std::complex<double> &impelement, double &noiseest,
00024     const double relativenoiselevel, const double absolutenoiselevel,
00025     boost::lagged_fibonacci607 &generator)
00026   {
00027     //save the original impedance element
00028     complex<double> oldimp(impelement);
00029     //determine the noise level for the real part
00030     double realnoiselevel = max(fabs(impelement.real() * relativenoiselevel),
00031         absolutenoiselevel);
00032     //and the imaginary part
00033     double imagnoiselevel = max(fabs(impelement.imag() * relativenoiselevel),
00034         absolutenoiselevel);
00035     //draw a sample from a normal distribution for the real part
00036     impelement = boost::variate_generator<boost::lagged_fibonacci607&,
00037     boost::normal_distribution<> >(generator, boost::normal_distribution<>(
00038         impelement.real(), realnoiselevel))();
00039     //and for the imaginary part
00040     impelement += complex<double> (0.0, 1.0)
00041         * boost::variate_generator<boost::lagged_fibonacci607&,
00042         boost::normal_distribution<> >(generator, boost::normal_distribution<>(
00043             oldimp.imag(), imagnoiselevel))();
00044 
00045     noiseest = max(relativenoiselevel * abs(oldimp), absolutenoiselevel);
00046   }
00047 
00048 int main(int argc, char *argv[])
00049   {
00050     string infilename, outfilename;
00051     double relativenoiselevel, absolutenoiselevel;
00052     MTStation Data;
00053     boost::lagged_fibonacci607 generator(
00054         static_cast<unsigned int> (std::time(0)));
00055     complex<double> I(0.0, 1.0);
00056     cout << "This is addnoise: Add random noise to MT impedance estimates."
00057         << endl << endl;
00058     cout << " Usage: addnoise  infilename  noiselevel" << endl;
00059     cout
00060         << " The output files will have the same name as the input files + .ns "
00061         << endl << endl;
00062     cout << " This is Version: " << version << endl << endl;
00063     if (argc > 4)
00064       {
00065         infilename = argv[1];
00066         outfilename = argv[2];
00067         relativenoiselevel = atof(argv[3]);
00068         absolutenoiselevel = atof(argv[4]);
00069       }
00070     else
00071       {
00072 
00073         infilename = AskFilename("Infilename: ");
00074         cout << "Relative Noiselevel:";
00075         cin >> relativenoiselevel;
00076         cout << "Absolute noiselevel:";
00077         cin >> absolutenoiselevel;
00078         outfilename = infilename + ".ns";
00079       }
00080 
00081     Data.GetData(infilename);
00082     double zxxerr, zxyerr, zyxerr, zyyerr;
00083     for (unsigned int i = 0; i < Data.GetMTData().size(); ++i)
00084       {
00085         AddNoise(Data.SetMTData().at(i).SetZxx(), zxxerr, relativenoiselevel,
00086             absolutenoiselevel, generator);
00087         AddNoise(Data.SetMTData().at(i).SetZxy(), zxyerr, relativenoiselevel,
00088             absolutenoiselevel, generator);
00089         AddNoise(Data.SetMTData().at(i).SetZyx(), zyxerr, relativenoiselevel,
00090             absolutenoiselevel, generator);
00091         AddNoise(Data.SetMTData().at(i).SetZyy(), zyyerr, relativenoiselevel,
00092             absolutenoiselevel, generator);
00093         Data.SetMTData().at(i).SetErrors(zxxerr, zxyerr, zyxerr, zyyerr);
00094       }
00095     Data.WriteAsMtt(outfilename);
00096   }

Generated on Tue Nov 3 13:24:13 2009 for GPLIB++ by  doxygen 1.5.8