From fca266d2666f5b8f33bd13fef01c5498c06a9c14 Mon Sep 17 00:00:00 2001 From: Kulraj <59130414+Kulraj69@users.noreply.github.com> Date: Wed, 25 Nov 2026 07:43:20 +0530 Subject: [PATCH 1/6] Pad waveform averages before the record starts Updated indexing logic to handle negative NumPy indices and removed unnecessary exception handling. --- src/wfdb/processing/filter.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/wfdb/processing/filter.py b/src/wfdb/processing/filter.py index 67458fad..154cadb8 100644 --- a/src/wfdb/processing/filter.py +++ b/src/wfdb/processing/filter.py @@ -134,10 +134,10 @@ def sigavg( for c, i in enumerate(indices): for j in range(rec.n_sig): - try: + # A negative NumPy index wraps to the end of the record. + # Pad both ends with zero instead of using unrelated samples. + if 0 <= samp + i < rec.sig_len: initial_sig_avgs[c][j] += rec.d_signal[samp + i][j] - except IndexError: - initial_sig_avgs[c][j] += 0 n_beats += 1 if n_beats < 1: From 2c468fc9b884d439d3503061b3473290ebe387f4 Mon Sep 17 00:00:00 2001 From: Kulraj <59130414+Kulraj69@users.noreply.github.com> Date: Wed, 25 Nov 2026 07:43:40 +0530 Subject: [PATCH 2/6] Test start padding using real local waveform records --- tests/test_sigavg_boundaries.py | 74 +++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 tests/test_sigavg_boundaries.py diff --git a/tests/test_sigavg_boundaries.py b/tests/test_sigavg_boundaries.py new file mode 100644 index 00000000..c00c2688 --- /dev/null +++ b/tests/test_sigavg_boundaries.py @@ -0,0 +1,74 @@ +import numpy as np +import pytest +from numpy.testing import assert_allclose + +import wfdb +from wfdb import processing + + +def write_record(tmp_path, name, values, sample): + wfdb.wrsamp( + name, + fs=10, + units=["mV"], + sig_name=["ECG"], + d_signal=np.asarray(values, dtype=np.int64).reshape(-1, 1), + fmt=["16"], + adc_gain=[1], + baseline=[0], + write_dir=str(tmp_path), + ) + wfdb.wrann( + name, + "atr", + sample=np.array([sample]), + symbol=["N"], + fs=10, + write_dir=str(tmp_path), + ) + return str(tmp_path / name) + + +@pytest.mark.parametrize("sample", [0, 1]) +@pytest.mark.parametrize("tail", [9, 9000]) +def test_sigavg_pads_before_start_without_wrapping(tmp_path, sample, tail): + values = list(range(10)) + values[-1] = tail + record = write_record(tmp_path, "start", values, sample) + + result = processing.sigavg( + record, + "atr", + return_df=True, + start_range=-0.2, + stop_range=0.2, + stop_time=0.5, + ) + + # Compute expected padding without indexing negative positions. + expected = [ + values[sample + offset] if sample + offset >= 0 else 0 + for offset in [-2, -1, 0, 1] + ] + assert_allclose(result["ECG"], expected) + + +@pytest.mark.parametrize("sample", [5, 9]) +def test_sigavg_interior_and_end_padding_unchanged(tmp_path, sample): + values = list(range(10)) + record = write_record(tmp_path, "control", values, sample) + + result = processing.sigavg( + record, + "atr", + return_df=True, + start_range=-0.2, + stop_range=0.2, + stop_time=1, + ) + + expected = [ + values[sample + offset] if sample + offset < 10 else 0 + for offset in [-2, -1, 0, 1] + ] + assert_allclose(result["ECG"], expected) From 3f84d6bd0146f4e0a0df09576a156d76c495dd0f Mon Sep 17 00:00:00 2001 From: Kulraj <59130414+Kulraj69@users.noreply.github.com> Date: Wed, 25 Nov 2026 07:43:59 +0530 Subject: [PATCH 3/6] Document zero padding before waveform start Update documentation to clarify waveform average padding behavior. --- changelog.d/sigavg-start-padding.rst | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 changelog.d/sigavg-start-padding.rst diff --git a/changelog.d/sigavg-start-padding.rst b/changelog.d/sigavg-start-padding.rst new file mode 100644 index 00000000..f56e653e --- /dev/null +++ b/changelog.d/sigavg-start-padding.rst @@ -0,0 +1,4 @@ +**Avoid wrapping waveform averages before the record starts** + ``processing.sigavg`` pads samples before the start of a record with zero, + matching its existing end-of-record padding, instead of including unrelated + samples from the end of the record through negative NumPy indices. From 9e23388119968c09ddcfa4de51e86de34cf28288 Mon Sep 17 00:00:00 2001 From: Kulraj <59130414+Kulraj69@users.noreply.github.com> Date: Wed, 25 Nov 2026 07:54:13 +0530 Subject: [PATCH 4/6] Calibrate out-of-range waveform padding to physical zero --- src/wfdb/processing/filter.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/wfdb/processing/filter.py b/src/wfdb/processing/filter.py index 154cadb8..790659af 100644 --- a/src/wfdb/processing/filter.py +++ b/src/wfdb/processing/filter.py @@ -135,9 +135,11 @@ def sigavg( for c, i in enumerate(indices): for j in range(rec.n_sig): # A negative NumPy index wraps to the end of the record. - # Pad both ends with zero instead of using unrelated samples. + # Pad in digital units so calibration yields physical zero. if 0 <= samp + i < rec.sig_len: initial_sig_avgs[c][j] += rec.d_signal[samp + i][j] + else: + initial_sig_avgs[c][j] += rec.baseline[j] n_beats += 1 if n_beats < 1: From a3e162bceb1494b88f8caa749313cff7e6f24e98 Mon Sep 17 00:00:00 2001 From: Kulraj <59130414+Kulraj69@users.noreply.github.com> Date: Wed, 25 Nov 2026 07:54:30 +0530 Subject: [PATCH 5/6] Cover nonzero calibration baselines at both record boundaries Added tests for signal averaging with zero padding and flat signals. --- tests/test_sigavg_boundaries.py | 75 +++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/tests/test_sigavg_boundaries.py b/tests/test_sigavg_boundaries.py index c00c2688..8d0ef6bd 100644 --- a/tests/test_sigavg_boundaries.py +++ b/tests/test_sigavg_boundaries.py @@ -72,3 +72,78 @@ def test_sigavg_interior_and_end_padding_unchanged(tmp_path, sample): for offset in [-2, -1, 0, 1] ] assert_allclose(result["ECG"], expected) + + +@pytest.mark.parametrize("samples", [[0], [9], [0, 5, 9]]) +@pytest.mark.parametrize("baseline,gain", [(100, 2), (-120, 4)]) +def test_sigavg_padding_is_zero_in_physical_units(tmp_path, samples, baseline, gain): + physical = np.array([3, -2, 0, 1, 4, -1, 2, 0, -3, 5]) + digital = baseline + gain * physical + wfdb.wrsamp( + "calibrated", + fs=10, + units=["mV"], + sig_name=["ECG"], + d_signal=digital[:, None], + fmt=["16"], + adc_gain=[gain], + baseline=[baseline], + write_dir=str(tmp_path), + ) + wfdb.wrann( + "calibrated", + "atr", + sample=np.array(samples), + symbol=["N"] * len(samples), + fs=10, + write_dir=str(tmp_path), + ) + result = processing.sigavg( + str(tmp_path / "calibrated"), + "atr", + return_df=True, + start_range=-0.2, + stop_range=0.2, + stop_time=1, + ) + expected = [ + sum( + physical[sample + offset] if 0 <= sample + offset < len(physical) else 0 + for sample in samples + ) + / len(samples) + for offset in [-2, -1, 0, 1] + ] + assert_allclose(result["ECG"], np.round(expected, 5)) + + +@pytest.mark.parametrize("sample", [0, 9]) +def test_sigavg_flat_zero_physical_signal_stays_zero_at_boundaries(tmp_path, sample): + wfdb.wrsamp( + "flat", + fs=10, + units=["mV"], + sig_name=["ECG"], + d_signal=np.full((10, 1), 100, dtype=np.int64), + fmt=["16"], + adc_gain=[2], + baseline=[100], + write_dir=str(tmp_path), + ) + wfdb.wrann( + "flat", + "atr", + sample=np.array([sample]), + symbol=["N"], + fs=10, + write_dir=str(tmp_path), + ) + result = processing.sigavg( + str(tmp_path / "flat"), + "atr", + return_df=True, + start_range=-0.2, + stop_range=0.2, + stop_time=1, + ) + assert_allclose(result["ECG"], 0) From f26db528d900803c77e5f3f7a4327d0a0fab1027 Mon Sep 17 00:00:00 2001 From: Kulraj <59130414+Kulraj69@users.noreply.github.com> Date: Wed, 25 Nov 2026 07:54:49 +0530 Subject: [PATCH 6/6] Clarify physical-unit padding in the changelog Updated the description of waveform average padding to clarify the handling of out-of-range positions and the use of the channel's digital baseline. --- changelog.d/sigavg-start-padding.rst | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/changelog.d/sigavg-start-padding.rst b/changelog.d/sigavg-start-padding.rst index f56e653e..140976a7 100644 --- a/changelog.d/sigavg-start-padding.rst +++ b/changelog.d/sigavg-start-padding.rst @@ -1,4 +1,5 @@ -**Avoid wrapping waveform averages before the record starts** - ``processing.sigavg`` pads samples before the start of a record with zero, - matching its existing end-of-record padding, instead of including unrelated - samples from the end of the record through negative NumPy indices. +**Pad waveform averages with zero in physical units** + ``processing.sigavg`` no longer includes unrelated end-of-record samples + through negative NumPy indices. Out-of-range positions at either end use + the channel's digital baseline so padding is zero after calibration, + including records with a nonzero baseline.