Alternative to scipy.lfilter

Viewed 726

is there a way to have an alternative implementation of lfilter within scipy? I want to use the cusignal library and lfilter is not supported at the moment.

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? Ultimately, I want to perform the code snippet above faster than it's current version.

Any ideas or thoughts would be appreciated!

1 Answers

Even though your filter is in principle an infinite impulse response (IIR) filter, the impulse response for this particular filter decays very fast. You can compute the impulse response by running an impulse through it with lfilter like lfilter(b, a, [1] + [0]*99). Here is what I get:

impulse response plot1

As you can see, the taps are nearly zero above sample 20 or so. So you can take the first 20 samples of the impulse response to make an accurate truncated FIR approximation. From there, you can apply that FIR approximation with any FIR filtering function, like np.convolve, scipy.signal.convolve, or scipy.signal.fftconvolve.

Another thought: With any of these filtering functions, you could try casting all the args to np.float32. They might internally switch to a 32-bit float implementation that is faster than the 64-bit float implementation.

Related