1 Pre-Lab: Lab 07: Sampling, Convolution, and FIR Filtering
1 Pre-Lab: Lab 07: Sampling, Convolution, and FIR Filtering
Pre-Lab and Warm-Up: You should read at least the Pre-Lab and Warm-up sections of this lab assignment
and go over all exercises in the Pre-Lab section before going to your assigned lab session.
Verification: The Warm-up section of each lab must be completed during your assigned Lab time and
the steps marked Instructor Verification must also be signed off during the lab time. One of the laboratory
instructors must verify the appropriate steps by signing on the Instructor Verification line. When you have
completed a step that requires verification, simply demonstrate the step to the TA or instructor. Turn in the
completed verification sheet to your TA when you leave the lab.
Lab Report: It is only necessary to turn in a report on Section 3 with graphs and explanations. You are
asked to label the axes of your plots and include a title for every plot. In order to keep track of plots, include
your plot inlined within your report. If you are unsure about what is expected, ask the TA who will grade
your report.
1 Pre-Lab
The goal of this lab is to learn how to implement FIR filters in M ATLAB, and then study the response of FIR
filters to various signals, including images and speech. As a result, you should learn how filters can create
interesting effects such as blurring and echoes. In addition, we will use FIR filters to study the convolution
operation and properties such as linearity and time-invariance.
In the experiments of this lab, you will use firfilt( ), or conv(), to implement 1-D filters and
conv2() to implement two-dimensional (2-D) filters. The 2-D filtering operation actually consists of 1-D
filters applied to all the rows of the image and then all the columns.
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, consider
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 = 31 ,
b1 = 31 , and b2 = 13 . t
h
k
n
M ATLAB has a built-in function, filter( ), for implementing the operation in (1), but we have
CD-ROM
also supplied another M-file firfilt( ) for the special case of FIR filtering. The function filter
SP First
implements a wider class of filters than just the FIR case. Technically speaking, the firfilt function toolbox
firfilt.m
implements the operation called convolution. The following M ATLAB statements implement the three-point
averaging system of (2):
nn = 0:99; %<--Time indices
xx = cos( 0.08*pi*nn ); %<--Input signal
bb = [1/3 1/3 1/3]; %<--Filter coefficients
yy = firfilt(bb, xx); %<--Compute the output
In this case, the input signal xx is a vector containing a cosine function. In general, the vector bb con-
tains the filter coefficients {bk } needed in (1). These are loaded into the bb vector in the following way:
length(yy) = length(xx)+length(bb)-1
In the experiments of this lab, you will use firfilt( ) to implement FIR filters and begin to understand
how the filter coefficients define a digital filtering algorithm. In addition, this lab will introduce examples to
show how a filter reacts to different frequency components in the input.
(c) Determine the locations of the spectrum lines for the discrete-time signal, x[n], found in the middle
panels. Click the Radian button to change the axis to from fˆ to ω̂.
(d) Determine the formula for the output signal, y(t) shown in the rightmost panels. What is the output
frequency in Hz?
(a) Click on the Get x[n] button and set the input to a finite-length pulse: x[n] = (u[n] − u[n − 10]).
(b) Set the filter to a three-point averager by using the Get h[n] button to create the correct impulse
response for the three-point averager. Remember that the impulse response is identical to the bk ’s for
an FIR filter. Also, the GUI allows you to modify the length and values of the pulse.
(d) When you move the mouse pointer over the index “n” below the signal plot and do a click-hold, you
will get a hand tool that allows you to move the “n”-pointer. By moving the pointer horizontally you
can observe the sliding window action of convolution. You can even move the index beyond the limits
of the window and the plot will scroll over to align with “n.”
bb = 1/3*ones(1,3);
(c) Explain the filtering action of the 3-point averager by comparing the plots in the previous part. This
filter might be called a “smoothing” filter. Note how the transitions in x[n] from zero to one, and from
one back to zero, have been “smoothed.”
2 Warm-up
2.1 Sampling and Aliasing
Use the con2dis GUI to do the following problem:
(d) Determine the locations in ω̂ of the lines in the spectrum of the discrete-time signal. Give numerical
values.
Instructor Verification (separate page)
(e) Change the sampling frequency to 12 Hz, and explain the appearance of the output signal.
(a) Set the input signal to be x[n] = (0.9)n (u[n] − u[n − 10]). Use the “Exponential” signal type within
Get x[n].
(b) Set the impulse response to be h[n] = δ[n] − 0.9δ[n − 1]. Once again, use the “Exponential” signal
type within Get h[n].
(c) Illustrate the output signal y[n] and explain why it is zero for almost all points. Compute the numerical
value of the last point in y[n], i.e., the one that is negative and non-zero.
(b) To illustrate the filtering action of the 5-point averager, you must make a plot of the input signal and
output signal together. Since x1 [n] and y1 [n] are discrete-time signals, a stem plot is needed. One
way to put the plots together is to use subplot(2,1,*) to make a two-panel display:
nn = first:last;
subplot(2,1,1);
stem(nn,x1(nn))
subplot(2,1,2);
stem(nn,y1(nn),’filled’) %--Make black dots
xlabel(’Time Index (n)’)
This code assumes that the output from firfilt is called y1. Try the plot with first equal to
the beginning index of the input signal, and last chosen to be the last index of the input. In other
words, the plotting range for both signals will be equal to the length of the input signal, even though
the output signal is longer.
(c) Since the previous plot is quite crowded, it is useful to show a small part of the signals. Repeat the
previous part with first and last chosen to display 30 points from the middle of the signals.
(d) Explain the filtering action of the 5-point averager by comparing the plots from parts (b) and (c). This
filter might be called a “smoothing” filter. Note how the transitions from one level to another have
been “smoothed.” Make a sketch of what would happen with a 2-point averager.
Instructor Verification (separate page)
on the input signal x[n] defined via the M ATLAB statement: xx = 256*(rem(0:100,50)<10); In
M ATLAB you must define the vector of filter coefficients bb needed in firfilt.
(a) Plot both the input and output waveforms x[n] and w[n] on the same figure, using subplot. Make
the discrete-time signal plots with M ATLAB’s stem function, but restrict the horizontal axis to the
range 0 ≤ n ≤ 75. Explain why the output appears the way it does by figuring out (mathematically)
the effect of the filter coefficients in (3).
(b) Note that w[n] and x[n] are not the same length. Determine the length of the filtered signal w[n], and
explain how its length is related to the length of x[n] and the length of the FIR filter. (If you need a
hint refer to Section 1.2.)
can be use to undo the effects of the FIR filter in the previous section (see the block diagram in Fig. 3). It
performs restoration, but it only does this approximately. Use the following steps to show how well it works
when r = 0.9 and M = 22.
(a) Process the signal w[n] from (3) with F ILTER -2 to obtain the output signal y[n].
(b) Make stem plots of w[n] and y[n] using a time-index axis n that is the same for both signals. Put the
stem plots in the same window for comparison—using a two-panel subplot.
(c) Since the objective of the restoration filter is to produce a y[n] that is almost identical to x[n], make a
plot of the error (difference) between x[n] and y[n] over the range 0 ≤ n < 50.
(b) What does the error plot and worst case error tell you about the quality of the restoration of x[n]?
How small do you think the worst case error has to be so that it cannot be seen on a plot?
(b) Describe the filter coefficients of this FIR filter, and determine its length.
(c) Implement the echo filter in (4) with the values of r and P determined in part (a). Use the speech
signal in the vector x2 found in the file labdat.mat. Listen to the result to verify that you have
produced an audible echo.
(d) (Optional) Implement an echo filter and apply it to your synthesized music from Lab #4. You will have
to change the calculation of P if you used fs = 11025 Hz. Reduce the echo time (from 0.2 secs. down
to zero) and try to determine the shortest echo time that can be perceived by human hearing.
Figure 3: Cascading two FIR filters: the second filter attempts to “deconvolve” the distortion introduced by
the first.
(b) Work out the impulse response h[n] of the cascaded system by hand to verify that your M ATLAB
result in part (a) is correct. (Hint: consult old Homework problems.)
(c) In a deconvolution application, the second system (FIR F ILTER -2) tries to undo the convolutional ef-
fect of the first. Perfect deconvolution would require that the cascade combination of the two systems
be equivalent to the identity system: y[n] = x[n]. If the impulse responses of the two systems are
h1 [n] and h2 [n], state the condition on h1 [n] ∗ h2 [n] to achieve perfect deconvolution.1
1
Note: the cascade of FIR F ILTER -1 and F ILTER -2 does not perform perfect deconvolution.
(b) Pick q = 0.9 in F ILTER -1 and filter the image echart in both directions: apply F ILTER -1 along the
horizontal direction and then filter the resulting image along the vertical direction also with F ILTER -1.
Call the result ech90.
(c) Deconvolve ech90 with FIR F ILTER -2, choosing M = 22 and r = 0.9. Describe the visual ap-
pearance of the output, and explain its features by invoking your mathematical understanding of the
cascade filtering process. Explain why you see “ghosts” in the output image, and use some previous
calculations to determine how big the ghosts (or echoes) are, and where they are located. Evaluate the
worst-case error in order to say how big the ghosts are relative to “black-white” transitions which are
0 to 255.
Include all images and plots for the previous two parts to support your discussions in the lab report.
(c) Reverberation requires multiple echoes. This can be accomplished by cascading several systems of
the form (5). Use the parameters determined in part (a), and derive (by hand) the impulse response
of a reverb system produced by cascading four “single echo” systems. Refer back to section 3.2 on
cascading filters. Recall that two filters are said to be “in cascade” if the output of the first filter is used
as the input to the second filter, and the output of the second filter is defined to be the output of the
overall cascade system. This can be repeated for as many filters as are needed in the cascade system.
(d) Filter the music signal with filter defined in part (c). Describe the sound that you hear and use the
impulse response to explain why it sounds that way.
t
h
k
n
(e) It will be difficult to make plots to show the echo and reverberation, but you should be able to do
CD-ROM
it with the M-file inout( ) which can plot two very long signals together on the same plot. It
inout.m
formats the plot so that the input signal occupies the first, third, and fifth lines, etc. while the output
signal is on the second, fourth, and sixth lines etc. Type help inout to find out more.
You should plot about 0.5 sec of the original and each processed music signal to show the delay effects
that are producing the echo(es). Pick a segment containing only a few notes so that you can see the
delayed signals. Label the plots to point out the differences between the original and the echoed/reverb
signals. Note: it will be tricky to illustrate the effect that you want to explain, but you have to find a
way to see the delayed versions of the original.
Part 2.1: Demonstrate that you can run the con2dis GUI. Calculate the locations of the spectrum lines for
the discrete-time signal. Write the values of ω̂ below.
Verified: Date/Time:
Part 2.2: Demonstrate that you can run the dconvdemo GUI. Explain why the output is zero for most
points.
Verified: Date/Time:
Part 2.5(a),(b) Process the input image echart with a 2-D filter that filters in both the horizontal and ver-
tical directions with a first difference filter. Explain how the filter changes the “image signal.”
Verified: Date/Time: