0% found this document useful (0 votes)
36 views36 pages

DSP Experiment 5 Lab Report

Uploaded by

moushmithondepu
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)
36 views36 pages

DSP Experiment 5 Lab Report

Uploaded by

moushmithondepu
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/ 36

DIGITAL SIGNAL PROCESSING LAB

EXPERIMENT 5

Part A: FIR Filter Design using Window Method

1. Compute the impulse response of the following filters and plot the filter functions.

a. Ideal Low pass filter


b. Ideal High pass filter
c. Ideal Band pass filter
d. Ideal Band reject filter

1.a).

Output
Code:

clc, clearvars, close all;

% Define the filter parameters


filter_order = 50;
cutoff_frequency = 0.2;

% Compute the impulse response


n = -filter_order/2 : filter_order/2;
impulse_response_low_pass = sin(2*pi*cutoff_frequency*n)./(pi*n);
impulse_response_low_pass(filter_order/2 + 1) = 2*cutoff_frequency;

% Plot the impulse response


subplot(3,1,1);
stem(n, impulse_response_low_pass);
title('Ideal Low Pass Filter Impulse Response');

% DTFT Function
frequencies = linspace(-pi, pi, 1000); % adjust the number of points as
needed

Xn = zeros(size(frequencies));
for i = 1:length(frequencies)
Xn(i) = exp(-1i * frequencies(i) * n) * impulse_response_low_pass';
end

% Plot the frequency domain representation (Magnitude)


subplot(2, 1, 2)
plot(frequencies, abs(Xn));
title('Frequency Domain Representation (Magnitude)');
xlabel('Frequency (rad/sample)');
ylabel('Magnitude');
1 b)

Output

Code:

clc, clearvars, close all


% Define the filter parameters
filter_order = 50;
cutoff_frequency = 0.2;
% Compute the impulse response
n = -filter_order/2 : filter_order/2;
impulse_response_high_pass = -sin(2*pi*cutoff_frequency*n)./(pi*n);
% Replace the NaN at the center with the actual limit value
impulse_response_high_pass(filter_order/2 + 1) = 1 - 2*cutoff_frequency;
% Plot the impulse response
subplot(2, 1, 1);
stem(n, impulse_response_high_pass);
title('Ideal High Pass Filter Impulse Response');

% DTFT Function
frequencies = linspace(-pi, pi, 1000);Xn = zeros(size(frequencies));
for i = 1:length(frequencies)
Xn(i) = exp(-1i * frequencies(i) * n) * impulse_response_high_pass';
end

% Plot the frequency domain representation (Magnitude)


subplot(2, 1, 2)
plot(frequencies, abs(Xn));
title('Frequency Domain Representation (Magnitude)');
xlabel('Frequency (rad/sample)');
ylabel('Magnitude');

1 c)

Output

Code

clc;
clearvars;
close all;

% Define the filter parameters


filter_order = 50;
cutoff_frequencies = [0.2, 0.6];

% Compute the impulse response


n = -filter_order/2 : filter_order/2;
impulse_response_band_pass = 2 * (sin(2*pi*cutoff_frequencies(2)*n) -
sin(2*pi*cutoff_frequencies(1)*n)) ./ (pi*n);

% Replace the NaN at the center with the actual limit value
impulse_response_band_pass(filter_order/2 + 1) = 2 * (cutoff_frequencies(2) -
cutoff_frequencies(1));
% Plot the impulse response
subplot(2,1,1)
stem(n, impulse_response_band_pass);
title('Ideal Band Pass Filter Impulse Response');
xlabel('Time (samples)');
ylabel('Amplitude');

% DTFT Function
frequencies = linspace(-pi, pi, 1000); % Adjust the number of points as needed
Xn = zeros(size(frequencies));
for i = 1:length(frequencies)
Xn(i) = sum(impulse_response_band_pass .* exp(-1i * frequencies(i) * n));
end

% Plot the frequency domain representation (Magnitude)


subplot(2, 1, 2)
plot(frequencies, abs(Xn));
title('Frequency Domain Representation (Magnitude)');
xlabel('Frequency (rad/sample)');
ylabel('Magnitude');

