forked from antonbarty/cheetah-2012
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peakdetect.cpp
205 lines (178 loc) · 4.83 KB
/
peakdetect.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* peakdetect.cpp
* --------------
* Created by Jonas Sellberg on 2011-07-18.
* Copyright 2011 SLAC National Accelerator Laboratory. All rights reserved.
*
* Object-oriented C++ version of the functionality available
* in the MATLAB script at http://billauer.co.il/peakdet.html
* ----------------------------------------------------------
* This file implements the PeakDetect class.
*/
#include "peakdetect.h"
#include "pointvector.h"
#include "point.h"
#include <iostream>
using namespace std;
/*
* Implementation notes: PeakDetect constuctors
* --------------------------------------------
* The constructor must allocate the storage for the point
* vectors of the maxima and minima and initialize the pointers
* to the data values.
*/
PeakDetect::PeakDetect(uint16_t *Yarray, unsigned length) {
x = NULL;
y = Yarray;
maxima = new PointVector();
minima = new PointVector();
this->length = length;
index = 0;
}
PeakDetect::PeakDetect(int *Xarray, uint16_t *Yarray, unsigned length) {
x = Xarray;
y = Yarray;
maxima = new PointVector();
minima = new PointVector();
this->length = length;
index = 0;
}
/*
* Implementation notes: ~PeakDetect
* ---------------------------------
* The destructor must deallocate the two point vectors
*/
PeakDetect::~PeakDetect() {
delete maxima;
delete minima;
}
/*
* Implementation notes: clear
* ---------------------------
* This method clears all maxima and minima that has been found
* and resets all instance variables.
*/
void PeakDetect::clear() {
maxima->clear();
minima->clear();
index = 0;
}
/*
* Implementation notes: findAll
* -----------------------------
* This method finds all maxima and minima that have a difference larger than delta.
* It starts looking for a minima and adds an additional minima after the last maxima.
*/
void PeakDetect::findAll(float delta) {
clear();
Point max(-1, -1);
Point min(-1, 65536);
bool findMax = false;
for (index = 0; index < length; index++) {
// check if max/min should be updated
if (y[index] > max.getY()) max = Point(index, y[index]);
if (minima->size()) {
if (y[index] < min.getY()) min = Point(index, y[index]);
} else {
if (y[index] <= min.getY()) min = Point(index, y[index]);
}
// check if max/min are larger than delta
if (findMax) {
if (y[index] < max.getY() - delta) {
maxima->add(max.getX(), max.getY());
min = Point(index, y[index]);
findMax = false;
}
} else {
if (y[index] > min.getY() + delta) {
minima->add(min.getX(), min.getY());
max = Point(index, y[index]);
findMax = true;
}
}
}
// add last minimum
minima->add(min.getX(), min.getY());
if (x != NULL) {
PointVector *maxtemp = new PointVector();
PointVector *mintemp = new PointVector();
for (int i = 0; i < maxima->size(); i++)
maxtemp->add(x[(maxima->get(i))->getX()], (maxima->get(i))->getY());
for (int i = 0; i < minima->size(); i++)
mintemp->add(x[(minima->get(i))->getX()], (minima->get(i))->getY());
delete maxima;
delete minima;
maxima = maxtemp;
minima = mintemp;
}
}
/*
* Implementation notes: findNext
* ------------------------------
* This function
*/
void PeakDetect::findNext(float delta) {
Point max(-1, -1);
Point min(-1, 65536);
bool findMax = true;
int maximaSize = 0;
int minimaSize = 1;
if (index) {
max = Point(index, y[index]);
min = max;
maximaSize = maxima->size();
minimaSize = minima->size();
}
while (maxima->size() - maximaSize < 1 || minima->size() - minimaSize < 1) {
if (index >= length) break;
// check if max/min should be updated
if (y[index] > max.getY()) {
if (x == NULL) max = Point(index, y[index]);
else max = Point(x[index], y[index]);
}
if (y[index] < min.getY()) {
if (x == NULL) min = Point(index, y[index]);
else min = Point(x[index], y[index]);
}
// check if max/min are larger than delta
if (findMax) {
if (y[index] < max.getY() - delta) {
maxima->add(max.getX(), max.getY());
// find first minimum and add it
if (maxima->size() == 1) {
int jmax;
if (x == NULL) jmax = max.getX();
else {
int k = 0;
while (true) {
if (x[k] == max.getX()) {
jmax = k;
break;
}
k++;
}
}
min = Point(-1, 65536);
for (int j = 0; j < jmax; j++) {
if (y[j] <= min.getY()) {
if (x == NULL) min = Point(j, y[j]);
else min = Point(x[index], y[index]);
}
}
minima->add(min.getX(), min.getY());
}
if (x == NULL) min = Point(index, y[index]);
else min = Point(x[index], y[index]);
findMax = false;
}
} else {
if (y[index] > min.getY() + delta) {
minima->add(min.getX(), min.getY());
if (x == NULL) max = Point(index, y[index]);
else max = Point(x[index], y[index]);
findMax = true;
}
}
if (++index == length) minima->add(min.getX(), min.getY());
}
}