在python中打开二进制数据

时间:2013-08-12 12:01:35

标签: python matlab numpy

我想从二进制数据文件中读取, 在我的脚本的旧matlab版本中,这是由

完成的
file=fread(data,'bit16');

哪个在python中是等价的? 我试过了

with open file(data, "rb") as f:
    d = np.fromfile(f, "<i2", count = 10000)

因为the matlab documentation表示bitn是带有n位

的有符号整数类型

我尝试了不同的dtypes ( "<>i2", "int16"),遗憾的是,这并没有给我提供严谨的数据。

1 个答案:

答案 0 :(得分:1)

您可以尝试使用与float16相关联的numpy.frombuffer数据类型来处理half precision floting point(matlab中的bit16)。类型doc是here