From 9a5646913dc87247347a54ae83f5b7744047a658 Mon Sep 17 00:00:00 2001 From: Jonatan Kronander Date: Fri, 13 Nov 2026 20:48:43 +0000 Subject: [PATCH 1/2] Fix wfdb_to_wav failing for every record 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 (#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 Claude-Session: https://claude.ai/code/session_01CaYn6ePikBune9dYf3oiPf --- changelog.d/wfdb-to-wav-error.rst | 2 ++ src/wfdb/io/convert/wav.py | 2 +- tests/io/test_convert.py | 27 +++++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 changelog.d/wfdb-to-wav-error.rst diff --git a/changelog.d/wfdb-to-wav-error.rst b/changelog.d/wfdb-to-wav-error.rst new file mode 100644 index 00000000..4026d220 --- /dev/null +++ b/changelog.d/wfdb-to-wav-error.rst @@ -0,0 +1,2 @@ +**Fix wfdb_to_wav failing for every record** + ``wfdb_to_wav`` now reads the record with ``smooth_frames=True``, since a .wav file holds one sample per frame for each signal. It used to read the expanded samples, and then failed with an ``AttributeError`` (a ``TypeError`` in older versions) because ``p_signal`` was not set. diff --git a/src/wfdb/io/convert/wav.py b/src/wfdb/io/convert/wav.py index ef3284c7..f9594591 100644 --- a/src/wfdb/io/convert/wav.py +++ b/src/wfdb/io/convert/wav.py @@ -62,7 +62,7 @@ def wfdb_to_wav( pn_dir=pn_dir, sampfrom=sampfrom, sampto=sampto, - smooth_frames=False, + smooth_frames=True, ) record_name_out = record_name.split(os.sep)[-1].replace("-", "_") diff --git a/tests/io/test_convert.py b/tests/io/test_convert.py index ae56b803..f72b7d5b 100644 --- a/tests/io/test_convert.py +++ b/tests/io/test_convert.py @@ -1,9 +1,11 @@ import os +import wave import numpy as np import pytest from wfdb.io.annotation import rdann +from wfdb.io.convert import wfdb_to_wav from wfdb.io.convert.csv import csv2ann, csv_to_wfdb from wfdb.io.convert.edf import read_edf from wfdb.io.record import rdheader, rdrecord @@ -322,3 +324,28 @@ def test_record_name_with_dots(self, tmp_path, monkeypatch): out_dir.mkdir() csv2ann(str(data_dir / "x.csv"), fs=100, write_dir=str(out_dir)) assert (out_dir / "x.atr").exists() + + +class TestWfdbToWav: + """Tests for wfdb_to_wav.""" + + # 11-bit samples, and a signal with two samples per frame + @pytest.mark.parametrize("record_name", ["100", "test01_00s_frame"]) + def test_samples(self, tmp_path, record_name): + """One 16-bit sample per frame is written for each signal (#410).""" + wav_file = str(tmp_path / "out.wav") + wfdb_to_wav(f"sample-data/{record_name}", output_filename=wav_file) + + # Samples are offset by the ADC zero and shifted to fill 16 bits + record = rdrecord(f"sample-data/{record_name}", physical=False) + shift = np.array([16 - res for res in record.adc_res]) + expected = (record.d_signal - record.adc_zero) << shift + + with wave.open(wav_file, "rb") as wav: + assert wav.getnchannels() == record.n_sig + assert wav.getsampwidth() == 2 + assert wav.getframerate() == record.fs + assert wav.getnframes() == record.sig_len + frames = wav.readframes(record.sig_len) + samples = np.frombuffer(frames, dtype=" Date: Sat, 14 Nov 2026 00:02:33 +0000 Subject: [PATCH 2/2] Write one sample per frame in the header of wfdb_to_wav 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 Claude-Session: https://claude.ai/code/session_01CaYn6ePikBune9dYf3oiPf --- changelog.d/wfdb-to-wav-error.rst | 2 +- src/wfdb/io/convert/wav.py | 2 +- tests/io/test_convert.py | 13 +++++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/changelog.d/wfdb-to-wav-error.rst b/changelog.d/wfdb-to-wav-error.rst index 4026d220..2b5fb8f1 100644 --- a/changelog.d/wfdb-to-wav-error.rst +++ b/changelog.d/wfdb-to-wav-error.rst @@ -1,2 +1,2 @@ **Fix wfdb_to_wav failing for every record** - ``wfdb_to_wav`` now reads the record with ``smooth_frames=True``, since a .wav file holds one sample per frame for each signal. It used to read the expanded samples, and then failed with an ``AttributeError`` (a ``TypeError`` in older versions) because ``p_signal`` was not set. + ``wfdb_to_wav`` now reads the record with ``smooth_frames=True``, since a .wav file holds one sample per frame for each signal. It used to read the expanded samples, and then failed with an ``AttributeError`` (a ``TypeError`` in older versions) because ``p_signal`` was not set. With ``write_header=True``, the header now gives one sample per frame for each signal, as in the .wav file, so a converted record with more than one sample per frame can be read back. diff --git a/src/wfdb/io/convert/wav.py b/src/wfdb/io/convert/wav.py index f9594591..7bfc065f 100644 --- a/src/wfdb/io/convert/wav.py +++ b/src/wfdb/io/convert/wav.py @@ -132,7 +132,7 @@ def wfdb_to_wav( record.file_name = record.n_sig * [record_name_out + ".wav"] record.block_size = record.n_sig * [0] record.fmt = record.n_sig * ["16"] - record.samps_per_fram = record.n_sig * [1] + record.samps_per_frame = record.n_sig * [1] record.init_value = sig_data[0][: record.n_sig].tolist() record.byte_offset = record.n_sig * [44] # Write the header file diff --git a/tests/io/test_convert.py b/tests/io/test_convert.py index f72b7d5b..e51aaa07 100644 --- a/tests/io/test_convert.py +++ b/tests/io/test_convert.py @@ -349,3 +349,16 @@ def test_samples(self, tmp_path, record_name): frames = wav.readframes(record.sig_len) samples = np.frombuffer(frames, dtype="