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?
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With