ad space

Low-Pass Filter Design and Analysis in MATLAB

Low-pass filters (LPF) are widely used in signal processing to remove high-frequency noise. Here we explore the design and analysis of a Butterworth LPF using MATLAB. We'll begin by using the Butterworth filter function to design the filter coefficients, and then we'll use the Z-transform to obtain the transfer function equation. We'll then plot the frequency response graph of the filter to visualize its performance, and finally, we'll plot the poles and zeros of the filter to better understand its behavior. By the end of this post, you'll have a solid understanding of how to design and analyze low-pass filters using MATLAB.

Step 1: Define Filter Parameters 

To design a low-pass filter, we first need to define the filter parameters. In our example, we have set the cutoff frequency to 200 Hz and the sampling frequency to 1000 Hz. 

% Define filter parameters
fc = 200; % Cutoff frequency
fs = 1000; % Sampling frequency

 Step 2: Design Butterworth 2nd order LPF

Using the frequency parameters, we can calculate the filter coefficients using the Butterworth filter design function butter().

[b, a] = butter(2, 2*fc/fs); % Calculate filter coefficients

[b, a] = butter(2, 2fc/fs) is a MATLAB command used to design a Butterworth low-pass filter. The command takes two arguments: the filter order (2 in this case) and the normalized cutoff frequency (2fc/fs). The filter coefficients are then calculated and returned in two vectors, 'b' and 'a', which represent the numerator and denominator coefficients of the transfer function. These coefficients are used to define the filter and can be used in conjunction with the z-transform to obtain the transfer function equation of the filter.

Step 3: Create Transfer Function object and extract coefficient

H = tf(b, a, 1/fs)
[num, den] = tfdata(H, 'v');

The code H = tf(b, a, 1/fs) creates a transfer function object H using the filter coefficients b and a that were calculated using the Butterworth filter design function. The third argument 1/fs specifies the sampling time of the system.

The function tfdata is then used to extract the numerator and denominator coefficients of the transfer function H into separate cell arrays num and den. The second argument 'v' specifies that the numerator and denominator should be returned as row vectors rather than matrices.

Step 4: Convert Transfer Function to Symbolic Expression 

Next, we convert the transfer function to a symbolic expression and then to a string. This allows us to display the transfer function equation in a readable format.

syms z;
H_sym = poly2sym(num, z) / poly2sym(den, z);
H_str = char(vpa(H_sym, 3));

Step 5: Display the equation of the Transfer Function

disp('Transfer function equation:');
disp(H_str);
Transfer function equation:
(0.413*z + 0.207*z^2 + 0.207)/(z^2 - 0.37*z + 0.196)

Step 6: Plot Zero and Poles of the Filter 

Using the zplane() function in MATLAB, we can plot the zero and poles of the filter. This helps us to understand the stability of the filter and its behavior in the frequency domain.

zplane(b, a);
title('Pole-Zero Plot of Low-Pass Filter');

 

Pole zero plot in Matlab

 Step 4: Plot Frequency Response of the Filter

Finally, we use the freqz() function in MATLAB to plot the frequency response of the filter. This shows us how the filter attenuates the high-frequency components of the signal.

freqz(b, a);

 

tranfer function frequency response matlab

The following is the complete code for Butterworth filter design, transfer function equation, poles and zero plot and frequency response.

clear
fc = 200; % Cutoff frequency
fs = 1000; % Sampling frequency
[b, a] = butter(2, 2*fc/fs); % Calculate filter coefficients

H = tf(b, a, 1/fs);
[num, den] = tfdata(H, 'v');

% Convert transfer function to symbolic expression and then to a string
syms z;
H_sym = poly2sym(num, z) / poly2sym(den, z);
H_str = char(vpa(H_sym, 3));

% Display transfer function equation
disp('Transfer function equation:');
disp(H_str);

% Plot zero and poles of the filter
zplane(b, a);
title('Pole-Zero Plot of Low-Pass Filter');

% Plot frequency response of H
freqz(b, a);

In summary, the Matlab code above designs a 2nd-order low-pass Butterworth filter with a cutoff frequency of 200 Hz and a sampling frequency of 1000 Hz. The filter coefficients are calculated using the "butter" function. The transfer function of the filter is obtained using the "tf" function, and the numerator and denominator coefficients are extracted using the "tfdata" function. Then, the transfer function is converted into a symbolic expression using the "poly2sym" function and displayed as a string using "vpa" and "char" functions. The zero and pole locations of the filter are plotted using "zplane" function, and the frequency response of the filter is plotted using the "freqz" function.

Conclusion

The Butterworth filter is a popular choice for designing low-pass filters due to its flat frequency response in the passband and its easy implementation. Here it was shown how to design a low-pass filter using the Butterworth filter in MATLAB. By following the steps outlined in this post, you can easily design your own low-pass filter and understand its behavior in the frequency domain.

 References:

[1] FIR and IIR filter design with Z-transform

[2] FIR filter design by frequency sampling  

[3] Computing the Z-transform in MATLAB

Post a Comment

Previous Post Next Post