Skip to content

Commit 79ceedf

Browse files
Mark gaps between output samples when downsampling in resample_sig
Only the input samples on either side of each output position were checked, so a gap between two output samples was lost when downsampling. Mark an output sample if a NaN lies within half an output spacing of it (and at least one input sample).
1 parent fd9e737 commit 79ceedf

3 files changed

Lines changed: 24 additions & 7 deletions

File tree

‎docs/changes.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Unreleased
4242
``resample_sig`` (and the functions that use it, such as ``resample_singlechan`` and ``resample_multichan``) returned only NaN values if the signal started with NaN. Leading NaN values now take the first valid value before resampling, as trailing ones already took the last.
4343

4444
**Keep missing values missing in resample_sig**
45-
``resample_sig`` (and ``resample_singlechan`` and ``resample_multichan``) now return NaN for output samples at or next to NaN values in the input. NaN values are still filled before resampling, so the rest of the signal is unaffected, but the filled values are no longer returned as if they were real data.
45+
``resample_sig`` (and ``resample_singlechan`` and ``resample_multichan``) now return NaN for output samples with a NaN input value within half an output sample spacing (or within one input sample, when upsampling). NaN values are still filled before resampling, so the rest of the signal is unaffected, but the filled values are no longer returned as if they were real data.
4646

4747
**Reject aux notes that can't be written in wrann**
4848
``wrann`` now raises a ``ValueError`` if an ``aux_note`` is longer than 255 characters or contains characters outside Latin-1, instead of writing a corrupt annotation file. A longer note used to be read back as many extra annotations, and other characters were silently replaced.

‎src/wfdb/processing/basic.py‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ def resample_sig(x, fs, fs_target):
4242
Returns
4343
-------
4444
resampled_x : ndarray
45-
Array of the resampled signal values. Samples at a NaN value in `x`,
46-
or between a NaN value and its neighbour, are NaN.
45+
Array of the resampled signal values. Samples with a NaN value in
46+
`x` within half an output sample spacing (or within one input
47+
sample, when upsampling) are NaN.
4748
resampled_t : ndarray
4849
Array of the resampled signal locations.
4950
@@ -63,11 +64,15 @@ def resample_sig(x, fs, fs_target):
6364
assert resampled_x.shape == resampled_t.shape and resampled_x.shape[0] == new_length
6465
assert np.all(np.diff(resampled_t) > 0)
6566

66-
# The filled values aren't real data, so mark output samples at or next to
67-
# a NaN input sample as NaN again.
67+
# The filled values aren't real data, so mark output samples with a NaN
68+
# input sample within half an output spacing (and at least one input
69+
# sample, so neighbours count when upsampling) as NaN again.
6870
if nan_mask.any():
69-
near_nan = np.interp(resampled_t, t, nan_mask.reshape((-1,)).astype(float))
70-
resampled_x[near_nan > 0] = np.nan
71+
half = max(fs / fs_target / 2, 1)
72+
n_nan = np.concatenate([[0], np.cumsum(nan_mask.reshape((-1,)))])
73+
lo = np.clip(np.floor(resampled_t - half).astype(int) + 1, 0, len(t))
74+
hi = np.clip(np.ceil(resampled_t + half).astype(int), 0, len(t))
75+
resampled_x[n_nan[hi] - n_nan[lo] > 0] = np.nan
7176

7277
return resampled_x, resampled_t
7378

‎tests/test_processing.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ def test_resample_marks_nan(self):
7171

7272
assert np.isnan(new_x).tolist() == ((new_t > 5) & (new_t < 7)).tolist()
7373

74+
@pytest.mark.parametrize(
75+
"gap, expected", [([500], [50]), (list(range(501, 509)), [50, 51])]
76+
)
77+
def test_resample_marks_nan_downsampling(self, gap, expected):
78+
"""Gaps between two output samples are still marked when downsampling."""
79+
x = np.sin(np.arange(1000) / 50)
80+
x[gap] = np.nan
81+
82+
new_x, _ = processing.resample_sig(x, fs=1000, fs_target=100)
83+
84+
assert np.flatnonzero(np.isnan(new_x)).tolist() == expected
85+
7486
def test_normalize_bound(self):
7587
sig, _ = wfdb.rdsamp("sample-data/100")
7688
lb = -5

0 commit comments

Comments
 (0)