Commit 37d5a12
Benjamin Moody
Implement reading signals in format 24.
In signal format 24, each sample is stored in three bytes, as a
little-endian signed integer. Since most architectures don't natively
support 24-bit integers, neither does numpy, and therefore in order to
read the signal, it must be converted from the on-disk format into
some format that numpy can work with (i.e., 32-bit signed integers.)
Previously, when loading a format-24 record using rdrecord or rdsamp,
the internal function _rd_dat_file made a halfhearted attempt to read
the signals, by extracting the two most significant bytes of each
sample, discarding the least significant byte, and storing the result
as a 16-bit integer. This meant not only that the precision was
silently reduced, but the resulting physical sample values were
incorrect by a factor of 256.
(Additionally, the implementation of _rd_dat_file was broken if the
signal file contained a prolog, if the signal file could not be
mmapped, or if the entire signal file was not meant to be read at
once.)
Fix this by handling format 24 in the same way as the other unaligned
formats (212, 310, and 311):
- Read the data initially as an array of unsigned bytes; this means
the 'dtype' passed to numpy.fromfile is numpy.dtype('<u1') and the
'count' is the number of bytes rather than the number of samples.
- Reformat the array of bytes into an array of integers in the
_blocks_to_samples function. This means format 24 must be
considered an "unaligned" rather than an "aligned" format.
In order to avoid making unnecessary copies of the data, rather than
using numpy arithmetic operations, the middle and low bytes are
*copied* from the input array into the corresponding locations in an
8-bit view of the 32-bit output array. This is dependent on the
system byte order.1 parent 5bb2e3c commit 37d5a12
1 file changed
Lines changed: 29 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
| |||
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
32 | | - | |
| 33 | + | |
33 | 34 | | |
34 | 35 | | |
35 | 36 | | |
| |||
1398 | 1399 | | |
1399 | 1400 | | |
1400 | 1401 | | |
| 1402 | + | |
| 1403 | + | |
| 1404 | + | |
1401 | 1405 | | |
1402 | 1406 | | |
1403 | 1407 | | |
| |||
1535 | 1539 | | |
1536 | 1540 | | |
1537 | 1541 | | |
| 1542 | + | |
| 1543 | + | |
| 1544 | + | |
| 1545 | + | |
| 1546 | + | |
| 1547 | + | |
| 1548 | + | |
| 1549 | + | |
| 1550 | + | |
| 1551 | + | |
| 1552 | + | |
| 1553 | + | |
| 1554 | + | |
| 1555 | + | |
| 1556 | + | |
| 1557 | + | |
| 1558 | + | |
| 1559 | + | |
| 1560 | + | |
| 1561 | + | |
1538 | 1562 | | |
1539 | 1563 | | |
1540 | 1564 | | |
| |||
0 commit comments