python – 信号压缩
作者:互联网
我需要“压缩”代表信号的python数组的大小.信号如下图所示.
signal = [
[0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,1.1], #time values
[1,1,1,2,3,4,4,4,4,2,1,1] #function values
]
压缩后,信号应如下面的代码所示.
signal_compressed = [
[0.0,0.2,0.3,0.4,0.5,0.8,0.9,1.0,1.1], #time values
[1,1,2,3,4,4,2,1,1] #function values
]
您会看到,如果存在具有常量值的区域,则仅存储该区域的第一个和最后一个值.
我写了以下算法来做到这一点.
signal_compressed = [[],[]]
old_value = None
for index, value in enumerate(signal[1]):
if value != old_value:
if index > 0:
if signal_compressed[0][-1] != signal[0][index - 1]:
signal_compressed[0].append(signal[0][index - 1])
signal_compressed[1].append(signal[1][index - 1])
signal_compressed[0].append(signal[0][index])
signal_compressed[1].append(value)
old_value = value
if signal_compressed[0][-1] < signal[0][-1]:
signal_compressed[0].append(signal[0][-1])
signal_compressed[1].append(signal[1][-1])
这个算法运行正常.对于具有大量恒定段的信号,他的工作速度非常快.但是,如果我尝试压缩没有恒定段的信号(例如正弦信号或噪声信号),算法的工作速度非常慢.
如何加速算法并保存功能?
解决方法:
以下是使用生成器执行此操作的一种方法:
def compress(signal):
prev_t, prev_val = None, None
for t, val in zip(*signal):
if val != prev_val:
if prev_t is not None:
yield prev_t, prev_val
yield t, val
prev_t, prev_val = None, val
else:
prev_t, prev_val = t, val
if prev_t is not None:
yield prev_t, prev_val
signal = [
[0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,1.1], #time values
[1,1,1,2,3,4,4,4,4,2,1,1] #function values
]
print zip(*compress(signal))
我认为转换信号更自然,存储方式如下:
[(0.0, 1),
(0.1, 1),
(0.2, 1),
(0.3, 2),
(0.4, 3),
(0.5, 4),
(0.6, 4),
(0.7, 4),
(0.8, 4),
(0.9, 2),
(1.0, 1),
(1.1, 1)]
这样就不需要两个zip(* seq)调用,整个处理可以动态完成.
最后,如果对于大输入来说这仍然太慢,那么可能值得研究使用NumPy.以下是一个这样的解决方案的概述:
import numpy as np
signal = [
[0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,1.1], #time values
[1,1,1,2,3,4,4,4,4,2,1,1] #function values
]
def npcompress(signal):
sig=np.array(signal)
idx = np.where(sig[1][1:] != sig[1][:-1])[0]
idx_arr = np.sort(np.array(list(set(idx) | set(idx + 1) | set([0]) | set([len(sig[1]) - 1]))))
return sig.T[idx_arr]
print npcompress(signal).T
标签:python,signal-processing,python-2-5 来源: https://codeday.me/bug/20190703/1370672.html