Skip to content

Commit 3775719

Browse files
committed
Optimise _ecg_findpeaks_elgendi
Signed-off-by: Stavros Avramidis <[email protected]>
1 parent cdbefba commit 3775719

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

neurokit2/ecg/ecg_findpeaks.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -738,24 +738,24 @@ def _ecg_findpeaks_elgendi(signal, sampling_rate=1000, **kwargs):
738738
window2 = int(0.6 * sampling_rate)
739739
mwa_beat = _ecg_findpeaks_MWA(abs(signal), window2)
740740

741-
blocks = np.zeros(len(signal))
742-
block_height = np.max(signal)
741+
blocks = mwa_qrs > mwa_beat
743742

744-
for i in range(len(mwa_qrs)): # pylint: disable=C0200
745-
blocks[i] = block_height if mwa_qrs[i] > mwa_beat[i] else 0
746743
QRS = []
744+
745+
qrs_duration_threshold = int(0.08 * Fs)
746+
rr_distance_threshold = int(0.3 * Fs)
747747

748-
for i in range(1, len(blocks)):
749-
if blocks[i - 1] == 0 and blocks[i] == block_height:
748+
for i, (prev, cur) in enumerate(zip(blocks, blocks[1:])):
749+
if prev < cur:
750750
start = i
751751

752-
elif blocks[i - 1] == block_height and blocks[i] == 0:
752+
elif prev > cur:
753753
end = i - 1
754754

755-
if end - start > int(0.08 * sampling_rate):
755+
if end - start > qrs_duration_threshold:
756756
detection = np.argmax(signal[start : end + 1]) + start
757757
if QRS:
758-
if detection - QRS[-1] > int(0.3 * sampling_rate):
758+
if detection - QRS[-1] > rr_distance_threshold:
759759
QRS.append(detection)
760760
else:
761761
QRS.append(detection)

0 commit comments

Comments
 (0)