mlabI

5
ECE 410 INTRODUCTION TO SIGNAL PROCESSING Matlab Project I Fall 2001 Issued: Monday, September 24, 2001 Due: Wednesday, October 17, 2001 The purpose of this project is to develop a thorough understanding of the Discrete Fourier Transform (DFT) and how it is used to implement digital filters. Part 1 focuses on the circular convolution property of the DFT. You will write and test Matlab functions that implement the circular flip, circular shift, and circular convolution operations. In addition you will examine the relationship between linear convo- lution and circular convolution. Part 2 considers the problem of using the DFT to implement linear, time-invariant filtering operations on long signals. Specifically, you will write Matlab functions that implement block convolution using the overlap-add and overlap-save strategies. A list of guidelines for preparing the writeup of this project are given below. Failure to comply with these guidelines will result in a grade of ZERO for the project. The report must be neatly handwritten or typed, and all pages must be numbered. All plots must be neatly annotated with x-axis and y-axis labels and a title. When referring to plots in the text, you should do at least one of the following: use figure numbers, e.g., “Figure 1 is a plot of the signal x[n].” cite the page number they are on, e.g., “The figure at the top of page 4 is a plot of x[n].” All Matlab code must be well-documented and should be included in an Appendix at the end of the report. 1 CIRCULAR CONVOLUTION The following exercises focus on the circular convolution property of the DFT. An N -point circular convolution is defined as follows y[n]= x[n] N h[n]= N -1 X m=0 x[m]h[(n - m)mod N ]. (1) Combining N -point vectors according to the circular convolution rule (Equation 1) is easy to visualize with some experience, and Matlab provides the means to do the visualization. Circular convolution requires that all indexing be done in a circular (or periodic) fashion. Thus a shifting operation becomes a rotation. In the exercises below you will develop functions for circular flipping, circular shifting, and then use those functions to implement circular convolution. 1.1 Circular Flip The circular flipping operation is defined as x[-n mod N ]= x[N - n] for n =0, 1,...,N - 1

Transcript of mlabI

Page 1: mlabI

ECE 410 INTRODUCTION TO SIGNAL PROCESSINGMatlab Project I

Fall 2001

Issued: Monday, September 24, 2001 Due: Wednesday, October 17, 2001

The purpose of this project is to develop a thorough understanding of the Discrete Fourier Transform(DFT) and how it is used to implement digital filters. Part 1 focuses on the circular convolution propertyof the DFT. You will write and test Matlab functions that implement the circular flip, circular shift, andcircular convolution operations. In addition you will examine the relationship between linear convo-lution and circular convolution. Part 2 considers the problem of using the DFT to implement linear,time-invariant filtering operations on long signals. Specifically, you will write Matlab functions thatimplement block convolution using the overlap-add and overlap-save strategies.

A list of guidelines for preparing the writeup of this project are given below. Failure to comply withthese guidelines will result in a grade ofZERO for the project.

• The report must be neatly handwritten or typed, and all pages must be numbered.

• All plots must be neatly annotated with x-axis and y-axis labels and a title.

• When referring to plots in the text, you should do at least one of the following:

– use figure numbers, e.g., “Figure 1 is a plot of the signalx[n].”

– cite the page number they are on, e.g., “The figure at the top of page 4 is a plot ofx[n].”

• All Matlab code must be well-documented and should be included in an Appendix at the end ofthe report.

1 CIRCULAR CONVOLUTION

The following exercises focus on the circular convolution property of the DFT. AnN−point circularconvolution is defined as follows

y[n] = x[n]©N h[n] =N−1∑m=0

x[m]h[(n−m)modN ]. (1)

CombiningN -point vectors according to the circular convolution rule (Equation 1) is easy to visualizewith some experience, and Matlab provides the means to do the visualization. Circular convolutionrequires that all indexing be done in a circular (or periodic) fashion. Thus a shifting operation becomesa rotation. In the exercises below you will develop functions for circular flipping, circular shifting, andthen use those functions to implement circular convolution.

