6

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?

2 Answers 2

7

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
Sign up to request clarification or add additional context in comments.

1 Comment

The bitwise_and operations are actually unnecessary. real = values << 4 >> 4 and imag = values >> 4 handles the sign and type correctly.
3

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

5 Comments

So there's no way to do it with memmap or anything like that?
Then would it not be more efficient to do: real = np.bitwise_and(a, 0x0f); imag = np.bitwise_and(a >> 4, 0x0f); where a is the array of bytes from the file?
@B-Brock: Yes, your suggestion would be much more efficient.
@B-Brock Definitely. I was going for a more demonstrative example rather than something advanced but fast. But you're definitely correct.
Okay, thank you for your input!--it implied what I wanted to know :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.