| 1 | // Signal peak detector using smoothed z-score algorithm. |
|---|
| 2 | // Detects when a continuous signal has a significant peak in values. Based on |
|---|
| 3 | // algorithm from the answer here: |
|---|
| 4 | // https://stackoverflow.com/questions/22583391/peak-signal-detection-in-realtime-timeseries-data/22640362#22640362 |
|---|
| 5 | // Author: Tony DiCola |
|---|
| 6 | // License: MIT License (https://opensource.org/licenses/MIT) |
|---|
| 7 | // Usage: |
|---|
| 8 | // - Create an instance of the PeakDetector class and configure its lag, threshold, |
|---|
| 9 | // and influence. These likely need to be adjust to fit your dataset. See the |
|---|
| 10 | // Stack Overflow question above for more details on their meaning. |
|---|
| 11 | // - Continually call detect and feed it a new sample value. Detect will return 0 |
|---|
| 12 | // if no peak was detected, 1 if a positive peak was detected and -1 if a negative |
|---|
| 13 | // peak was detected. |
|---|
| 14 | #ifndef PEAKDETECTOR_H |
|---|
| 15 | #define PEAKDETECTOR_H |
|---|
| 16 | |
|---|
| 17 | class PeakDetector { |
|---|
| 18 | public: |
|---|
| 19 | PeakDetector(const int lag=5, const float threshold=3.5, const float influence=0.5): |
|---|
| 20 | _lag(lag), _threshold(threshold), _influence(influence), _avg(0.0), _std(0.0), _primed(false), _index(0) |
|---|
| 21 | { |
|---|
| 22 | // Allocate memory for last samples (used during averaging) and set them all to zero. |
|---|
| 23 | _filtered = new float[lag]; |
|---|
| 24 | for (int i=0; i<lag; ++i) { |
|---|
| 25 | _filtered[i] = 0.0; |
|---|
| 26 | } |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | ~PeakDetector() { |
|---|
| 30 | // Deallocate memory for samples. |
|---|
| 31 | if (_filtered != NULL) { |
|---|
| 32 | delete[] _filtered; |
|---|
| 33 | } |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | int detect(float sample) { |
|---|
| 37 | // Detect if the provided sample is a positive or negative peak. |
|---|
| 38 | // Will return 0 if no peak detected, 1 if a positive peak and -1 |
|---|
| 39 | // if a negative peak. |
|---|
| 40 | int result = 0; |
|---|
| 41 | // Fill up filtered samples if not yet primed with enough available samples. |
|---|
| 42 | if (_primed && (abs(sample-_avg) > (_threshold*_std))) { |
|---|
| 43 | // Detected a peak! |
|---|
| 44 | // Determine type of peak, positive or negative. |
|---|
| 45 | if (sample > _avg) { |
|---|
| 46 | result = 1; |
|---|
| 47 | } |
|---|
| 48 | else { |
|---|
| 49 | result = -1; |
|---|
| 50 | } |
|---|
| 51 | // Save this sample but scaled down based on influence. |
|---|
| 52 | _filtered[_index] = (_influence*sample) + ((1.0-_influence)*_filtered[_previousIndex()]); |
|---|
| 53 | } |
|---|
| 54 | else { |
|---|
| 55 | // Did not detect a peak, or not yet primed with enough samples. |
|---|
| 56 | // Just record this sample and move on. |
|---|
| 57 | _filtered[_index] = sample; |
|---|
| 58 | } |
|---|
| 59 | // Increment index of next filtered sample. |
|---|
| 60 | _incrementIndex(); |
|---|
| 61 | // When primed update the average and standard deviation of the most recent |
|---|
| 62 | // filtered values. |
|---|
| 63 | if (_primed) { |
|---|
| 64 | // Compute mean of filtered values. |
|---|
| 65 | _avg = 0.0; |
|---|
| 66 | for (int i=0; i<_lag; ++i) { |
|---|
| 67 | _avg += _filtered[i]; |
|---|
| 68 | } |
|---|
| 69 | _avg = _avg/float(_lag); |
|---|
| 70 | // Compute standard deviation of filtered values. |
|---|
| 71 | _std = 0.0; |
|---|
| 72 | for (int i=0; i<_lag; ++i) { |
|---|
| 73 | _std += pow(_filtered[i]-_avg, 2.0); |
|---|
| 74 | } |
|---|
| 75 | _std = sqrt(_std/float(_lag)); |
|---|
| 76 | } |
|---|
| 77 | return result; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | float getAvg() { |
|---|
| 81 | // Return the current signal average, useful for debugging. |
|---|
| 82 | return _avg; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | float getStd() { |
|---|
| 86 | // Return the current signal standard deviation, useful for debugging. |
|---|
| 87 | return _std; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | private: |
|---|
| 91 | float _lag; |
|---|
| 92 | float _threshold; |
|---|
| 93 | float _influence; |
|---|
| 94 | float* _filtered; |
|---|
| 95 | float _avg; |
|---|
| 96 | float _std; |
|---|
| 97 | bool _primed; |
|---|
| 98 | int _index; |
|---|
| 99 | |
|---|
| 100 | void _incrementIndex() { |
|---|
| 101 | // Increment the index of the current sample. |
|---|
| 102 | _index += 1; |
|---|
| 103 | if (_index >= _lag) { |
|---|
| 104 | // Loop back to start of sample buffer when full, but be sure to note |
|---|
| 105 | // when this happens to indicate we are primed with enough samples. |
|---|
| 106 | _index = 0; |
|---|
| 107 | _primed = true; |
|---|
| 108 | } |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | int _previousIndex() { |
|---|
| 112 | // Find the index of the last sample. |
|---|
| 113 | int result = _index-1; |
|---|
| 114 | if (result < 0) { |
|---|
| 115 | result = _lag-1; |
|---|
| 116 | } |
|---|
| 117 | return result; |
|---|
| 118 | } |
|---|
| 119 | }; |
|---|
| 120 | |
|---|
| 121 | #endif |
|---|
| 122 | |
|---|