1 d)

Output
Code

clc, clearvars, close all;

% Define the filter parameters


filter_order = 50;
cutoff_frequencies = [0.2, 0.6];

% Compute the impulse response


n = -filter_order/2 : filter_order/2;
impulse_response_band_reject = (sin(2*pi*cutoff_frequencies(1)*n) -
sin(2*pi*cutoff_frequencies(2)*n))./(pi*n);

% Replace the NaN at the center with the actual limit value
impulse_response_band_reject(filter_order/2 + 1) = 1- 2*(cutoff_frequencies(2)
- cutoff_frequencies(1));

% Plot the impulse response


subplot(2,1,1)
stem(n, impulse_response_band_reject);
title('Ideal Band Reject Filter Impulse Response');

% DTFT Function
frequencies = linspace(-pi, pi, 1000); % adjust the number of points as needed
Xn = zeros(size(frequencies));
for i = 1:length(frequencies)
Xn(i) = exp(-1i * frequencies(i) * n) * impulse_response_band_reject';
end

% Plot the frequency domain representation (Magnitude)


subplot(2, 1, 2)
plot(frequencies, abs(Xn));
title('Frequency Domain Representation (Magnitude)');
xlabel('Frequency (rad/sample)');
ylabel('Magnitude');
Discussion:

Low-pass Filter (LPF):

A low-pass filter allows low-frequency components of a signal to pass through while attenuating
higher frequencies.
It is often used to remove noise or high-frequency interference from a signal, leaving only the
desired low-frequency components.

High-pass Filter (HPF):

A high-pass filter allows high-frequency components of a signal to pass through while


attenuating lower frequencies.
It is commonly used to remove baseline drift or low-frequency noise from a signal, leaving only
the desired high-frequency components.

Band-pass Filter (BPF):

A band-pass filter allows a specific range of frequencies, known as the passband, to pass
through while attenuating frequencies outside this range.
It is useful for isolating specific frequency components of a signal while rejecting others.

Band-stop Filter (BSF) or Notch Filter:

A band-stop filter, also known as a notch filter, suppresses a specific range of frequencies,
known as the stopband, while allowing frequencies outside this range to pass through.
It is commonly used to remove interference or noise at specific frequencies while preserving the
rest of the signal

Impulse response h(t) (continuous time) or h[n] (discrete time) represents the system's output
when the input is an impulse function δ(t) or δ[n], respectively.

Discrete -Time Fourier transform or DTFT converts function from Time Domain to Frequency
Domain. It’s Equation is:
a) Output
Code:

% Rectangular Window

clc;
clearvars;
close all;

% Define the time domain signal

m = -50:1:50; % Time index


z = -pi:0.001*pi:pi; % Frequency index
M = 15; % Length of the rectangular window
rn = 1.*(m>=0 & m<=(M-1)); % Rectangular window function

% Compute the Discrete-Time Fourier Transform (DTFT) of the signal


xcheck = DTFT(rn, z, m);

% Plotting
figure
subplot(2,1,1)
plot(m, rn);
title("Rectangle Window in Time domain");
subplot(2,1,2)
plot(z, 20*log10(abs(xcheck)));
title("Rectangle Window in Frequency Domain");

% DTFT function definition


function X = DTFT(x, w, n)
X = zeros(length(w), 1); % Initialize DTFT output
for k = 1:length(w)
for i = 1:length(n)
X(k) = X(k) + x(i) * exp(-1i * w(k) * n(i));
end
end
end
b) Output:

Code:

%Bartlett Window

clc;
clearvars;
close all;

% Define parameters
M = 15; % Length of the Bartlett window
m = 0:1:M-1; % Time index
z = -pi:0.001*pi:pi; % Frequency index

% Compute Bartlett window


bn = 1 - (2 * (abs(m - (M-1)/2))) / (M-1);

% Compute the Discrete-Time Fourier Transform (DTFT) of the Bartlett


window
br_check = DTFT(bn, z, m);

