Skip to content

Match WFDB C rounding for smoothed digital frames - #821

Merged
tompollard merged 2 commits into
mainfrom
fix-digital-frame-rounding
Dec 1, 2026
Merged

tompollard merged 2 commits into
mainfrom
fix-digital-frame-rounding

Conversation

@tompollard

Copy link
Copy Markdown
Member

Summary

  • Round digital frame averages to the nearest integer, with exact halfway cases rounded upward, matching the WFDB C meansamp change. Use quotient and remainder so the rounding step cannot overflow an int64 sum.
  • Cover positive and negative ties, odd samples-per-frame, invalid samples, large integers, and reads through both rdrecord and read_signals.
  • Keep the historical C reference files for the existing skew tests. They predate this change, so those tests now allow the expected one-digital-unit rounding difference; the new tests assert the exact new rule.

Fixes #335.

Checks

  • Full offline suite with FutureWarning treated as an error: 1,548 passed, 4 skipped.
  • Record tests after the final overflow-safe rounding change: 143 passed.
  • Ruff check and format: passed.

@tompollard
tompollard force-pushed the fix-digital-frame-rounding branch from eec50d3 to f4eccbc Compare December 1, 2026 04:45

@tompollard tompollard left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks, the rounding matches the C change exactly: floor(q) + (r >= ceil(n/2)) is equivalent to C's floor((sum + n/2) / n), ties included, and it can't overflow. On real data, the first frame of 041s (raw III samples 168 168 166 164) now gives III=167, I=-42, V=139, matching the C formula applied to the raw samples, where main gave 166 and 138. The offline suite, Ruff and the strict docs build pass locally.

One thing to fix: the changelog only mentions digital reads, but physical uniform reads of multi-frequency records change too, since they're derived from the rounded digital values. On the PR's own test file, the physical values go from [0.0, 0.0, 0.005, -0.005] on main to [0.005, 0.0, 0.01, -0.005]. Could the entry say that uniform reads of multi-frequency records, digital and physical, can differ by up to one ADC unit?

Optional: if WFDB ≥ 10.7 is available, regenerating the reference files behind test_4a, test_4b and test_4c would let them assert exact values again instead of a one-unit tolerance.

@tompollard

Copy link
Copy Markdown
Member Author

Thanks. I updated the changelog in c9cab04 to say uniform reads of multi-frequency records are affected, including physical values derived from the rounded digital samples; the difference can be up to one ADC unit. I have not regenerated the C reference files because the C tools are not installed here. The new targeted tests assert the exact rounding rule, and the existing reference tests retain the one-unit tolerance.

@tompollard tompollard left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks, the changelog now covers both digital and physical uniform reads. The rounding is unchanged from what I checked against the C formula. Not regenerating the reference files without the C tools is fine. The full offline suite, Ruff and the strict docs build pass locally.

Ready to merge once CI is green.

@tompollard
tompollard merged commit 6b7d605 into main Dec 1, 2026
19 checks passed
@tompollard
tompollard deleted the fix-digital-frame-rounding branch December 1, 2026 04:54
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.

Change rounding method for low-resolution / smooth_frames?

1 participant