lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




On Thu, Jul 4, 2019 at 9:24 AM Andrew Gierth <andrew@tao11.riddles.org.uk> wrote:
>>>>> "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.

How embarrassing; thank you Andrew. I had started this last night at home with an MIT-BIH file from here:
https://physionet.org/physiobank/database/mitdb/. I didn't have that dataset handy here at work and in a rush to get the email out I took the line "Most of the signal files in PhysioBank are written in format 212" at its face value.

This is a side project that is only partly related to work so it will have to wait until tonight. 
Thanks again,
Russ


 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.