diff --git a/changelog.d/wfdb-to-wav-error.rst b/changelog.d/wfdb-to-wav-error.rst new file mode 100644 index 00000000..2b5fb8f1 --- /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. 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 ef3284c7..7bfc065f 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("-", "_") @@ -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 ae56b803..e51aaa07 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,41 @@ 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="