0% found this document useful (0 votes)
19 views6 pages

Exp5 in Detail

The document describes MATLAB code for designing a low-pass FIR filter using rectangular and Hamming windows. The code takes in user inputs for cutoff frequency, sampling frequency, and filter length. It calculates the frequency response using a sinc approximation and applies window functions to shape the response. Two figures are generated to compare the magnitude plots of the filter's frequency response when using rectangular and Hamming windows.

Uploaded by

ketaki.kulkarni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views6 pages

Exp5 in Detail

The document describes MATLAB code for designing a low-pass FIR filter using rectangular and Hamming windows. The code takes in user inputs for cutoff frequency, sampling frequency, and filter length. It calculates the frequency response using a sinc approximation and applies window functions to shape the response. Two figures are generated to compare the magnitude plots of the filter's frequency response when using rectangular and Hamming windows.

Uploaded by

ketaki.kulkarni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Certainly!

Let's go through the provided MATLAB code line by line:

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

Fc = input("Enter Cutoff Freq");


Fs = input("Enter Sampling Frequency");
N = input("Enter the length of filter");

● 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;

● Calculates the normalized cutoff frequency (Wc) in radians per sample.


matlab

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

hd = [hd1, hd0, hd2];

● 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;

● Generates a rectangular window (y1) of length N and applies it to the frequency


response (hd). The result is stored in h1n.

matlab

Copy code

y2 = hamming(N);
h2n = hd' .* y2;

● Generates a Hamming window (y2) of length N and applies it to the frequency


response (hd). The result is stored in h2n.

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:

Questions about Filter Design:


​ Question: What is the purpose of the cutoff frequency (Fc) in the context of a
low-pass FIR filter?
● Answer: The cutoff frequency determines the point at which the filter
starts attenuating the high-frequency components of the signal.
​ Question: How does the normalized cutoff frequency (Wc) relate to the cutoff
frequency and the sampling frequency?
● Answer:
● ��=2�����
● Wc=
● Fs
● 2πFc
● ​

● , 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.

Questions about Frequency Response Calculation:


​ Question: Explain how the frequency response of the filter is calculated for the
left side (hd1) and the right side (hd2) of the filter.
● Answer: The frequency response is calculated using a sinc function
approximation, with appropriate indexing for the left and right sides of the
filter.
​ Question: What is the significance of hd0 in the frequency response?
● Answer: hd0 represents the value of the frequency response at zero
frequency, often used as the DC gain of the filter.
​ Question: How is the complete frequency response vector hd formed from the
left, center, and right portions?
● Answer: The left, center, and right portions of the frequency response are
concatenated to form the complete frequency response vector hd.

Questions about Windowing:


​ Question: Why are window functions (rectwin and hamming) applied to the
frequency response?
● Answer: Window functions are applied to shape the frequency response
and control the trade-off between main lobe width and side lobe levels.
​ Question: What is the purpose of using a rectangular window (rectwin) in this
context?
● Answer: The rectangular window is used to emphasize the simplicity of
the filter design and to observe the effect on the frequency response.
​ Question: How does the Hamming window differ from the rectangular window in
terms of its impact on the frequency response?
● Answer: The Hamming window provides a smoother transition, reducing
the side lobe levels compared to the rectangular window.

Questions about Plotting:


​ Question: Why are two separate figures (figure(1) and figure(2)) used for
plotting the frequency responses?
● Answer: Two separate figures are used to clearly visualize the frequency
response using different window functions.
​ Question: What information is conveyed by the magnitude plot of the frequency
response?
● Answer: The magnitude plot shows the amplitude of the filter response at
different frequencies, providing insights into the filter's behavior.

You might also like