Skip to content

Include the right edge of the search window in correct_peaks - #611

Merged
tompollard merged 1 commit into
MIT-LCP:mainfrom
TheNetherWatcher:shift-peaks-window
Oct 31, 2026
Merged

tompollard merged 1 commit into
MIT-LCP:mainfrom
TheNetherWatcher:shift-peaks-window

Conversation

@TheNetherWatcher

Copy link
Copy Markdown
Contributor

correct_peaks is meant to search search_radius samples on both sides of each peak, but shift_peaks slices sig[ind - r : min(ind + r, sig_len - 1)]. That leaves out ind + r, and the last sample of the signal, so a peak there can't be found:

sig = np.zeros(30); sig[13] = 5; sig[29] = 5
processing.correct_peaks(sig, np.array([10]), search_radius=3, smooth_window_size=9, peak_dir="up")  # [7], expected [13]
processing.correct_peaks(sig, np.array([27]), search_radius=5, smooth_window_size=9, peak_dir="up")  # [22], expected [29]

Fix: slice up to min(ind + r + 1, sig_len).

Effect on record 100 (correct_peaks on the reference beats, XQRS and GQRS, radius 10, 36 and 61, all four peak_dir options):

  • With the radius used in the existing test (61), only one peak moves, and only for peak_dir="down": the last beat, now on the final sample.
  • With radius 10 on GQRS beats ("up", "both", "compare"), almost every peak moves one sample right, from ind + 9 to ind + 10. In all 2271 cases the new sample is higher in the smoothed-difference signal, so the old window was cutting off the maximum.
  • The existing test_correct_peaks expectations are unchanged.
  • XQRS and GQRS don't call correct_peaks, so their detections are unchanged.

This is the same kind of off-by-one as #572 / #573, but in shift_peaks rather than find_local_peaks. The two PRs touch different functions and don't depend on each other.

Tests: a peak exactly search_radius after the guess, and a peak on the last sample. The test fails on main. pytest -m "not network" and the docs build pass.

@tompollard tompollard left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Thanks for spotting that! Yes, I've opened #613 for it: the offset was subtracted instead of added, so flipping the sign fixes both of your examples. I also rebased this PR onto main after #601 was merged.

@TheNetherWatcher
TheNetherWatcher force-pushed the shift-peaks-window branch 2 times, most recently from 8a9c982 to dd83a76 Compare October 31, 2026 18:42
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.

@tompollard tompollard left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks great, thanks!

@tompollard
tompollard merged commit 7846e62 into MIT-LCP:main Oct 31, 2026
18 checks passed
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.
@TheNetherWatcher
TheNetherWatcher deleted the shift-peaks-window branch November 1, 2026 06:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants