Main Content

freqz

Frequency response of digital filter

Description

[h,w] = freqz(b,a,n) returns the frequency response of the specified digital filter. Specify a digital filter with numerator coefficients b and denominator coefficients a. The function returns the n-point frequency response vector in h and the corresponding angular frequency vector w.

example

[h,w] = freqz(B,A,"ctf",n) returns the n-point frequency response of the digital filter represented as Cascaded Transfer Functions (CTF) with numerator coefficients B and denominator coefficients A. (since R2024b)

example

[h,w] = freqz({B,A,g},"ctf",n) returns the n-point frequency response of the digital filter in CTF format. Specify the filter with numerator coefficients B, denominator coefficients A, and scaling values g across filter sections. (since R2024b)

example

[h,w] = freqz(d,n) returns the n-point complex frequency response for the digital filter d.

example

[h,w] = freqz(sos,n) returns the n-point complex frequency response corresponding to the second-order sections matrix sos.

example

[h,w] = freqz(___,n,"whole") returns the frequency response at n sample points around the entire unit circle.

[h,f] = freqz(___,n,fs) returns the frequency response vector h and the corresponding physical frequency vector f for a digital filter designed to filter signals sampled at a rate fs.

[h,f] = freqz(___,n,"whole",fs) returns the frequency vector at n points ranging between 0 and fs.

h = freqz(___,w) returns the frequency response vector h evaluated at the normalized frequencies supplied in w.

h = freqz(___,f,fs) returns the frequency response vector h evaluated at the physical frequencies supplied in f.

freqz(___) with no output arguments plots the frequency response of the filter.

example

Examples

collapse all

Compute and display the magnitude response of the third-order IIR lowpass filter described by the following transfer function:

H(z)=0.05634(1+z-1)(1-1.0166z-1+z-2)(1-0.683z-1)(1-1.4461z-1+0.7957z-2).

Express the numerator and denominator as polynomial convolutions. Find the frequency response at 2001 points spanning the complete unit circle.

b0 = 0.05634;
b1 = [1  1];
b2 = [1 -1.0166 1];
a1 = [1 -0.683];
a2 = [1 -1.4461 0.7957];

b = b0*conv(b1,b2);
a = conv(a1,a2);

[h,w] = freqz(b,a,'whole',2001);

Plot the magnitude response expressed in decibels.

plot(w/pi,20*log10(abs(h)))
ax = gca;
ax.YLim = [-100 20];
ax.XTick = 0:.5:2;
xlabel('Normalized Frequency (\times\pi rad/sample)')
ylabel('Magnitude (dB)')

Figure contains an axes object. The axes object with xlabel Normalized Frequency ( times pi blank rad/sample), ylabel Magnitude (dB) contains an object of type line.

Since R2024b

Design a 40th-order lowpass Chebyshev type II digital filter with a stopband edge frequency of 0.4 and stopband attenuation of 50 dB. Plot the frequency response of the filter using its coefficients in the CTF format.

[B,A] = cheby2(40,50,0.4,"ctf");

freqz(B,A,"ctf")

Figure contains 2 axes objects. Axes object 1 with title Phase, xlabel Normalized Frequency (\times\pi rad/sample), ylabel Phase (degrees) contains an object of type line. Axes object 2 with title Magnitude, xlabel Normalized Frequency (\times\pi rad/sample), ylabel Magnitude (dB) contains an object of type line.

Design a 30th-order bandpass elliptic digital filter with passband edge frequencies of 0.3 and 0.7, passband ripple of 0.1 dB, and stopband attenuation of 50 dB. Plot the frequency response of the filter using its coefficients and gain in the CTF format.

[B,A,g] = ellip(30,0.1,50,[0.3 0.7],"ctf");
freqz({B,A,g},"ctf")

Figure contains 2 axes objects. Axes object 1 with title Phase, xlabel Normalized Frequency (\times\pi rad/sample), ylabel Phase (degrees) contains an object of type line. Axes object 2 with title Magnitude, xlabel Normalized Frequency (\times\pi rad/sample), ylabel Magnitude (dB) contains an object of type line.

Design an FIR lowpass filter of order 80 using a Kaiser window with β=8. Specify a normalized cutoff frequency of 0.5π rad/sample. Display the magnitude and phase responses of the filter.

