Skip to content

Commit 8b3d307

Browse files
author
Benjamin Moody
committed
_blocks_to_samples: handle partial blocks in formats 310/311.
In formats 310 and 311, each block of three samples is written as four bytes. _rd_dat_signals will retrieve the minimum range of bytes (as determined by _dat_read_params and _required_byte_num) that are needed in order to decode the desired samples; thus, the data passed to _blocks_to_samples may include an incomplete block at the end. The previous implementation of _blocks_to_samples was meant to pad the input data to a multiple of four bytes. However, this logic was wrong: added_samps was always set to zero, so the intended extra bytes were not appended, and (if the lack of extra bytes didn't cause an error) the wrong number of samples was returned to the caller. In fact, the subsequent statements for decoding blocks into samples already worked correctly for an unpadded input array (since each input slice is correctly truncated to the length of the output slice.) So remove the padding logic entirely.
1 parent 6bede91 commit 8b3d307

File tree

1 file changed

+0
-26
lines changed

1 file changed

+0
-26
lines changed

wfdb/io/_signal.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,14 +1520,6 @@ def _blocks_to_samples(sig_data, n_samp, fmt):
15201520
sig[sig > 2047] -= 4096
15211521

15221522
elif fmt == '310':
1523-
# Easier to process when dealing with whole blocks
1524-
if n_samp % 3:
1525-
n_samp = upround(n_samp,3)
1526-
added_samps = n_samp % 3
1527-
sig_data = np.append(sig_data, np.zeros(added_samps, dtype='uint8'))
1528-
else:
1529-
added_samps = 0
1530-
15311523
sig_data = sig_data.astype('int16')
15321524
sig = np.zeros(n_samp, dtype='int16')
15331525

@@ -1539,24 +1531,11 @@ def _blocks_to_samples(sig_data, n_samp, fmt):
15391531
# Third signal is 5 msb of second byte and 5 msb of forth byte
15401532
sig[2::3] = np.bitwise_and((sig_data[1::4] >> 3), 0x1f)[0:len(sig[2::3])] + 32 * np.bitwise_and(sig_data[3::4] >> 3, 0x1f)[0:len(sig[2::3])]
15411533

1542-
# Remove trailing samples read within the byte block if
1543-
# originally not 3n sampled
1544-
if added_samps:
1545-
sig = sig[:-added_samps]
1546-
15471534
# Loaded values as un_signed. Convert to 2's complement form:
15481535
# values > 2^9-1 are negative.
15491536
sig[sig > 511] -= 1024
15501537

15511538
elif fmt == '311':
1552-
# Easier to process when dealing with whole blocks
1553-
if n_samp % 3:
1554-
n_samp = upround(n_samp,3)
1555-
added_samps = n_samp % 3
1556-
sig_data = np.append(sig_data, np.zeros(added_samps, dtype='uint8'))
1557-
else:
1558-
added_samps = 0
1559-
15601539
sig_data = sig_data.astype('int16')
15611540
sig = np.zeros(n_samp, dtype='int16')
15621541

@@ -1568,11 +1547,6 @@ def _blocks_to_samples(sig_data, n_samp, fmt):
15681547
# Third sample is 4 msb of third byte and 6 msb of forth byte
15691548
sig[2::3] = (sig_data[2::4] >> 4)[0:len(sig[2::3])] + 16 * np.bitwise_and(sig_data[3::4], 0x7f)[0:len(sig[2::3])]
15701549

1571-
# Remove trailing samples read within the byte block if
1572-
# originally not 3n sampled
1573-
if added_samps:
1574-
sig = sig[:-added_samps]
1575-
15761550
# Loaded values as un_signed. Convert to 2's complement form.
15771551
# Values > 2^9-1 are negative.
15781552
sig[sig > 511] -= 1024

0 commit comments

Comments
 (0)