Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to load 4-bit data into numpy array

I have a binary data file that contains 8-bit complex samples--i.e. 4 bits and 4 bits for imaginary and real (Q and I) components from MSB to LSB.

How can I get this data into a numpy complex number array?

like image 422
Brian Avatar asked Oct 14 '14 20:10

Brian


2 Answers

There is no support for doing 8-bit complex numbers (4-bit real, 4-bit imaginary). As such the following method is a good way to efficiently read them into separate numpy arrays for complex and imaginary.

values = np.fromfile("filepath", dtype=int8)
real = np.bitwise_and(values, 0x0f)
imag = np.bitwise_and(values >> 4, 0x0f)

then if you want one complex array,

signal = real + 1j * imag

There are more ways for converting two real arrays to a complex array here: https://stackoverflow.com/a/2598820/1131073


If the values are 4-bit ints that could be negative (i.e. two's complement applies), you can use arithmetic bit shifting to separate out the two channels correctly:

real = (np.bitwise_and(values, 0x0f) << 4).astype(np.int8) >> 4
imag = np.bitwise_and(values, 0xf0).astype(int) >> 4
like image 55
Brian Avatar answered Nov 18 '22 12:11

Brian


Using this answer for reading data in one byte at a time, this should work:

with open("myfile", "rb") as f:
    byte = f.read(1)
    while byte != "":
        # Do stuff with byte.
        byte = f.read(1)
        real = byte >> 4
        imag = byte & 0xF
        # Store numbers however you like
like image 45
Phonon Avatar answered Nov 18 '22 13:11

Phonon