freqz
Builtin index · Signal processing
result = freqz(filt, [n=512])
Input: filt, n · Output: result
What it is
filt: a Model (kind "filter"); n (optional number, default 512): number of frequency points.
Overloads
One call shape — this function is not overloaded.
Code Input Output Description
result = freqz(filt, [n=512])filt Model filt: a Model (kind "filter").
n number, optional (default 512) n (optional number, default 512): number of frequency points.
result vector or CVec
Returns a length-n complex vector (CVec), the response H(e^{jω}) at n points from DC to Nyquist inclusive. abs(freqz(filt)) is the magnitude response, angle(freqz(filt)) the phase. Pairs with linspace(0, fs/2, n) for the matching frequency axis.
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
Fs = 2000
fc = 60
lp = butter (4 , "low" , fc, Fs)
H = freqz (lp, 512 )
print (H)
mag_db = 20 * log10 (abs (H))
i_fc = round (fc / (Fs / 2 ) * (length (H) - 1 ))
i_dc = 0
i_nyq = length (H) - 1
print ("Gain at DC: {mag_db[i_dc]:.2f} dB" )
print ("Gain near cutoff ({fc} Hz): {mag_db[i_fc]:.2f} dB (expect close to -3 dB)" )
print ("Gain at Nyquist: {mag_db[i_nyq]:.2f} dB" )
from tour
fs = 1000
b = butter (4 , "low" , 100 , fs)
h = freqz (b, 512 )
f = linspace (0 , fs / 2 , 512 )
plot (f, mag2db (abs (h)), color = "#4169e1" , width = 2 )
xscale ("log" )
ylim (-80 , 5 )
xlabel ("frequency (Hz)" )
ylabel ("magnitude (dB)" )
title ("4th-order Butterworth, 100 Hz cutoff" )
output · plot: figure 8 (512 points)
-80
-60
-40
-20
0
0
150
300
450
Magnitude Response
Frequency (Hz)
Magnitude (dB)
-150
-100
-50
0
50
100
150
0
150
300
450
Phase Response
Frequency (Hz)
Phase (deg)
-250
-200
-150
-100
-50
0
0
150
300
450
Group Delay
Frequency (Hz)
Delay (samples)
-120
-80
-40
0
40
10 −272
10 −204
10 −136
10 −68
10 0
x=-0.135, y=0.991
x=-0.135, y=-0.991
x=0.57, y=0.821
x=0.57, y=-0.821
4th-order Butterworth, 100 Hz
cutoff
frequency (Hz)
magnitude (dB)
from signal-processing
fs = 1000
t = (0 to 999 ) / fs
signal = sin (2 * pi * 50 * t) + 0.5 * sin (2 * pi * 220 * t) + 0.1 * randn (1000 , seed = 2 )
lp = butter (4 , "low" , 100 , fs) # 4th-order lowpass, 100 Hz cutoff
notch = butter (2 , "notch" , [45 , 55 ], fs) # removes 45-55 Hz mains hum
filtered = sosfilt (lp, signal) # causal, one pass
zero_phase = filtfilt (lp, signal) # zero-phase, double effective order
print ("lp is stable: {is_stable(lp)}, notch is stable: {is_stable(notch)}" )
H = freqz (lp, 512 )
plot (linspace (0 , fs/2 , 512 ), mag2db (abs (H)))
output lp is stable: true, notch is stable: true
· plot: figure 3 (512 points)
-200
-100
0
100
200
300
400
500
-200
0
200
400
600
800
x=0.21, y=0
x=0.21, y=0
x=0.21, y=0
x=0.21, y=0
x=0.21, y=0
x=0.21, y=0
x=0.21, y=$10^{-9}$
x=0.21, y=$1.1\times10^{-9}$
x=0.21, y=$1.2\times10^{-9}$
x=0.21, y=$1.3\times10^{-9}$
x=0.21, y=$1.4\times10^{-9}$
x=0.21, y=$1.5\times10^{-9}$
x=0.21, y=$1.7\times10^{-9}$
x=0.21, y=$1.8\times10^{-9}$
x=0.21, y=$2\times10^{-9}$
x=0.21, y=$2.1\times10^{-9}$
x=0.21, y=$2.3\times10^{-9}$
x=0.21, y=$2.5\times10^{-9}$
x=0.21, y=$2.7\times10^{-9}$
x=0.21, y=$2.9\times10^{-9}$
x=0.21, y=$3.2\times10^{-9}$
x=0.21, y=$3.4\times10^{-9}$
x=0.21, y=$3.7\times10^{-9}$
x=0.21, y=$4\times10^{-9}$
x=0.21, y=$4.4\times10^{-9}$
x=0.21, y=$4.8\times10^{-9}$
x=0.21, y=$5.2\times10^{-9}$
x=0.21, y=$5.6\times10^{-9}$
x=0.21, y=$6.1\times10^{-9}$
x=0.21, y=$6.6\times10^{-9}$
x=0.21, y=$7.1\times10^{-9}$
x=0.21, y=$7.7\times10^{-9}$
x=0.21, y=$8.4\times10^{-9}$
x=0.21, y=$9.1\times10^{-9}$
x=0.21, y=$9.9\times10^{-9}$
x=0.21, y=$1.1\times10^{-8}$
x=0.21, y=$1.2\times10^{-8}$
x=0.21, y=$1.3\times10^{-8}$
x=0.21, y=$1.4\times10^{-8}$
x=0.21, y=$1.5\times10^{-8}$
x=0.21, y=$1.6\times10^{-8}$
x=0.21, y=$1.7\times10^{-8}$
x=0.21, y=$1.9\times10^{-8}$
x=0.21, y=$2\times10^{-8}$
x=0.21, y=$2.2\times10^{-8}$
x=0.21, y=$2.4\times10^{-8}$
x=0.21, y=$2.6\times10^{-8}$
x=0.21, y=$2.8\times10^{-8}$
x=0.21, y=$3.1\times10^{-8}$
x=0.21, y=$3.3\times10^{-8}$
x=0.21, y=$3.6\times10^{-8}$
x=0.21, y=$3.9\times10^{-8}$
x=0.21, y=$4.2\times10^{-8}$
x=0.21, y=$4.6\times10^{-8}$
x=0.21, y=$5\times10^{-8}$
x=0.21, y=$5.4\times10^{-8}$
x=0.21, y=$5.9\times10^{-8}$
x=0.21, y=$6.4\times10^{-8}$
x=0.21, y=$6.9\times10^{-8}$
x=0.21, y=$7.5\times10^{-8}$
x=0.21, y=$8.1\times10^{-8}$
x=0.21, y=$8.8\times10^{-8}$
x=0.21, y=$9.5\times10^{-8}$
x=0.21, y=$10^{-7}$
x=0.21, y=$1.1\times10^{-7}$
x=0.21, y=$1.2\times10^{-7}$
x=0.21, y=$1.3\times10^{-7}$
x=0.21, y=$1.4\times10^{-7}$
x=0.21, y=$1.5\times10^{-7}$
x=0.21, y=$1.7\times10^{-7}$
x=0.21, y=$1.8\times10^{-7}$
x=0.21, y=$2\times10^{-7}$
x=0.21, y=$2.1\times10^{-7}$
x=0.21, y=$2.3\times10^{-7}$
x=0.21, y=$2.5\times10^{-7}$
x=0.21, y=$2.7\times10^{-7}$
x=0.21, y=$3\times10^{-7}$
x=0.21, y=$3.2\times10^{-7}$
x=0.21, y=$3.5\times10^{-7}$
x=0.21, y=$3.8\times10^{-7}$
x=0.21, y=$4.1\times10^{-7}$
x=0.21, y=$4.4\times10^{-7}$
x=0.21, y=$4.8\times10^{-7}$
x=0.21, y=$5.2\times10^{-7}$
x=0.21, y=$5.7\times10^{-7}$
x=0.21, y=$6.1\times10^{-7}$
x=0.21, y=$6.7\times10^{-7}$
x=0.21, y=$7.2\times10^{-7}$
x=0.21, y=$7.8\times10^{-7}$
x=0.21, y=$8.5\times10^{-7}$
x=0.21, y=$9.2\times10^{-7}$
x=0.21, y=$10^{-6}$
x=0.21, y=$1.1\times10^{-6}$
x=0.21, y=$1.2\times10^{-6}$
x=0.21, y=$1.3\times10^{-6}$
x=0.21, y=$1.4\times10^{-6}$
x=0.21, y=$1.5\times10^{-6}$
x=0.21, y=$1.6\times10^{-6}$
x=0.21, y=$1.8\times10^{-6}$
x=0.21, y=$1.9\times10^{-6}$
x=0.21, y=$2.1\times10^{-6}$
x=0.21, y=$2.2\times10^{-6}$
x=0.21, y=$2.4\times10^{-6}$
x=0.21, y=$2.6\times10^{-6}$
x=0.21, y=$2.9\times10^{-6}$
x=0.21, y=$3.1\times10^{-6}$
x=0.21, y=$3.4\times10^{-6}$
x=0.21, y=$3.6\times10^{-6}$
x=0.21, y=$4\times10^{-6}$
x=0.21, y=$4.3\times10^{-6}$
x=0.21, y=$4.7\times10^{-6}$
x=0.21, y=$5\times10^{-6}$
x=0.21, y=$5.5\times10^{-6}$
x=0.21, y=$5.9\times10^{-6}$
x=0.21, y=$6.4\times10^{-6}$
x=0.21, y=$7\times10^{-6}$
x=0.21, y=$7.6\times10^{-6}$
x=0.21, y=$8.2\times10^{-6}$
x=0.21, y=$8.9\times10^{-6}$
x=0.21, y=$9.6\times10^{-6}$
x=0.21, y=$10^{-5}$
x=0.21, y=$1.1\times10^{-5}$
x=0.21, y=$1.2\times10^{-5}$
x=0.21, y=$1.3\times10^{-5}$
x=0.21, y=$1.4\times10^{-5}$
x=0.21, y=$1.6\times10^{-5}$
x=0.21, y=$1.7\times10^{-5}$
x=0.21, y=$1.8\times10^{-5}$
x=0.21, y=$2\times10^{-5}$
x=0.21, y=$2.2\times10^{-5}$
x=0.21, y=$2.3\times10^{-5}$
x=0.21, y=$2.5\times10^{-5}$
x=0.21, y=$2.8\times10^{-5}$
x=0.21, y=$3\times10^{-5}$
x=0.21, y=$3.2\times10^{-5}$
x=0.21, y=$3.5\times10^{-5}$
x=0.21, y=$3.8\times10^{-5}$
x=0.21, y=$4.1\times10^{-5}$
x=0.21, y=$4.5\times10^{-5}$
x=0.21, y=$4.9\times10^{-5}$
x=0.21, y=$5.3\times10^{-5}$
x=0.21, y=$5.7\times10^{-5}$
x=0.21, y=$6.2\times10^{-5}$
x=0.21, y=$6.7\times10^{-5}$
x=0.21, y=$7.3\times10^{-5}$
x=0.21, y=$7.9\times10^{-5}$
x=0.21, y=$8.6\times10^{-5}$
x=0.21, y=$9.3\times10^{-5}$
x=0.21, y=$10^{-4}$
x=0.21, y=$1.1\times10^{-4}$
x=0.21, y=$1.2\times10^{-4}$
x=0.21, y=$1.3\times10^{-4}$
x=0.21, y=$1.4\times10^{-4}$
x=0.21, y=$1.5\times10^{-4}$
x=0.21, y=$1.6\times10^{-4}$
x=0.21, y=$1.8\times10^{-4}$
x=0.21, y=$1.9\times10^{-4}$
x=0.21, y=$2.1\times10^{-4}$
x=0.21, y=$2.3\times10^{-4}$
x=0.21, y=$2.5\times10^{-4}$
x=0.21, y=$2.7\times10^{-4}$
x=0.21, y=$2.9\times10^{-4}$
x=0.21, y=$3.1\times10^{-4}$
x=0.21, y=$3.4\times10^{-4}$
x=0.21, y=$3.7\times10^{-4}$
x=0.21, y=$4\times10^{-4}$
x=0.21, y=$4.3\times10^{-4}$
x=0.21, y=$4.7\times10^{-4}$
x=0.21, y=$5.1\times10^{-4}$
x=0.21, y=$5.5\times10^{-4}$
x=0.21, y=$6\times10^{-4}$
x=0.21, y=$6.5\times10^{-4}$
x=0.21, y=$7\times10^{-4}$
x=0.21, y=$7.6\times10^{-4}$
x=0.21, y=$8.2\times10^{-4}$
x=0.21, y=$8.9\times10^{-4}$
x=0.21, y=$9.7\times10^{-4}$
x=0.21, y=0.001
x=0.21, y=0.001
x=0.21, y=0.001
x=0.21, y=0.001
x=0.21, y=0.001
x=0.21, y=0.002
x=0.21, y=0.002
x=0.21, y=0.002
x=0.21, y=0.002
x=0.21, y=0.002
x=0.209, y=0.002
x=0.209, y=0.002
x=0.209, y=0.003
x=0.209, y=0.003
x=0.209, y=0.003
x=0.209, y=0.003
x=0.209, y=0.003
x=0.209, y=0.004
x=0.208, y=0.004
x=0.208, y=0.004
x=0.208, y=0.004
x=0.207, y=0.004
x=0.207, y=0.005
frequency (Hz)
from signal-processing
How it works
freqz(filt, [n=512]) — complex frequency response H(e^{jω}) at n points from DC to Nyquist inclusive (see numeric::filter::freqz's own doc comment for the exact convention). Pairs with linspace(0, fs/2, n) for the matching frequency axis, since Qu has no multi-return to hand both back from one call. abs(freqz(filt)) is the magnitude response, angle(freqz(filt)) the phase. For an FIR filter this is the simpler sum_k b[k]*e^{-jkω} direct evaluation — no denominator at all.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.