我刚接触Python,试图做一些基本的信号处理工作,但是遇到了严重的性能问题。有没有一种Python技巧可以向量化地完成这个任务?基本上,我正在尝试实现一个一阶滤波器,但是滤波器特性可能会从一个样本变为另一个样本。如果只有一个滤波器,我会使用numpy.signal.lfilter(),但这有点棘手。以下是代码片段,速度非常慢:
#filter state
state = 0
#perform filtering
for sample in amplitude:
if( sample == 1.0 ): #attack filter
sample = (1.0 - att_coeff) * sample + att_coeff * state
else: #release filter
sample = (1.0 - rel_coeff) * sample + rel_coeff * state
state = sample