% Plotting
subplot(2,1,1)
stem(m, bn);
title("Bartlett Window in time domain")
subplot(2,1,2)
plot(z, 20*log10(abs(br_check)))
title("Bartlett Window in frequency domain")

% DTFT function definition


function X = DTFT(x, w, n)
X = zeros(length(w), 1); % Initialize DTFT output
for k = 1:length(w)

for i = 1:length(n)
X(k) = X(k) + x(i) * exp(-1i * w(k) * n(i));
end
end
end

c) Output
Code:

clc, clearvars, close all;


% Variables
N = 15;
n = 0:N;
values = zeros(size(n));
%values
values = 0.5 - 0.5 * cos(2 * pi * n / (N-1));
% Plot the impulse response
subplot(2, 1, 1);
stem(n, values);
title('Hanning Window (Time Domain)');
xlabel('n');
ylabel('Amplitude');
% Compute and plot the Fourier Transform
N = length(n);
frequencies = (-N/2:N/2-1) / N;
values_fft = zeros(size(frequencies));
for k = 1:length(frequencies)
sum_val = 0;
for t = 1:N
sum_val = sum_val + values(t) * exp(-1i * 2 * pi * frequencies(k)
* n(t));
end
values_fft(k) = abs(sum_val);
end
subplot(2, 1, 2);
stem(frequencies, values_fft);
title('Fourier Transform of Hanning Window (Frequency Domain)');
xlabel('Normalized Frequency');
ylabel('Magnitude');
d) Output

Code:

%% BlackMan Window
clc;
clearvars;
close all;

% Define parameters
M = 15; % Length of the Blackman window
m = 0:1:M-1; % Time index
z = -pi:0.001*pi:pi; % Frequency index

% Compute Blackman window


bln = 0.42 - 0.5*cos(2*pi*m/(M)) + 0.08*cos(4*pi*m/(M));

% Compute the Discrete-Time Fourier Transform (DTFT) of the Blackman


window
bl_check = DTFT(bln, z, m);

% Plotting
subplot(2,1,1)
stem(m, bln);
title("Blackman Window in time domain")
subplot(2,1,2)
plot(z, 20*log10(abs(bl_check)))
title("Blackman Window in Frequency domain")

% DTFT function definition


function X = DTFT(x, w, n)
X = zeros(length(w), 1); % Initialize DTFT output
for k = 1:length(w)
% Compute DTFT sum for each frequency
for i = 1:length(n)
X(k) = X(k) + x(i) * exp(-1i * w(k) * n(i));
end
end
end
Discussion

Windowing is a technique used in signal processing to mitigate the effects of spectral leakage
and finite-duration effects when analyzing signals in the frequency domain. It involves
multiplying a finite portion of a signal, often referred to as a window, by a window function that
smoothly tapers the edges of the signal to zero. This process reduces artifacts introduced by
abrupt transitions at the edges of the signal, improving the accuracy of frequency analysis
techniques such as the Discrete Fourier Transform (DFT) or Discrete-Time Fourier Transform
(DTFT). Windowing helps to enhance the resolution and reduce distortion in the frequency
domain representation of signals.

Here are four commonly used windows along with their formulae:

1. Rectangular Window:

Formula:

Characteristics: Simplest window with constant value within the interval and zero elsewhere. It
has poor frequency resolution and high spectral leakage.

2. Hamming Window:

Formula:

Characteristics: Tapers smoothly to zero at the edges, reducing spectral leakage compared to
the rectangular window. Provides reasonable frequency resolution.
3. Hanning Window:

Formula:

Characteristics: Similar to the Hamming window but with slightly sharper roll-off. Also reduces
spectral leakage and provides good frequency resolution.

4. Blackman Window:

Formula:

Characteristics: Provides even better attenuation of side lobes and narrower main lobe
compared to the Hamming and Hanning windows. Offers improved frequency resolution.

These windows are commonly used in various signal processing applications to improve the
accuracy of frequency analysis by reducing spectral leakage and providing better frequency
resolution. The choice of window depends on the specific requirements of the application,
balancing factors such as main lobe width, side lobe levels, and frequency resolution.