1.1 Circular Flip

The circular flipping operation is defined as

x[−n modN ] = x[N − n] for n = 0, 1, . . . , N − 1

Page 2: mlabI

(a) Write a Matlab function calledcflip to implement the circular flipping operation.

(b) Define several test signals and use them to verify that your function is working. Include plots of afew (two or three) test cases in your writeup,i.e.,plots of both the original signal and the flippedsignal. You will probably want to use thesubplot command to get these plots on the samepage.

1.2 Circular Shift

The circular shifting operation is defined as

x[(n−m) modN ]

(a) Write a function Matlab calledcshift to implement the circular shifting operation.Hint:First write a short function that computesn modN . Matlab’s rem function will be useful forthis task, but is not sufficient by itself because it will not handle the case wheren is negative.However, a simple modification that uses two calls torem will guarantee an answer in the range[0, N − 1]. Take advantage of the fact that(n+N) modN = n modN .

Once you have code to computen modN , writing the rest of thecshift function should beeasy.

(b) Define several test signals and use them to verify that your function is working. Include plots oftwo or three of these test cases as documentation in your writeup. You should verify that yourfunction handles the case wherem is negative and also the case wherem is greater thanN .

(c) We know that a circular shift corresponds to multiplying the DFT by complex exponential. Verifythe results of your test cases by

• Taking the DFT of the test signal:x[n] −→ X[k]

• Multiplying by a complex exponential:Xshift[k] = W−mkN X[k]

• Computing the inverse DFT:Xshift[k] −→ xshift[n]

1.3 Circular Convolution

There are two ways to write a function for circular convolution: 1) in the transform domain and 2) inthe time domain. In this exercise you will develop a Matlab function, calledcconv , that implementsboth methods. The Matlab function will require four inputs: the signal vectorsx[n] andh[n], the lengthof the circular convolution,N , and a variable that indicates which method to use. The function returnsa single outputy[n]. Include a comments section at the beginning of the m-file that is similar to thefollowing:

%% CCONV Computes N-point circular convolution of two vectors. The%% user specifies whether to use a time or frequency-domain%% implementation.%%%% Usage:

Page 3: mlabI

%% y=cconv(x,h,N,method)%%%% Variables:%% x = signal vector x[n]%% h = signal vector h[n]%% N = number of points in the circular convolution%% method = implementation of circular convolution%% ’freq’ = Frequency-domain%% ’time’ = Time-domain%% y = result: N-point circular convolution of x and h%%

%% File : cconv%% Author : The DSP Wizard/Wizardess%% Date : September 26, 2001

(a) The frequency-domain implementation uses the fact that the circular convolution ofx[n] andh[n]is equivalent to the multiplication of their DFT’s,i.e.,X[k] ·H[k]. Use this idea to implement thefrequency-domain method incconv . This should require three calls to Matlab’sfft function.Try (and plot!) the following simple tests forN = 16 andN = 21.

(i) An impulse atn = a convolved with an impulse atn = b, wherea andb are integers. Inthis case the output has only one nonzero value, so determine its location. Try with differentvalues ofa andb.

(ii) Convolve two short pulses. Letx[n] be a pulse of length 5 andh[n] a pulse of length 8,starting atn = 4. Verify that your function computes the correct output.

(iii) Two long pulses such that the output wraps around. Letx[n] be a pulse of length 11 andh[n]a pulse of length 7, starting atn = 5. Compute the output and check its correctness versus ahand calculation. Explain why the answer is different for the length-16 and length-21 cases.

(b) Implement the time-domain option for circular convolution in yourcconv function, i.e., imple-ment This can be done withfor loops, but good Matlab programming style replacesfor loopswith vector operations whenever possible. Since each output point in the circular convolution isan inner product ofx with a circularly-flipped and shifted version ofh, only onefor loop isneeded. Write a function calledcconv2 based on this idea. Test your function using some of theexamples from part (a). Include several of the test cases in your writeup.

1.4 Relationship to Linear Convolution