b = fir1(80,0.5,kaiser(81,8));
freqz(b,1)

Figure contains 2 axes objects. Axes object 1 with title Phase, xlabel Normalized Frequency (\times\pi rad/sample), ylabel Phase (degrees) contains an object of type line. Axes object 2 with title Magnitude, xlabel Normalized Frequency (\times\pi rad/sample), ylabel Magnitude (dB) contains an object of type line.

Design the same filter using designfilt. Display its magnitude and phase responses.

d = designfilt("lowpassfir",FilterOrder=80, ...
    CutoffFrequency=0.5,Window={"kaiser",8});
freqz(d)

Figure contains 2 axes objects. Axes object 1 with title Phase, xlabel Normalized Frequency (\times\pi rad/sample), ylabel Phase (degrees) contains an object of type line. Axes object 2 with title Magnitude, xlabel Normalized Frequency (\times\pi rad/sample), ylabel Magnitude (dB) contains an object of type line.

Design an FIR bandpass filter with passband between 0.35π and 0.8π rad/sample and 3 dB of ripple. The first stopband goes from 0 to 0.1π rad/sample and has an attenuation of 40 dB. The second stopband goes from 0.9π rad/sample to the Nyquist frequency and has an attenuation of 30 dB. Compute the frequency response. Plot its magnitude in both linear units and decibels. Highlight the passband.

sf1 = 0.1;
pf1 = 0.35;
pf2 = 0.8;
sf2 = 0.9;
pb = linspace(pf1,pf2,1e3)*pi;

bp = designfilt("bandpassfir", ...
    StopbandAttenuation1=40,StopbandFrequency1=sf1, ...
    PassbandFrequency1=pf1,PassbandRipple=3, ...
    PassbandFrequency2=pf2,StopbandFrequency2=sf2, ...
    StopbandAttenuation2=30);

[h,w] = freqz(bp,1024);
hpb = freqz(bp,pb);

subplot(2,1,1)
plot(w/pi,abs(h),pb/pi,abs(hpb),'.-')
axis([0 1 -1 2])
legend("Response","Passband",Location="south")
ylabel("Magnitude")

subplot(2,1,2)
plot(w/pi,db(h),pb/pi,db(hpb),".-")
axis([0 1 -60 10])
xlabel("Normalized Frequency (\times\pi rad/sample)")
ylabel("Magnitude (dB)")

Figure contains 2 axes objects. Axes object 1 with ylabel Magnitude contains 2 objects of type line. These objects represent Response, Passband. Axes object 2 with xlabel Normalized Frequency (\times\pi rad/sample), ylabel Magnitude (dB) contains 2 objects of type line.

Compute and display the magnitude response of the third-order IIR lowpass filter described by the following transfer function:

H(z)=0.05634(1+z-1)(1-1.0166z-1+z-2)(1-0.683z-1)(1-1.4461z-1+0.7957z-2).

Express the transfer function in terms of second-order sections. Find the frequency response at 2001 points spanning the complete unit circle.

b0 = 0.05634;
b1 = [1  1];
b2 = [1 -1.0166 1];
a1 = [1 -0.683];
a2 = [1 -1.4461 0.7957];

sos1 = [b0*[b1 0] [a1 0]];
sos2 = [b2 a2];

[h,w] = freqz([sos1;sos2],'whole',2001);

Plot the magnitude response expressed in decibels.

plot(w/pi,20*log10(abs(h)))
ax = gca;
ax.YLim = [-100 20];
ax.XTick = 0:.5:2;
xlabel('Normalized Frequency (\times\pi rad/sample)')
ylabel('Magnitude (dB)')

Figure contains an axes object. The axes object with xlabel Normalized Frequency ( times pi blank rad/sample), ylabel Magnitude (dB) contains an object of type line.

Input Arguments

collapse all

Transfer function coefficients, specified as vectors. Express the transfer function in terms of b and a as

H(z)=B(z)A(z)=b1+b2z1+bnz(n1)+bn+1zna1+a2z1+amz(m1)+am+1zm

Example: b = [1 3 3 1]/6 and a = [3 0 1 0]/3 specify a third-order Butterworth filter with normalized 3 dB frequency 0.5π rad/sample.

Data Types: double | single
Complex Number Support: Yes

