Exp5 in Detail
Exp5 in Detail
matlab
Copy code
clc;
clf;
close all;
clear all;
● These lines clear the command window (clc), close all open figures (clf), close
all open windows (close all), and clear all variables from the workspace (clear
all).
matlab
Copy code
● The user is prompted to input values for the cutoff frequency (Fc), sampling
frequency (Fs), and the length of the filter (N).
matlab
Copy code
M = (N-1)/2;
● Calculates half of the filter length (M). This is used in the subsequent lines to
define the range of indices for the filter coefficients.
matlab
Copy code
Wc = (2*pi*Fc)/Fs;
Copy code
hd0 = Wc/pi;
● Sets the value of the frequency response at zero frequency (hd0). This value is
often used as the DC gain of the filter.
matlab
Copy code
n = 0:(M-1);
hd1 = (Wc/pi) * (sin(Wc * (n - M))) ./ (Wc * (n - M));
● Defines the frequency response (hd1) for indices from 0 to M-1. It uses the sinc
function to approximate the response.
matlab
Copy code
n1 = M+1:N-1;
hd2 = (Wc/pi) * (sin(Wc * (n1 - M))) ./ (Wc * (n1 - M));
● Defines the frequency response (hd2) for indices from M+1 to N-1. This covers the
right side of the frequency response.
matlab
Copy code
● Concatenates the left, center, and right portions of the frequency response to
create the complete frequency response vector (hd).
matlab
Copy code
disp('hd1= ');
disp(hd1);
disp('hd2= ');
disp(hd2);
disp('hd0= ');
disp(hd0);
● Displays the values of hd1, hd2, and hd0 in the command window.
matlab
Copy code
y1 = rectwin(N);
h1n = hd' .* y1;
matlab
Copy code
y2 = hamming(N);
h2n = hd' .* y2;
matlab
Copy code
figure(1);
freqz(h1n);
xlabel('Frequency');
ylabel('Magnitude');
title('Magnitude Plot (FIR LPF RECTANGULAR)');
● Creates a figure, plots the frequency response of the filter using the rectangular
window (h1n), and adds labels and a title to the plot.
matlab
Copy code
figure(2);
freqz(h2n);
xlabel('Frequency');
ylabel('Magnitude');
title('Magnitude Plot (FIR LPF HAMMING)');
● Creates another figure, plots the frequency response of the filter using the
Hamming window (h2n), and adds labels and a title to the plot.
Sure, here are some potential viva questions related to the provided MATLAB code for
designing a low-pass FIR filter using rectangular and Hamming windows, along with
their answers:
● , where
● ��
● Fc is the cutoff frequency and
● ��
● Fs is the sampling frequency.
Question: What does the variable N represent in the code, and how is it related to
the filter length?
● Answer: N represents the length of the FIR filter, and it is used to define the
filter length in the code.
Question: Why is half of the filter length calculated as M = (N-1)/2?
● Answer: In FIR filter design, it's common to work with symmetric filters.
Calculating half of the filter length simplifies the indexing for the filter
coefficients.