Main Lobe Width (MLW): Refers to the width of the main lobe of a window function in the
frequency domain. A narrower MLW indicates better frequency resolution.

Side Lobe Attenuation (SLA): Indicates the level of suppression of the side lobes relative to
the main lobe. Higher SLA means better suppression of unwanted frequency components,
reducing spectral leakage and improving accuracy in frequency analysis.
3) Design a linear Phase Type 1 FIR filter with the following specifications using window
method. Choose an appropriate window function based on the given specifications and
compute the order of the filter. Plot the window function, ideal impulse response and
impulse response of the designed filter. Plot the magnitude and phase spectrum of the
designed filter and verify that the given specifications and linear phase property is met.
(40 Marks)
a. Pass band edge frequency = 1000Hz; Stop band edge frequency = 1500 Hz Minimum
stop band attenuation = 50 dB; Maximum pass band attenuation = 0.9 dB Sampling
Frequency = 8000Hz

OUTPUT
Code

clc;
clearvars;
close all;

% Given specifications

passband_frequency = 1000; % Pass band edge frequency in Hz


stopband_frequency = 1500; % Stop band edge frequency in Hz
sampling_frequency = 8000; % Sampling frequency in Hz
% Convert frequencies to normalized radian frequencies

wp = 2 * pi * (passband_frequency / sampling_frequency);
ws = 2 * pi * (stopband_frequency / sampling_frequency);
wc = (wp + ws) / 2;

% Calculate filter order using the Hamming window method

N = 8 * pi / (ws - wp);
if (rem(N, 2) == 0)
N = N + 1;
end
M = (N - 1) / 2;

% Generate Hamming window and ideal impulse response

for n = 0:N-1
hamming_window(n+1) = 0.54 + (0.46 * cos((2 * pi * (n - M)) / (N - 1)));
if (n == M)
ideal_impulse_response(n+1) = wc / pi;
else
ideal_impulse_response(n+1) = sin(wc * (n - M)) / (pi * (n - M));
end
End

% Generate designed filter impulse response by multiplying with the window

for n = 0:N-1
designed_filter_impulse_response(n+1) = ideal_impulse_response(n+1) *
hamming_window(n+1);
End

% Plotting

figure;
subplot(3, 1, 1);
stem(hamming_window);
title('Hamming Window');
xlabel('n');
ylabel('w(n)');

subplot(3, 1, 2);
plot(ideal_impulse_response);
title('Ideal Impulse Response');
xlabel('n');
ylabel('h(n)');
subplot(3, 1, 3);
plot(designed_filter_impulse_response);
title('Designed Filter Impulse Response');
xlabel('n');
ylabel('y(n)');

% Frequency analysis

w = -pi:0.001*pi:pi;
Y = calculate_DTFT(designed_filter_impulse_response);

Figure;

subplot(2, 1, 1);
plot((w * sampling_frequency) / (2 * pi), 20 * log10(abs(Y)));
title('Magnitude Response of Designed Filter');
xlabel('Frequency (Hz)');
ylabel('|Y(w)|');

subplot(2, 1, 2);
plot(w, angle(Y));
title('Phase Response of Designed Filter');
xlabel('Frequency (rad/s)');
ylabel('∠Y(w)');

% Function to calculate DTFT

function X = calculate_DTFT(x)
n = -32:1:32;
w = -pi:0.001*pi:pi;

X = zeros(length(w));
for k = 1:length(w)
for i = 1:length(n)
X(k) = X(k) + x(i) * exp(-1i * w(k) * n(i));
end
end
end

b) Pass band edge frequency = 1500Hz; Stop band edge frequency = 1000 Hz Minimum stop band
attenuation = 50 dB; Maximum pass band attenuation = 0.9 dB Sampling Frequency = 8000Hz

OUTPUT
CODE

% Clear workspace
clc;
clearvars;
close all;

% Given filter specifications

passband_frequency = 1500; % Pass band edge frequency in Hz


stopband_frequency = 1000; % Stop band edge frequency in Hz
sampling_frequency = 8000; % Sampling frequency in Hz
% Convert frequencies to normalized radian frequencies

