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?

Brian
  • 3,453
  • 2
  • 27
  • 39

2 Answers2

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
Community
  • 1
  • 1
Brian
  • 3,453
  • 2
  • 27
  • 39
  • The `bitwise_and` operations are actually unnecessary. `real = values << 4 >> 4` and `imag = values >> 4` handles the sign and type correctly. – Vincent Aug 28 '18 at 14:20
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
Community
  • 1
  • 1
Phonon
  • 12,549
  • 13
  • 64
  • 114
  • So there's no way to do it with memmap or anything like that? – Brian Oct 14 '14 at 20:27
  • 2
    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? – Brian Oct 14 '14 at 20:34
  • @B-Brock: Yes, your suggestion would be much more efficient. – Warren Weckesser Oct 14 '14 at 21:21
  • @B-Brock Definitely. I was going for a more demonstrative example rather than something advanced but fast. But you're definitely correct. – Phonon Oct 14 '14 at 22:11
  • Okay, thank you for your input!--it implied what I wanted to know :) – Brian Oct 15 '14 at 20:20