Skip to content

Fix correct_peaks for peaks near the start of the signal - #613

Merged
tompollard merged 2 commits into
MIT-LCP:mainfrom
TheNetherWatcher:correct-peaks-early
Oct 31, 2026
Merged

tompollard merged 2 commits into
MIT-LCP:mainfrom
TheNetherWatcher:correct-peaks-early

Conversation

@TheNetherWatcher

Copy link
Copy Markdown
Contributor

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:

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.

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.
For peaks closer to the start than search_radius, the search window
starts at 0, so argmax already gives the absolute index. The offset was
subtracted instead of added, moving these peaks to the wrong place,
often before the start of the signal.
@tompollard
tompollard merged commit 7099aae into MIT-LCP:main Oct 31, 2026
18 checks passed
@TheNetherWatcher
TheNetherWatcher deleted the correct-peaks-early branch November 1, 2026 06:12
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