Skip to content

Commit 5b56407

Browse files
author
Benjamin Moody
committed
adc: combine shared logic into one inner function.
1 parent e013d5b commit 5b56407

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

wfdb/io/_signal.py

+20-21
Original file line numberDiff line numberDiff line change
@@ -532,33 +532,22 @@ 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-
np.copyto(ch_p_signal, d_nan, where=ch_nanlocs)
543-
ch_d_signal = ch_p_signal.astype(intdtype, copy=False)
544-
return ch_d_signal
545-
546-
# Convert a 2D physical signal array to digital. Note that the
547-
# input array is modified!
548-
def adc_inplace_2d(p_signal):
535+
# Convert a physical (1D or 2D) signal array to digital. Note that
536+
# the input array is modified!
537+
def adc_inplace(p_signal, adc_gain, baseline, d_nan):
549538
nanlocs = np.isnan(p_signal)
550-
np.multiply(p_signal, self.adc_gain, p_signal)
551-
np.add(p_signal, self.baseline, p_signal)
539+
np.multiply(p_signal, adc_gain, p_signal)
540+
np.add(p_signal, baseline, p_signal)
552541
np.round(p_signal, 0, p_signal)
553-
np.copyto(p_signal, d_nans, where=nanlocs)
542+
np.copyto(p_signal, d_nan, where=nanlocs)
554543
d_signal = p_signal.astype(intdtype, copy=False)
555544
return d_signal
556545

557546
# Do inplace conversion and set relevant variables.
558547
if inplace:
559548
if expanded:
560549
for ch, ch_p_signal in enumerate(self.e_p_signal):
561-
ch_d_signal = adc_inplace_1d(
550+
ch_d_signal = adc_inplace(
562551
ch_p_signal,
563552
self.adc_gain[ch],
564553
self.baseline[ch],
@@ -568,15 +557,20 @@ def adc_inplace_2d(p_signal):
568557
self.e_d_signal = self.e_p_signal
569558
self.e_p_signal = None
570559
else:
571-
self.d_signal = adc_inplace_2d(self.p_signal)
560+
self.d_signal = adc_inplace(
561+
self.p_signal,
562+
self.adc_gain,
563+
self.baseline,
564+
d_nans,
565+
)
572566
self.p_signal = None
573567

574568
# Return the variable
575569
else:
576570
if expanded:
577571
e_d_signal = []
578572
for ch, ch_p_signal in enumerate(self.e_p_signal):
579-
ch_d_signal = adc_inplace_1d(
573+
ch_d_signal = adc_inplace(
580574
ch_p_signal.copy(),
581575
self.adc_gain[ch],
582576
self.baseline[ch],
@@ -586,7 +580,12 @@ def adc_inplace_2d(p_signal):
586580
return e_d_signal
587581

588582
else:
589-
return adc_inplace_2d(self.p_signal.copy())
583+
return adc_inplace(
584+
self.p_signal.copy(),
585+
self.adc_gain,
586+
self.baseline,
587+
d_nans,
588+
)
590589

591590
def dac(self, expanded=False, return_res=64, inplace=False):
592591
"""

0 commit comments

Comments
 (0)