DSP Proj Lab

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

Digital Signal Processing

EEE-324
Lab Project

Submitted Khansa Maryam


By: Javeria Javed
Maimoona Aziz
Registration FA20-BEE-232
No: FA20-BEE-079
FA20-BEE-094
Class: BEE-6B

Submitted Sir Mubeen Sabir


To:
Lab Project Task
Digital FIR Lowpass Filter

Question No#1:
Design a digital FIR lowpass filter (with the following specifications):

𝑤𝑝 = 0.2𝜋, 𝑅𝑝 = 0.25 𝑑𝐵, 𝑤𝑠 = 0.3𝜋, 𝐴𝑠 = 50 𝑑𝐵

Choose an appropriate window function from Table 7.1 of book “Digital signal
processing using MATLAB” 3rd edition by Vinay K. Ingle and John G. Proakis.

(i) Plot the impulse response and frequency responses of the designed filter.
(ii) Chose an input signal of your choice (plot it as well) and show filtered
signal. Do this for both: first by adjusting frequency of input signal within
pass band and then adjusting it into stop band.

 Objectives:
The purpose of this project is to design a digital FIR lowpass filter based on the given
specifications:

 The filter should have a passband cutoff frequency of wp = 0.2π


 Passband ripple of Rp = 0.25 dB
 Stopband cutoff frequency of ws = 0.3π
 Stopband attenuation of As = 50 dB
 The filter will be designed deciding on any specific window function, which
will be used in plotting the impulse response and frequency response of the
filter, and applying the filter to input signals within the passband and
stopband.

 Low Pass Filter:


In Digital Signal Processing (DSP), a lowpass filter (LPF) is one which allows low-
frequency components of a signal to pass through while suppressing or attenuating the
high frequency components of a spectrum while ‘passing’ the low frequencies within
a specified range.
A lowpass filter defines a cutoff frequency, which represents the point where the filter
starts attenuating higher frequencies. Below the cutoff frequency (known as the
passband), the filter allows the signal to pass through with minimal attenuation or
distortion. Above the cutoff frequency (known as the stopband) , the filter
progressively attenuates the signal, reducing the amplitudes of higher-frequency
components

Input signal -> Low pass filter -> Output signal

Uses:
LPF are used to remove noise, smooth signals, anti-aliasing in analog-to-digital
conversion, and extract desired frequency components from a signal while attenuating
unwanted high-frequency components.

 FIR Lowpass Filter:


Finite Impulse Response (FIR) low pass filter is to shape the frequency response of
the filter so that it allows frequencies below a certain cutoff frequency to pass through
with minimal attenuation while attenuating frequencies above the cutoff. The
characteristics of the filter, such as the cutoff frequency and the amount of
attenuation, are determined by means of filter
coefficients.

Advantages:
The benefit of an FIR low pass filter is its linear
phase response, which preserves the phase
relationships of different frequency components in the filtered signal. This
characteristic is important in applications where phase coherence is essential, such as
audio processing.

Applications:
1. Signal Conditioning: In signal
processing applications, lowpass FIR
filters are used to preprocess signals by
removing high-frequency noise or
interference. This helps improve the signal quality and enhance subsequent
analysis or processing steps.

2. Anti-Aliasing: When digitizing analog signals, such as audio or sensor data,


aliasing can occur if high-frequency components are not properly attenuated
before sampling. A lowpass FIR filter is used as an anti-aliasing filter to
ensure that only frequencies within the desired Nyquist range are sampled
accurately.

3. Image Processing: In image processing, lowpass FIR filters can be applied to


reduce noise, blur, or smooth images. By removing high-frequency details,
these filters can help enhance image quality, reduce artifacts, or extract
specific features from the image.

4. Communications: In wireless communication systems, lowpass FIR filters are


used for channel equalization, pulse shaping, and baseband filtering. They
help shape the transmitted signal spectrum, limit out-of-band interference, and
improve the overall signal quality.

 Impulse Response:
