Signal Processing First 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 MATLAB, 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. 1.1 Two GUIS This lab involves on the use of two MATLAB GUIs: one for sampling and aliasing and one for convolution. 1. con2dis: GUI for sampling and aliasing. An input sinusoid and its spectrum is tracked through A/D and D/A converters. 2. dconvdemo: GUI for discrete-time convolution. This is exactly the same as the MATLAB functions conv() and firfilt ( ) used to implement FIR filters. 1.2 Overview of Filtering For this lab, we will define an FIR filter as a discrete-time system that converts an input signal x[n] into an output signal y[n] by means of the weighted summation: M CD-ROM Chap 4 demos con2dis CD-ROM Chap 5 demos dconvdemo y[n] = [ bk x[n = k] - k=0 (1) 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 McClellan, Schafer, and Yoder, Signal Processing First, ISBN 0-13-065562-7. Prentice Hall, Upper Saddle River, NJ 07458. 2003 Pearson Education, Inc. 1 the system for which the output values are given by u[n] = - \}x[n] + ¹³x[n − 1] + ¾½³x[n − 2] (2) = }} {x[n] + x[n − 1] + x[n − 2]} This equation states that the nt 2th 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 b₁'s are bo b₁ = ½³, and b₂ = ½³. - - CD-ROM MATLAB has a built-in function, filter( ), for implementing the operation in (1), but we have also supplied another M-file firfilt( ) for the special case of FIR filtering. The function filter implements a wider class of filters than just the FIR case. Technically speaking, the firfilt function toolbox implements the operation called convolution. The following MATLAB statements implement the three-point averaging system of (2): nn = 0:99; XX = cos( 0.08*pi*nn ); bb = [1/3 1/3 1/3]; уу = firfilt (bb, xx); %<--Time indices %<--Input signal %<--Filter coefficients %<--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 {b} needed in (1). These are loaded into the bb vector in the following way: bb = [b0, b1, b2, ... , bм]. In MATLAB, all sequences have finite length because they are stored in vectors. If the input signal has, for example, L samples, we would normally only store the L samples in a vector, and would assume that x[n] = 0 for n outside the interval of L samples; i.e., we do not have to store any zero samples unless it suits our purposes. If we process a finite-length signal through (1), then the output sequence y[n] will be longer than x[n] by M samples. Whenever firfilt( ) implements (1), we will find that 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. 1.3 Pre-Lab: Run the GUIS The first objective of this lab is to demonstrate usage of the two GUIs. First of all, you must download the ZIP files for each and install them. Each one installs as a directory containing a number of files. You can put the GUIs on the matlabpath, or you can run the GUIs from their home directories. 1.4 Sampling and Aliasing Demo In this demo, you can change the frequency of an input signal that is a sinusoid, and you can change the sampling frequency. The GUI will show the sampled signal, x[n], it spectrum and also the reconstructed output signal, y(t) with its spectrum. Figure 1 shows the interface for the con2dis GUI. In order to see the entire GUI, you must select Show All Plots under the Plot Options menu. In the pre-Lab, you should perform the following steps with the con2dis GUI: (a) Set the input to x(t) = cos(40πt) SP First firfilt.m McClellan, Schafer, and Yoder, Signal Processing First, ISBN 0-13-065562-7. Prentice Hall, Upper Saddle River, NJ 07458. 2003 Pearson Education, Inc. 2 - 6x Figure No. 1 Plot Options Exit 1 0.5 -0.5 0 -1 b 0 Input: cos(2 16.1 t) 0.5 0 cos(2π 0.81 n) 0.5 0 Output: cos(2 3.9 t) www.www W 0.1 0.2 -0.5 -1 0.3 0.4 0.5 0 1 2 3 4 5 6 7 8 9 10 Time (sec) Continuous Time Spectrum Time (samples) Discrete Time Spectrum -0.5 -1 0 0.1 0.2 0.3 0.4 0.5 Time (sec) Continuous Time Spectrum 1 1 1 ALIASING! 0.8 0.8 0.8 0.6 0.4 0.2 0.6 0.4 0.2 0.6 0.4 0.2 0 0 -20 -10 0 f (Hz) 10 20 -1 -0.5 0 0.5 1 -20 -10 0 10 20 20 Rad/sec C Radian f = 10/1 f (Hz) f = 16.1 (Hz) Phase = 0.0 = 20.0 (Hz) 16.1 20 0 Figure 1: Continuous to discrete demo interface. (b) Set the sampling rate to fs = 24 samples/sec. (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 ŷ to ŵ. (d) Determine the formula for the output signal, y(t) shown in the rightmost panels. What is the output frequency in Hz? 1.5 Discrete-Time Convolution Demo In this demo, you can select an input signal x[n], as well as the impulse response of the filter h[n]. Then the demo shows the “flipping and shifting” used when a convolution is computed. This corresponds to the sliding window of the FIR filter. Figure 2 shows the interface for the dconvdemo GUI. In the pre-lab, you should perform the following steps with the dconvdemo GUI. (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. (c) Use the GUI to produce the output signal. McClellan, Schafer, and Yoder, Signal Processing First, ISBN 0-13-065562-7. Prentice Hall, Upper Saddle River, NJ 07458. 2003 Pearson Education, Inc. 3 Discrete Convolution Demo v2.03 Plot Options Help 1 Signal 0.8 Flipped Signal 0.6- 0.5 0 Input 1 -x Impulse Response 0.4 0.2 n 0 0 0 20 40 0 5 10 0 ↑ n = 4 0.5 Multiplication 0.4 Get x[n] Get h[n] Flip x[n] Flip h[n] 0.3- 0.25 Signal Axis: 0.1- o = x[k] 0 ↓ n = 4 2.5 Linear Convolution 2- 1.5 1- 0.5 o = h[n-k] Multiplication Axis: x[k]h[n-k] Convolution Axis: y[n] = x[k]h[n-k] Close Help 0 -25 -20 -15 -10 -5 0 5 10 15 20 25 Figure 2: Interface for discrete-time convolution GUI. (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.” 1.6 Filtering via Convolution You can perform the same convolution as done by the dconvdemo GUI by using the MATLAB function firfilt, or conv. The preferred function is firfilt. (a) For the Pre-Lab, you should do the filtering with a 3-point averager. The filter coefficient vector for the 3-point averager is defined via: bb = 1/3*ones (1,3); Use firfilt to process an input signal that it a length-10 pulse: 1 for n = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 = x[n] = { 0 elsewhere xn={ NOTE: in MATLAB indexing can be confusing. Our mathematical signal definitions start at n = 0, but MATLAB starts its indexing at “1”. Nevertheless, we can ignore the difference and pretend that MAT- LAB is indexing from zero, as long as we don't try to write x[0] in MATLAB. Thus we can generate the length-10 pulse and put it inside of a longer vector via xx [ones (1,10), zeros(1,5)]. = McClellan, Schafer, and Yoder, Signal Processing First, ISBN 0-13-065562-7. Prentice Hall, Upper Saddle River, NJ 07458. 2003 Pearson Education, Inc. (b) To illustrate the filtering action of the 3-point averager, it is informative to make a plot of the input signal and output signals together. Since x[n] and y[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; %--- use first=1 and last=length(xx) subplot(2,1,1); stem (nn, xx(nn)) subplot (2,1,2); stem(nn, yy(nn), 'filled') xlabel('Time Index (n)') --Make black dots This code assumes that the output from firfilt is called yy. 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 (which was "padded" with five extra zero samples). (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: (a) Input frequency is 12 Hz. (b) Sampling frequency is 15 Hz. (c) Determine the frequency of the reconstructed output signal (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. 2.2 Discrete-Time Convolution In this section, you will generate filtering results needed in a later section. Use the discrete-time convolution GUI, dconvdemo, to do the following: - - (a) Set the input signal to be x[n] = (0.9)" (u[n] — u[n — 10]). Use the “Exponential” signal type within Get x[n]. (b) Set the impulse response to be h[n] = d[n] − 0.96[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. Instructor Verification (separate page) McClellan, Schafer, and Yoder, Signal Processing First, ISBN 0-13-065562-7. Prentice Hall, Upper Saddle River, NJ 07458. 2003 Pearson Education, Inc. 5