00001 #include <iostream>
00002 #include <string>
00003 #include <fstream>
00004 #include <vector>
00005 #include "ShortCorr.h"
00006 #include "convert.h"
00007 #include "WFunc.h"
00008 #include "TimeSeriesData.h"
00009
00010 using namespace std;
00011 using namespace gplib;
00012
00013 string version = "$Id: mtucorr.cpp 1816 2009-09-07 11:28:35Z mmoorkamp $";
00014
00015
00016 void CalcCorrAndWrite(ttsdata &Data, ttsdata &Short, ttsdata &Corr, string name)
00017 {
00018 ofstream outfile(name.c_str());
00019 ShortCorr(Data.begin(), Data.end(), Short.begin(), Short.end(),
00020 Corr.begin());
00021 copy(Corr.begin(), Corr.end(), ostream_iterator<double> (outfile, "\n"));
00022 }
00023
00024
00025 void FindNextSpike(ttsdata::iterator &currstart, ttsdata &Data, ttsdata &Spike,
00026 const int trailpoints, const int decaypoints, const double diffthreshold)
00027 {
00028 ttsdata diffdata;
00029 diffdata.reserve(Data.size());
00030 adjacent_difference(Data.begin(), Data.end(), back_inserter(diffdata));
00031 ttsdata::iterator currdiff = diffdata.begin() + distance(Data.begin(),
00032 currstart) + trailpoints;
00033 ttsdata::iterator currdata = currstart + trailpoints;
00034 bool spikefound = false;
00035 Spike.assign(trailpoints + decaypoints, 0.0);
00036 while (!spikefound && (currdata + decaypoints < Data.end()))
00037 {
00038 if (*currdiff > diffthreshold)
00039 {
00040 copy(currdata - trailpoints, currdata + decaypoints, Spike.begin());
00041 spikefound = true;
00042 currstart = currdata;
00043 }
00044 ++currdiff;
00045 ++currdata;
00046 }
00047 }
00048
00049
00050 double RemoveSpike(ttsdata &Data, ttsdata &Spike, const double corrthreshold,
00051 const int minspikeavg, string outname, const size_t iteration)
00052 {
00053 ttsdata Corr(Data.size(), 0.0);
00054 SubMean(Spike.begin(), Spike.end());
00055 CalcCorrAndWrite(Data, Spike, Corr, outname + stringify(iteration)
00056 + ".corr");
00057
00058 ttsdata::iterator datait = Data.begin();
00059 ttsdata::iterator corrit = Corr.begin();
00060 const int shortsize = Spike.size();
00061 double spikecount = 0;
00062 ttsdata avgspike(Spike.size(), 0.0);
00063 while (datait + shortsize < Data.end())
00064 {
00065 if (*corrit > corrthreshold)
00066 {
00067 ttsdata currsegment(datait, datait + shortsize);
00068 SubMean(currsegment.begin(), currsegment.end());
00069 transform(currsegment.begin(), currsegment.end(), avgspike.begin(),
00070 avgspike.begin(), plus<double> ());
00071 spikecount += 1.0;
00072 }
00073 ++datait;
00074 ++corrit;
00075 }
00076 cout << " No. of spikes before average: " << spikecount << " ";
00077 if (spikecount < minspikeavg)
00078 {
00079 cout << " below threshold of: " << minspikeavg << " not used." << endl;
00080 return 0.0;
00081 }
00082 else
00083 {
00084 cout << endl;
00085 double maxamp = *max_element(avgspike.begin(), avgspike.end());
00086 transform(avgspike.begin(), avgspike.end(), avgspike.begin(),
00087 boost::bind(multiplies<double> (), _1, 1. / maxamp));
00088 ofstream avgfile((static_cast<std::string> ("spike") + stringify(
00089 iteration) + static_cast<std::string> (".avg")).c_str());
00090 copy(avgspike.begin(), avgspike.end(), ostream_iterator<double> (
00091 avgfile, "\n"));
00092
00093 spikecount = 0.0;
00094
00095 CalcCorrAndWrite(Data, avgspike, Corr, outname + stringify(iteration)
00096 + ".avg.corr");
00097 datait = Data.begin();
00098 corrit = Corr.begin();
00099 while (datait + shortsize < Data.end())
00100 {
00101 if (*corrit > corrthreshold)
00102 {
00103 ttsdata currsegment(datait, datait + shortsize);
00104 SubMean(currsegment.begin(), currsegment.end());
00105 double segmaxamp = *max_element(currsegment.begin(),
00106 currsegment.end());
00107 ttsdata avgadjust(avgspike.begin(), avgspike.end());
00108 transform(avgadjust.begin(), avgadjust.end(),
00109 avgadjust.begin(), boost::bind(multiplies<double> (), _1,
00110 segmaxamp));
00111 transform(datait, datait + shortsize, avgadjust.begin(),
00112 datait, minus<double> ());
00113 spikecount += 1.0;
00114 }
00115 ++datait;
00116 ++corrit;
00117 }
00118 cout << " No. of spikes after average: " << spikecount << endl;
00119 }
00120 return spikecount;
00121 }
00122
00123
00124 int main(int argc, char *argv[])
00125 {
00126 TimeSeriesData TsData;
00127
00128 string masterfilename, shortfilename;
00129
00130 double corrthreshold = 0.9;
00131 double diffthreshold = 10000;
00132 int trailpoints = 5;
00133 int decaypoints = 20;
00134 int iterations = 50;
00135 int minspikeavg = 10;
00136
00137 cout
00138 << " This is mtucorr: Substract spikeform from a mtu time series for cow-fence denoising"
00139 << endl << endl;
00140 cout
00141 << " Usage mtucorr You will have to enter the mtufilename and a number of parameters that determine which spike will be removed"
00142 << endl;
00143 cout
00144 << " The output will have the same name as the Master filename with ending appended"
00145 << endl << endl;
00146 cout << " This is Version: " << version << endl << endl;
00147
00148 cout << "Time Series filename: ";
00149 cin >> masterfilename;
00150 TsData.GetData(masterfilename);
00151 cout << "Minimum differential spike height: ";
00152 cin >> diffthreshold;
00153 cout << "Number of trailing points: ";
00154 cin >> trailpoints;
00155 cout << "Number of decay points: ";
00156 cin >> decaypoints;
00157 cout << "Minimum correlation: ";
00158 cin >> corrthreshold;
00159 cout << "Minimum number of samples in average: ";
00160 cin >> minspikeavg;
00161 cout << "Number of Iterations: ";
00162 cin >> iterations;
00163 ttsdata ShortData;
00164
00165 ttsdata::iterator currexstart = TsData.GetData().GetEx().GetData().begin();
00166 ttsdata::iterator curreystart = TsData.GetData().GetEy().GetData().begin();
00167 ttsdata::iterator currhxstart = TsData.GetData().GetHx().GetData().begin();
00168 ttsdata::iterator currhystart = TsData.GetData().GetHy().GetData().begin();
00169 double exspikes = 0;
00170 double eyspikes = 0;
00171 for (int i = 0; i < iterations; ++i)
00172 {
00173 cout << "Iteration: " << i + 1 << endl;
00174 cout << " Ex Component" << endl;
00175
00176 FindNextSpike(currexstart, TsData.GetData().GetEx().GetData(),
00177 ShortData, trailpoints, decaypoints, diffthreshold);
00178 exspikes += RemoveSpike(TsData.GetData().GetEx().GetData(), ShortData,
00179 corrthreshold, minspikeavg, masterfilename, i);
00180 cout << " Ey Component" << endl;
00181 FindNextSpike(curreystart, TsData.GetData().GetEy().GetData(),
00182 ShortData, trailpoints, decaypoints, diffthreshold);
00183 eyspikes += RemoveSpike(TsData.GetData().GetEy().GetData(), ShortData,
00184 corrthreshold, minspikeavg, masterfilename, i);
00185 }
00186 cout << "Total Spikes removed: " << endl;
00187 cout << " Ex: " << exspikes << endl;
00188 cout << " Ey: " << eyspikes << endl;
00189 ofstream cleanfile((masterfilename + ".ex.clean").c_str());
00190 copy(TsData.GetData().GetEx().GetData().begin(),
00191 TsData.GetData().GetEx().GetData().end(), ostream_iterator<double> (
00192 cleanfile, "\n"));
00193 TsData.WriteAsMtu(masterfilename + ".clean");
00194 }
00195