Skip to content

Commit b6215cb

Browse files
authored
Include the right edge in find_local_peaks window (#573)
Fixes #572. The window now covers `radius` samples on both sides like the docstring says, and the third loop that could never run is gone (#474). test_processing.py wasn't being collected because the classes were named `test_*`, so I renamed them. The new test fails on main. XQRS uses `find_local_peaks` for its candidates, so its output shifts a little. On MIT-BIH 100, 101, 105, 108, 119, 200, 203, 207 and 208 it finds 21 fewer of 20856 beats (sensitivity 0.9777 to 0.9766), mostly in 208, and false positives stay at 733. If you'd rather keep XQRS as it was I can look at that side too.
2 parents a96668a + 4df94ba commit b6215cb

3 files changed

Lines changed: 19 additions & 16 deletions

File tree

‎docs/changes.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ Unreleased
8080
**Use only beat annotations in ann2rr**
8181
``ann2rr`` now computes intervals between beat (QRS) annotations only, as the WFDB ``ann2rr`` program does, so rhythm changes, comments and other non-beat annotations no longer split an RR interval in two. The first interval is still measured from the start of the record. To include all annotations as before, pass ``all_annotations=True`` (the equivalent of ``ann2rr -A``).
8282

83+
**Include the right edge of the window in find_local_peaks**
84+
``find_local_peaks`` now compares each sample with ``radius`` samples on both sides, as documented, and is about 3 times faster. The sample ``radius`` positions to the right, and the last sample of the signal, used to be left out. ``XQRS`` uses ``find_local_peaks`` to find candidate peaks, so its detections may change slightly.
85+
8386
Version 4.3.1 (Feb 2026)
8487
-----------------------------
8588

‎src/wfdb/processing/peaks.py‎

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ def find_local_peaks(sig, radius):
5959
6060
A sample is a local peak if it is the largest value within the <radius> samples on
6161
its left and right. In cases where it shares the max value with nearby samples, the
62-
middle sample is classified as the local peak.
62+
first of these samples is classified as the local peak. The search then continues
63+
<radius> samples later, so a flat top that is wider than <radius> samples gives a
64+
peak every <radius> samples along it.
6365
6466
Parameters
6567
----------
@@ -75,28 +77,15 @@ def find_local_peaks(sig, radius):
7577
7678
"""
7779
# TODO: Fix flat mountain scenarios.
80+
sig = np.asarray(sig)
7881
if np.min(sig) == np.max(sig):
7982
return np.empty(0)
8083

8184
peak_inds = []
8285

8386
i = 0
84-
while i < radius + 1:
85-
if sig[i] == max(sig[: i + radius]):
86-
peak_inds.append(i)
87-
i += radius
88-
else:
89-
i += 1
90-
91-
while i < len(sig):
92-
if sig[i] == max(sig[i - radius : i + radius]):
93-
peak_inds.append(i)
94-
i += radius
95-
else:
96-
i += 1
97-
9887
while i < len(sig):
99-
if sig[i] == max(sig[i - radius :]):
88+
if sig[i] == sig[max(0, i - radius) : i + radius + 1].max():
10089
peak_inds.append(i)
10190
i += radius
10291
else:

‎tests/test_processing.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@ def test_find_peaks_empty(self):
105105
assert hp.shape == (0,)
106106
assert sp.shape == (0,)
107107

108+
def test_find_local_peaks(self):
109+
# the sample `radius` positions to the right is part of the window
110+
x = np.array([0, 1, 2, 3, 2, 1, 0], dtype=float)
111+
assert np.array_equal(processing.find_local_peaks(x, 1), [3])
112+
x = np.array([0, 1, 3, 2, 10, 5, 1, 0], dtype=float)
113+
assert np.array_equal(processing.find_local_peaks(x, 2), [4])
114+
115+
def test_find_local_peaks_list(self):
116+
"""A list works as well as an array."""
117+
assert processing.find_local_peaks([0, 1, 2, 3, 2, 1, 0], 1).tolist() == [3]
118+
108119
def test_gqrs(self):
109120
record = wfdb.rdrecord(
110121
"sample-data/100",

0 commit comments

Comments
 (0)