Repository navigation
Include the right edge of the search window in correct_peaks - #611
Merged
Merged
Conversation
tompollard
reviewed
Oct 31, 2026
tompollard
left a comment
Member
There was a problem hiding this comment.
Thanks, this looks good! The new slice matches the documented search radius, and the test fails on main.
While testing, I noticed a separate bug at the other end of the signal, in the "adjust early values" loop just below. When ind < search_radius, the window starts at 0, so argmax already gives the absolute index, but the loop subtracts search_radius - ind when it should add it. On main and on this branch:
sig = np.zeros(30); sig[0] = 5
processing.correct_peaks(sig, np.array([2]), 5, 9, "up") # [-6], expected [0]
sig = np.zeros(30); sig[4] = 5
processing.correct_peaks(sig, np.array([2]), 5, 9, "up") # [-2], expected [4]That's not caused by this PR, so it can be a separate fix. Would you like to take it on?
TheNetherWatcher
force-pushed
the
shift-peaks-window
branch
from
October 31, 2026 17:39
13cd081 to
d568d90
Compare
Contributor
Author
TheNetherWatcher
force-pushed
the
shift-peaks-window
branch
2 times, most recently
from
October 31, 2026 18:42
8a9c982 to
dd83a76
Compare
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.
TheNetherWatcher
force-pushed
the
shift-peaks-window
branch
from
October 31, 2026 18:49
dd83a76 to
da08f5b
Compare
tompollard
added a commit
that referenced
this pull request
Oct 31, 2026
Found by @tompollard while reviewing #611. For a peak closer to the start than `search_radius`, the search window starts at 0 instead of `ind - search_radius`, so `argmax` already gives the absolute index. The "adjust early values" loop then subtracted `search_radius - ind` when it should have added it, moving the peak to the wrong place, often before the start of the signal: ```python sig = np.zeros(30); sig[0] = 5 processing.correct_peaks(sig, np.array([2]), 5, 9, "up") # [-6], expected [0] sig = np.zeros(30); sig[4] = 5 processing.correct_peaks(sig, np.array([2]), 5, 9, "up") # [-2], expected [4] ``` The fix flips the sign. Peaks further than `search_radius` from the start are unaffected. This is based on #611, since both add tests in the same place. Only the last commit is new here; I'll rebase once #611 is merged. Tests: a peak near the start at four positions, upward and downward. All 8 cases fail on `main`. `pytest -m "not network"` passes (108 tests), and the docs build passes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
correct_peaksis meant to searchsearch_radiussamples on both sides of each peak, butshift_peaksslicessig[ind - r : min(ind + r, sig_len - 1)]. That leaves outind + r, and the last sample of the signal, so a peak there can't be found:Fix: slice up to
min(ind + r + 1, sig_len).Effect on record 100 (
correct_peakson the reference beats, XQRS and GQRS, radius 10, 36 and 61, all fourpeak_diroptions):peak_dir="down": the last beat, now on the final sample."up","both","compare"), almost every peak moves one sample right, fromind + 9toind + 10. In all 2271 cases the new sample is higher in the smoothed-difference signal, so the old window was cutting off the maximum.test_correct_peaksexpectations are unchanged.correct_peaks, so their detections are unchanged.This is the same kind of off-by-one as #572 / #573, but in
shift_peaksrather thanfind_local_peaks. The two PRs touch different functions and don't depend on each other.Tests: a peak exactly
search_radiusafter the guess, and a peak on the last sample. The test fails onmain.pytest -m "not network"and the docs build pass.