Skip to content

Commit 3bc22f4

Browse files
committed
fix adc parameter calculation
1 parent 378a802 commit 3bc22f4

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

wfdb/io/_signal.py

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -488,13 +488,13 @@ def dac(self, expanded=False, return_res=64, inplace=False):
488488

489489
def calc_adc_params(self):
490490
"""
491-
Compute appropriate gain and baseline parameters given the physical
492-
signal and the fmts.
491+
Compute appropriate adc_gain and baseline parameters given the
492+
physical signal and the fmts.
493493
494-
digital - baseline / gain = physical
495-
physical * gain + baseline = digital
494+
digital - baseline / adc_gain = physical
495+
physical * adc_gain + baseline = digital
496496
"""
497-
gains = []
497+
adc_gains = []
498498
baselines = []
499499

500500
# min and max ignoring nans, unless whole channel is nan.
@@ -518,34 +518,47 @@ def calc_adc_params(self):
518518

519519
# If the entire signal is nan, just put any.
520520
if pmin == np.nan:
521-
gain = 1
521+
adc_gain = 1
522522
baseline = 1
523-
# If the signal is just one value, store all values as digital 1.
523+
# If the signal is just one value, store all values as 1.
524524
elif pmin == pmax:
525525
if pmin == 0:
526-
gain = 1
526+
adc_gain = 1
527527
baseline = 1
528528
else:
529-
gain = 1 / pmin
529+
adc_gain = 1 / pmin
530530
baseline = 0
531-
# Regular mixed signal case
532-
# Todo:
531+
# Regular varied signal case. Map pmax->dmax, pmin->dmin
533532
else:
534-
gain = (dmax-dmin) / (pmax-pmin)
535-
baseline = dmin - gain*pmin
533+
# The equation is: p = (d - b) / g
534+
# pmax maps to dmax, and pmin maps to dmin. Gradient
535+
# will be close to delta(d) / delta(p), since intercept
536+
# baseline has to be an integer.
537+
adc_gain = (dmax-dmin) / (pmax-pmin)
538+
baseline = dmin - adc_gain*pmin
539+
540+
# The baseline needs to be an integer
541+
if pmin > 0:
542+
baseline = int(np.ceil(baseline))
543+
else:
544+
baseline = int(np.floor(baseline))
545+
546+
# We set the gain to map pmin to dmin, and p=0 to
547+
# baseline.
536548

537-
# What about roundoff error? Make sure values don't map to beyond
538-
# range.
539-
baseline = int(baseline)
549+
# This up/down round logic of baseline is to ensure
550+
# there is no overshoot of dmax. Now pmax will map to
551+
# dmax or dmax-1 which is also fine.
552+
adc_gain = (dmin - baseline) / pmin
540553

541554
# WFDB library limits...
542-
if abs(gain)>214748364 or abs(baseline)>2147483648:
555+
if abs(adc_gain)>214748364 or abs(baseline)>2147483648:
543556
raise Exception('adc_gain and baseline must have magnitudes < 214748364')
544557

545-
gains.append(gain)
558+
adc_gains.append(adc_gain)
546559
baselines.append(baseline)
547560

548-
return (gains, baselines)
561+
return (adc_gains, baselines)
549562

550563

551564
def convert_dtype(self, physical, return_res, smooth_frames):

0 commit comments

Comments
 (0)