Lab 6 DSP
Lab 6 DSP
MATLAB help window – type “helpwin” at the command prompt. This will launch a new window
where all MATLAB core functions are categorized. To find a function, select the appropriate
category and select the operator you need to learn more about its syntax and operation.
MATLAB help desk – to launch, type “helpdesk” at the command prompt. The help desk will give
you web pages with more detailed help documents and examples of how functions can be used.
MATLAB function help - by typing in “help <function_name>” you can receive help on a
particular function. This is useful if you know the name of the function but are not sure about the
syntax.
MATLAB demos – type in “demo” at the command prompt. This takes you to a window where
simple demos show different capabilities of the software. This is a good starting point for new
users. Throughout out this lab, use the MATLAB beginner’s manual and the MATLAB help pages
as reference.
Filters
Filters are a basic component of all signal processing and telecommunication system. A filter is a
device or process that removes some unwanted components or features from a signal. Filtering is
a class of signal processing used to complete or partial suppression of some aspects of the signal.
Most often, this means to remove some frequencies and not others in order to suppress the
interfacing signals and reduce the background noise.
There are four main types of frequency selective filters: low-pass, high-pass, band-pass and band-
stop as shown in figure below.
Low-Pass Filters: An ideal Low-Pass filter is the one that allows to pass all frequency components
of a signal below a designated cutoff frequency ωc, and rejects to pass all frequency components
of a signal above ωc. Its frequency response satisfies:
High-Pass Filters: An ideal High-Pass filter is the one that allows to pass all frequency
components of a signal above a designated cutoff frequency ωc, and rejects to pass all frequency
components of a signal below ωc. Its frequency response satisfies:
Band-Pass Filters: An ideal Band-Pass filter is the one that allows to pass all frequency
components of a signal within a certain range, and rejects to pass all frequency components of a
signal outside of that range. Its frequency response satisfies:
Band-Stop Filters: An ideal Band-Stop filter is the one that rejects to pass all frequency
components of a signal within a certain range, and allows to pass all frequency components of a
signal outside of that range. Its frequency response satisfies:
Where "y(n)" is the filter output at discrete time instances "n", "bk" is the k-th feedforward tap, or
the filter coefficient, and "x(n – k)" is the filter input delayed by "k" samples. The "Σ" denotes
summation from k = 0 to k = M -1, where "M" is the number of the feedforward taps in the FIR
filter. Note that the FIR filter output depends only on the previous "M" inputs. This feature is why
the impulse response for a FIR filter is finite.
Infinite response is truncated to get finite impulse response. Placing a window of finite length does
this. Types of windows available are Rectangular, Bartlett, Hamming, Hanning, Blackmann
window etc. This FIR filter is an all zero filter.
Order of an FIR filter is considerably higher than that of an equivalent IIR filter meeting the same
specifications; this leads to higher computational complexity for FIR.
Code A
% Display results
disp('Output y[n]:');
disp(y);
Code B
% Specifications
fs = 1000; % Sampling frequency (Hz)
fc = 150; % Cutoff frequency (Hz)
N = 21; % Filter order (must be odd for
symmetric impulse response)
% Impulse response
figure;
impz(b);
title('Impulse Response of FIR Filter');
N = 21;
fc = 120; % cutoff frequencies equal to 120 Hz
fs = 500; % sampling frequency equal to 500 Hz
Wn = f/(0.5*fs); Simplifies Design Calculations
b = fir1(N, Wn);
The type of the window can be set by adding the additional parameter (window, specified by a
column vector (window_vect) to fir1 function:
N = 51;
window_vect = window(@gausswin,N,2.5); % 2.5 is the spread
b = fir1(N, Wn, window_vect);
The function window can be found in Signal Processing Toolbox (The MathWorks, Inc. 2010b)
and is used to specify various windows (@bartlett, @hann, @barthannwin, @blackman,
@bohmanwin, @chebwin, @kaiser, @flattopwin, @taylorwin, @gausswin, @nuttallwin,
@hamming, @parzenwin, @rectwin, @tukeywin, @triang, @blackmanharris) by a column
vector.
By default, the fir1 function designs the low-pass filter if Wn variable has one value or band-pass
filter if Wn variable is a row vector with two values (Wn = [Wn_low Wn_up]), defining the lower
(W_low) an upper (Wn_up) cutoff frequencies. The other types of the digital filter can be set by
additional parameter ’high’ for the high-pass filter and ’stop’ for the band-stop filter:
n = 50;
Wn_low = 0.2;
Wn_up = 0.4;
Wn = [Wn_low Wn_up];
b = fir1(n, Wn, ’stop’);
Three main characteristics of the FIR filter (impulse response, frequency response and phase
response) can be plotted by the use of impz and freqz functions respectively. MATLABTM Signal
Processing Toolbox (The MathWorks, Inc. 2010b) also offers a Filter Visualization Tool (fvtool)
which enables easy and quick view of various filter characteristics. For FIR filters these
characteristics can be easily extracted without these specialized functions.
FIR Filter Design using FDATool: This mandatory part of the laboratory is dedicated to learn
how to use the innovative MATLAB tools for FIR filter design. This session helps in
understanding the graphical user interface (GUI) for digital filter design found in DSP System
Toolbox (The MathWorks, Inc. 2010a).
1. Analyze following windows in time and frequency domain. Comment on your findings in terms
of Passband, Stopband parameters. You might use windows visualization tool in MATLAB for
this purpose. (Bartlett, Hamming, Hanning, Blackman or rectwin).
2. Design the following versions of Finite Impulse Response (FIR) Filter using fir1() using
windows of your own choice. (Bartlett, Hamming, Hanning, Blackman or rectwin).
% Sampling frequency
Fs = 1000; % Hz
% Normalized frequencies (divided by Nyquist frequency, Fs/2)
f_low = 0.2; % Low cutoff (normalized, 0 to 1 range)
f_high = 0.4; % High cutoff (normalized, 0 to 1 range)
filter_order = 4; % Order of the filter (can be adjusted)
……………………………………
% Filter Specifications
Low Pass FIR Filter
f_p = 1000; % Passband frequency (Hz)
f_s = 1500; % Stopband frequency (Hz)
f_samp = 8000; % Sampling frequency (Hz)
N = 50; % Filter order
% Transfer Function
b = h; % Numerator coefficients (FIR filter coefficients)
a = 1; % Denominator coefficient for FIR filters (a single 1)
Generate a signal in matlab with ten sinusoiudal frequencies and apply low pass filter on it in
matlab.
% Parameters
fs = 1000; % Sampling frequency (Hz)
t = 0:1/fs:1-1/fs; % Time vector (1 second duration)
frequencies = [10, 20, 50, 100, 150, 200, 300, 400, 500, 600]; % 10 frequencies
amplitudes = [1, 0.8, 1.2, 1, 0.7, 0.5, 0.3, 0.2, 0.1, 0.05]; % Amplitudes for each frequency
3. Design the following Finite Impulse Response (FIR) Filters using fdatool.
4. Use Digital Filte to remove noise from a noisy sine signal of 5 Hz. Use FIR with windows design
method.
Below is the complete MATLAB code for the low-pass filter design using the specifications and approach
described:
% Filter specifications
wp = 0.2 * pi; % Passband cutoff frequency (radians)
ws = 0.3 * pi; % Stopband cutoff frequency (radians)
delta1 = 0.01; % Maximum passband ripple
%Passband ripple refers to the variation in the magnitude of the frequency response within the
passband of a filter. 0.010 (1%)
delta2 = 0.01; % Maximum stopband attenuation
% Design the low-pass filter using the fir1 function with Kaiser window
filter_coeffs = fir1(N, wp/pi, 'low', kaiser(N+1, A));
Questions