You are on page 1of 4

EECE359: Matlab Exercise 2

EECE 359 MATLAB Exercise 2 1 Introduction


This assignment gives Matlab examples for the material covered in Chapter 2 of the Lecture Notes. Commands indicated by the >> should be typed in the Matlab Command Window. The Matlab built-in help command provides a lot of useful information. For example, to nd out how to use the Matlab conv command, just type help conv in the Matlab Command Window. This exercise builds on the previous Matlab exercise, so please have a look at it if you have not already done so.

2 Discrete-time Convolution
Matlab has built-in support for discrete-time convolution using the conv function. Before trying the exercises below, consult the Matlab help, i.e. try running help conv. It turns out that conv can be used for other things besides discrete-time convolution. What is its other use?

2.1

Convolution of two impulses

In this section, we will examine the result of a convolution of two impulses, e.g. y [n] = [n] [n 1]. First, create the variables (we will create them on the time axis n = [0, 1, 2]): >> x1 = [1 0 0]; >> x2 = [0 1 0]; Now, we can convolve them, and create the proper time vector for the output >> y = conv(x1,x2); >> t = 0:length(y)-1; And, to see the result graphically, lets plot the output: >> stem(t,y); What can you observe about the output of this system? Try changing the times the two impulses occur. What happens to the output of the system? Try changing the order of convolution, i.e. try conv(x2,x1). Does this change the output?

EECE359: Matlab Exercise 2

2.2

Convolution of an impulse and a square wave

In this section, we will examine the result of a convolution y [n] = [n 1]xs [n], were xs [n] = 1, 0 n 5 0, otherwise (1)

Enter the following code to create this signals >> x = [0 1 0 0 0 0]; >> xs = [1 1 1 1 1 1]; Now, convolve the signals and create a time vector: >> y = conv(x,xs); >> t = 0:length(y)-1; Try plotting the output of the system using stem. What can you observe about the output? Try shifting the time square wave and/or the impulse. How does this change the convolution output?

2.3

Convolution of two square waves

In this section, we will try convolving two square waves together. We use again the function xs [n] dened in Equation 1, and look at the convolution y [n] = xs [n]xs [n]. Create xs as we did above. Now, we can perform the convolution: >> y = conv(xs,xs); >> t = 0:length(y)-1; Plot the output using the stem function as we did before. What can you observe about the convolution of two square waves?

3 Animated Discrete-Time Convolution


In this section, you will learn to create Matlab script les (called M-les), and you will write a Matlab script which shows an animation of the convolution process. The signals we are convolving are from page 36 of the Lecture Notes.

3.1

Creating an M-le

Create a new M-le (File New M-File), and enter the following code into it (the line numbers at the left margin are only for reference, and should not be entered in the le):

EECE359: Matlab Exercise 2


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

clear all; close all; % t x h y Example from p. 36 = [-4 -3 -2 -1 0 1 = [ 0 0 0 0 1 2 = [ 0 0 0 0 2 1 = [ 0 0 0 0 0 0 of Lecture Notes 2 3 4]; 3 0 0]; 0 0 0]; 0 0 0];

yc = 1; for n=min(t):max(t), pause(3); % flip h ht = fliplr(h); if n<0, % shift to the left ht = [ht(-n+1:length(h)) zeros(1,-n)]; else % shift to the right ht = [zeros(1,n) ht(1:length(h)-n)]; end y(yc) = sum(x.*ht); yc = yc + 1; subplot(2,1,1); stem(t,x); hold on; stem(t,ht,filled,r); hold off; xlabel(t); legend(x[n],h[n-k],0); title([n= num2str(n)]); subplot(2,1,2); stem(t,y); xlabel(t); ylabel(y[n]); end

Once you have veried that you entered the le contents properly, save this le as animate.m (remember the directory where you saved it).

EECE359: Matlab Exercise 2

3.2

Running the M-le

Use the Current Directory window in your Matlab session to change the directory to the one in which you saved your animate.m le. Now, type animate into your Matlab Command Window. This will execute the code you created in the le animate.m. Assuming you copied the code from Section 3.1 properly, you should see a display window appear with two plots, and demonstrate graphically the process of convolution.

3.3

Explanation of the Code

Below you will nd an explanation of some new Matlab concepts used in the M-le given in Section 3.1. Line 1: clear all removes all previously created variables, and close all closes all open plot windows. Line 1140: this is a Matlab for loop, which ends with end. The value of n takes on all integer values from min(t) to max(t). The Matlab functions min and max return the minimum and maximum values in a vector, respectively. Line 12: pause(3) causes Matlab to stop and wait for 3 seconds. Line 15: fliplr reverses the contents of a vector v old = [v1 , v2 , . . . , vn ] to be v new = [vn , vn1 , . . . , v1 ] Lines 1723: this is a Matlab if,then,else structure. Lines 19,22: we create a new (temporary) vector by adding zeros at the left or the right (as required) Line 25: this is the actual convolution sum. Note that we are doing the convolution one step at a time, so we cant use the Matlab conv function. Lines 28, 37: subplot creates two plots inside one plotting window (see the Matlab help for more details) Lines 30,32: hold allows you to plot-over an already-existing plot (this is how we create the overlay of two different functions in the upper plot) Lines 33,34,35,39: xlabel, legend, title are various Matlab commands to add text to your plot. See the help for more details.

You might also like