wp = 2 * pi * (passband_frequency / sampling_frequency);
ws = 2 * pi * (stopband_frequency / sampling_frequency);
wc = (wp + ws) / 2;

% Calculate filter order using the Hamming window method

N = 8 * pi / (wp - ws);
if (rem(N, 2) == 0)
N = N + 1;
end
M = (N - 1) / 2;

% Generate Hamming window and ideal impulse response

for n = 0:N-1
hamming_window(n+1) = 0.54 + (0.46 * cos((2 * pi * (n - M)) / (N - 1)));
if (n == M)
ideal_impulse_response(n+1) = (pi - wc) / pi;
else
ideal_impulse_response(n+1) = -sin(wc * (n - M)) / (pi * (n - M));
end
End

% Generate designed filter impulse response by multiplying with the window

for n = 0:N-1
designed_filter_impulse_response(n+1) = ideal_impulse_response(n+1) *
hamming_window(n+1);
End

% Plotting

Figure;

subplot(3, 1, 1);
stem(hamming_window);
title('Hamming Window');
xlabel('n');
ylabel('win(n)');

subplot(3, 1, 2);
plot(ideal_impulse_response);
title('Ideal Impulse Response');
xlabel('n');
ylabel('h(n)');
subplot(3, 1, 3);
plot(designed_filter_impulse_response);
title('Designed Filter Impulse Response');
xlabel('n');
ylabel('y(n)');

% Frequency analysis

w = -pi:0.001*pi:pi;
Y = calculate_DTFT(designed_filter_impulse_response);

Figure;

subplot(2, 1, 1);
plot((w * sampling_frequency) / (2 * pi), 20 * log10(abs(Y)));
title('Magnitude Response of Designed Filter');
xlabel('Frequency (Hz)');
ylabel('|Y(w)|');

subplot(2, 1, 2);
plot(w, angle(Y));
title('Phase Response of Designed Filter');
xlabel('Frequency (rad/s)');
ylabel('∠Y(w)');

% Function to calculate DTFT

function X = calculate_DTFT(x)
n = -32:1:32;
w = -pi:0.001*pi:pi;

X = zeros(length(w));
for k = 1:length(w)
for i = 1:length(n)
X(k) = X(k) + x(i) * exp(-1i * w(k) * n(i));
end
end
end

DISCUSSION

Theoretical Background :

In digital signal processing, a Type 1 linear-phase FIR filter is designed to have a


symmetric impulse response, leading to a linear phase response. The linear phase
property is crucial in applications where phase distortion needs to be minimized, such
as in audio processing or communications.
The window method is one of the techniques used to design FIR filters. This method
involves multiplying an ideal impulse response by a window function to obtain a
finite-length filter. The choice of window function plays a significant role in shaping the
frequency response and controlling the trade-off between the main lobe width and
sidelobe levels.

Significance :

​ Linear Phase Property:


● Linear phase filters preserve the relative timing of different components in
a signal. This property is essential in applications like audio processing,
where preserving the timing relationships between different frequency
components is critical for maintaining the integrity of the signal.
​ Windowing:
● Windowing is employed to truncate the ideal impulse response to a finite
length. The Hamming window, chosen for this question, is known for its
balance between main lobe width and sidelobe suppression.
​ Attenuation Specifications:
● The specified minimum stopband attenuation (50 dB) and maximum
passband attenuation (0.9 dB) set stringent requirements for the filter
design. Meeting these specifications ensures effective suppression of
unwanted frequencies and minimal distortion in the passband.

Results and Observations :

​ Window Function:
● The Hamming window is employed due to its desirable characteristics. It
minimizes sidelobe levels while maintaining a reasonably narrow main
lobe, contributing to effective frequency response shaping.
​ Filter Order Calculation:
● The filter order is determined based on the specified attenuation
requirements and frequency band edges. A higher order is often required
to achieve better stopband attenuation.
​ Impulse Response:
● The ideal impulse response is modified by the Hamming window to obtain
the designed filter's impulse response. The windowing process introduces
ripples in the passband, but the Hamming window helps balance the
trade-off between main lobe width and sidelobe levels.
​ Frequency Response:
● The magnitude and phase spectra of the designed filter are plotted. The
magnitude spectrum illustrates how well the filter meets the given
specifications, with attention to stopband attenuation and passband ripple.
The phase spectrum confirms the linear phase property.

