Skip to content

wfdb_to_mat silently wraps samples that don't fit the output type #641

Closed

Description

@tompollard

wfdb_to_mat picks the output type (uint8, int16 or int32) from adc_res alone, and then casts the samples to it without checking that they fit. When a valid sample is outside the range of that type, it wraps silently. It can even wrap to the invalid sample value, so it is read back as missing.

This happens when the shift by adc_zero moves a sample out of range, or when the header's resolution is smaller than the values actually stored. For example, with format 16 records:

Input Written to the .mat Read back
adc_zero=1, samples [-32767, 0, 32767] [-32768, -1, 32766] the first sample becomes the invalid value (NaN)
adc_zero=-1, samples [-32767, 0, 32767] [-32766, 1, -32768] 32767 wraps to the invalid value
adc_res=8, samples [100, 300, -300] uint8 [228, 172, 84] 300 and -300 are corrupted

This happens on main, and also with the changes in #633.

Possible fixes:

  • check the min and max of the shifted samples (leaving out missing samples) against the output type, and use the next larger type if they don't fit
  • or raise an error instead of writing wrong values

Found while reviewing #633.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions