将二进制阵列转换为INT16



阵列是波形。当每个字节是1个样本时,我可以轻松地将二进制数组转换为int8。使用12bit时,我可以将仪器设置为每个样本发送2个字节(单词模式)。我一直在网络上寻找将2个比值/示例二进制数组转换为int16向量的,但到目前为止还没有。这与每个样品1个字节一起工作

data = numpy.fromstring(dataword, dtype=numpy.int8)

使用拆卸

相同
data = numpy.array(unpack('%sb' %len(dataword) ,dataword))

无法弄清楚如何使其与2个Bytes/样本一起使用。谢谢

,而不是这样使用struct.unpack

data = numpy.array(unpack('%sb' %len(dataword) ,dataword))

您应该这样使用它:

data = numpy.array(unpack('%sh' % len(dataword) / struct.calcsize('h'), dataword))
#----------------------------^ notice the 'h', signed short, 16 bits

您必须将len(dataword)除以您正在读取的数字大小,在这种情况下是两个字节。最好使用calcsize,但是如果您已经知道,只需除以两个。

最新更新