00001 #include "CDespike.h"
00002 #include <iostream>
00003 #include <iomanip>
00004 #include <string>
00005 #include <fstream>
00006 #include "tsfuncs.h"
00007
00008 using namespace std;
00009 CDespike::CDespike()
00010 {
00011 ReachedEnd = false;
00012 CurrentIndex = 0;
00013 }
00014 CDespike::~CDespike()
00015 {
00016 Data = NULL;
00017 Time = NULL;
00018 }
00019
00020 void CDespike::Restart()
00021 {
00022 ReachedEnd = false;
00023 CurrentTime = Time->begin();
00024 CurrentSpike = Data->begin();
00025 CurrentIndex = 0;
00026 }
00027
00028 void CDespike::RemoveSpikes(const titdata begin, const titdata end)
00029 {
00030 CurrentSpike = begin;
00031 CurrentIndex = begin - Data->begin();
00032 CurrentTime = Time->begin() + (begin - Data->begin());
00033 ReachedEnd = false;
00034
00035 vector<double>::iterator datait;
00036 vector<double>::iterator stackit;
00037
00038
00039
00040 while (ReachedEnd == false)
00041 {
00042 FindNextSpike(begin,end);
00043 if ( (distance(begin,CurrentSpike) > TrailPoints) &&
00044 (distance(CurrentSpike,end) > (SpikeLength - TrailPoints)) )
00045 {
00046 datait = CurrentSpike;
00047 advance(datait, -TrailPoints);
00048 stackit = SpikeForm.begin();
00049 for (int j = 0; j < SpikeLength; ++j)
00050 {
00051 *(datait) -= *(stackit);
00052
00053 datait++;
00054 stackit++;
00055 }
00056 }
00057 }
00058 }
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 int CDespike::StackSpikes()
00118 {
00119 return StackSpikes(Data->begin(),Data->end());
00120 }
00121
00122 int CDespike::WriteSpikeForm(string filename)
00123 {
00124 ofstream outfile;
00125
00126 outfile.open((filename+".spikeform").c_str());
00127 for (int i = 0; i < SpikeForm.size(); ++i)
00128 {
00129 outfile.precision(5);
00130 outfile << setw(15) << setfill(' ') << i;
00131 outfile << setw(15) << setfill(' ') << SpikeForm.at(i);
00132 }
00133 outfile.close();
00134 return 0;
00135 }
00136
00137 int CDespike::StackSpikes(const titdata begin, const titdata end)
00138 {
00139 int NoEstimates = 0;
00140 titdata StackStart;
00141 titdata AvgStart;
00142 double SpikeMean = 0;
00143
00144 CurrentSpike = begin;
00145 CurrentIndex = distance(Data->begin(),begin);
00146 CurrentTime = Time->begin() + distance(Data->begin(),begin);
00147 ReachedEnd = false;
00148 SpikeIndices.clear();
00149 SpikeForm.assign(SpikeLength,0);
00150 SpikeMax = 0;
00151
00152
00153 while (ReachedEnd == false)
00154 {
00155 FindNextSpike(begin,end);
00156 SpikeIndices.push_back(CurrentIndex);
00157 if ( (distance(begin,CurrentSpike) > TrailPoints) &&
00158 (distance(CurrentSpike,end) > (SpikeLength - TrailPoints)) )
00159 {
00160 AvgStart = CurrentSpike;
00161 advance(AvgStart, -TrailPoints);
00162 StackStart = SpikeForm.begin();
00163 for (int j = 0; j < SpikeLength; ++j)
00164 {
00165 *(StackStart) += *(AvgStart);
00166 StackStart++;
00167 AvgStart++;
00168 }
00169 NoEstimates++;
00170 CurrentSpike += SpikeLength;
00171 CurrentIndex += SpikeLength;
00172 CurrentTime += SpikeLength;
00173 if (CurrentSpike > end)
00174 ReachedEnd = true;
00175 }
00176 }
00177
00178 for (StackStart = SpikeForm.begin(); StackStart != SpikeForm.end(); ++StackStart)
00179 {
00180 *(StackStart) /= NoEstimates;
00181 SpikeMean += *(StackStart);
00182 if (*StackStart > SpikeMax)
00183 SpikeMax = *StackStart;
00184 }
00185 SpikeMean /= SpikeLength;
00186 for (StackStart = SpikeForm.begin(); StackStart != SpikeForm.end(); ++StackStart)
00187 {
00188 *(StackStart) -= SpikeMean;
00189 }
00190 cout << "Stacked Estimates :" << NoEstimates << endl;
00191 return NoEstimates;
00192 }
00193
00194 void CDespike::FindAllSpikes()
00195 {
00196 SpikeIndices.clear();
00197 Restart();
00198 while (ReachedEnd == false)
00199 {
00200 FindNextSpike();
00201 SpikeIndices.push_back(CurrentIndex);
00202 }
00203
00204 }
00205
00206 void CDespike::FindNextSpike()
00207 {
00208 FindNextSpike(Data->begin(),Data->end());
00209 }
00210
00211 void CDespike::FindNextSpike(const titdata begin, const titdata end)
00212 {
00213 titdata next;
00214 titdata last;
00215 double prediff, postdiff;
00216
00217 next = CurrentSpike;
00218 last = CurrentSpike;
00219 next++;
00220 last--;
00221
00222 do
00223 {
00224 CurrentIndex++;
00225 CurrentSpike++;
00226 CurrentTime++;
00227 next++;
00228 last++;
00229 prediff = *CurrentSpike - *last;
00230 postdiff = *next - *CurrentSpike;
00231
00232 }
00233 while ( (next < end)&& !(IsSpikePreRel(prediff,postdiff,*CurrentSpike,HeightThreshold)));
00234 if (next >= end)
00235 ReachedEnd = true;
00236 }
00237