Experiment 7 Student’s Manual
American International University- Bangladesh
Department of Electrical and Electronic Engineering
EEE3211: Digital Signal ProcessingLaboratory
Title: Digital Filter Design and FIR Filtering of Sinusoidal Waveforms.
Introduction:
An analog signal may be processed by a digital filter by first being digitized and represented
as a sequence of numbers, then manipulated mathematically, and then reconstructed as a new
analog signal. Digital filters may be more expensive due to complexity but they make many
designs practical, which are impossible for analog filters. Digital filters are now common in
everyday electronics such as radios, cell phones, and stereo receivers.
The goal of this lab is to learn how to-
1. Design few digital filter techniques.
2. implement FIR filters in MATLABTM,
3. study the response of FIR filters to inputs such as complex exponentials.
4. use FIR filters to study properties such as linearity and time-invariance.
Theory and Methodology:
The FIR filtering expression can be written as:
M
y [ n]= ∑ bk x [ n− k ]
k= 0 … … … (1)
An FIR filter is a discrete-time system that converts an input signal x(n) into an output signal
y(n) by means of the weighted summation. Equation (1) gives a rule for computing the nth
value of the output sequence from certain values of the input sequence. The filter coefficients
{bk} are constants that define the filter’s behavior. As an example, a system can be considered
for which the output values are given by
1 1 1 1
y (n)= x (n)+ x ( n− 1)+ x (n− 2)= [ x (n)+ x ( n− 1)+ x ( n− 2)]
3 3 3 3
… … … (2)
This equation states that the nth value of the output sequence is the average of the nth value of
the input sequence x(n) and the two preceding values, x(n - 1) and x(n - 2). For this example
the bk’s are b0 = 1/3, b1 =1/3, and b2 = 1/3.
In our theory classes we examined filters that average input samples over a certain interval.
These filters are called “running average” filters or “averagers” and they have the following
form:
1 M
y (n)= ∑ b x ( n− k )
M +1 k = 0 k … … … (3)
The frequency response for the three-point running average operator is given by:
H (e j ω)= 2cosω+ 1 e− j ω
3
A digital filter can be described as a moving average filter:-
y(n) = b0x(n) + b1x(n-1) + b2x(n-2) + b3x(n-3) +..... - a1y(n-1) - a2y(n-2) + a3y(n-3) - .....
© Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 1
Experiment 7 Student’s Manual
or,
b0x(n) + b1x(n-1) + b2x(n-2) + b3x(n-3) +..... - y(n) - a1y(n-1) - a2y(n-2) + a3y(n-3).......... = 0
And the above equation can be rewritten in matrix form:
[] []
x ( n) y ( n)
x ( n− 1) y ( n− 1)
x ( n− 2) y ( n− 2)
[ b b b b b ...]◻ − [1 a a a a ...]◻ =0
x ( n− 3) y ( n− 3)
0 1 2 3 4 1 2 3 4
x ( n− 4) y ( n− 4)
... ...
Which can be again re-written as:
B*X - A*Y=0 … … … (4)
And a moving average filter with time window length of 4 is defined as:
y(n)= 0.25*x(n)+ 0.25*x(n-1)+ 0.25*x(n-2)+ 0.25*x(n-3)
The A and B matrix for this filter are:
B= [b0 b1 b2 b3 ...]= [0.25 0.25 0.25 0.25]
A= [1 a1 a2 a3 ...]= [1]
MATLABTM has a built-in function for implementing the operation in (1) to (4); namely, the
function filter( )(see in the Appendix of this lab sheet), which implements an operation called
convolution.
y=filter(B,A,x)
[Here A & B are coefficients of the filter in equation 1, x is the input signal and y is the
output of the filter]
After implementing coefficients A and B to the filter we usually test the filter by using a
signal generator and oscilloscope. Command freqz() in MATLAB do the oscilloscope and
signal generator job together.
[h,w]=freqz(B,A,n,Fs)
Where; A and B is the same in Equation 1, n is number of points we want to see the
frequency response graph, Fs is sampling frequency and h is the frequency response. Note
that if we drop the n the default is 512 and if drop Fs the default is pi=3.14.
In addition, this lab will introduce examples to show how a filter reacts to different frequency
components in the input.
Pre-Lab Homework:
1. Study the Digital Filter section in the text/reference book thoroughly.
2. Find out why the digital filters are necessary.
3. Study the FIR Filter section in the text/reference book thoroughly.
4. Find out why the impulse responses are finite here.
5. Find out the key differences between different types (IIR and FIR) filter.
© Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 2
Experiment 7 Student’s Manual
Apparatus:
1. A computer (capable of running MATLABTM)
2. MATLABTM (the latest version)
Precautions:
All the computers in the DSP laboratory are running Windows operating system. Unlike
Ubuntu or MacOSX, Windows is very much affected by virus. So to avoid infecting the virus
please double check your USB drive before connecting to the computer.
Experimental Procedure:
A. Frequency Response of the Three-Point Averager
The codes A from the Appendix will compute H(ejw) numerically and plot its magnitude and
phase versus ω. The function freqz( ) evaluates the frequency response for all frequencies in
the vector ww. The filter coefficients are defined in the assignment to vector bb.
B. Filter transfer functions
B.1. The Filter Transfer Functions for 4-point averaging filter is in Appendix B.1
B.2. The Filter Transfer Functions for 40-point averaging filter is in Appendix B.2
C. Frequency Response for FIR Filtering
We will introduce examples to show how a filter reacts to different frequency components in
the input. MATLABTM has a built-in function for computing the frequency response of a
discrete-time LTI system. It is called freqz( ). The codes from Appendix C, show how to use
freqz to compute and plot the magnitude (absolute value) of the frequency response of a two-
point averaging system as a function of ω in the range -π≤ω≤π and for this, the equation for
the digital filter is:-
𝑦(𝑛) = 𝑥(𝑛) − 𝑥(𝑛 − 1)
We will always use capital H for the frequency response. For FIR filters of the form of (1),
the second argument of freqz( , 1, ) must always be equal to 1.
D. Designing an Averaging Filter (using built-in function “filter”)
D.1. First we will generate three cosine signals and we will combine them to form a signal
that will be investigated in this experiment. And then we will generate a noise signal and add
the noise with the previously generated signal. (See Appendix D.1)
D.2. In the generated plots one figure shows the original signal and the other one shows the
signal where noise has been added to the original signal.
D.3. Now, we should use a 4-point averaging filter defined by:
x(n)+ x(n − 1)+ x(n − 2)+ x(n − 3)
1 1 1 1
y(n)=
4 4 4 4
© Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 3
Experiment 7 Student’s Manual
The code for this 4-point averaging filter is in Appendix D.2.
D.4. Now, we should use 40-point averaging filter. (See Appendix D.3)
D.5. The generated figure shows the effects of using higher order filter. We also can test
the signal for a moving average filter of length of 40, by just changing B to B=0.025*ones
(40, 1);
Simulation and Measurement:
In this section the corresponding simulation screen-shots should be added, and codes should
be shown in Appendice section.
Results and Discussion:
Interpret the data/findings and determine the extent to which the experiment was successful
in complying with the goal that was initially set. Discuss any mistake you might have made
while conducting the investigation and describe ways the study could have been improved.
Questions for report writing:
Exercise -1
Now we will study how a filter affects sinusoidal inputs, and begin to understand the
performance of the filter as a function of the input frequency. We will be interested in
filtering discrete-time sinusoids of the form
x(n)=Acos(ŵn+φ); for n=0, 1, 2, . . . , L–1 (5)
The discrete-time frequency for a discrete-time cosine wave, ŵ, always satisfies 0≤ŵ≤π. If
the discrete-time sinusoid is produced by sampling a continuous-time cosine, the discrete-
time frequency is ŵ=2π(f/fs). Generate L= 50 samples of a discrete-time cosine wave with
A=7, φ=π/3 and ŵ=0.125π. Store this signal in the vector xx. Now use filter( ) to implement
the following filter on the signal xx.
y(n) = 5x(n)-5x(n-1) ............ (5)
This is called a first-difference filter, but with a gain of five. In Matlab you must define the
vector bb needed in filter( ).
1. Note that y(n) and x(n) are not the same length. What is the length of the filtered
signal, and why is it that length?
2. Plot the first 50 samples of both waveforms x(n) and y(n) on the same figure,
using subplot. Use the stem function to make a discrete-time signal plot, but label
the x-axis to run over the range 0≤n≤49.
3. Verify the amplitude and phase of x(n) directly from its plot in the time domain.
4. From the plot, observe that with the exception of the first sample y(0), the
sequence y(n) seems to be a scaled and shifted cosine wave of the same frequency
as the input. Explain why the first sample is different from the others.
5. Determine the frequency, amplitude and phase of y(n) directly from the plot.
Ignore the first output point, y(0).
6. Characterize the filter performance at the input frequency by computing the
relative amplitude and phase, i.e., the ratio of output to input amplitudes and the
difference of output and input phases.
© Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 4
Experiment 7 Student’s Manual
Exercise -2
1. Now multiply the vector xx from Section 3.2 by two to get xa=2*xx. Generate the
signal ya by filtering xa with the first difference filter given by (5). Repeat the relative
amplitude and phase measurements described in the previous section.
2. Now generate a new input vector xb corresponding to the discrete-time signal xb(n) = 8
cos(0.25πn) and then filter it through the first difference operator to getyb(n). Then
repeat the relative amplitude and phase measurements as before. In this case the
measurement of phase might be a bit tricky because there are only a few samples per
period. Record how the amplitude, phase, and frequency of the output yb change
compared to the input.
3. Now form another input signal xc that is the sum of xa and xb. Run xc through the filter
to get yc and then plot yc. Compare yc to a plot of ya+yb. Are they equal? Explain any
differences that you observe.
Reference(s):
1. John G. Proakis&Dimitris G. Manolakis, “ Digital Signal Processing – Principles,
Algorithms and Applications”, Prentice – Hall India, 3rd Edition.
© Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 5