Conclusions :

​ Filter Performance:
● The Hamming window successfully shapes the filter's frequency response,
meeting the specified stopband and passband requirements. The linear
phase property is evident from the phase spectrum.
​ Trade-offs and Design Choices:
● The choice of the Hamming window reflects a trade-off between main lobe
width and sidelobe levels. Adjusting the filter order may be necessary to
further optimize these trade-offs based on specific application
requirements.
​ Practical Considerations:
● The results highlight the practicality of the window method in designing
FIR filters with linear phase characteristics. Understanding the trade-offs
involved in windowing is crucial for achieving the desired filter
performance.

In summary, the design and analysis of the Type 1 linear-phase FIR filter using the
Hamming window successfully meets the given specifications, demonstrating the
significance of appropriate window functions and design choices in digital signal
processing applications.

4) An FIR filter having the following specifications is to be designed using the window
method. Design the given filter using Rectangular, Hamming and Blackman windows.
Plot the window function, ideal impulse response and impulse response of the designed
filter. Also plot the magnitude and phase spectrum of the designed filter. Compare the
performance of the filters designed using different windows and give appropriate
comments based on your results. (20 Marks)
OUTPUT
CODE

clc;
clearvars;
close all;

% Filter parameters

w1 = 0.2 * pi;
w2 = 0.5 * pi;
w3 = 0.15 * pi;
w4 = 0.7 * pi;
attenuation1 = -1;
attenuation 2 = -60;
% Filter length

N = 10;
w_freq = linspace(0, pi, 1000);

% Ideal filter design

ideal_filter = zeros(1, N+1);


for n = 1:N+1
if (n == (N+1)/2)
ideal_filter(n) = 1 - (w4/pi - w3/pi);
else
ideal_filter(n) = (sin(w4*(n-(N+1)/2))/(pi*(n-(N+1)/2))) -
(sin(w3*(n-(N+1)/2))/(pi*(n-(N+1)/2)));
end
End

% Rectangular window

rectangular_window = ones(1, N+1);


rectangular_response = zeros(1, N+1);
for i = 1:N+1
rectangular_response(i) = rectangular_window(i) * ideal_filter(i);
end
rectangular_frequency_response = freqz(rectangular_response, 1, w_freq);

% Hamming window

hamming_window = 0.54 - 0.46 * cos(2*pi*(0:N)/N);


hamming_response = zeros(1, N+1);
for i = 1:N+1
hamming_response(i) = hamming_window(i) * ideal_filter(i);
end
hamming_frequency_response = freqz(hamming_response, 1, w_freq);

% Blackman window

blackman_window = 0.42 - 0.5 * cos(2*pi*(0:N)/N) + 0.08 * cos(4*pi*(0:N)/N);


blackman_response = zeros(1, N+1);
for i = 1:N+1
blackman_response(i) = blackman_window(i) * ideal_filter(i);
end
blackman_frequency_response = freqz(blackman_response, 1, w_freq);

% Plot window functions, ideal impulse responses, and impulse responses


figure;

subplot(3, 1, 1);
stem(rectangular_response);
title('Rectangular Window');
xlabel('Sample');
ylabel('Amplitude');

subplot(3, 1, 3);
stem(blackman_response);
title('Blackman Window');
xlabel('Sample');
ylabel('Amplitude');

subplot(3, 1, 2);
stem(hamming_response);
title('Hamming Window');
xlabel('Sample');
ylabel('Amplitude');

% Plot magnitude and phase spectra

figure;

subplot(3, 2, 1);
plot(w_freq, 20*log10(abs(rectangular_frequency_response)));
title('Magnitude Response (Rectangular)');
xlabel('Frequency (rad/sample)');
ylabel('Magnitude (dB)');
ylim([-70, 5]);

