wfdb-python icon indicating copy to clipboard operation
wfdb-python copied to clipboard

calc_adc_params can produce off-by-one problems, crashing wrsamp

Open Ivorforce opened this issue 1 year ago • 4 comments

I have some data that crashes wrsamp because calc_adc_params produces values that don't fit the data (off by one):

chmin = -2147483648
chmax = 2147483648
IndexError: Channel 0 contain values outside allowed range [-2147483648, 2147483647] for fmt 32

As you can see, chmax is just above the dmax of 2147483647 for 32.

This is some data for reproduction: data.float32.zip

You can try it with:

data = np.reshape(np.fromfile("data.float32", dtype=np.float32), [-1, 12])
wfdb.wrsamp(
    record_name="test",
    sig_name=[f"d{x}" for x in range(12)],
    units=["mv"] * 12,
    fs=500,
#    fmt=["16"] * 12,
    p_signal=data
)

Note how passing fmt=16 fixes the problem in this particular case, probably by chance because it happens to not trigger the bug.

Ivorforce avatar Nov 05 '24 15:11 Ivorforce

I am encountering a similar problem where the calculated digital values are outside of the valid range when using fmt='32' and pmax < 0. It appears that this is related to the cal_adc_gain_baseline function in _signal.py. The current code is

adc_gain = (MAX_I32) - dmin / abs(pmin)

Shouldn't it be

adc_gain = (MAX_I32 - dmin) / abs(pmin)

tcpan avatar Apr 29 '25 14:04 tcpan

I've found that it's apparently a floating point precision error. When I add a float64 conversion before the wrsamp call, it works as expected:

data = np.reshape(np.fromfile("data.float32", dtype=np.float32), [-1, 12])
wfdb.wrsamp(
    record_name="test",
    sig_name=[f"d{x}" for x in range(12)],
    units=["mv"] * 12,
    fs=500,
    p_signal=data
)

It seems a bit flimsy if it works with float64 but not float32, but perhaps it's because the parameter calculation auto-expands into float64, but adc() doesn't, so there is a mismatch of precision causing the error. At the very least, wfdb should probably auto-convert to float64 before adc().

Ivorforce avatar Jul 24 '25 12:07 Ivorforce