>>>>> "Russell" == Russell Haley <firstname.lastname@example.org> writes:
Russell> I'm trying to parse 12 bit numbers out of a binary file. From
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:
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.
How embarrassing; thank you Andrew. I had started this last night at home with an MIT-BIH file from here:
This is a side project that is only partly related to work so it will have to wait until tonight.
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...