You are on page 1of 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 lters. Part 1 focuses on the circular convolution property of the DFT. You will write and test Matlab functions that implement the circular ip, circular shift, and circular convolution operations. In addition you will examine the relationship between linear convolution and circular convolution. Part 2 considers the problem of using the DFT to implement linear, time-invariant ltering operations on long signals. Specically, 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 gure numbers, e.g., Figure 1 is a plot of the signal x[n]. cite the page number they are on, e.g., The gure 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.

CIRCULAR CONVOLUTION

The following exercises focus on the circular convolution property of the DFT. An N point circular convolution is dened as follows
N 1

y[n] = x[n]

h[n] =
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 ipping, circular shifting, and then use those functions to implement circular convolution.

1.1

Circular Flip

The circular ipping operation is dened as x[n mod N ] = x[N n] for n = 0, 1, . . . , N 1

(a) Write a Matlab function called cflip to implement the circular ipping operation. (b) Dene several test signals and use them to verify that your function is working. Include plots of a few (two or three) test cases in your writeup, i.e., plots of both the original signal and the ipped signal. You will probably want to use the subplot command to get these plots on the same page.

1.2

Circular Shift

The circular shifting operation is dened as x[(n m) mod N ] (a) Write a function Matlab called cshift to implement the circular shifting operation. Hint: First write a short function that computes n mod N . Matlabs rem function will be useful for this task, but is not sufcient by itself because it will not handle the case where n is negative. However, a simple modication that uses two calls to rem will guarantee an answer in the range [0, N 1]. Take advantage of the fact that (n + N ) mod N = n mod N . Once you have code to compute n mod N , writing the rest of the cshift function should be easy. (b) Dene several test signals and use them to verify that your function is working. Include plots of two or three of these test cases as documentation in your writeup. You should verify that your function handles the case where m is negative and also the case where m is greater than N . (c) We know that a circular shift corresponds to multiplying the DFT by complex exponential. Verify the results of your test cases by Taking the DFT of the test signal: x[n] X[k]
mk Multiplying by a complex exponential: Xshift [k] = WN 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) in the time domain. In this exercise you will develop a Matlab function, called cconv, that implements both methods. The Matlab function will require four inputs: the signal vectors x[n] and h[n], the length of the circular convolution, N , and a variable that indicates which method to use. The function returns a single output y[n]. Include a comments section at the beginning of the m-le that is similar to the following: %% CCONV Computes N-point circular convolution of two vectors. The %% user specifies whether to use a time or frequency-domain %% implementation. %% %% Usage:

%% %% %% %% %% %% %% %% %% %% %% %% %% %%

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 Author Date

: : :

cconv The DSP Wizard/Wizardess September 26, 2001

(a) The frequency-domain implementation uses the fact that the circular convolution of x[n] and h[n] is equivalent to the multiplication of their DFTs, i.e., X[k] H[k]. Use this idea to implement the frequency-domain method in cconv. This should require three calls to Matlabs fft function. Try (and plot!) the following simple tests for N = 16 and N = 21. (i) An impulse at n = a convolved with an impulse at n = b, where a and b are integers. In this case the output has only one nonzero value, so determine its location. Try with different values of a and b. (ii) Convolve two short pulses. Let x[n] be a pulse of length 5 and h[n] a pulse of length 8, starting at n = 4. Verify that your function computes the correct output. (iii) Two long pulses such that the output wraps around. Let x[n] be a pulse of length 11 and h[n] a pulse of length 7, starting at n = 5. Compute the output and check its correctness versus a hand 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 your cconv function, i.e., implement This can be done with for loops, but good Matlab programming style replaces for loops with vector operations whenever possible. Since each output point in the circular convolution is an inner product of x with a circularly-ipped and shifted version of h, only one for loop is needed. Write a function called cconv2 based on this idea. Test your function using some of the examples 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 not always the same. Some values may be correct, while others are corrupted because of time aliasing. In block 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 12 0 elsewhere

h[n] =

1 0 n 11 0 elsewhere

Let the length of the circular convolution be N = 21. Determine which values of the circular convolution are the same as the linear convolution result. Plot the result and indicate the output indices where the values are bad. (b) Suppose the two signals are dened as x[n] = (0.9)n 0 n 12 0 elsewhere 1 9 n 20 0 elsewhere

h[n] =

Plot the results and indicate where the good and bad values are now that h[n] has leading zeroes. (c) Consider the following example, which relates to the overlap-save situation. x[n] = 1 0 n 16 0 elsewhere

h[n] =

sin(n/13) 0 n 99 0 elsewhere

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

BLOCK CONVOLUTION

While short signals provide useful illustrations of convolution, they are not typical of practical ltering problems. In many applications the input x[n] is very long with respect to the lter impulse response h[n]. It is often impractical to process the entire input signal at once. An alternative solution is to break the input into blocks, lter each block separately and then combine the blocks appropriately to obtain the output. There are two basic strategies for block processing, which are known as overlapadd and overlap-save, respectively. Matlabs fftfilt program uses the overlap-add approach. In the following exercises you will develop your own Matlab implementations of overlap-add and overlap-save block 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 is L and the length of the impulse response is P , then an N = L + P 1 point DFT is used to implement the lter for each section. (The L + P 1-point DFT avoids problems with time-aliasing inherent in the circular convolution.) The result for each block overlaps with the following 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 function should be the input signal vector x, the lter vector h, and the DFT size. Use the following as a header for the m-le:

%% 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 Author Date : : : oladd The DSP Wizard/Wizardess September 26, 2001

(b) Test your code using the signal and lter dened in the le projIdata.mat, available on the course website. You can verify the results by comparing them to the result of convolving x and h using the built-in conv function. Make sure that your function works for any reasonable DFT length (i.e., longer than the lter).

2.2

Overlap-Save Implementation

The overlap-save method uses a different strategy to break up the input signal. If the length of the circular convolution (i.e., the DFT length) is chosen to be N = L + P 1, overlap-save uses input segments of length N . The starting location of each input segment is skipped by an amount L, so there is an overlap of P 1 points with the previous section. Thus this method could also be called the overlapped inputs method. The L good points resulting from the N -point circular convolution of each section with the lter are retained. No additions are needed to create the output. (a) Write a Matlab function called olsave to implement overlap-save block convolution. The inputs to this function should be the input signal vector x, the lter vector h, and the DFT size. Use a header for your function similar to the one for oladd. (b) Test your code as you did the oladd function, i.e., using the data in projIdata.mat. Make sure that your function works for any reasonable DFT-length (i.e., longer than the lter itself).

You might also like