Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Round sample values in physical-to-digital conversion #419

Merged
merged 3 commits into from
Sep 13, 2022
Merged

Conversation

bemoody
Copy link
Collaborator

@bemoody bemoody commented Sep 1, 2022

When converting physical (floating-point) values to digital (integer) values, we generally want to round to the nearest integer, not truncate towards zero.

(Translating floating-point data to integers is more complicated than you'd think, but we should try to avoid creating new sources of errors.)

One example:

>>> p = wfdb.rdrecord('sample-data/v102s', physical=True)
>>> d = wfdb.rdrecord('sample-data/v102s', physical=False)
>>> p.adc() == d.d_signal
array([[ True,  True,  True,  True],
       [ True,  True, False,  True],
       [ True,  True,  True,  True],
       ...,
       [ True,  True,  True,  True],
       [ True,  True,  True,  True],
       [ True,  True,  True,  True]])

(we would of course expect these arrays to be equal.)

This fixes issue #418.

edit: I am not good at copy and paste

Benjamin Moody added 3 commits September 1, 2022 14:42
The variable e_p_signal is not defined here; self.e_p_signal is what
was intended.
When converting physical to digital values, the input values are
floating-point and therefore may have rounding errors.  We want to
round each value to the nearest integer before calling istype, in
order to avoid adding a bias towards zero.

This applies to adc in all four modes: expanded and not expanded,
in-place and not-in-place.

("In-place" here means both that we can overwrite the original
floating-point arrays, and that the record attributes will be updated
afterwards.  In "not-in-place" mode, we are operating on copies of the
original arrays.)
When converting physical to digital values, the input values are
floating-point and therefore may have rounding errors.  We want to
ensure that the package rounds each value to the nearest integer, in
order to avoid adding a bias towards zero.

This test case tests all four code paths in Record.adc, and also
checks that the high-level wfdb.wrsamp function works correctly for a
round trip from physical to digital to physical.  Note that wrsamp
currently doesn't support expanded/multi-frequency data, so that is
not tested.
Copy link
Member

@tompollard tompollard left a comment

Choose a reason for hiding this comment

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

Looks good to me!

@tompollard tompollard merged commit f07bfff into main Sep 13, 2022
@tompollard tompollard deleted the adc-fixes branch September 13, 2022 19:47
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