This repository has been archived by the owner on Jan 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpanTompkins.cpp
316 lines (272 loc) · 6.7 KB
/
panTompkins.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include <memory>
#include <stdexcept>
#include <deque>
#include "threshold.h"
#include "filters.h"
typedef int dataType;
struct panTompkins
{
private:
size_t samplefrequency;
size_t window;
size_t rrmin;
size_t rrmax;
size_t slopemax;
threshold<dataType> threshold_i;
threshold<dataType> threshold_f;
int rr1[8];
int rr2[8];
int rravg1;
int rravg2;
int rrlow = 0;
int rrhigh = 0;
int rrmiss = 0;
bool regular = true;
public:
std::deque<size_t> qpeaks;
std::deque<size_t> rpeaks;
std::deque<size_t> speaks;
explicit panTompkins(size_t fs = 360) :
samplefrequency(fs),
window(0.15*fs), //150 ms
rrmin(0.2*fs), //200 ms
rrmax(0.36*fs), //360 ms
slopemax(0.75*fs), //10 ???
{
}
void detectPeaks(const std::deque<dataType> & signal);
private:
dataType findMaxSlope(size_t i, std::deque<dataType> & values)
{
assert(i>=slopemax);
dataType max_value = 0;
for (size_t j = i - slopemax; j <= i; ++j)
if (values[j] > max_value) max_value = values[j];
return max_value;
}
void detectRpeaks(const std::deque<dataType> & bandpass, size_t delay);
void updateRR(int index)
{
rravg1 = 0;
for (size_t i = 0; i < 7; ++i)
{
rr1[i] = rr1[i+1];
rravg1 += rr1[i];
}
rr1[7] = index; //add
rravg1 += rr1[7];
rravg1 *= 0.125; // 1/8
if ((rr1[7] >= rrlow) && (rr1[7] <= rrhigh))
{
rravg2 = 0;
for (size_t i = 0; i < 7; ++i)
{
rr2[i] = rr2[i+1];
rravg2 += rr2[i];
}
rr2[7] = rr1[7]; //add
rravg2 += rr2[7];
rravg2 *= 0.125; // 1/8
rrlow = 0.92 * rravg2;
rrhigh = 1.16 * rravg2;
rrmiss = 1.66 * rravg2;
}
bool prevRegular = regular;
if (rravg1 == rravg2)
{
regular = true;
} else {
regular = false;
if (prevRegular)
{
threshold_i.half();
threshold_f.half();
}
}
}
};
const std::deque<size_t> findLeftMin(const std::deque<dataType> & bandpass, const std::deque<size_t> & peaks)
{
std::deque<size_t> result;
for (size_t i = 0; i < peaks.size(); ++i)
{
size_t n = peaks[i];
if (n-1<0) break;
while(bandpass[n]>bandpass[n-1])
{
--n;
if (n<0) break;
}
result.push_back(n);
}
return result;
}
const std::deque<size_t> findRightMin(const std::deque<dataType> & bandpass, const std::deque<size_t> & peaks)
{
std::deque<size_t> result;
for (size_t i = 0; i < peaks.size(); ++i)
{
size_t n = peaks[i];
if (n+1>=bandpass.size()) break;
while(bandpass[n]>bandpass[n+1])
{
++n;
if (n>=bandpass.size()) break;
}
result.push_back(n);
}
return result;
}
// input signal 900-1000
// TODO normalize input -2..+2
void panTompkins::detectPeaks(const std::deque<dataType> & signal)
{
if (signal.size() < samplefrequency*2)
throw std::length_error("input signal too short");
// DC was not proposed on the original paper.
// It is not necessary and can be removed if your sensor or database has no DC noise.
std::deque<dataType> dcblock = dcFilter(signal);
//1) Bandpass filter = LP + HP filter, filters work only for 200 Hz sampling rate
std::deque<dataType> lowpass = lowPassFilter(dcblock);
//??? normalize(lowpass);
std::deque<dataType> highpass = highPassFilter(lowpass);
//??? normalize(highpass);
const double LOW_CUT = 5.0;
const double HIGH_CUT = 15.0;
std::deque<dataType> bandpass = bandPassFilter(highpass, LOW_CUT, HIGH_CUT, samplefrequency/2); //???
//qpeaks.clear();
//rpeaks.clear();
//speaks.clear();
//TODO calculate sum delay by filters and flush peaks from begin
size_t delay = 0; //TODO
detectRpeaks(bandpass, delay);
qpeaks = findLeftMin(bandpass, rpeaks);
speaks = findRightMin(bandpass, rpeaks);
}
void panTompkins::detectRpeaks(const std::deque<dataType> & bandpass, size_t delay)
{
//2) Differentiator
std::deque<dataType> derivative = derivativeFilter(bandpass); //???, samplefrequency);
//??? normalize(derivative);
//3) Squaring
std::deque<dataType> squared = squaredFilter(derivative);
//4) Moving window integration
std::deque<dataType> integral = MWI(squared, window);
for (size_t i = 0; i < 8; ++i)
{
rr1[i] = 0;
rr2[i] = 0;
}
size_t lastQRS = 0;
dataType lastSlope = 0;
regular = true;
rpeaks.clear();
std::deque<bool> peaks(bandpass.size());
for (size_t current = 0; current < bandpass.size(); ++current)
{
bool qrs = false;
size_t sample = current+1; //???
if ((integral[current] >= threshold_i.i1) &&
(bandpass[current] >= threshold_f.i1))
{
if (sample - lastQRS > rrmin)
{
dataType currentSlope = findMaxSlope(current, squared); //integral
if (sample - lastQRS <= rrmax)
{
if (currentSlope <= lastSlope/2)
{
//T-wave found
qrs = false;
} else
{
lastSlope = currentSlope;
threshold_i.updateSignal(integral[current]);
threshold_f.updateSignal(bandpass[current]);
qrs = true;
}
} else
{
lastSlope = currentSlope;
threshold_i.updateSignal(integral[current]);
threshold_f.updateSignal(bandpass[current]);
qrs = true;
}
} else
{
threshold_i.updateNoise(integral[current]);
threshold_f.updateNoise(bandpass[current]);
qrs = false;
peaks[current] = qrs;
//if (sample > DELAY + BUFFSIZE) output(rpeaks[0]);
continue;
}
}
if (qrs)
{
updateRR(sample - lastQRS);
lastQRS = sample;
} else
{
//back search
if ((sample - lastQRS > (size_t)rrmiss) &&
(sample - lastQRS > rrmin))
{
size_t i;
//do search
for (i = current - (sample - lastQRS) + rrmin;
i < (size_t)current;
++i)
{
if ( (integral[i] > threshold_i.i2) &&
(bandpass[i] > threshold_f.i2))
{
dataType currentSlope = findMaxSlope(i, squared); //integral
if (
(currentSlope < lastSlope/2) &&
(i + sample) < lastQRS + 0.36*lastQRS)
{
//T-wave found
qrs = false;
} else
{
lastSlope = currentSlope;
threshold_i.updateSignalBackSearch(integral[i]);
threshold_f.updateSignalBackSearch(bandpass[i]);
qrs = true;
updateRR(sample - (current - i) - lastQRS);
lastQRS = sample - (current - i);
break;
}
}
} //for
if (qrs)
{
peaks[current] = false;
peaks[i] = true;
//if (sample > DELAY + BUFFSIZE) output(rpeaks[0]);
continue;
}
} //if
if (!qrs)
{
if ((integral[current] >= threshold_i.i1) ||
(bandpass[current] >= threshold_f.i1))
{
threshold_i.updateNoise(integral[current]);
threshold_f.updateNoise(bandpass[current]);
}
}
} // !qrs
peaks[current] = qrs;
//if (sample > DELAY + BUFFSIZE) output(rpeaks[0]);
}
for (size_t i = 0; i < peaks.size(); ++i)
{
if (peaks[i])
{
assert(i>delay)
rpeaks.push_back(i-delay);
}
}
}