Lowpass Filter in python

filter, numpy, python, scipy

Solution

A very basic approach would be to invoke

# spell out the args that were passed to the Matlab function
N = 10
Fc = 40
Fs = 1600
# provide them to firwin
h = scipy.signal.firwin(numtaps=N, cutoff=40, nyq=Fs/2)
# 'x' is the time-series data you are filtering
y = scipy.signal.lfilter(h, 1.0, x)

This should yield a filter similar to the one that ends up being made in the Matlab code. If your goal is to obtain functionally equivalent results, this should provide a useful filter.

However, if your goal is that the python code provide exactly the same results, then you'll have to look under the hood of the `design` call (in Matlab); From my quick check, it's not trivial to parse through the Matlab calls to identify exactly what it is doing, i.e. what design method is used and so on, and how to map that into corresponding `scipy` calls. If you really want compatibility, and you only need to do this for a limited number of filters, you could, by hand, look at the `Hd.Numerator` field -- this array of numbers directly corresponds to the `h` variable in the python code above. So if you copy those numbers into an array by hand, you'll get numerically equivalent results.

Problem

I am trying to convert a Matlab code to Python. I want to implement `fdesign.lowpass()` of Matlab in Python. What will be the exact substitute of this Matlab code using `scipy.signal.firwin()`: ``` demod_1_a = mod_noisy * 2.*cos(2*pi*Fc*t+phi); d = fdesign.lowpass('N,Fc', 10, 40, 1600); Hd = design(d); y = filter(Hd, demod_1_a); ```

Original source