00001 #include <iostream>
00002 #include <string>
00003 #include <algorithm>
00004 #include <cmath>
00005 #include <sstream>
00006 #include <numeric>
00007 #include "TimeSeriesData.h"
00008 #include "StackedSpectrum.h"
00009 #include "WFunc.h"
00010 #include <boost/bind.hpp>
00011
00012 using namespace std;
00013 using namespace gplib;
00014
00015 string version = "$Id: DelayFilter.cpp 1816 2009-09-07 11:28:35Z mmoorkamp $";
00016
00017 void SubShiftedComponent(vector<double> &Original, vector<double> &Shifted,
00018 const int shiftsteps)
00019 {
00020 transform(Original.begin(), Original.end() - shiftsteps, Shifted.begin()
00021 + shiftsteps, Original.begin(), minus<double> ());
00022 const double factor = 1. / sqrt(2.);
00023 transform(Original.begin(), Original.end(), Original.begin(), boost::bind(
00024 multiplies<double> (), _1, factor));
00025 }
00026
00027 void FindSpikeDistances(const vector<double> &ts)
00028 {
00029 const int blocklength = 2400;
00030 const int nsegments = ts.size() / blocklength;
00031 const double threshold = 1e4;
00032 double spikedist = 0;
00033 int spikecount = 0;
00034 vector<double>::const_iterator currstart(ts.begin()), currend(ts.begin()
00035 + blocklength);
00036 vector<double> differences(blocklength, 0.0);
00037 for (int i = 0; i < nsegments; ++i)
00038 {
00039 adjacent_difference(currstart, currend, differences.begin());
00040 vector<double>::iterator firstspike = find_if(differences.begin(),
00041 differences.end(), boost::bind(greater<double> (), boost::bind(
00042 (double(*)(double)) &abs, _1), threshold));
00043 vector<double>::iterator nextspike = firstspike;
00044 while (nextspike != differences.end())
00045 {
00046 nextspike = find_if(firstspike + 1, differences.end(), boost::bind(
00047 greater<double> (), boost::bind((double(*)(double)) &abs, _1),
00048 threshold));
00049 if (nextspike != currend)
00050 {
00051 spikedist += distance(firstspike, nextspike);
00052 ++spikecount;
00053 }
00054 firstspike = nextspike;
00055 }
00056 currstart += blocklength;
00057 currend += blocklength;
00058 }
00059 cout << "Spike count: " << spikecount << " Avg. Dist: " << spikedist
00060 / spikecount << endl;
00061 }
00062
00063 int main()
00064 {
00065 TimeSeriesData OriginalData, ShiftedData;
00066
00067 string infilename, outfilename, search;
00068 int seglength = 2400;
00069 double freqstep;
00070 int startindex, endindex, maxindex = 0;
00071 const double searchstart = 0.2;
00072 const double searchend = 1.0;
00073 double absmax = 0;
00074 int shiftsteps;
00075
00076 cout << "This is dfilter: Apply a delay filter to MT time series file"
00077 << endl << endl;
00078 cout << "At this point no command line parameters, only interactive."
00079 << endl << endl;
00080 cout << "This is Version: " << version << endl << endl;
00081
00082 cout << "Input Filename: ";
00083 cin >> infilename;
00084 OriginalData.GetData(infilename);
00085 cout << "Output Filename: ";
00086 cin >> outfilename;
00087 cout << " Enter 'a' for automatic, or number of points to shift: ";
00088 cin >> search;
00089 freqstep = OriginalData.GetData().GetSamplerate() * 1. / seglength;
00090 if (search == "a")
00091 {
00092 vector<complex<double> > ExSpectrum(seglength / 2 + 1);
00093
00094 cout << "Performing search" << endl;
00095 FindSpikeDistances(OriginalData.GetData().GetEx().GetData());
00096 StackedSpectrum(OriginalData.GetData().GetEx().GetData().begin(),
00097 OriginalData.GetData().GetEx().GetData().end(), ExSpectrum.begin(),
00098 seglength, Hanning());
00099
00100 startindex = round(searchstart / freqstep);
00101 endindex = round(searchend / freqstep);
00102 for (int i = startindex; i < endindex; ++i)
00103 {
00104 if (abs(ExSpectrum.at(i)) > absmax)
00105 {
00106 maxindex = i;
00107 absmax = abs(ExSpectrum.at(i));
00108 }
00109 }
00110 shiftsteps = round(OriginalData.GetData().GetSamplerate() / (maxindex
00111 * freqstep)) - 1;
00112 cout << "Spike frequency: " << maxindex * freqstep << " Height: "
00113 << absmax << endl;
00114 }
00115 else
00116 {
00117 istringstream convStream(search);
00118 convStream >> shiftsteps;
00119 maxindex = OriginalData.GetData().GetSamplerate() / (shiftsteps
00120 * freqstep);
00121 }
00122
00123 cout << "Shifted by: " << shiftsteps << " exact: "
00124 << OriginalData.GetData().GetSamplerate() / (maxindex * freqstep)
00125 << endl;
00126 ShiftedData = OriginalData;
00127
00128 SubShiftedComponent(OriginalData.GetData().GetEx().GetData(),
00129 ShiftedData.GetData().GetEx().GetData(), shiftsteps);
00130 SubShiftedComponent(OriginalData.GetData().GetEy().GetData(),
00131 ShiftedData.GetData().GetEy().GetData(), shiftsteps);
00132 SubShiftedComponent(OriginalData.GetData().GetHx().GetData(),
00133 ShiftedData.GetData().GetHx().GetData(), shiftsteps);
00134 SubShiftedComponent(OriginalData.GetData().GetHy().GetData(),
00135 ShiftedData.GetData().GetHy().GetData(), shiftsteps);
00136 SubShiftedComponent(OriginalData.GetData().GetHz().GetData(),
00137 ShiftedData.GetData().GetHz().GetData(), shiftsteps);
00138
00139 OriginalData.WriteAsMtu(outfilename);
00140 return 0;
00141 }