CDespike.cpp

Go to the documentation of this file.
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         //copy(Data->begin(),Data->end(),back_insert_iterator<vector<double> > (CleanData));
00039         //Data = &CleanData;
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                                 //*(datait) += mean;
00053                                 datait++;
00054                                 stackit++;
00055                         }
00056                 }
00057         }
00058 }
00059 
00060 /*void CDespike::RemoveSpikes()
00061 {
00062         Restart();
00063         titdata current;
00064         titdata next;
00065         titdata last;
00066         titdata currenttime;
00067         titdata lasttime;
00068         double max = 0;
00069         int nspikes = 0;
00070         double prediff,postdiff,timediff;
00071         titspikeindex index;
00072         
00073         current = Data->begin()+1;
00074         currenttime = Time->begin()+1;
00075         next = current+1;
00076         last = current-1;
00077         lasttime = currenttime -1;
00078         /*while ( next != Data->end())
00079         {
00080                 postdiff = *next - *current;
00081                 prediff =  *current - *last;
00082                 timediff = *currenttime - *lasttime;
00083                 if (((fabs(prediff) > HeightThreshold) || (fabs(postdiff) > HeightThreshold))
00084                         && (timediff <= 2*samplerate) &&( (postdiff * prediff) < 0))
00085                 {       
00086                         cout << " Before " << *current << " " << *last << " " << *next << " " << postdiff <<" " << prediff;
00087                         *current = (*next + *last)/2.;
00088                         cout << " After: " << *current << " " << *last << " " << *next << endl;
00089                         nspikes++;
00090                         if (fabs(prediff) > max)
00091                                 max = prediff;
00092                 }*/
00093 /*              index = SpikeIndices.begin();
00094                 while (distance(index,SpikeIndices.end()) > 2)
00095                 {
00096                         postdiff = Data->at(*(index) +1) - Data->at(*index);
00097                         prediff = Data->at(*index) - Data->at(*(index) -1);
00098                         /*Data->at(*index) = (Data->at(*(index) +1) + Data->at(*(index) +2)
00099                                  + Data->at(*(index) -1) + Data->at(*(index) -2))/4.;*/
00100 /*                      Data->at(*index) = (Data->at(*(index) +1) 
00101                                  + Data->at(*(index) -1))/2.;
00102                         index++;
00103                         nspikes++;
00104                         if (fabs(prediff) > max)
00105                                 max = prediff;
00106                 }
00107                 /*current++;
00108                 next++;
00109                 last++;
00110                 currenttime++;
00111                 lasttime++;
00112         }*/
00113 /*      cout << "Max: " << max <<endl;
00114         cout << "NSpikes: " << nspikes <<endl;
00115         cout << "NPoints: " << Data->size() <<endl;
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 

Generated on Thu Nov 22 13:58:24 2007 for GPLIB++ by  doxygen 1.5.1