Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog.d/wfdb-to-wav-error.rst
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 2 additions & 2 deletions src/wfdb/io/convert/wav.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("-", "_")

Expand Down Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions tests/io/test_convert.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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="<i2").reshape(expected.shape)
np.testing.assert_array_equal(samples, expected)

@pytest.mark.parametrize("record_name", ["100", "test01_00s_frame"])
def test_write_header(self, tmp_path, monkeypatch, record_name):
"""The header written with write_header=True describes the .wav file."""
record_path = os.path.abspath(f"sample-data/{record_name}")
monkeypatch.chdir(tmp_path)
wfdb_to_wav(record_path, write_header=True)

# Read back as one sample per frame, with the same physical values
record = rdrecord(record_name)
expected = rdrecord(record_path)
assert record.samps_per_frame == [1] * expected.n_sig
np.testing.assert_array_equal(record.p_signal, expected.p_signal)
Loading