The impulse response of a system, often denoted as h(t) or h[n], refers to the output of
the system when an impulse function is applied as the input. An impulse function is a
signal that is zero everywhere except at a single point, where it has a value of one.

Impulse Response

MATLAB Implementation:
In MATLAB, you can implement the impulse response of a system using the built-in
`impulse` function. The `impulse` function calculates the response of a linear time-
invariant (LTI) system to an impulse input, i.e:

t = 0:dt:T; % Time vector

impulse_response = impulse(system, t);


By analyzing the impulse response, we can gain insights into the behavior of a
system. For example, the frequency content of the impulse response can be used to
determine the system's frequency response and its ability to pass or attenuate specific
frequency components.

 Frequency Response:
The frequency response of a system is a measure of how the system responds to
different frequencies in the input signal. It describes the system's behavior as a
function of frequency and provides important information about its filtering
characteristics and amplitude/phase response.

Visualization:
 The frequency response is commonly visualized using a Bode plot, which
shows the magnitude response and phase response as a function of frequency.
 The magnitude response is typically plotted on a logarithmic scale (in
decibels) to show the gain/attenuation.
 The phase response is plotted in degrees to show the phase shift.

Advantages:
By manipulating the frequency response, we can achieve desired signal processing
goals, such as removing unwanted frequencies, emphasizing specific frequency
ranges, or compensating for phase distortions.

Adjusting frequency of signal with pass band:


The effectiveness and obstacles of adjusting the frequency within the passband
depend on the specific filter's characteristics and the desired frequency range. The
filter's passband width and steepness of the frequency response will determine the
level of control and accuracy in adjusting the frequency.

Additionally, the specific implementation details and tools used may vary based on
your application and the software or hardware platform you are working with.

Adjusting frequency of signal into stop band:


To adjust the frequency into the stop band of a filter does not guarantee complete
elimination of the signal. The effectiveness of this adjustment will depend on the
filter's stop band characteristics, such as its attenuation level and the sharpness of the
frequency response roll-off.
 Window Design Technique:
As per the reference book, section 7.3, various sorts of window design techniques are
elaborated to carry out the task of designing an FIR, in order to choose a proper ideal
frequency-selective filter & then to truncate its impulse response, to obtain a linear-
phase & causal FIR filter. Therefore, it’s imperative to select an appropriate
windowing function & an ideal filter. Various window designs are mentioned in the
book, such as rectangular window, Bartlett window, Hann window, Hamming
window Blackman window etc. Out of these, we opt for Hamming Window.

From book

Reason to choose Hamming Window:


The Hamming window is a popular design technique used for designing Finite
Impulse Response (FIR) lowpass filters. It offers several advantages that make it a
favorable choice in many applications. Here are some advantages of using the
Hamming window design technique:
1. Amplitude Tapering: The Hamming window applies a tapering effect to the
filter coefficients, gradually reducing their values towards the ends of the
window. This tapering minimizes abrupt transitions and reduces the impact of
spectral leakage, which can occur when a signal's frequency components
extend beyond the window length. The amplitude tapering of the Hamming
window helps mitigate the effects of spectral distortion.
2. Symmetry: The Hamming window exhibits a symmetric shape, which
simplifies the filter design and implementation process. The symmetric
property ensures that the resulting FIR filter is linear phase, meaning that it
introduces a constant delay across all frequencies. Linear phase filters are
desirable in applications where preserving the phase relationship of the signal
is crucial, such as audio or communication systems.
3. Ease of Implementation: The Hamming window is relatively straightforward
to implement since it involves only simple mathematical calculations. Its
equation is widely available and can be easily programmed in various DSP
platforms or software tools.
 Hamming Window:
The Hamming window is a type of window function commonly used in signal
processing. It is designed to reduce the outcomes of spectral leakage and decrease the
amplitude of the sidelobes inside the frequency domain.

From Section 7.3.4

From Section 7.3.4

MATLAB Implementation:
 As for this task, the Hamming window (w_ham) is created using the
