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 SingleCandidateTHR,
|
---|
35 | const float DoubleCandidateTHR,
|
---|
36 | const float nextDiffTHR
|
---|
37 | //const float nextNextDiffTHR
|
---|
38 | ){
|
---|
39 | vector<float> * NextNeighborMean = computeN1mean(src);
|
---|
40 |
|
---|
41 | if (src.size() != NextNeighborMean->size())
|
---|
42 | //TODO .. if verbositylevel .. say something, use the return code to tell the caller..
|
---|
43 | dest.clear();
|
---|
44 | dest = src;
|
---|
45 |
|
---|
46 | float diff, nextDiff, nextNextDiff;
|
---|
47 | int checkDouble;
|
---|
48 |
|
---|
49 | // find the spike and replace it by mean value of neighbors
|
---|
50 | for (unsigned int i = 0; i < src.size(); i++) {
|
---|
51 |
|
---|
52 | diff = src[i] - (*NextNeighborMean)[i];
|
---|
53 | checkDouble = 0;
|
---|
54 | if ( diff < SingleCandidateTHR ){ // a single spike candidate
|
---|
55 | // check consistency with a single channel spike
|
---|
56 | nextDiff = src[i+1] - (*NextNeighborMean)[i+1];
|
---|
57 | if ( nextDiff > -1.6 * diff )
|
---|
58 | {
|
---|
59 | dest[i+1] = ( src[i] + src[i+3] )/2.;
|
---|
60 | i = i + 2;
|
---|
61 | printf("single spike\n");
|
---|
62 | }
|
---|
63 | else{
|
---|
64 | // do notthing - not really a single spike, but check if it is a double
|
---|
65 | checkDouble = 1;
|
---|
66 | }
|
---|
67 | }
|
---|
68 | if (diff < DoubleCandidateTHR || checkDouble == 1) { // a double spike candidate
|
---|
69 | nextDiff = src[i+1] - (*NextNeighborMean)[i+1];
|
---|
70 | nextNextDiff = src[i+2] - (*NextNeighborMean)[i+2];
|
---|
71 | //printf("double spike candidate\n");
|
---|
72 | // printf("%d %f %f %f\n", i, diff, nextDiff, nextNextDiff);
|
---|
73 | // printf("%f %f %f\n", DoubleCandidateTHR, nextDiffTHR, nextNextDiffTHR);
|
---|
74 | // check the consistency with a double spike
|
---|
75 | if ( ( nextDiff > nextDiffTHR * DoubleCandidateTHR * -1. ) && ( nextNextDiff > nextDiffTHR * DoubleCandidateTHR * -1. ) ){
|
---|
76 | dest[i+1] = (src[i] + src[i+3]) / 2. ;
|
---|
77 | dest[i+2] = (src[i] + src[i+3]) / 2.;
|
---|
78 | //(*NextNeighborMean)[i+2] = (src[i+1] - src[i+3] / 2.);
|
---|
79 | i = i + 3; //do not care about the next sample it was the spike
|
---|
80 | printf("double spike\n");
|
---|
81 | }
|
---|
82 | // treatment for the end of the pipeline must be added !!!
|
---|
83 | }
|
---|
84 | } // end of spike search and correction
|
---|
85 | delete NextNeighborMean;
|
---|
86 | return;
|
---|
87 | }
|
---|