Repository navigation
Fix wfdb_to_wav failing for every record - #632
Conversation
wfdb_to_wav read the record with smooth_frames=False and then called record.adc(), which converts p_signal. Since version 4.0.0, rdrecord with smooth_frames=False always returns the expanded samples in e_p_signal and leaves p_signal unset, so the conversion failed for every record (MIT-LCP#410). Read the record with smooth_frames=True instead, as suggested in the issue. A .wav file holds one sample per frame for each signal, so this is also what is needed for signals with more than one sample per frame. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CaYn6ePikBune9dYf3oiPf
With write_header=True, wfdb_to_wav set samps_per_fram instead of samps_per_frame, so the header kept the samples per frame of the input record. Now that a record with more than one sample per frame can be converted, its header described the .wav file wrongly, and reading the converted record back failed with "Samples were not loaded correctly". The .wav file holds one sample per frame for each signal, which is also what mit2wav writes in its header. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CaYn6ePikBune9dYf3oiPf
tompollard
left a comment
There was a problem hiding this comment.
Thanks for this, and for the careful write-up of what is left alone. Both changes look correct to me, and I don't see anything that should block merging. The new tests pass locally, and ruff is clean.
smooth_frames=Trueis the right fix.rdrecordaverages frames on the digital values before converting to physical, sorecord.adc()gets back integer frame means, and the output follows themit2wavrule.test01_00s_framecovers this.- The
samps_per_frametypo fix is covered bytest_write_header[test01_00s_frame]. - The tests read the output with
waveand compare it with the shift rule rather than with the implementation. Usingmonkeypatch.chdiralso keeps the header from being written next to the input record. Exact equality onp_signalis safe there, because gain and baseline are scaled by the same power of two.
A few things that could be follow-ups rather than changes here:
-
Invalid samples become 0 below 12 bits. This is not caused by this PR, but it isn't in the list of things left alone. For a format 212 record with an invalid sample (-2048):
adc_resvalid samples invalid sample in the .wav 12 1600, 3200-3276811 3200, 64000At 11 bits,
-2048 << 5overflows int16 and wraps to 0, so a gap in record 100 or other MIT-BIH records reads as a real zero. Mapping invalid samples to -32768 before the shift would fix it. -
Reading with
physical=Falseand usingrecord.d_signalwould avoid the physical-to-digital round trip throughadc()and a full-length float64 copy of the signal. -
No test covers
sampfrom/sampto, the other path intordrecord. A short one would also check thatinit_valuecomes from the first frame read. -
The docstring still documents
channels, which is ignored. A note there (or passing it through tordrecord) would help until it's handled.
tompollard
left a comment
There was a problem hiding this comment.
Approving. The follow-ups from my earlier comment are filed as separate issues.
Follow-ups to #632 for `wfdb_to_wav`. They change the same few lines, so they are in one PR. - **Read digital samples (#638).** The record is read with `physical=False`, and `d_signal` is used directly. Before, physical values were read and converted back with `adc()`. That needed a full-length float64 copy, and the output depended on how `adc()` rounds. The old comment about the mismatches from that conversion is removed. - **`channels` (#640).** It is now passed to `rdrecord`. Before, it was ignored and every signal was written. - **Invalid samples (#637).** They are written as -32768, the format 16 invalid value, so they are read back as missing (also with `write_header=True`). Before, they were shifted like any other sample, and below 12 bits they overflowed. For a format 212 record with an invalid sample (-2048): | `adc_res` | before | now | |---|---|---| | 12 | `-32768` | `-32768` | | 11 | `0` | `-32768` | Format 8 has no invalid value, so those channels are not changed. - **Tests (#639).** New tests for `sampfrom`/`sampto` (including `init_value` in the header written with `write_header=True`), for `channels`, and for invalid samples with 11 and 12 bits. The `channels` test and the 11-bit invalid sample test fail on `main`. The others already pass there, as expected. Testing: `uv run pytest tests/io tests/test_record.py -m "not network" -W error` passes (114), as do `ruff format --check` and `ruff check`. Changelog entry added in `changelog.d/`. Fixes #637 Fixes #638 Fixes #639 Fixes #640 <sub>@tompollard 🟥 red 🐳 whale · quince</sub>
wfdb_to_wavfails for every record (#410):As diagnosed in the issue, it reads the record with
smooth_frames=Falseand then callsrecord.adc(), which convertsp_signal. Since 4.0.0,rdrecordwithsmooth_frames=Falsereturns the expanded samples ine_p_signaland leavesp_signalasNone. This PR reads the record withsmooth_frames=True, which is also what a .wav file needs: one sample per frame for each signal.That makes the
write_header=Truecode reachable, so the second commit fixes a typo there (samps_per_framinstead ofsamps_per_frame): for a record with more than one sample per frame, the header kept the input's samples per frame, and the converted record could not be read back.The tests convert
sample-data/100(11-bit) andsample-data/test01_00s_frame(two samples per frame), read the .wav file with the standard library'swavemodule and compare it with(d_signal - adc_zero) << (16 - adc_res), the rulemit2wavuses. A second test reads back the record written withwrite_header=True. Both fail onmainand pass here. For signals with more than one sample per frame, the frame means come fromrdrecord, which truncates them (#335), so they can differ by one unit frommit2wavbuilt with WFDB 10.7.The silent file mentioned in the issue
The reporter's .wav file had no sound after changing this line. This PR does not change that, so it says "Refs" rather than "Fixes". With a resolution of 0 in the header the shift is 16 bits, and above 16 bits it is negative, which NumPy evaluates to 0. Either way every sample is 0, and the bits-per-sample field of the .wav header is set to that resolution although two bytes are written per sample. Reproduced with:
wrsamp(..., fmt=["32"]): all samples are 0, and the header says 32 bits per samplesample-data/flacformats: the 24-bit signal is all 0sample-data/v102s(resolution 0): all samples are 0, and the header says 0 bits per sample, whichwaverejectsmit2wavbehaves the same: the WFDB library keeps an explicit resolution of 0 (it only fills in a default when the field is missing, although header(5) says that 0 also means the 12-bit default), and a negative shift is undefined in C. So there is no behaviour to copy, and a fix needs a decision: scale the samples down to 16 bits, assume a resolution, or raise an error. I could not download the reporter's VOICED records here, so I don't know which case they hit. For 9 to 16 bits the output follows the rule above exactly in the sample records I checked; with 8 bits the samples are right too, but the header says 8 bits per sample, sowaveand libsndfile read each byte as a sample.Also left alone:
channelsis never passed tordrecord.write_header=True, the header is written to the current directory under the input record's name, so next to the input record it replaces the input header (mit2wavrefuses this). It also names the signal file<record>.waveven ifoutput_filenameis different, and it keeps the checksums of the input record, which do not match the shifted samples.uv run pytest -m "not network"(350 passed),ruff format --check,ruff checkandtowncrier build --draftpass. A changelog entry is included.Refs #410