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);
Examples of low-pass filters occur in acoustics, optics and electronics. A stiff physical barrier tends to reflect higher sound frequencies, and so acts as an acoustic low-pass filter for transmitting sound. When music is playing in another room, the low notes are easily heard, while the high notes are attenuated.
Using a stereo system as a practical example, a capacitor connected in series with the tweeter (treble) speaker will serve as a high-pass filter, imposing a high impedance to low-frequency bass signals, thereby preventing that power from being wasted on a speaker inefficient for reproducing such sounds.
A low-pass filter allows for easy passage of low-frequency signals from source to load, and difficult passage of high-frequency signals. Inductive low-pass filters insert an inductor in series with the load; capacitive low-pass filters insert a resistor in series and a capacitor in parallel with the load.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With