The comparison of circular convolution output to a linear convolution output shows that they are notalways the same. Some values may be correct, while others are corrupted because of time aliasing. Inblock convolution (overlap-save, in particular), it is often necessary to identify the good and bad points.

(a) Consider the circular convolution of the following signals:

x[n] =

{(0.9)n 0 ≤ n ≤ 120 elsewhere

Page 4: mlabI

h[n] =

{1 0 ≤ n ≤ 110 elsewhere

Let the length of the circular convolution beN = 21. Determine which values of the circularconvolution are the same as the linear convolution result. Plot the result and indicate the outputindices where the values are “bad”.

(b) Suppose the two signals are defined as

x[n] =

{(0.9)n 0 ≤ n ≤ 120 elsewhere

h[n] =

{1 9 ≤ n ≤ 200 elsewhere

Plot the results and indicate where the good and bad values are now thath[n] has leading zeroes.

(c) Consider the following example, which relates to the overlap-save situation.

x[n] =

{1 0 ≤ n ≤ 160 elsewhere

h[n] =

{sin(nπ/13) 0 ≤ n ≤ 990 elsewhere

Suppose that a 100-point circular convolution is performed. (There isno zero-padding ofh[n].)Plot the results and determine the indices of the good and bad points in the output.

2 BLOCK CONVOLUTION

While short signals provide useful illustrations of convolution, they are not typical of practical filteringproblems. In many applications the inputx[n] is very long with respect to the filter impulse responseh[n]. It is often impractical to process the entire input signal at once. An alternative solution is tobreak the input into blocks, filter each block separately and then combine the blocks appropriately toobtain the output. There are two basic strategies for block processing, which are known as overlap-add and overlap-save, respectively. Matlab’sfftfilt program uses the overlap-add approach. In thefollowing exercises you will develop your own Matlab implementations of overlap-add and overlap-saveblock convolution.

2.1 Overlap-Add Implementation

The overlap-add method works by breaking the long input signal into small non-overlapping sections.If the length of these sections isL and the length of the impulse response isP , then anN = L+ P − 1point DFT is used to implement the filter for each section. (TheL+ P − 1-point DFT avoids problemswith time-aliasing inherent in the circular convolution.) The result for each block overlaps with thefollowing block. These overlapping sections must be added together to compute the output.

(a) Write a Matlab function to implement overlap-add block convolution. The inputs to this functionshould be the input signal vectorx , the filter vectorh, and the DFT size. Use the following as aheader for the m-file:

Page 5: mlabI

%% OLADD Filtering via overlap-add block convolution.%%%% Usage:%% y=oladd(x,h,N)%%%% Variables:%% x = signal vector x[n]%% h = filter vector h[n]%% N = DFT size (choose a power of 2 for fastest results)%% y = Result: x[n]*h[n]%%

%% File : oladd%% Author : The DSP Wizard/Wizardess%% Date : September 26, 2001

(b) Test your code using the signal and filter defined in the fileprojIdata.mat , available on thecourse website. You can verify the results by comparing them to the result of convolvingx andh using the built-inconv function. Make sure that your function works for any reasonable DFTlength (i.e., longer than the filter).

2.2 Overlap-Save Implementation

The overlap-save method uses a different strategy to break up the input signal. If the length of thecircular convolution (i.e., the DFT length) is chosen to beN = L + P − 1, overlap-save uses inputsegments of lengthN . The starting location of each input segment is skipped by an amountL, so thereis an overlap ofP − 1 points with the previous section. Thus this method could also be called theoverlapped inputsmethod. TheL “good” points resulting from theN -point circular convolution ofeach section with the filter are retained. No additions are needed to create the output.

(a) Write a Matlab function calledolsave to implement overlap-save block convolution. The inputsto this function should be the input signal vectorx , the filter vectorh, and the DFT size. Use aheader for your function similar to the one foroladd .

(b) Test your code as you did theoladd function, i.e., using the data inprojIdata.mat . Makesure that your function works for any reasonable DFT-length (i.e., longer than the filter itself).