GPLIB++
seisnoise.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <string>
3 #include "SeismicDataComp.h"
4 #include <algorithm>
5 #include "Util.h"
6 #include <boost/random/lagged_fibonacci.hpp>
7 #include <boost/random/normal_distribution.hpp>
8 #include <boost/random/variate_generator.hpp>
9 #include <boost/bind.hpp>
10 
11 using namespace std;
12 using namespace gplib;
13 
14 /*! \file
15  * This program adds random gaussian noise to a single sac file. The noise
16  * level is specified relative to the maximum amplitude in the time series. The noise
17  * is completely random and uncorrelated between datapoints. This might not be very
18  * typical for seismic noise that is often signal generated and has characteristic
19  * frequencies, but seems to be sufficient to perform basic tests on synthetic data.
20  */
21 int main(int argc, char* argv[])
22  {
23  SeismicDataComp Data;
24  string datafilename, outfilename;
25  double relnoise, absnoise;
26 
27  string version = "$Id: seisnoise.cpp 1882 2014-06-12 08:15:20Z mmoorkamp $";
28  cout << endl << endl;
29  cout << "Program " << version << endl;
30  cout << " Add random noise to a seismogram" << endl;
31  cout << " Input is a single component file, output a sac file with added noise"
32  << endl;
33  cout
34  << " The noise level is specified relative to the maximum amplitude and as a minimum absolute value."
35  << endl;
36  cout << endl << endl;
37  if (argc > 4)
38  {
39  datafilename = argv[1];
40  outfilename = argv[2];
41  relnoise = atof(argv[3]);
42  absnoise = atof(argv[4]);
43  }
44  else
45  {
46  datafilename = AskFilename("Input filename: ");
47  outfilename = AskFilename("Output filename: ");
48  cout << "Relative Noise level: ";
49  cin >> relnoise;
50  cout << "Absolute Noise level: ";
51  cin >> absnoise;
52  }
53  Data.ReadData(datafilename);
54  const double maxelem = *max_element(Data.GetData().begin(), Data.GetData().end());
55  boost::lagged_fibonacci607 generator(static_cast<unsigned int>(std::time(0)));
56  boost::normal_distribution<> noise_dist(0.0, std::max(relnoise * maxelem, absnoise));
57  boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > noise(
58  generator, noise_dist);
59 
60  //add noise to each point of the time series
61  transform(Data.GetData().begin(), Data.GetData().end(), Data.GetData().begin(),
62  boost::bind(plus<double>(), _1, boost::bind(noise)));
63  Data.WriteAsSac(outfilename);
64  }
int ReadData(const std::string &filename, tseismicdataformat format=sac)
Read in data from a file, as we cannot determine the type from the ending we have to provide it...
std::vector< double > & GetData()
Access for data vector, for ease of use and efficiency we return a reference.
string version
Definition: makeinput.cpp:16
int main(int argc, char *argv[])
Definition: seisnoise.cpp:21
int WriteAsSac(const std::string &filename) const
Write the data in sac binary format.