Number of frequency points over which to evaluate response, specified as a positive integer scalar no less than 2. When n is absent, it defaults to 512. For best results, set n to a value greater than the filter order.

Since R2024b

Cascaded transfer function (CTF) coefficients, specified as scalars, vectors, or matrices. B and A list the numerator and denominator coefficients of the cascaded transfer function, respectively.

B must be of size L-by-(m + 1) and A must be of size L-by-(n + 1), where:

  • L represents the number of filter sections.

  • m represents the order of the filter numerators.

  • n represents the order of the filter denominators.

For more information about the cascaded transfer function format and coefficient matrices, see Specify Digital Filters in CTF Format.

Note

If any element of A(:,1) is not equal to 1, then freqz normalizes the filter coefficients by A(:,1). In this case, A(:,1) must be nonzero.

Data Types: double | single
Complex Number Support: Yes

Since R2024b

Scale values, specified as a real-valued scalar or as a real-valued vector with L + 1 elements, where L is the number of CTF sections. The scale values represent the distribution of the filter gain across sections of the cascaded filter representation.

The freqz function applies a gain to the filter sections using the scaleFilterSections function depending on how you specify g:

  • Scalar — The function distributes the gain uniformly across all filter sections.

  • Vector — The function applies the first L gain values to the corresponding filter sections and distributes the last gain value uniformly across all filter sections.

Data Types: double | single

Digital filter, specified as a digitalFilter object. Use designfilt to generate a digital filter based on frequency-response specifications.

Example: d = designfilt('lowpassiir','FilterOrder',3,'HalfPowerFrequency',0.5) specifies a third-order Butterworth filter with normalized 3 dB frequency 0.5π rad/sample.

Second-order section coefficients, specified as a matrix. sos is a K-by-6 matrix, where the number of sections, K, must be greater than or equal to 2. If the number of sections is less than 2, the function treats the input as a numerator vector. Each row of sos corresponds to the coefficients of a second-order (biquad) filter. The ith row of sos corresponds to [bi(1) bi(2) bi(3) ai(1) ai(2) ai(3)].

Example: s = [2 4 2 6 0 2;3 3 0 6 0 0] specifies a third-order Butterworth filter with normalized 3 dB frequency 0.5π rad/sample.

Data Types: double | single
Complex Number Support: Yes

Sample rate, specified as a positive scalar. When the unit of time is seconds, fs is expressed in hertz.

Data Types: double

Angular frequencies, specified as a vector and expressed in rad/sample. w must have at least two elements, because otherwise the function interprets it as n. w = π corresponds to the Nyquist frequency.

Frequencies, specified as a vector. f must have at least two elements, because otherwise the function interprets it as n. When the unit of time is seconds, f is expressed in hertz.

Data Types: double

Output Arguments

collapse all

Frequency response, returned as a vector. If you specify n, then h has length n. If you do not specify n, or specify n as the empty vector, then h has length 512.

If the input to freqz is single precision, the function computes the frequency response using single-precision arithmetic. The output h is single precision.

Angular frequencies, returned as a vector. w has values ranging from 0 to π. If you specify 'whole' in your input, the values in w range from 0 to 2π. If you specify n, w has length n. If you do not specify n, or specify n as the empty vector, w has length 512.

Frequencies, returned as a vector and expressed in hertz. f has values ranging from 0 to fs/2 Hz. If you specify 'whole' in your input, the values in f range from 0 to fs Hz. If you specify n, f has length n. If you do not specify n, or specify n as the empty vector, f has length 512.

More About

collapse all

Cascaded Transfer Functions

Partitioning an IIR digital filter into cascaded sections improves its numerical stability and reduces its susceptibility to coefficient quantization errors. The cascaded form of a transfer function H(z) in terms of the L transfer functions H1(z), H2(z), …, HL(z) is

H(z)=l=1LHl(z)=H1(z)×H2(z)××HL(z).

Specify Digital Filters in CTF Format

You can specify digital filters in the CTF format for analysis, visualization, and signal filtering. Specify a filter by listing its coefficients B and A. You can also include the filter scaling gain across sections by specifying a scalar or vector g.

Filter Coefficients

When you specify the coefficients as L-row matrices,

