以下matlab函数解码信号delta编码:
% Method : DeltaCompression
% Description : Decode delta compressed signal
%
function data = DeltaCompression(file, data)
if ftell(file) == -1
data.Time = [];
data.Signal = [];
return;
else
fseek(file, 0, 'eof');
stop = ftell(file);
fseek(file, data.DataOffset, 'bof');
start = ftell(file);
end
signal = zeros(round((stop-start)/2), 1);
buffer = zeros(4, 1);
index = 1;
while ftell(file) < stop
buffer(1) = fread(file, 1, 'int16', 'b');
buffer(2) = buffer(4);
if bitshift(buffer(1), 12, 'int16') == 0
signal(index:end) = [];
break
end
for i = 1:bitand(buffer(1), 4095, 'int16');
buffer(3) = fread(file, 1, 'int16', 'b');
if buffer(3) ~= -32768
buffer(2) = buffer(2) + buffer(3);
else
buffer(2) = fread(file, 1, 'int32', 'b');
end
signal(index) = buffer(2);
index = index + 1;
end
buffer(4) = buffer(2);
end
我正在尝试将该代码移植到python。这是一个有效的例子:
import numpy as np
import matplotlib.pyplot as plt
# The start position of the data
data_offset = 6144
def parse_data(file_):
# Go to the end of the file and calculate how many points 8 byte floats
# there are
file_.seek(0, 2)
n_points = (file_.tell() - data_offset) // 8
# Read the data into a numpy array
file_.seek(data_offset)
values = np.fromfile(file_, dtype=np.int8, count=n_points)
values = values.tolist()
decoded_vals = []
last = 0
for index, val in enumerate(values):
delta = val
val = delta + last
last = val
decoded_vals.append(val)
return decoded_vals
if __name__ == '__main__':
with open("VWD1A.ch", 'rb') as file_:
values = parse_data(file_)
fig = plt.figure(1)
ax = plt.subplot(111)
plt.plot(values)
plt.show()
我正在尝试解码delta编码的数据。我几乎可以肯定delta解压缩应该可以工作,但我不确定我是否正确解析了二进制文件。 以下是二进制文件的链接:https://www.dropbox.com/s/b7p3l2wn2iaen5z/VWD1A.ch?dl=0
matlab函数做了一些我不理解的奇怪事情。你能否向我解释一下matlab函数在做什么以及我应该在哪里开始将它移植到python(我对matlab一无所知)?
这是预期结果的图片(我现在不介意轴的值,我只想看到这一个峰值):