Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cd3959d

Browse files
committed
ensure dynamic mapping for adc does not escape boundaries
1 parent 262b693 commit cd3959d

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

wfdb/io/_signal.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -528,15 +528,20 @@ def calc_adc_params(self):
528528
else:
529529
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:
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.
534537
gain = (dmax-dmin) / (pmax-pmin)
535538
baseline = dmin - gain*pmin
536-
537-
# What about roundoff error? Make sure values don't map to beyond
538-
# range.
539-
baseline = int(baseline)
539+
# The baseline needs to be an integer
540+
baseline = np.floor(baseline)
541+
# Adjust the gain to map pmin to dmin. This is to ensure
542+
# there is no overshoot of dmax. Now pmax will map to
543+
# dmax or dmax-1 which is also fine.
544+
gain = (dmin - baseline / pmin)
540545

541546
# WFDB library limits...
542547
if abs(gain)>214748364 or abs(baseline)>2147483648:

0 commit comments

Comments
 (0)