Skip to content

Zero-pad waveform averages before the record starts - #731

Merged
tompollard merged 6 commits into
MIT-LCP:mainfrom
Kulraj69:fix-sigavg-start-padding
Nov 25, 2026
Merged

tompollard merged 6 commits into
MIT-LCP:mainfrom
Kulraj69:fix-sigavg-start-padding

Conversation

@Kulraj69

@Kulraj69 Kulraj69 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Problem

processing.sigavg reads negative sample positions as NumPy negative indices. For a beat near the start of a record, the averaging window before time zero therefore contains unrelated samples from the end of the record.

This is reachable through the public API with real local records and annotations. With a beat at sample zero, changing only the final sample from 9 to 9000 changes the pre-record estimate.

Change

Check both bounds before adding a sample to the average. For an out-of-range position at either end, add the channel's digital baseline so the padding becomes zero in physical units after calibration. The time grid and denominator are unchanged.

The first revision only skipped out-of-range samples. Review identified that digital zero is not physical zero for a nonzero baseline. This revision corrects both boundaries and adds eight regressions, including a flat zero-mV signal with baseline 100 and gain 2, positive and negative baselines, different gains, and mixed boundary/interior beats.

The tests write and read actual WFDB records and annotations. They also retain the original first/second-sample tail-independence tests and interior/end-padding controls. Expected results use explicit bounded physical-unit averages.

Validation

Against upstream 98df2c9fba00943004ceac42677744f85a98e702, using Python 3.12.16:

  • Original six-case regression file against unpatched upstream: 4 failed, 2 passed.
  • Expanded fourteen-case regression file against the first published revision: 8 failed, 6 passed.
  • Revised regression file plus tests/test_processing.py and tests/test_hr.py, excluding network tests: 230 passed. Repeated against the exact files downloaded from the revised branch: 230 passed.
  • Ruff 0.17.3 check and format passed for the changed Python files.
  • Strict Sphinx 9.0.4 HTML build (-W --keep-going -E) passed, including the revised changelog.

Other private fixes were not applied during these isolated runs. Full-repository tests and other Python/dependency versions were not run locally. Remote CI is separate from these local results.

Prepared and tested with AI assistance. I have not obtained independent human review of the revised code; the earlier changes-requested review remains pending re-review.

Updated indexing logic to handle negative NumPy indices and removed unnecessary exception handling.
Update documentation to clarify waveform average padding behavior.

@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.

The bounds check fixes the negative-index wraparound, and the focused tests pass. I found one edge case to address before approval: sigavg returns physical units, but skipping an out-of-range position contributes digital zero before the code subtracts rec.baseline[j] and divides by rec.adc_gain[j].

For example, with a flat signal of digital value 100, baseline 100, and gain 2, the signal is 0 mV, but a beat at sample zero produces -50 mV at both pre-record positions. The existing end padding has the same issue. Could you contribute the channel's digital baseline for out-of-range positions at both ends, so the calibrated padding is 0 in the returned units, and add a regression case with a nonzero baseline?

I ran the new and existing focused processing tests locally: 222 passed. Ruff check and format also passed.

Added tests for signal averaging with zero padding and flat signals.
Updated the description of waveform average padding to clarify the handling of out-of-range positions and the use of the channel's digital baseline.

@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 @Kulraj69

@tompollard
tompollard merged commit 63a801b into MIT-LCP:main Nov 25, 2026
19 checks passed
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