DSP Experiment 5 Lab Report
DSP Experiment 5 Lab Report
EXPERIMENT 5
1. Compute the impulse response of the following filters and plot the filter functions.
1.a).
Output
Code:
% 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
Output
Code:
% 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
1 c)
Output
Code
clc;
clearvars;
close all;
% 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
1 d)
Output
Code
% 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));
% 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
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.
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.
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;
% 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");
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
% 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")
for i = 1:length(n)
X(k) = X(k) + x(i) * exp(-1i * w(k) * n(i));
end
end
end
c) Output
Code:
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
% 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")
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
wp = 2 * pi * (passband_frequency / sampling_frequency);
ws = 2 * pi * (stopband_frequency / sampling_frequency);
wc = (wp + ws) / 2;
N = 8 * pi / (ws - wp);
if (rem(N, 2) == 0)
N = N + 1;
end
M = (N - 1) / 2;
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
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 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;
wp = 2 * pi * (passband_frequency / sampling_frequency);
ws = 2 * pi * (stopband_frequency / sampling_frequency);
wc = (wp + ws) / 2;
N = 8 * pi / (wp - ws);
if (rem(N, 2) == 0)
N = N + 1;
end
M = (N - 1) / 2;
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
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 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 :
Significance :
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);
% Rectangular window
% Hamming window
% Blackman window
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');
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:
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.
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;
% Filter specifications
passband_frequency = 1000; % Pass band frequency
stopband_frequency = 1500; % Stop band frequency
sampling_rate = 8000; % Sampling rate
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.
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.