Skip to content

Commit 6db09b6

Browse files
author
Benjamin Moody
committed
Record.adc: round samples rather than truncating.
When converting physical to digital values, the input values are floating-point and therefore may have rounding errors. We want to round each value to the nearest integer before calling istype, in order to avoid adding a bias towards zero. This applies to adc in all four modes: expanded and not expanded, in-place and not-in-place. ("In-place" here means both that we can overwrite the original floating-point arrays, and that the record attributes will be updated afterwards. In "not-in-place" mode, we are operating on copies of the original arrays.)
1 parent 6437ed3 commit 6db09b6

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

wfdb/io/_signal.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,7 @@ def adc(self, expanded=False, inplace=False):
548548
self.baseline[ch],
549549
self.e_p_signal[ch],
550550
)
551+
np.round(self.e_p_signal[ch], 0, self.e_p_signal[ch])
551552
self.e_p_signal[ch] = self.e_p_signal[ch].astype(
552553
intdtype, copy=False
553554
)
@@ -558,6 +559,7 @@ def adc(self, expanded=False, inplace=False):
558559
nanlocs = np.isnan(self.p_signal)
559560
np.multiply(self.p_signal, self.adc_gain, self.p_signal)
560561
np.add(self.p_signal, self.baseline, self.p_signal)
562+
np.round(self.p_signal, 0, self.p_signal)
561563
self.p_signal = self.p_signal.astype(intdtype, copy=False)
562564
self.d_signal = self.p_signal
563565
self.p_signal = None
@@ -572,6 +574,7 @@ def adc(self, expanded=False, inplace=False):
572574
ch_d_signal = self.e_p_signal[ch].copy()
573575
np.multiply(ch_d_signal, self.adc_gain[ch], ch_d_signal)
574576
np.add(ch_d_signal, self.baseline[ch], ch_d_signal)
577+
np.round(ch_d_signal, 0, ch_d_signal)
575578
ch_d_signal = ch_d_signal.astype(intdtype, copy=False)
576579
ch_d_signal[ch_nanlocs] = d_nans[ch]
577580
d_signal.append(ch_d_signal)
@@ -582,6 +585,7 @@ def adc(self, expanded=False, inplace=False):
582585
d_signal = self.p_signal.copy()
583586
np.multiply(d_signal, self.adc_gain, d_signal)
584587
np.add(d_signal, self.baseline, d_signal)
588+
np.round(d_signal, 0, d_signal)
585589
d_signal = d_signal.astype(intdtype, copy=False)
586590

587591
if nanlocs.any():

0 commit comments

Comments
 (0)