hamming() function with a length of M. The window function is multiplied
element-wise with the ideal impulse response to obtain the actual impulse
response (h).
 w=hamming(M) returns the M-point Hamming window function in array w

 Tasks:
(i) Plot the impulse response and frequency responses of the
designed filter.
MATLAB Code:
%PART A
% filter specifiaction
wp = 0.2*pi;
ws = 0.3*pi;
Rp = 0.25; %dB
As = 50; %dB
%calculate filter order
tr_width = ws - wp;
M = ceil(6.6*pi/tr_width) + 1
M = 67
n=[0:1:M-1];
% Ideal LPF cutoff frequency
wc = (ws+wp)/2,
%ideal impulse response
hd = ideal_lp(wc,M);
%apply window function
w_ham= (hamming(M))';
h = hd .* w_ham;
%frequency response
[db,mag,pha,grd,w] = freqz_m(h,[1]);
H=freqz_m(h,[1]);
delta_w = 2*pi/1000;

% plots
figure;
stem(n,hd,'b', 'LineWidth', 2);
title('Ideal Impulse Response')
axis([0 M-1 -0.1 0.3]);
xlabel('n');
ylabel('hd(n)')

figure;
stem(n,w_ham,'b', 'LineWidth', 2);
title('Hamming Window')
axis([0 M-1 0 1.1]);
xlabel('n');
ylabel('w(n)')
figure;
stem(n,h,'r', 'LineWidth', 2);
title('Actual Impulse Response')
axis([0 M-1 -0.1 0.3]);
xlabel('n');
ylabel('h(n)')

figure;
plot(w/pi,db,'b', 'LineWidth', 2);
title('Magnitude Response in dB');grid
axis([0 1 -100 10]);
xlabel('frequency in pi units');
ylabel('Decibels')
figure;
plot(w,H,'r', 'LineWidth', 2);
xlabel('Frequency');
ylabel('Magnitude (dB)');
title('Frequency Response');
grid on;

Explanation:
This code is used to design a low-pass filter & to plot impulse & frequency response,
using the windowing method. The step-to-step explanation of the functionality is as
below:

1. Definition of parameters: wp and ws represent the passband and stopband edge


frequencies respectively, measured in radians per sample. Rp is the maximum
passband ripple in decibels, and As is the minimum stopband attenuation in
decibels.
2. Calculating Filter Order: The transition width of the filter, tr_width, is
calculated as the difference between the stopband and passband edge
frequencies. The filter order, M, is then determined using the formula
ceil(6.6*pi/tr_width) + 1. The ceil function rounds up to the nearest integer
value.
3. Generating Ideal Impulse Response: The ideal cutoff frequency for the low-
pass filter, wc, is computed as the average of the passband and stopband edge
frequencies. The ideal_lp function is called to generate the ideal impulse
response, hd, of length M.
4. Applying Window Function: Hamming window, represented by w_ham, is
generated using the hamming function. The window is a sequence of weights
applied to the ideal impulse response to shape it and reduce the magnitude of
side lobes in the frequency domain.
5. Frequency Response Calculation: The frequency response of the filter is
calculated using the freqz_m function. It returns the magnitude response in
decibels, db, magnitude response, mag, phase response, pha, group delay,
grd, and frequency values, w. The function is called twice, once for plotting
purposes and once to store the frequency response in H.
6. Plots: The code includes several plot commands to visualize the various
components of the filter design. The plots display the ideal impulse response,
the Hamming window, the actual impulse response, and the magnitude and
frequency response of the filter.
(ii) Chose an input signal of your choice (plot it as well) and
show filtered signal. Do this for both: first by adjusting
frequency of input signal within pass band and then
adjusting it into stop band
MATLAB Code:
%PART B
% Generate input signal
fs = 3*pi; % Sampling frequency
t = linspace(0, 1, 1000); % Time vector
fpass = 0.4 * fs; % Frequency within the passband
fstop = 0.65 * fs; % Frequency within the stopband
xpass = 3*sin(fpass * t);
xstop = 2*sin(fstop * t);

