1

好的,首先,大家好 :) 我正在运行 Python 3.2 x86 @ windows

所以我有一个具有以下结构的二进制文件(6 412字节):

header: 256 bytes
maxZ: 4 bytes (1 32-bit word)
Zvalues: 2048 bytes (512 32-bit words)
maxX: *same as maxZ*
Xvalues: *same as Zvalues*
maxY: *same as maxZ*
Yvalues: *same as Zvalues*

我知道(由于格式规范)数字存储为 32 位整数,写为 2 的补码

假设我不需要读取标题,这就是我要做的(我有 1539 个 4 字节样本要读取):

import struct
file = open('C:/02.adb', 'rb')
file.seek(256)
fmt = '<i'
nbytes = 4
for i in range(1539):
    packed_data = file.read(nbytes)
    unpacked_data = struct.unpack(fmt, packed_data)
    print('Sample: ',i,'Value: ',unpacked_data[0])
file.close()

它读取文件并将其内容打印到标准输出,但标志有问题。我想我不是在这里读数字作为 2 的补码......我该如何改进脚本?

tnx 引起注意

补充(23.10.12):

我意识到,通过编写一个更大的外部脚本的简短版本,我刚刚解决了主要问题本身——我自己的无意识。

可以从文件头读取的样本数不是存储在其中的确切样本总数,而是 ZXY 值组合的数量,不包括存储在每个通道数据块的第一个字中的那些最大幅度。

这是脚本的最终简短版本及其输出(是的,有符号整数的 python 表示是“2 的补码”):

import struct
file = open('C:/02.adb', 'rb')
file.seek(256)
transfer_ratio = 31446.540881
fmt = '<i'
nbytes = 4
samples = []
for i in range(1539):
    packed_data = file.read(nbytes)
    unpacked_data = struct.unpack(fmt, packed_data)
    samples.append(int(unpacked_data[0])/transfer_ratio)
for i in range(32):
    print('Ch1_val: ',samples[i+1],' Ch2_val: ',samples[i+1+513],' Ch3_val: ',samples[i+1+(513*2)])
file.close()

输出:

Ch1_val:  42.20127119933322  Ch2_val:  36.47237399942374  Ch3_val:  -3.4996535999447054
Ch1_val:  41.37017819934635  Ch2_val:  36.64441199942102  Ch3_val:  -4.35822179993114
Ch1_val:  42.43585979932951  Ch2_val:  34.9874729994472  Ch3_val:  -4.125827399934812
Ch1_val:  44.08278179930349  Ch2_val:  34.42912859945602  Ch3_val:  -4.1604257999342655
Ch1_val:  44.50289159929685  Ch2_val:  35.507053199438985  Ch3_val:  -3.092836199951133
Ch1_val:  43.69110119930968  Ch2_val:  35.53335179943857  Ch3_val:  -2.613991799958699
Ch1_val:  42.73662419932476  Ch2_val:  34.1708807994601  Ch3_val:  -5.254886399916972
Ch1_val:  42.14097839933417  Ch2_val:  34.08460739946146  Ch3_val:  -8.582724599864394
Ch1_val:  41.982105599336684  Ch2_val:  35.58544019943775  Ch3_val:  -8.84688719986022
Ch1_val:  42.503816399328436  Ch2_val:  36.06170879943022  Ch3_val:  -6.777056999892922
Ch1_val:  43.17568679931782  Ch2_val:  35.291926199442386  Ch3_val:  -4.984618199921243
Ch1_val:  42.4303583993296  Ch2_val:  36.03750899943061  Ch3_val:  -4.502275799928864
Ch1_val:  39.92264219936922  Ch2_val:  38.354075399394006  Ch3_val:  -6.050744999904398
Ch1_val:  37.65501599940505  Ch2_val:  38.48181599939198  Ch3_val:  -9.010943399857627
Ch1_val:  37.04642759941466  Ch2_val:  35.152642199444585  Ch3_val:  -9.89107199984372
Ch1_val:  36.571876199422164  Ch2_val:  32.21988719949093  Ch3_val:  -7.850465999875962
Ch1_val:  34.488403799455085  Ch2_val:  32.62164839948458  Ch3_val:  -7.712485799878142
Ch1_val:  32.00259779949436  Ch2_val:  34.05570119946192  Ch3_val:  -11.215064999822802
Ch1_val:  31.132422599508107  Ch2_val:  33.44755799947153  Ch3_val:  -12.461179799803112
Ch1_val:  31.245853199506314  Ch2_val:  31.730771399498654  Ch3_val:  -8.723057999862176
Ch1_val:  30.40820939951955  Ch2_val:  31.03355639950967  Ch3_val:  -6.433076399898357
Ch1_val:  28.474165199550107  Ch2_val:  30.714824999514704  Ch3_val:  -9.730672799846255
Ch1_val:  26.481449999581592  Ch2_val:  29.53720739953331  Ch3_val:  -13.227496199791005
Ch1_val:  24.79611359960822  Ch2_val:  28.60448159954805  Ch3_val:  -12.1075955998087
Ch1_val:  23.74938479962476  Ch2_val:  28.95253259954255  Ch3_val:  -9.411305399851301
Ch1_val:  23.92663799962196  Ch2_val:  29.24305739953796  Ch3_val:  -8.856490799860067
Ch1_val:  24.37285559961491  Ch2_val:  28.72398599954616  Ch3_val:  -9.611549999848137
Ch1_val:  23.155774199634138  Ch2_val:  29.22521759953824  Ch3_val:  -10.701304199830918
Ch1_val:  20.822671799671003  Ch2_val:  31.46641799950283  Ch3_val:  -12.57088979980138
Ch1_val:  20.15302739968158  Ch2_val:  32.95071479947938  Ch3_val:  -13.581175799785417
Ch1_val:  21.440195999661245  Ch2_val:  32.28628559948987  Ch3_val:  -11.66643419981567
Ch1_val:  21.51502139966006  Ch2_val:  31.7910959994977  Ch3_val:  -8.843802599860268
4

1 回答 1

1

我相当确定 Python 使用二进制补码表示有符号整数。一个常见的问题是字节顺序。规范应该提到数字是小端还是大端。<i在 Python 中,您可以使用和加载>i

于 2012-10-22T20:11:45.797 回答