6 #include <boost/shared_ptr.hpp>
7 #include <boost/date_time/posix_time/posix_time.hpp>
8 #include <boost/program_options.hpp>
14 #include "netcdfcpp.h"
19 using namespace gplib;
21 string version =
"$Id: mtuadaptive.cpp 1838 2010-03-04 16:19:34Z mmoorkamp $";
23 void GetNNSetup(
const size_t filterlength,
const size_t hiddenlayers,
const size_t ntimeseries,
27 SigmoidalNeuron::bipolar);
28 for (
size_t i = 0; i < hiddenlayers; ++i)
30 NNLayers.push_back(typeVector);
32 typeVector.assign(1, SigmoidalNeuron::identity);
33 NNLayers.push_back(typeVector);
36 void WriteNetCDF(
const string name,
const boost::numeric::ublas::matrix<
double,
37 boost::numeric::ublas::column_major> &Matrix)
39 NcFile mtrescdf(name.c_str(), NcFile::Replace);
40 NcDim* xd = mtrescdf.add_dim(
"x", Matrix.size1());
41 NcDim* yd = mtrescdf.add_dim(
"y", Matrix.size2());
42 NcVar* x = mtrescdf.add_var(
"x", ncFloat, xd);
43 NcVar* y = mtrescdf.add_var(
"y", ncFloat, yd);
44 NcVar* z = mtrescdf.add_var(
"z", ncFloat, xd, yd);
45 float *xvals =
new float[xd->size()];
46 float *yvals =
new float[yd->size()];
47 float *zvals =
new float[xd->size() * yd->size()];
48 for (
size_t i = 0; i < Matrix.size1(); ++i)
51 for (
size_t j = 0; j < Matrix.size2(); ++j)
54 zvals[(Matrix.size1() - 1 - i) * Matrix.size2() + j] = Matrix(i, j);
57 x->put(xvals, xd->size());
58 y->put(yvals, yd->size());
59 z->put(zvals, z->edges());
62 namespace po = boost::program_options;
64 int main(
int argc,
char *argv[])
67 <<
"This is mtuadaptive: Apply an adaptive filter (LMS or RLS) to cancel noise in MT time series"
70 <<
" The program will ask for reference and input filename, further settings are read from 'mtuadaptive.conf' "
73 <<
" Output will be 2 Phoenix format files with ending '.clean' and '.eps' where the chosen channel is overwritten"
75 cout <<
" This is Version: " <<
version << endl << endl;
77 int filterlength = 0, shift = 0, hiddenlayers;
78 double mu, lambda, alpha, delta;
80 po::options_description desc(
"General options");
81 desc.add_options()(
"help",
"produce help message")(
"filterlength",
82 po::value<int>(&filterlength)->default_value(10),
83 "The length of the adaptive filter")(
"shift",
84 po::value<int>(&shift)->default_value(0),
85 "The shift in samples between the time series")(
"mu",
86 po::value<double>(&mu)->default_value(1.0),
87 "Stepsize for LMS adaptive filter")(
"lambda",
88 po::value<double>(&lambda)->default_value(1.0),
"")(
"alpha", po::value<
89 double>(&alpha)->default_value(1.0),
"")(
"delta", po::value<double>(
90 &delta)->default_value(1.0),
"")(
"hiddenlayers",
91 po::value<int>(&hiddenlayers)->default_value(1),
92 "The number of hiddenlayers for the neural network");
95 po::store(po::parse_command_line(argc, argv, desc), vm);
100 std::cout << desc <<
"\n";
107 string tsfilename, noisefilename;
111 int filterchoice = -1;
112 cout <<
"Which type of adaptive filter do you want ?" << endl;
113 cout <<
" 1: LMS" << endl;
114 cout <<
" 2: RLS" << endl;
115 cout <<
" 3: AMRLS" << endl;
116 cout <<
" 4: NN" << endl;
119 cout <<
"Reference Data: ";
120 cin >> noisefilename;
121 ReferenceData.
GetData(noisefilename);
122 cout <<
"Component Index (Hx,Hy,Ex,Ey): ";
124 cout <<
"Input Time Series Filename: ";
127 cout <<
"Input Start time: " << InputData.
GetData().
GetTime().front()
129 cout <<
"Reference Start time: "
134 cerr <<
"Time series not synchronized !" << endl;
141 cout <<
"Removing " << lengthdiff
142 <<
" datapoints from Reference time-series." << endl;
148 cout <<
"Removing " << lengthdiff
149 <<
" datapoints from Input time-series." << endl;
153 cout <<
"Input End time: " << InputData.
GetData().
GetTime().back() << endl;
154 cout <<
"Reference End time: " << ReferenceData.
GetData().
GetTime().back()
173 cerr <<
"Component index not valid !";
178 cout <<
"Number of input channels: ";
183 switch (filterchoice)
194 delta, lambda, alpha);
197 GetNNSetup(filterlength, hiddenlayers, ntimeseries, NNLayers, NNmaxinit);
199 mu, NNLayers, NNmaxinit);
202 cerr <<
"Invalid number input, don't know what to do and will exit ! ";
209 for (
int i = 0; i < ntimeseries; ++i)
211 cout <<
"Component Index (Hx,Hy,Ex,Ey): ";
228 cerr <<
"Component index not valid !";
237 cout <<
" First iteration: " << endl << endl;
241 cout << endl << endl <<
" Second iteration: " << endl << endl;
248 ReferenceData.
WriteBack(noisefilename +
".clean");
249 ofstream cleanfile((noisefilename +
".clean").c_str());
251 Canceller.
GetOutChannels().front()->GetData().end(), ostream_iterator<
252 double> (cleanfile,
"\n"));
256 ReferenceData.
WriteBack(noisefilename +
".eps");
257 ofstream epsfile((noisefilename +
".eps").c_str());
259 Canceller.
GetEpsValues().front().end(), ostream_iterator<double> (
const std::vector< std::vector< double > > & GetEpsValues()
Return the vector of channel approximation errors.
std::vector< double > & GetData()
Access for data vector, for ease of use and efficiency we return a reference.
void AddReferenceChannel(TimeSeriesComponent &Channel)
Add a reference channel to the filter, some AdaptiveFilter objects require only one reference...
Apply an adaptive filter to a time-series.
TimeSeries & GetData()
return a reference to the actual object stored in the pointer
TimeSeriesComponent & GetEx()
gplib::rmat GetWeightHistory()
Return a matrix with the values of the weights at iterations specified by weightsaveintervall.
void ShowProgress(const bool what)
Do we want visual feedback of the progess on the screen, if yes draw a simple progress indicator in t...
void GetNNSetup(const size_t filterlength, const size_t hiddenlayers, const size_t ntimeseries, NeuralNetwork::ttypeArray &NNLayers, double &NNmaxinit)
void erase(const int startindex, const int endindex)
Erase data between startindex and endindex.
void AddInputChannel(TimeSeriesComponent &Channel)
Add an input channel to the filter.
std::vector< ttypeVector > ttypeArray
An implementation of the Recursive Least Squares filter with adptive memory as described in Hakin...
TimeSeriesComponent & GetHy()
Implements a recursive least-squares adaptive filter, as described in Haykin, p. 443.
void SetWeightSaveIntervall(const int intervall)
Set the distance between iterations at which the weights are saved.
A generic base class for all types of adaptive filters.
TimeSeriesComponent is the base storage class for all types of time series data.
std::vector< SigmoidalNeuron::tneurontype > ttypeVector
void FilterData()
Filter the input channels with the current settings.
int main(int argc, char *argv[])
void SetShift(const int theshift)
Set the shift between the input time series and the reference time series.
void WriteBack(std::string filename_base)
Write in the format it was originally read in.
Implements a LMS adaptive filter.
TimeSeriesData stores a pointer to the different components of magnetotelluric data and provides func...
size_t Size()
Return the size of the time series, throws if one of the components has a different size...
const std::vector< boost::shared_ptr< TimeSeriesComponent > > & GetOutChannels()
Return the vector of output channels.
TimeSeriesComponent & GetEy()
void WriteNetCDF(const string name, const boost::numeric::ublas::matrix< double, boost::numeric::ublas::column_major > &Matrix)
TimeSeriesComponent & GetHx()
Access function for Hx, returns reference for efficiency.