Skip to content

Commit d629897

Browse files
author
Benjamin Moody
committed
adc (expanded=False): move shared logic to a function.
1 parent 7704233 commit d629897

File tree

1 file changed

+17
-24
lines changed

1 file changed

+17
-24
lines changed

wfdb/io/_signal.py

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

535+
# Convert a 2D physical signal array to digital. Note that the
536+
# input array is modified!
537+
def adc_inplace_2d(p_signal):
538+
nanlocs = np.isnan(p_signal)
539+
np.multiply(p_signal, self.adc_gain, p_signal)
540+
np.add(p_signal, self.baseline, p_signal)
541+
np.round(p_signal, 0, p_signal)
542+
d_signal = p_signal.astype(intdtype, copy=False)
543+
544+
if nanlocs.any():
545+
for ch in range(d_signal.shape[1]):
546+
if nanlocs[:, ch].any():
547+
d_signal[nanlocs[:, ch], ch] = d_nans[ch]
548+
return d_signal
549+
535550
# Do inplace conversion and set relevant variables.
536551
if inplace:
537552
if expanded:
@@ -556,19 +571,7 @@ def adc(self, expanded=False, inplace=False):
556571
self.e_d_signal = self.e_p_signal
557572
self.e_p_signal = None
558573
else:
559-
p_signal = self.p_signal
560-
nanlocs = np.isnan(p_signal)
561-
np.multiply(p_signal, self.adc_gain, p_signal)
562-
np.add(p_signal, self.baseline, p_signal)
563-
np.round(p_signal, 0, p_signal)
564-
d_signal = p_signal.astype(intdtype, copy=False)
565-
566-
if nanlocs.any():
567-
for ch in range(d_signal.shape[1]):
568-
if nanlocs[:, ch].any():
569-
d_signal[nanlocs[:, ch], ch] = d_nans[ch]
570-
571-
self.d_signal = d_signal
574+
self.d_signal = adc_inplace_2d(self.p_signal)
572575
self.p_signal = None
573576

574577
# Return the variable
@@ -587,17 +590,7 @@ def adc(self, expanded=False, inplace=False):
587590
d_signal.append(ch_d_signal)
588591

589592
else:
590-
p_signal = self.p_signal.copy()
591-
nanlocs = np.isnan(p_signal)
592-
np.multiply(p_signal, self.adc_gain, p_signal)
593-
np.add(p_signal, self.baseline, p_signal)
594-
np.round(p_signal, 0, p_signal)
595-
d_signal = p_signal.astype(intdtype, copy=False)
596-
597-
if nanlocs.any():
598-
for ch in range(d_signal.shape[1]):
599-
if nanlocs[:, ch].any():
600-
d_signal[nanlocs[:, ch], ch] = d_nans[ch]
593+
d_signal = adc_inplace_2d(self.p_signal.copy())
601594

602595
return d_signal
603596

0 commit comments

Comments
 (0)