Skip to content

Commit d568d90

Browse files
Include the right edge of the search window in correct_peaks
shift_peaks sliced sig[ind - r : min(ind + r, sig_len - 1)], which leaves out ind + r and the last sample of the signal. A peak exactly search_radius samples after the original index, or on the last sample, could not be found.
1 parent 577a18b commit d568d90

3 files changed

Lines changed: 19 additions & 1 deletion

File tree

‎docs/changes.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ Unreleased
3535
**Use Ruff for formatting and linting**
3636
The ``dev`` extra now includes ``ruff`` instead of ``black`` and ``pylint``, and the code is formatted with a line length of 88 (the default).
3737

38+
**Fix search window in correct_peaks**
39+
``correct_peaks`` now searches ``search_radius`` samples on both sides of each peak, including the last sample of the signal. It used to stop one sample short on the right, so a peak exactly ``search_radius`` samples after the original index, or on the last sample, could not be found.
40+
3841
Version 4.3.1 (Feb 2026)
3942
-----------------------------
4043

‎src/wfdb/processing/peaks.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def shift_peaks(sig, peak_inds, search_radius, peak_up):
225225
for i in range(n_peaks):
226226
ind = peak_inds[i]
227227
local_sig = sig[
228-
max(0, ind - search_radius) : min(ind + search_radius, sig_len - 1)
228+
max(0, ind - search_radius) : min(ind + search_radius + 1, sig_len)
229229
]
230230

231231
if peak_up:

‎tests/test_processing.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,21 @@ def test_correct_peaks(self):
173173

174174
assert np.array_equal(yz, expected_peaks)
175175

176+
def test_correct_peaks_window_edges(self):
177+
"""Peaks at the edge of the search radius or the signal are found."""
178+
sig = np.zeros(30)
179+
sig[13] = 5
180+
sig[29] = 5
181+
182+
# Peak exactly search_radius samples after the guess
183+
assert processing.correct_peaks(
184+
sig, np.array([10]), search_radius=3, smooth_window_size=9, peak_dir="up"
185+
).tolist() == [13]
186+
# Peak on the last sample of the signal
187+
assert processing.correct_peaks(
188+
sig, np.array([27]), search_radius=5, smooth_window_size=9, peak_dir="up"
189+
).tolist() == [29]
190+
176191

177192
class TestComputeHr:
178193
"""Tests for compute_hr."""

0 commit comments

Comments
 (0)