subplot(3, 2, 2);
plot(w_freq, angle(rectangular_frequency_response));
title('Phase Response (Rectangular)');
xlabel('Frequency (rad/sample)');
ylabel('Phase (radians)');

subplot(3, 2, 3);
plot(w_freq, 20*log10(abs(hamming_frequency_response)));
title('Magnitude Response (Hamming)');
xlabel('Frequency (rad/sample)');
ylabel('Magnitude (dB)');

subplot(3, 2, 4);
plot(w_freq, angle(hamming_frequency_response));
title('Phase Response (Hamming)');
xlabel('Frequency (rad/sample)');
ylabel('Phase (radians)');
subplot(3, 2, 5);
plot(w_freq, 20*log10(abs(blackman_frequency_response)));
title('Magnitude Response (Blackman)');
xlabel('Frequency (rad/sample)');
ylabel('Magnitude (dB)');

subplot(3, 2, 6);
plot(w_freq, angle(blackman_frequency_response));
title('Phase Response (Blackman)');
xlabel('Frequency (rad/sample)');
ylabel('Phase (radians)');

DISCUSSION

Theoretical Background:

In digital signal processing, the window method is a common technique used for designing finite
impulse response (FIR) filters. The process involves multiplying an ideal impulse response by a
window function. The window function helps shape the ideal response to meet the desired
specifications.

Design Specifications:

● Passband: 0.2π ≤ ω ≤ 0.5π


● Stopband: 0 ≤ ω ≤ 0.15π and 0.7π ≤ ω ≤ π
● Attenuation in the stopband: -60 dB
● The filter design will be performed using Rectangular, Hamming, and Blackman
windows.

Results:

​ Rectangular Window:
● The rectangular window results in a sinc-like frequency response.
● The main lobe of the sinc response is wide, leading to a poor frequency
selectivity.
● The stopband attenuation may not meet the specified -60 dB requirement.
​ Hamming Window:
● The Hamming window offers improved frequency selectivity compared to the
rectangular window.
● It provides better attenuation in the stopband, resulting in a more accurate
approximation to the ideal response.
● The trade-off is a wider main lobe compared to more advanced windows.

​ Blackman Window:
● The Blackman window further improves the frequency selectivity and stopband
attenuation compared to the Hamming window.
● It provides a more precise fit to the ideal response with a narrower main lobe.
● The transition between the passband and stopband is smoother, reducing
spectral leakage.

Observations and Conclusions:

​ Frequency Response:
● The Blackman window offers the best frequency selectivity and stopband
attenuation, followed by the Hamming and Rectangular windows.
​ Impulse Response:
● The window functions shape the ideal impulse response, influencing the filter's
time-domain characteristics.
● The Rectangular window results in a rectangular-like impulse response, while
Hamming and Blackman windows produce smoother responses.
​ Magnitude and Phase Spectra:
● The magnitude spectrum shows the frequency selectivity and stopband
attenuation.
● The phase spectrum provides information about the phase shift introduced by the
filter.
​ Overall Performance:
● The choice of window significantly affects the filter's performance.
● The Blackman window generally yields the best compromise between frequency
selectivity and stopband attenuation.
● The Rectangular window may be suitable for certain applications where simplicity
is more critical than performance.
​ Conclusion:
● The Blackman window is recommended for this application due to its superior
frequency response characteristics.
● The design process involves trade-offs between frequency selectivity, stopband
attenuation, and transition width.
● The performance of each window should be evaluated based on specific
application requirements.

Key Takeaways:

The window method provides a versatile approach for FIR filter design. Choosing an appropriate
window is crucial for achieving the desired frequency response characteristics. The Blackman
window, in this case, stands out for its balanced performance, but the choice depends on the
specific application's requirements and constraints.
OUTPUT
Code:

% Clear the command window, close all figures, and clear workspace variables
clc;
close all;
clearvars;

% Generate time samples


time_samples = linspace(-32, 32, 65);

% Filter specifications
passband_frequency = 1000; % Pass band frequency
stopband_frequency = 1500; % Stop band frequency
sampling_rate = 8000; % Sampling rate

% Generate the input signal


