我有一个包含 8 位复杂样本的二进制数据文件——即4 位和 4 位用于从 MSB 到 LSB 的虚部和实部(Q 和 I)分量。
如何将这些数据放入 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
这里有更多方法可以将两个实数数组转换为复数数组:https://stackoverflow.com/a/2598820/1131073
如果值是可能为负的 4 位整数(即应用二进制补码),您可以使用算术位移来正确分离两个 channel :
real = (np.bitwise_and(values, 0x0f) << 4).astype(np.int8) >> 4
imag = np.bitwise_and(values, 0xf0).astype(int) >> 4
关于python - 如何将 4 位数据加载到 numpy 数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26369520/