我有一个包含8位复杂样本(即从最高位到最低位的虚部和实部(Q和I)各4位)的二进制数据文件。
我该如何将这些数据转换成numpy复数数组?
我有一个包含8位复杂样本(即从最高位到最低位的虚部和实部(Q和I)各4位)的二进制数据文件。
我该如何将这些数据转换成numpy复数数组?
没有支持进行8位复数(4位实数,4位虚数)的操作。
因此,以下方法是将其有效地读入到分别用于复数和虚数的numpy数组中的好方法。
values = np.fromfile("filepath", dtype=int8)
real = np.bitwise_and(values, 0x0f)
imag = np.bitwise_and(values >> 4, 0x0f)
如果您想要一个复杂的数组,
signal = real + 1j * imag
real = (np.bitwise_and(values, 0x0f) << 4).astype(np.int8) >> 4
imag = np.bitwise_and(values, 0xf0).astype(int) >> 4
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
real = np.bitwise_and(a, 0x0f); imag = np.bitwise_and(a >> 4, 0x0f);
其中 a
是文件中的字节数组? - Brian