我需要“压缩”代表信号的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])
此算法运行正常。对于具有大量恒定段的信号,他的工作速度非常快。但是,如果我尝试压缩没有恒定段的信号(例如正弦信号或噪声信号),算法的工作速度非常慢。
如何加速算法并保存功能?
答案 0 :(得分: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))
我认为转换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
答案 1 :(得分:1)
您可以使用itertools.groupby()
:
In [93]: from itertools import groupby
In [94]: from operator import itemgetter
In [95]: ti=[0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,1.1]
In [96]: fun=[1,1,1,2,3,4,4,4,4,2,1,1]
In [97]: def func(ti,func):
new_time=[]
new_func=[]
for k,v in groupby(enumerate(func),itemgetter(1)):
lis=list(v)
if len(lis)>1:
ind1,ind2=lis[0][0],lis[-1][0]
new_time.extend([ti[ind1],ti[ind2]])
new_func.extend([func[ind1],func[ind2]])
else:
new_time.append(ti[lis[0][0]])
new_func.append(func[lis[0][0]])
return new_time,new_func
....:
In [98]: func(ti,fun)
Out[98]: ([0.0, 0.2, 0.3, 0.4, 0.5, 0.8, 0.9, 1.0, 1.1],
[1, 1, 2, 3, 4, 4, 2, 1, 1])