Matlab freqz function in Python
Matlab freqz function in Python
In version 1.2.0 of SciPy, the fs
argument was added to scipy.signal.freqz
. So you can write
f, h = freqz(range(1, 6), 1, fs=12.5)
For example,
In [18]: f, h = freqz(range(1, 6), 1, fs=12.5)
In [19]: len(f)
Out[19]: 512
In [20]: f[:10] # Matches the Matlab output shown in the question.
Out[20]:
array([0. , 0.01220703, 0.02441406, 0.03662109, 0.04882812,
0.06103516, 0.07324219, 0.08544922, 0.09765625, 0.10986328])
In older versions of SciPy,
signal.signal.freqz
doesnt have an option to return the frequencies in Hz, so youll have to scale the frequencies yourself afterwards.
The equivalent of Matlabs
[h, f] = freqz(b, a, n, fs)
using freqz
from scipy.signal
is:
w, h = freqz(b, a, worN=n)
f = fs * w / (2*np.pi)
For example,
In [15]: import numpy as np
In [16]: from scipy.signal import freqz
In [17]: w, h = freqz(range(1,6), 1, worN=512)
In [18]: h[:6]
Out[18]:
array([ 15.00000000+0.j , 14.99755288-0.24541945j,
14.99021268-0.49073403j, 14.97798292-0.73583892j,
14.96086947-0.98062944j, 14.93888050-1.22500102j])
In [19]: w[:6]
Out[19]:
array([ 0. , 0.00613592, 0.01227185, 0.01840777, 0.02454369,
0.03067962])
In [20]: f = 12.5*w/(2*np.pi)
In [21]: f[:6]
Out[21]:
array([ 0. , 0.01220703, 0.02441406, 0.03662109, 0.04882812,
0.06103516])
In both languages freqz
expects numerator coefficients b
for the first argument, not a
like you wrote. Should be
freqz(b, a, ...)
Looks like you are trying to find the response of an FIR filter, for which there are only numerator coefficients and a
is always 1.