Skip to content

Format 8 reads that start after frame 0 return wrong values #909

Done

Description

@tompollard

Format 8 stores each sample as the difference from the previous one, starting from the header's initial value. When a read starts after the first frames, the decoder restarts that running sum at the window start instead of carrying it from frame 0. The returned values are wrong, and nothing reports an error.

Using the format 8 channel of sample-data/binformats (channel 0, initial value -2047), on current main (239206d):

import wfdb

path = "sample-data/binformats"
full = wfdb.read_signals(path, kind="digital", layout="uniform", channels=[0])
values = full.signals.values[:, 0]

for start in (1, 2, 477):
    window = wfdb.read_signals(
        path, kind="digital", layout="uniform", channels=[0],
        start=start, stop=start + 3,
    )
    print(start, values[start:start + 3].tolist(), window.signals.values[:, 0].tolist())
start full read, sliced windowed read
1 [-1920, -1793, -1666] [-1920, -1793, -1666]
2 [-1793, -1666, -1539] [-1920, -1793, -1666]
477 [570, 442, 314] [-2175, -2303, -2431]

Each windowed read restarts from the initial value at the window start. rdrecord(path, physical=False, sampfrom=477, sampto=480, channels=[0]) returns the same wrong values, and so does v5.17.0, so this has been in the shared decoder for a long time, not in the new readers. Physical reads are affected the same way, through calibration of the wrong digital values.

Expected: a window returns the same samples as the full read sliced to that window. That requires decoding a format 8 channel from frame 0, or at least from a point where the running value is known, up to the window start. WFDB's own library has to do the same for this format.

Suggested tests:

  • for several start values, including 1, 2, a mid-record frame and the last frame, both kinds and both layouts, compare a windowed read with the full read sliced to the same window;
  • the same comparison through rdrecord(..., sampfrom=...);
  • a format 8 segment inside a multi-segment record, where the segment's local window starts after frame 0.

Format 8 is rare in PhysioNet data, but any windowed read of it is silently wrong. A changelog entry should say which reads were affected.

Activity

  1. tompollard commented on Dec 4, 2026

    @tompollard
    MemberAuthor

    A correction to my description above: I wrote that WFDB's own library "has to do the same", meaning reconstruct the running value from frame 0. It doesn't. In lib/signal.c, isgsetframe handles format 8 with a plain byte seek (case 8: case 80: default: b = nn;). It discards the bytes before the target sample without accumulating them, and sets the running value to the header's initval only when the signal is opened. So rdsamp -f N on a format 8 record starts from the initial value at frame N, as wfdb-python did.

    We've decided to return the values the format defines anyway (#911), because the alternative gives wrong samples. Windowed format 8 reads will therefore intentionally differ from WFDB's rdsamp, and #911 should document that.

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