Skip to content

Commit 235e142

Browse files
author
Benjamin Moody
committed
adc (expanded=True): move shared logic to a function.
1 parent 8945861 commit 235e142

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

wfdb/io/_signal.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,17 @@ def adc(self, expanded=False, inplace=False):
532532
# To do: choose the minimum return res needed
533533
intdtype = "int64"
534534

535+
# Convert a single physical channel to digital. Note that the
536+
# input array is modified!
537+
def adc_inplace_1d(ch_p_signal, adc_gain, baseline, d_nan):
538+
ch_nanlocs = np.isnan(ch_p_signal)
539+
np.multiply(ch_p_signal, adc_gain, ch_p_signal)
540+
np.add(ch_p_signal, baseline, ch_p_signal)
541+
np.round(ch_p_signal, 0, ch_p_signal)
542+
ch_d_signal = ch_p_signal.astype(intdtype, copy=False)
543+
ch_d_signal[ch_nanlocs] = d_nan
544+
return ch_d_signal
545+
535546
# Convert a 2D physical signal array to digital. Note that the
536547
# input array is modified!
537548
def adc_inplace_2d(p_signal):
@@ -551,13 +562,12 @@ def adc_inplace_2d(p_signal):
551562
if inplace:
552563
if expanded:
553564
for ch in range(self.n_sig):
554-
ch_p_signal = self.e_p_signal[ch]
555-
ch_nanlocs = np.isnan(ch_p_signal)
556-
np.multiply(ch_p_signal, self.adc_gain[ch], ch_p_signal)
557-
np.add(ch_p_signal, self.baseline[ch], ch_p_signal)
558-
np.round(ch_p_signal, 0, ch_p_signal)
559-
ch_d_signal = ch_p_signal.astype(intdtype, copy=False)
560-
ch_d_signal[ch_nanlocs] = d_nans[ch]
565+
ch_d_signal = adc_inplace_1d(
566+
self.e_p_signal[ch],
567+
self.adc_gain[ch],
568+
self.baseline[ch],
569+
d_nans[ch],
570+
)
561571
self.e_p_signal[ch] = ch_d_signal
562572
self.e_d_signal = self.e_p_signal
563573
self.e_p_signal = None
@@ -570,13 +580,12 @@ def adc_inplace_2d(p_signal):
570580
if expanded:
571581
d_signal = []
572582
for ch in range(self.n_sig):
573-
ch_p_signal = self.e_p_signal[ch].copy()
574-
ch_nanlocs = np.isnan(ch_p_signal)
575-
np.multiply(ch_p_signal, self.adc_gain[ch], ch_p_signal)
576-
np.add(ch_p_signal, self.baseline[ch], ch_p_signal)
577-
np.round(ch_p_signal, 0, ch_p_signal)
578-
ch_d_signal = ch_p_signal.astype(intdtype, copy=False)
579-
ch_d_signal[ch_nanlocs] = d_nans[ch]
583+
ch_d_signal = adc_inplace_1d(
584+
self.e_p_signal[ch].copy(),
585+
self.adc_gain[ch],
586+
self.baseline[ch],
587+
d_nans[ch],
588+
)
580589
d_signal.append(ch_d_signal)
581590

582591
else:

0 commit comments

Comments
 (0)