% Apply the filter to the input signals


ypass = conv(xpass, h, 'same');
ystop = conv(xstop, h, 'same');

% Plot the input signals and filtered signals


figure;
plot(t, xpass, 'g', 'LineWidth', 2);
axis([0 1 0 4]);
xlabel('Time');
ylabel('Amplitude');
title('Input signal (Passband)');
grid on;
figure;
plot(t, ypass, 'b', 'LineWidth', 2);
axis([0 1 0 4]);
xlabel('Time');
ylabel('Amplitude');
title('Filtering a Signal within the Passband');
grid on;

figure;
plot(t, xstop, 'g', 'LineWidth', 2);
axis([0 1 0 2]);
xlabel('Time');
ylabel('Amplitude');
title('Input Signal (Stopband)');
grid on;
figure;
plot(t, ystop, 'b', 'LineWidth', 2);
axis([0 1 0 2]);
xlabel('Time');
ylabel('Amplitude');
title('Filtering a Signal within the Stopband');
grid on;

Explanation:
This code demonstrates the application of the previously designed low-pass filter to
input signals within the passband and stopband. In detail explanation is as follows:

1. Definition of Parameters: The sampling frequency is defined as fs and creating


a time vector t using the linspace function. The time vector spans from 0 to 1
with 1000 points, representing the duration of the signals.

2. Generation of Input Signals: Two input signals are generated, one as xpass
representing a sinusoidal signal with a frequency fpass within the passband,
and second as xstop represents a sinusoidal signal with a frequency fstop
within the stopband.

3. Filtering the Input Signals: The input signals are convolved with the filter
impulse response h using the conv function. The 'same' parameter ensures
that the output signal has the same length as the input signal. The filtered
signals are stored in ypass and ystop respectively.

4. Plotting the Input and Filtered Signals: The code includes four plot commands
to visualize the input signals and the filtered signals. The first two plots show
the input signal within the passband (xpass) and the corresponding filtered
signal (ypass). The next two plots display the input signal within the stopband
(xstop) and the filtered signal (ystop).

 Critical Analysis:
FIR filters are widely utilized due to their numerous advantages. They benefit from
efficient design algorithms, inherent stability when implemented in a non-recursive
form, the ability to achieve linear phase easily, seamless extensibility to multirate
scenarios, and robust hardware support. This project demonstrates the capabilities of
the DSP System Toolbox for planning lowpass FIR filters with diverse characteristics.

The fundamental concept behind the window design approach involves selecting an
appropriate ideal frequency-selective filter, which inherently possesses a noncausal
and infinite-duration impulse response. Subsequently, the impulse response is
truncated (or windowed) to obtain a linear-phase and causal FIR filter. Thus, the key
focus of this method lies in choosing suitable windowing functions and ideal filters.

For this particular project, the filter specifications, filter length (M), and Hamming
window function (w(n)) were chosen. Initially, the project explored a continuous low-
pass filter and simulated its behavior using a digital filter. A band-stop filter, also
known as a band-rejection filter, is designed to block a specific band of frequencies
while leaving others unaltered. On the other hand, a band-pass filter (BPF) allows
frequencies within a certain range to pass through while rejecting those outside the
range.

In Part (i) of the project, the execution focused on analyzing the filter characteristics
without an input signal. However, in Part (ii), an input signal of our choosing was
employed to evaluate the performance of the Hamming window.

Overall, this project highlights the strengths and functionality of FIR filters,
emphasizes the significance of appropriate windowing functions and ideal filters, and
showcases the design capabilities available in the DSP System Toolbox.
References:
 “Digital signal processing using MATLAB” 3rd edition by Vinay K. Ingle and
John G. Proakis
 https://www.sciencedirect.com/topics/engineering/lowpass-filter
 https://www.elprocus.com/fir-filter-for-digital-signal-processing/
 http://signalsandsystems.wikidot.com/impulse-response

You might also like