[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Parse 12 bit numbers from binary file
- From: Andrew Gierth <andrew@...>
- Date: Thu, 04 Jul 2019 17:24:23 +0100
>>>>> "Russell" == Russell Haley <russ.haley@gmail.com> writes:
Russell> Hi,
Russell> I'm trying to parse 12 bit numbers out of a binary file. From
Russell> https://www.physionet.org/physiotools/wag/signal-5.htm:
Russell> Format 212
Russell> Each sample is represented by a 12-bit two’s complement
Russell> amplitude. The first sample is obtained from the 12 least
Russell> significant bits of the first byte pair (stored least
Russell> significant byte first). The second sample is formed from the
Russell> 4 remaining bits of the first byte pair (which are the 4 high
Russell> bits of the 12-bit sample) and the next byte (which contains
Russell> the remaining 8 bits of the second sample). The process is
Russell> repeated for each successive pair of samples. Most of the
Russell> signal files in PhysioBank are written in format 212
Russell> The file I am trying to read is here:
Russell> https://physionet.org/pn3/twadb/twa00.dat
That file is clearly not in the format described. For one thing, the
data in it shows a clear 4-byte (not 3-byte) stride; for another, the
corresponding header file says it's in format 16, not 212.
Russell> c1, c2, index = string.unpack('<h<b', str, index)
Russell> samp1 = c1 >> 0x04
Russell> samp2 = c1 >> 0x0c & c2
That & there is clearly wrong, and you're putting the bits in the wrong
place too. And yes, the signedness is wrong - you should probably read
only unsigned values and convert to signed after messing with the bits.
But first you'd need to find some data in this format to test against...
--
Andrew.