B=[b11b12b1,m+1b21b22b2,m+1bL1bL2bL,m+1],A=[a11a12a1,n+1a21a22a2,n+1aL1aL2aL,n+1],

it is assumed that you have specified the filter as a sequence of L cascaded transfer functions, such that the full transfer function of the filter is

H(z)=b11+b12z1++b1,m+1zma11+a12z1++a1,n+1zn×b21+b22z1++b2,m+1zma21+a22z1++a2,n+1zn××bL1+bL2z1++bL,m+1zmaL1+aL2z1++aL,n+1zn,

where m ≥ 0 is the numerator order of the filter and n ≥ 0 is the denominator order.

  • If you specify both B and A as vectors, it is assumed that the underlying system is a one-section IIR filter (L = 1), with B representing the numerator of the transfer function and A representing its denominator.

  • If B is scalar, it is assumed that the filter is a cascade of all-pole IIR filters with each section having an overall system gain equal to B.

  • If A is scalar, it is assumed that the filter is a cascade of FIR filters with each section having an overall system gain equal to 1/A.

Note

  • To convert second-order section matrices to cascaded transfer functions, use the sos2ctf function.

  • To convert a zero-pole-gain filter representation to cascaded transfer functions, use the zp2ctf function.

Coefficients and Gain

If you have an overall scaling gain or multiple scaling gains factored out from the coefficient values, you can specify the coefficients and gain as a cell array of the form {B,A,g}. Scaling filter sections is especially important when you work with fixed-point arithmetic to ensure that the output of each filter section has similar amplitude levels, which helps avoid inaccuracies in the filter response due to limited numeric precision.

The gain can be a scalar overall gain or a vector of section gains.

  • If the gain is scalar, the value applies uniformly to all the cascade filter sections.

  • If the gain is a vector, it must have one more element than the number of filter sections L in the cascade. Each of the first L scale values applies to the corresponding filter section, and the last value applies uniformly to all the cascade filter sections.

If you specify the coefficient matrices and gain vector as

B=[b11b12b1,m+1b21b22b2,m+1bL1bL2bL,m+1],A=[a11a12a1,n+1a21a22a2,n+1aL1aL2aL,n+1],g=[g1g2gLgS],

it is assumed that the transfer function of the filter system is

H(z)=gS(g1b11+b12z1++b1,m+1zma11+a12z1++a1,n+1zn×g2b21+b22z1++b2,m+1zma21+a22z1++a2,n+1zn××gLbL1+bL2z1++bL,m+1zmaL1+aL2z1++aL,n+1zn).

Tips

  • You can obtain filters in CTF format, including the scaling gain. Use the outputs of digital IIR filter design functions, such as butter, cheby1, cheby2, and ellip. Specify the "ctf" filter-type argument in these functions and specify to return B, A, and g to get the scale values. (since R2024b)

  • If you have an irreducible multirate filter, use the freqzmr (DSP System Toolbox) function to analyze the filter in the frequency domain. For more information on irreducible multirate filters, see Overview of Multirate Filters (DSP System Toolbox). (since R2024a)

  • The freqzmr (DSP System Toolbox) function requires DSP System Toolbox™. (since R2024a)

Algorithms

The frequency response of a digital filter can be interpreted as the transfer function evaluated at z = e [1].

freqz determines the transfer function from the (real or complex) numerator and denominator polynomials you specify and returns the complex frequency response, H(e), of a digital filter. The frequency response is evaluated at sample points determined by the syntax that you use.

freqz generally uses an FFT algorithm to compute the frequency response whenever you do not supply a vector of frequencies as an input argument. It computes the frequency response as the ratio of the transformed numerator and denominator coefficients, padded with zeros to the desired length.

When you do supply a vector of frequencies as input, freqz evaluates the polynomials at each frequency point and divides the numerator response by the denominator response. To evaluate the polynomials, the function uses Horner's method.

References

[1] Oppenheim, Alan V., and Ronald W. Schafer, with John R. Buck. Discrete-Time Signal Processing. 2nd Ed. Upper Saddle River, NJ: Prentice Hall, 1999.

[2] Lyons, Richard G. Understanding Digital Signal Processing. Upper Saddle River, NJ: Prentice Hall, 2004.

Extended Capabilities

Version History

Introduced before R2006a

expand all

See Also

Apps

Functions