Skip to content

Commit 332c896

Browse files
committed
iterate through correct peaks during learning. Fixes MIT-LCP#130
1 parent c3ae596 commit 332c896

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

wfdb/processing/qrs.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import copy
22
import numpy as np
3+
34
from scipy import signal
45
from sklearn.preprocessing import normalize
56

@@ -28,7 +29,8 @@ class XQRS(object):
2829
- Conduct learning if specified, to initialize running
2930
parameters of noise and qrs amplitudes, the qrs detection
3031
threshold, and recent rr intervals. If learning is unspecified
31-
or fails, use default parameters.
32+
or fails, use default parameters. See the docstring for the
33+
`_learn_init_params` method of this class for details.
3234
- Run the main detection. Iterate through the local maxima of
3335
the mwi signal. For each local maxima:
3436
@@ -221,12 +223,13 @@ def _learn_init_params(self, n_calib_beats=8):
221223
# Find the local peaks of the signal.
222224
peak_inds_f = find_local_peaks(self.sig_f, self.qrs_radius)
223225

224-
peak_inds_f_r = np.where(peak_inds_f > self.qrs_width)[0]
225-
peak_inds_f_l = np.where(peak_inds_f <= self.sig_len - self.qrs_width)[0]
226+
# Peak numbers at least qrs_width away from signal boundaries
227+
peak_nums_r = np.where(peak_inds_f > self.qrs_width)[0]
228+
peak_nums_l = np.where(peak_inds_f <= self.sig_len - self.qrs_width)[0]
226229

227230
# Skip if no peaks in range
228-
if (not peak_inds_f.size or not peak_inds_f_r.size
229-
or not peak_inds_f_l.size):
231+
if (not peak_inds_f.size or not peak_nums_r.size
232+
or not peak_nums_l.size):
230233
if self.verbose:
231234
print('Failed to find %d beats during learning.'
232235
% n_calib_beats)
@@ -235,7 +238,7 @@ def _learn_init_params(self, n_calib_beats=8):
235238

236239
# Go through the peaks and find qrs peaks and noise peaks.
237240
# only inspect peaks with at least qrs_radius around either side
238-
for peak_num in range(peak_inds_f_r[0], peak_inds_f_l[0]):
241+
for peak_num in range(peak_nums_r[0], peak_nums_l[-1]):
239242
i = peak_inds_f[peak_num]
240243
# Calculate cross-correlation between the filtered signal
241244
# segment and a ricker wavelet
@@ -291,6 +294,7 @@ def _learn_init_params(self, n_calib_beats=8):
291294
noise_amp_recent=noise_amp,
292295
rr_recent=rr_recent,
293296
last_qrs_ind=last_qrs_ind)
297+
self.learned_init_params = True
294298

295299
# Failed to find enough calibration beats. Use default values.
296300
else:
@@ -348,6 +352,8 @@ def _set_default_init_params(self):
348352
rr_recent=rr_recent,
349353
last_qrs_ind=last_qrs_ind)
350354

355+
self.learned_init_params = False
356+
351357
def _is_qrs(self, peak_num, backsearch=False):
352358
"""
353359
Check whether a peak is a qrs complex. It is classified as qrs

0 commit comments

Comments
 (0)