25 PeakDetection::PeakDetection(
const UINT lowPassFilterSize,
const UINT searchWindowSize){
27 this->lowPassFilterSize = lowPassFilterSize;
28 this->searchWindowSize = searchWindowSize;
30 inputTimeoutCounter = 0;
33 inputTimeoutLimit = 10;
34 searchHistorySize = 10;
35 deadZoneThreshold = 0.01;
39 setSearchWindowSize( searchWindowSize );
44 this->lowPassFilterSize = rhs.lowPassFilterSize;
45 this->searchWindowSize = rhs.searchWindowSize;
46 this->lowPassFilter = rhs.lowPassFilter;
49 PeakDetection::~PeakDetection(){
55 this->lowPassFilterSize = rhs.lowPassFilterSize;
56 this->searchWindowSize = rhs.searchWindowSize;
57 this->lowPassFilter = rhs.lowPassFilter;
62 bool PeakDetection::update(
const Float x){
65 if( ++inputTimeoutCounter >= inputTimeoutLimit ){
66 inputTimeoutCounter = inputTimeoutLimit;
68 }
else enableSearch =
false;
74 Float filteredValue = lowPassFilter.
filter(x);
77 Float firstDeriv = filteredValue - filteredDataBuffer.
getBack();
80 Float secondDeriv = firstDeriv - firstDerivBuffer.
getBack();
83 secondDeriv = deadZoneFilter.
filter( secondDeriv );
86 filteredDataBuffer.
push_back( filteredValue );
88 secondDerivBuffer.
push_back( secondDeriv );
91 unsigned int peakType = NO_PEAK_FOUND;
94 globalMaximaPeakInfo.peakType = NO_PEAK_FOUND;
95 globalMinimaPeakInfo.peakType = NO_PEAK_FOUND;
98 if( ++maximaCounter >= searchHistorySize ){
100 globalMaximaPeakInfo.peakValue = DEFAULT_GLOBAL_MAXIMA_VALUE;
104 if( ++minimaCounter >= searchHistorySize ){
106 globalMinimaPeakInfo.peakValue = DEFAULT_GLOBAL_MINIMA_VALUE;
110 for(
unsigned int i=1; i<searchWindowSize-1; i++){
111 peakType = NO_PEAK_FOUND;
112 if( (secondDerivBuffer[i-1] <= 0 && secondDerivBuffer[i] > 0) || (secondDerivBuffer[i-1] <= 0 && secondDerivBuffer[i+1] > 0) ){
115 peakType = LOCAL_MAXIMA_FOUND;
118 if( filteredDataBuffer[i] > globalMaximaPeakInfo.peakValue ){
120 globalMaximaPeakInfo.peakType = GLOBAL_MAXIMA_FOUND;
121 globalMaximaPeakInfo.peakIndex = i;
122 globalMaximaPeakInfo.peakValue = filteredDataBuffer[i];
125 peakInfo.push_back(
PeakInfo(peakType,i,filteredDataBuffer[i]) );
130 if( (secondDerivBuffer[i-1] >= 0 && secondDerivBuffer[i] < 0) || (secondDerivBuffer[i-1] >= 0 && secondDerivBuffer[i+1] < 0) ){
133 peakType = LOCAL_MINIMA_FOUND;
135 if( filteredDataBuffer[i] < globalMinimaPeakInfo.peakValue ){
137 globalMinimaPeakInfo.peakType = GLOBAL_MINIMA_FOUND;
138 globalMinimaPeakInfo.peakIndex = i;
139 globalMinimaPeakInfo.peakValue = filteredDataBuffer[i];
142 peakInfo.push_back(
PeakInfo(peakType,i,filteredDataBuffer[i]) );
150 return peakDetected ?
true :
false;
153 bool PeakDetection::reset(){
155 lowPassFilter.
init(lowPassFilterSize,1);
158 deadZoneFilter.
init(-deadZoneThreshold,deadZoneThreshold,1);
161 filteredDataBuffer.
clear();
162 firstDerivBuffer.
clear();
163 secondDerivBuffer.
clear();
164 peakTypesBuffer.
clear();
166 filteredDataBuffer.
resize( searchWindowSize, 0 );
167 firstDerivBuffer.
resize( searchWindowSize, 0 );
168 secondDerivBuffer.
resize( searchWindowSize, 0 );
169 peakTypesBuffer.
resize( searchWindowSize, NO_PEAK_FOUND );
172 peakDetected =
false;
173 inputTimeoutCounter = 0;
177 globalMaximaPeakInfo.peakType = NO_PEAK_FOUND;
178 globalMaximaPeakInfo.peakIndex = 0;
179 globalMaximaPeakInfo.peakValue = DEFAULT_GLOBAL_MAXIMA_VALUE;
180 globalMinimaPeakInfo.peakType = NO_PEAK_FOUND;
181 globalMinimaPeakInfo.peakIndex = 0;
182 globalMinimaPeakInfo.peakValue = DEFAULT_GLOBAL_MINIMA_VALUE;
187 bool PeakDetection::setSearchWindowSize(
const UINT searchWindowSize){
188 this->searchWindowSize = searchWindowSize;
bool push_back(const T &value)
Float filter(const Float x)
Float filter(const Float x)
bool init(UINT filterSize, UINT numDimensions)
bool init(Float lowerLimit, Float upperLimit, UINT numDimensions)
bool resize(const unsigned int newBufferSize)