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 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
N
X −1
y[n] = x[n]
N h[n] = x[m]h[(n − m)mod N ]. (1)
m=0

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


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

(b) Define 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 flipped
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 defined 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 . Matlab’s rem function will be useful for
this task, but is not sufficient by itself because it will not handle the case where n is negative.
However, a simple modification 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) Define 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]


• Multiplying by a complex exponential: Xshift [k] = WN−mk 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-file 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 : cconv
%% Author : The DSP Wizard/Wizardess
%% Date : 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 DFT’s, i.e., X[k] · H[k]. Use this idea to implement the
frequency-domain method in cconv. This should require three calls to Matlab’s 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., imple-
ment 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-flipped 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:
(
(0.9)n 0 ≤ n ≤ 12
x[n] =
0 elsewhere
(
1 0 ≤ n ≤ 11
h[n] =
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 defined as
(
(0.9)n 0 ≤ n ≤ 12
x[n] =
0 elsewhere
(
1 9 ≤ n ≤ 20
h[n] =
0 elsewhere
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.
(
1 0 ≤ n ≤ 16
x[n] =
0 elsewhere
(
sin(nπ/13) 0 ≤ n ≤ 99
h[n] =
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.

2 BLOCK CONVOLUTION
While short signals provide useful illustrations of convolution, they are not typical of practical filtering
problems. In many applications the input x[n] is very long with respect to the filter 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, filter 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 overlap-
add and overlap-save, respectively. Matlab’s 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 filter 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 filter vector h, and the DFT size. Use the following as a
header for the m-file:
%% 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 file 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 filter).

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 filter 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 filter 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 filter itself).

You might also like