How to calculate IIR filter coefficients in Matlab

In signal processing, infinite impulse response (IIR) filters are widely used to extract useful information from a noisy signal. The filter's coefficients define the IIR filter's response characteristics, making them a crucial component in the filter design process. Here we will explore how to calculate IIR filter coefficients in MATLAB.

Before we dive into the calculations, let's first understand some basic concepts. An IIR filter is defined by the following difference equation:

\( H(z)=\frac{\sum_{k=0}^{M-1}b_{k}z^{-k}}{\sum_{k=0}^{N-1}a_{k}z^{-k}} \) ---->(1)

or, \( H(z) = \frac{b_0 + b_1 z^{-1} + b_2 z^{-2} + ... + b_n z^{-n}}{a_0 + a_1 z^{-1} + a_2 z^{-2} + ... + a_n z^{-n}}\)

y(n) is the filter output x(n) is the input signal b is the vector of numerator coefficients a is the vector of denominator coefficients N is the filter order

The numerator coefficients \(b_0,b_1,b_2...b_N\) and denominator coefficients \(a_0,a_1,a_2...a_N\) are the filter coefficients that we need to calculate. The filter order determines the number of coefficients that need to be calculated.

How to calculate IIR filter coefficients in Matlab

In MATLAB, the Signal Processing Toolbox provides various functions to design and analyze IIR filters. One such function is "butter," which is used to design a Butterworth filter. The following code generates the coefficients of a Butterworth filter of order N=4 with a cutoff frequency of 1kHz and sampling frequency of 44.1kHz.

% Define the filter parameters
fs = 44.1e3; % Sampling frequency
fc = 1e3; % Cutoff frequency
N = 4; % Filter order

% Design the filter
[b, a] = butter(N, fc/(fs/2), 'low');

% Print the filter coefficients
disp("Numerator coefficients (b):");
disp(b);
disp("Denominator coefficients (a):");
disp(a);

% Compute the frequency response
[h, w] = freqz(b, a);

% Convert from normalized frequency to Hz for axis
f = w/pi*fs/2; 

% Plot the magnitude and phase responses
subplot(2,1,1);
plot(f, 20*log10(abs(h)));
xlabel('Frequency (Hz)');
ylabel('Magnitude (dB)');
title('Magnitude Response');

subplot(2,1,2);
plot(f, unwrap(angle(h)));
xlabel('Frequency (Hz)');
ylabel('Phase (rad)');
title('Phase Response');

 This code uses the butter function to design a fourth-order Butterworth low-pass filter with a cutoff frequency of 1 kHz and a sampling frequency of 44.1 kHz. The resulting filter coefficients are stored in the b and a variables. The disp function is used to print the coefficients to the console.

The following shows the output, that is the calculated IIR filter numerator and denominator coefficients.

IIR coefficients

Note that for an IIR filter of order N, the denominator coefficients of the transfer function will have N+1 coefficients, and the numerator coefficients will have N+1 coefficients as well. This is because the transfer function is expressed as the ratio of two polynomials, and the degree of the polynomials is one more than the order of the filter.

Plot the IIR digital filter response

To view the filter response, that is the magnitude and phase response graph we can use the following matlab code.

% Compute the frequency response
[h, w] = freqz(b, a);

% Convert from normalized frequency to Hz for axis
f = w/pi*fs/2; 

% Plot the magnitude and phase responses
subplot(2,1,1);
plot(f, 20*log10(abs(h)));
xlabel('Frequency (Hz)');
ylabel('Magnitude (dB)');
title('Magnitude Response');

subplot(2,1,2);
plot(f, unwrap(angle(h)));
xlabel('Frequency (Hz)');
ylabel('Phase (rad)');
title('Phase Response');

The following shows the IIR filter frequency response graph designed above.

IIR coefficient frequency response
Similarly we can calculate FIR filter coefficients and plot the FIR filter frequency response graphs. So here it was shown how easily one can use Matlab to calculate FIR or IIR filter coefficients.

 References

[1] FIR Filter Frequency Response Matlab Program

[2] Low-Pass Filter Design and Analysis in MATLAB 

[3] FIR filter design by frequency sampling

Post a Comment

Previous Post Next Post