繁体   English   中英

将二进制数组转换为int16

[英]convert binary array to int16

该阵列是一个波形。 当每个字节为1个样本时,我可以轻松地将二进制数组转换为int8 当使用12bit时,我可以将仪器设置为每个样本发送2个字节(字模式)。 我一直在网上寻找将2bytes / sample二进制数组转换为int16向量的方法,但到目前为止还无法完成。 每个样本1个字节

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

同样使用解压

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

无法弄清楚如何使其与2bytes / sample一起工作。 谢谢

而不是像这样使用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 ,但是,如果您已经知道,只需除以二即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM