1 | #include "SpikeRemoval.h"
|
---|
2 |
|
---|
3 | #include <iostream>
|
---|
4 | using namespace std;
|
---|
5 |
|
---|
6 | // compute the mean of the left and right neighbors of a channel
|
---|
7 | vector<float> * computeN1mean( vector<float> &src)
|
---|
8 | {
|
---|
9 | vector<float> * dest = new vector<float>;
|
---|
10 |
|
---|
11 | dest->push_back(src[1]);
|
---|
12 |
|
---|
13 | for(unsigned int i = 1; i < src.size() - 1; i++){
|
---|
14 | /* if (i == 0){ // use right sample es mean
|
---|
15 | N1mean[i] = Ameas[i+1];
|
---|
16 | }
|
---|
17 | else if ( i == Samples-1 ){ //use left sample as mean
|
---|
18 | N1mean[i] = Ameas[i-1];
|
---|
19 | }
|
---|
20 | else{
|
---|
21 | N1mean[i] = ( Ameas[i-1] + Ameas[i+1] ) / 2.;
|
---|
22 | }
|
---|
23 | */
|
---|
24 | dest->push_back( ( src[i-1] + src[i+1] ) / 2. );
|
---|
25 | }
|
---|
26 |
|
---|
27 | dest->push_back(src[src.size() - 1]);
|
---|
28 | return dest;
|
---|
29 | } // end of computeN1mean computation
|
---|
30 |
|
---|
31 | void removeSpikes(
|
---|
32 | vector<float> &src,
|
---|
33 | vector<float> &dest,
|
---|
34 | const float CandidateTHR,
|
---|
35 | const float nextDiffTHR,
|
---|
36 | const float nextNextDiffTHR
|
---|
37 | ){
|
---|
38 | //vector with Mean values of both neighbor samples of a sample
|
---|
39 | vector<float> * NextNeighborMean = computeN1mean(src);
|
---|
40 |
|
---|
41 | if (src.size() != NextNeighborMean->size())
|
---|
42 | {
|
---|
43 | //TODO .. if verbositylevel .. say something, use the return code to tell the caller..
|
---|
44 | dest.clear();
|
---|
45 | }
|
---|
46 | dest = src;
|
---|
47 |
|
---|
48 | float diff, nextDiff, nextNextDiff;
|
---|
49 |
|
---|
50 | // find the spike and replace it by mean value of neighbors
|
---|
51 | for (unsigned int i = 0; i < src.size(); i++) {
|
---|
52 |
|
---|
53 | //calculate difference of sample value and average of both it's neighbors
|
---|
54 | diff = src[i] - (*NextNeighborMean)[i];
|
---|
55 |
|
---|
56 | //compare sample value to average of both neighbors
|
---|
57 | if ( diff < CandidateTHR ){ // a spike candidate
|
---|
58 | // check consistency with a single channel spike
|
---|
59 |
|
---|
60 | // check if the nectneighbor is a spike candidate too
|
---|
61 | if ( src[i+2] - ( src[i] + src[i+3] )/2. > 10. )
|
---|
62 | {
|
---|
63 | dest[i+1] = ( src[i] + src[i+3] )/2.;
|
---|
64 | dest[i+2] = ( src[i] + src[i+3] )/2.;
|
---|
65 |
|
---|
66 | i = i + 3;
|
---|
67 | }
|
---|
68 | else
|
---|
69 | {
|
---|
70 | //calculate difference of sample value and average of both it's neighbors of next sample
|
---|
71 | nextDiff = src[i+1] - (*NextNeighborMean)[i+1];
|
---|
72 | //calculate difference of sample value and average of both it's neighbors of over-next sample
|
---|
73 | nextNextDiff = src[i+2] - (*NextNeighborMean)[i+2];
|
---|
74 |
|
---|
75 | if ( ( nextDiff > nextDiffTHR * diff ) && ( nextNextDiff < nextNextDiffTHR ) )
|
---|
76 | {
|
---|
77 | dest[i+1] = (*NextNeighborMean)[i+1];
|
---|
78 | (*NextNeighborMean)[i+2] = (src[i+1] - src[i+3] / 2.);
|
---|
79 |
|
---|
80 | i = i + 2;//do not care about the next sample it was the spike
|
---|
81 | }
|
---|
82 | // treatment for the end of the pipeline must be added !!!
|
---|
83 | }
|
---|
84 | }
|
---|
85 | } // end of spike search and correction
|
---|
86 | delete NextNeighborMean;
|
---|
87 | return;
|
---|
88 | }
|
---|