11 using namespace gplib;
13 string version =
"$Id: mtucorr.cpp 1816 2009-09-07 11:28:35Z mmoorkamp $";
18 ofstream outfile(name.c_str());
19 ShortCorr(Data.begin(), Data.end(), Short.begin(), Short.end(),
21 copy(Corr.begin(), Corr.end(), ostream_iterator<double> (outfile,
"\n"));
25 void FindNextSpike(ttsdata::iterator &currstart, ttsdata &Data, ttsdata &Spike,
26 const int trailpoints,
const int decaypoints,
const double diffthreshold)
29 diffdata.reserve(Data.size());
30 adjacent_difference(Data.begin(), Data.end(), back_inserter(diffdata));
31 ttsdata::iterator currdiff = diffdata.begin() + distance(Data.begin(),
32 currstart) + trailpoints;
33 ttsdata::iterator currdata = currstart + trailpoints;
34 bool spikefound =
false;
35 Spike.assign(trailpoints + decaypoints, 0.0);
36 while (!spikefound && (currdata + decaypoints < Data.end()))
38 if (*currdiff > diffthreshold)
40 copy(currdata - trailpoints, currdata + decaypoints, Spike.begin());
50 double RemoveSpike(ttsdata &Data, ttsdata &Spike,
const double corrthreshold,
51 const int minspikeavg,
string outname,
const size_t iteration)
53 ttsdata Corr(Data.size(), 0.0);
54 SubMean(Spike.begin(), Spike.end());
58 ttsdata::iterator datait = Data.begin();
59 ttsdata::iterator corrit = Corr.begin();
60 const int shortsize = Spike.size();
61 double spikecount = 0;
62 ttsdata avgspike(Spike.size(), 0.0);
63 while (datait + shortsize < Data.end())
65 if (*corrit > corrthreshold)
67 ttsdata currsegment(datait, datait + shortsize);
68 SubMean(currsegment.begin(), currsegment.end());
69 transform(currsegment.begin(), currsegment.end(), avgspike.begin(),
70 avgspike.begin(), plus<double> ());
76 cout <<
" No. of spikes before average: " << spikecount <<
" ";
77 if (spikecount < minspikeavg)
79 cout <<
" below threshold of: " << minspikeavg <<
" not used." << endl;
85 double maxamp = *max_element(avgspike.begin(), avgspike.end());
86 transform(avgspike.begin(), avgspike.end(), avgspike.begin(),
87 boost::bind(multiplies<double> (), _1, 1. / maxamp));
88 ofstream avgfile((static_cast<std::string> (
"spike") + stringify(
89 iteration) + static_cast<std::string> (
".avg")).c_str());
90 copy(avgspike.begin(), avgspike.end(), ostream_iterator<double> (
97 datait = Data.begin();
98 corrit = Corr.begin();
99 while (datait + shortsize < Data.end())
101 if (*corrit > corrthreshold)
103 ttsdata currsegment(datait, datait + shortsize);
104 SubMean(currsegment.begin(), currsegment.end());
105 double segmaxamp = *max_element(currsegment.begin(),
107 ttsdata avgadjust(avgspike.begin(), avgspike.end());
108 transform(avgadjust.begin(), avgadjust.end(),
109 avgadjust.begin(), boost::bind(multiplies<double> (), _1,
111 transform(datait, datait + shortsize, avgadjust.begin(),
112 datait, minus<double> ());
118 cout <<
" No. of spikes after average: " << spikecount << endl;
124 int main(
int argc,
char *argv[])
128 string masterfilename, shortfilename;
130 double corrthreshold = 0.9;
131 double diffthreshold = 10000;
133 int decaypoints = 20;
135 int minspikeavg = 10;
138 <<
" This is mtucorr: Substract spikeform from a mtu time series for cow-fence denoising"
141 <<
" Usage mtucorr You will have to enter the mtufilename and a number of parameters that determine which spike will be removed"
144 <<
" The output will have the same name as the Master filename with ending appended"
146 cout <<
" This is Version: " <<
version << endl << endl;
148 cout <<
"Time Series filename: ";
149 cin >> masterfilename;
150 TsData.
GetData(masterfilename);
151 cout <<
"Minimum differential spike height: ";
152 cin >> diffthreshold;
153 cout <<
"Number of trailing points: ";
155 cout <<
"Number of decay points: ";
157 cout <<
"Minimum correlation: ";
158 cin >> corrthreshold;
159 cout <<
"Minimum number of samples in average: ";
161 cout <<
"Number of Iterations: ";
173 cout <<
"Iteration: " << i + 1 << endl;
174 cout <<
" Ex Component" << endl;
177 ShortData, trailpoints, decaypoints, diffthreshold);
179 corrthreshold, minspikeavg, masterfilename, i);
180 cout <<
" Ey Component" << endl;
182 ShortData, trailpoints, decaypoints, diffthreshold);
184 corrthreshold, minspikeavg, masterfilename, i);
186 cout <<
"Total Spikes removed: " << endl;
187 cout <<
" Ex: " << exspikes << endl;
188 cout <<
" Ey: " << eyspikes << endl;
189 ofstream cleanfile((masterfilename +
".ex.clean").c_str());
double RemoveSpike(ttsdata &Data, ttsdata &Spike, const double corrthreshold, const int minspikeavg, string outname, const size_t iteration)
Remove spikes with the given form from the time-series.
std::vector< double > & GetData()
Access for data vector, for ease of use and efficiency we return a reference.
TimeSeries & GetData()
return a reference to the actual object stored in the pointer
TimeSeriesComponent & GetEx()
void SubMean(InputIterator begin, InputIterator end, typename std::iterator_traits< InputIterator >::value_type mean)
Substract the mean from each element in the container, mean is passed as a parameter.
int main(int argc, char *argv[])
The program mtucorr tries to identify spikes in the electric field of MT time-series data and remove ...
TimeSeriesComponent & GetHy()
void WriteAsMtu(std::string filename_base)
Write data to file in Phoenix MTU format.
void CalcCorrAndWrite(ttsdata &Data, ttsdata &Short, ttsdata &Corr, string name)
Calculate the correlation between the filed component and a spike and write the correlation to a file...
TimeSeriesData stores a pointer to the different components of magnetotelluric data and provides func...
void FindNextSpike(ttsdata::iterator &currstart, ttsdata &Data, ttsdata &Spike, const int trailpoints, const int decaypoints, const double diffthreshold)
Find the next spike by examining the first difference.
TimeSeriesComponent & GetEy()
TimeSeriesComponent & GetHx()
Access function for Hx, returns reference for efficiency.