具体代码见GitHub
但是因为某种不知名的bug,会导致最后一个csi的数据包长度明显小于前面的包,用这段代码就会出现index out of range的bug,需要修改一下wifilib.py模块里面的read_bf_file函数,在for循环前读取一下正常的csi数据包的长度(213),然后每次循环前都对比一下当前包的长度是否也是这么大,如果不是则退出循环
修改后的read_bf_file函数如下:
def read_bf_file(filename, decoder="python"):
with open(filename, "rb") as f:
bfee_list = []
field_len = int.from_bytes(f.read(2), byteorder='big', signed=False)
while field_len != 0:
bfee_list.append(f.read(field_len))
field_len = int.from_bytes(f.read(2), byteorder='big', signed=False)
dicts = []
count = 0 # % Number of records output
broken_perm = 0 # % Flag marking whether we've encountered a broken CSI yet
triangle = [0, 1, 3] # % What perm should sum to for 1,2,3 antennas
len_arr=len(bfee_list[0])
for array in bfee_list:
if len(array)!=len_arr:
break
#% Read size and code
code = array[0]
# there is CSI in field if code == 187,If unhandled code skip (seek over) the record and continue
if code != 187:
#% skip all other info
continue
else:
# get beamforming or phy data
count =count + 1
timestamp_low = int.from_bytes(array[1:5], byteorder='little', signed=False)
bfee_count = int.from_bytes(array[5:7], byteorder='little', signed=False)
Nrx = array[9]
Ntx = array[10]
rssi_a = array[11]
rssi_b = array[12]
rssi_c = array[13]
noise = array[14] - 256
agc = array[15]
antenna_sel = array[16]
b_len = int.from_bytes(array[17:19], byteorder='little', signed=False)
fake_rate_n_flags = int.from_bytes(array[19:21], byteorder='little', signed=False)
payload = array[21:] #get payload
calc_len = (30 * (Nrx * Ntx * 8 * 2 + 3) + 6) / 8
perm = [1,2,3]
perm[0] = ((antenna_sel) & 0x3)
perm[1] = ((antenna_sel >> 2) & 0x3)
perm[2] = ((antenna_sel >> 4) & 0x3)
#Check that length matches what it should
if (b_len != calc_len):
print("MIMOToolbox:read_bfee_new:size","Wrong beamforming matrix size.")
#Compute CSI from all this crap :
if decoder=="python":
csi = parse_csi(payload,Ntx,Nrx)
else:
csi = None
print("decoder name error! Wrong encoder name:",decoder)
return
# % matrix does not contain default values
if sum(perm) != triangle[Nrx-1]:
print('WARN ONCE: Found CSI (',filename,') with Nrx=', Nrx,' and invalid perm=[',perm,']\n' )
else:
csi[:,perm,:] = csi[:,[0,1,2],:]
# dict,and return
bfee_dict = {
'timestamp_low': timestamp_low,
'bfee_count': bfee_count,
'Nrx': Nrx,
'Ntx': Ntx,
'rssi_a': rssi_a,
'rssi_b': rssi_b,
'rssi_c': rssi_c,
'noise': noise,
'agc': agc,
'antenna_sel': antenna_sel,
'perm': perm,
'len': b_len,
'fake_rate_n_flags': fake_rate_n_flags,
'calc_len': calc_len,
'csi': csi}
dicts.append(bfee_dict)
return dicts
参考链接:https://blog.csdn.net/YuanDianO/article/details/99936627