pythonscipydigital-filter

How to use Python's scipy.signal.remez output for scipy.signal.lfilter?


I am creating a bandpass filter in python. I calculated the transfer function coefficients using signal.remez, and the frequency response looks just how I want it to. However, signal.remez returns a single 1D array of coefficients. I was expecting two arrays, a set each for the numerator and denominator of the transfer function.

How do I use the output of the python remez algorithm in signal.lfilter?

Code Snippet:

from scipy.signal import lfilter, remez

def Rfilter(data, samplerate):
    g           = samplerate/2.
    f           = g/62.5
    e           = f*0.875
    d           = e*(3./7.)
    c           = d*(4./15.)
    coeff           = remez(400, [0, c, d, e, f, g], [0, 1, 0], Hz=samplerate, maxiter=100)
    return coeff


infile      = 'data.csv'
data        = open(infile, 'r')
data        = data.readlines()
X           = []
V           = []


for line in data:
    line        = line.strip().split(',')
    X.append(float(line[0]))
    V.append(float(line[1]))


timestep    = X[1] - X[0]
samplerate  = 1/timestep

#Here is the array of coefficients that remez returns. 
coeff       = Rfilter(V, samplerate)

#From coeff, need to generate a and b, the numerator and denomenator coefficients
#FV             = lfilter(a, b, V)

Solution

  • It's an old question, but with over 1000 views, perhaps there are still people showing up here with similar questions.

    scipy.signal.remez computes the coefficients of a finite impulse response (FIR) filter. The output is just one set of coefficients. You say you expected two sets of coefficients, which means you expected remez to design an infinite impulse response (IIR) filter, but remez does not do that.

    You can apply the filter with a convolution function such as numpy.convolve or scipy.signal.convolve. You can also use scipy.signal.lfilter. lfilter accepts the coefficients of an IIR filter. The first two arguments of lfilter, b and a, are the coefficients of the numerator and denominator of the IIR filter. You can pass an FIR filter to lfilter by setting b to the coefficients returned by remez, and setting a=1. (In other words, an "IIR" filter with a trivial denominator is, in fact, an FIR filter.)