input_signal = (sin(2*pi*200*(time_samples/sampling_rate)) +
sin(2*pi*500*(time_samples/sampling_rate)) +
sin(2*pi*1000*(time_samples/sampling_rate)) +
sin(2*pi*2000*(time_samples/sampling_rate)) +
sin(2*pi*4000*(time_samples/sampling_rate)));

% Convert frequencies to radians


wp = 2 * pi * (passband_frequency / sampling_rate);
ws = 2 * pi * (stopband_frequency / sampling_rate);
wc = (wp + ws) / 2;
N = round(8 * pi / (ws - wp)); % Filter length
% Make the filter Type 1
if (rem(N, 2) == 0)
N = N + 1;
end
% Window and impulse response
M = (N - 1) / 2;
for n = 0:N-1
window(n+1) = 0.54 + (0.46 * cos((2 * pi * (n - M)) / (N - 1)));
if (n == M)
impulse_response(n+1) = wc / pi;
else
impulse_response(n+1) = sin(wc * (n - M)) / (pi * (n - M));
end
end
% Apply the window to the impulse response
for n = 0:N-1
filtered_response(n+1) = impulse_response(n+1) * window(n+1);
End

% Plot the magnitude and phase response


w = -pi:0.001*pi:pi ;
DTFT_response = calculate_DTFT(filtered_response);
figure
plot((w * sampling_rate) / (2 * pi), 20 * log10(abs(DTFT_response)));
title('Designed filter Impulse response')
xlabel('Frequency (Hz)')
ylabel('|Y(w)|')

% Plot the frequency domain representation of the input signal


figure
freq_response_input = calculate_DTFT(input_signal);
plot((w * sampling_rate) / (2 * pi), abs(freq_response_input));
xlabel("Frequency (Hz)")
ylabel("Amplitude")
title("Frequency Domain Representation of given function")

% Convolve the input signal with the designed filter


output_signal = calculate_DTFT(my_conv(input_signal, filtered_response));

% Plot the frequency domain representation of the output signal


figure
plot((w * sampling_rate) / (2 * pi), abs(output_signal));
xlabel("Frequency (Hz)")
ylabel("Amplitude")
title("Frequency Domain Representation of output")

% Function to calculate DTFT


function DTFT_result = calculate_DTFT(x)
n = -32:1:32;
w = -pi:0.001*pi:pi;
DTFT_result = zeros(size(w));
for k = 1:length(w)
for i = 1:length(n)
DTFT_result(k) = DTFT_result(k) + x(i) * exp(-1i * w(k) * n(i));
end
end
End

% Function to perform convolution


function convolution_result = my_conv(x_n, h_n)
m = length(x_n);
n = length(h_n);
X = [x_n, zeros(1, n)];
H = [h_n, zeros(1, m)];

for i = 1:n+m-1
y(i) = 0;
for j = 1:i
y(i) = y(i) + X(j) * H(i-j+1);
end
end
convolution_result = y;
end

DISCUSSION

1. Windowing: Applying a Hamming window to the ideal impulse response of the low-pass filter.
The windowing process involves multiplying each sample of the impulse response by the
corresponding sample of the Hamming window. This technique is used to mitigate the
undesirable effects of spectral leakage.

2. Frequency Response: Obtaining the frequency response of the designed filter using the
Discrete-Time Fourier Transform (DTFT). The frequency response provides insights into how
the filter behaves across different frequencies, showcasing its passband and stopband
characteristics.

3. Input Signal : Generating a composite input signal consisting of multiple sinusoidal


components with varying frequencies. This signal serves as a representative input for evaluating
the performance of the designed low-pass filter.

4. Convolution: Applying convolution to implement the designed low-pass filter on the composite
input signal. Convolution in the time domain involves combining the input signal with the filter's
impulse response to obtain the filtered output.

5. Frequency Domain Representation: Calculating the frequency domain representation of both


the input signal and the filtered output using the DTFT. This visualization aids in understanding
the spectral content of the signals before and after the application of the low-pass filter.

T K SREEVATSA MURTHY (B210656EC)


THONDEPU MOUSHMI (B210745EC)
GROUP D12

You might also like