简体   繁体   English

替代 scipy.lfilter

[英]Alternative to scipy.lfilter

is there a way to have an alternative implementation of lfilter within scipy?有没有办法在 scipy 中实现 lfilter 的替代实现? I want to use the cusignal library and lfilter is not supported at the moment.我想使用 cusignal 库,目前不支持 lfilter。

Here's my my current code that I want to speed up:这是我当前想要加速的代码:

from scipy import signal
import numpy as np

data = np.random.rand(192,334)
a = [1,-1.086740193996892,0.649914553946275,-0.124948974636730]
b = [0.054778173164082,0.164334519492245,0.164334519492245,0.054778173164082]

x[range(0, len(x)),:] = signal.lfilter(b, a, x[range(0, len(x)),:])

Is there a way I can use numpy's convolve function or scipy's fftconvolve or firfilter to perform this operation?有没有办法可以使用 numpy 的卷积函数或 scipy 的 fftconvolve 或 firfilter 来执行此操作? Ultimately, I want to perform the code snippet above faster than it's current version.最终,我希望比当前版本更快地执行上面的代码片段。

Any ideas or thoughts would be appreciated!任何想法或想法将不胜感激!

Even though your filter is in principle an infinite impulse response (IIR) filter, the impulse response for this particular filter decays very fast.尽管您的滤波器原则上是无限脉冲响应 (IIR) 滤波器,但此特定滤波器的脉冲响应衰减非常快。 You can compute the impulse response by running an impulse through it with lfilter like lfilter(b, a, [1] + [0]*99) .您可以通过使用 lfilter 运行脉冲来计算脉冲响应,例如lfilter(b, a, [1] + [0]*99) Here is what I get:这是我得到的:

脉冲响应图1

As you can see, the taps are nearly zero above sample 20 or so.如您所见,在样本 20 左右以上,抽头几乎为零。 So you can take the first 20 samples of the impulse response to make an accurate truncated FIR approximation.因此,您可以采用脉冲响应的前 20 个样本来进行精确的截断 FIR 近似。 From there, you can apply that FIR approximation with any FIR filtering function, like np.convolve, scipy.signal.convolve, or scipy.signal.fftconvolve.从那里,您可以将该 FIR 近似应用于任何 FIR 滤波函数,例如 np.convolve、scipy.signal.convolve 或 scipy.signal.fftconvolve。

Another thought: With any of these filtering functions, you could try casting all the args to np.float32.另一个想法:使用这些过滤函数中的任何一个,您都可以尝试将所有 args 转换为 np.float32。 They might internally switch to a 32-bit float implementation that is faster than the 64-bit float implementation.他们可能会在内部切换到比 64 位浮点实现更快的 32 位浮点实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM