EECE 359 MATLAB Exercise 2 1 Introduction 2 Discrete-time ...
EECE 359 MATLAB Exercise 2 1 Introduction 2 Discrete-time ...
EECE 359 MATLAB Exercise 2 1 Introduction 2 Discrete-time ...
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
<strong>EECE</strong><strong>359</strong>: Matlab <strong>Exercise</strong> 2 1<br />
1 <strong>Introduction</strong><br />
<strong>EECE</strong> <strong>359</strong><br />
<strong>MATLAB</strong> <strong>Exercise</strong> 2<br />
This assignment gives Matlab examples for the material covered in Chapter 2 of the Lecture<br />
Notes. Commands indicated by the >> should be typed in the Matlab Command<br />
Window. The Matlab built-in help command provides a lot of useful information. For example,<br />
to find out how to use the Matlabconv command, just typehelp conv in the Matlab<br />
Command Window.<br />
This exercise builds on the previous Matlab exercise, so please have a look at it if you<br />
have not already done so.<br />
2 <strong>Discrete</strong>-<strong>time</strong> Convolution<br />
Matlab has built-in support for discrete-<strong>time</strong> convolution using the conv function. Before<br />
trying the exercises below, consult the Matlab help, i.e. try running help conv. It turns out<br />
that conv can be used for other things besides discrete-<strong>time</strong> convolution. What is its other<br />
use?<br />
2.1 Convolution of two impulses<br />
In this section, we will examine the result of a convolution of two impulses, e.g. y[n] =<br />
δ[n]∗δ[n − 1]. First, create the variables (we will create them on the <strong>time</strong> axis n = [0, 1, 2]):<br />
>> x1 = [1 0 0];<br />
>> x2 = [0 1 0];<br />
Now, we can convolve them, and create the proper <strong>time</strong> vector for the output<br />
>> y = conv(x1,x2);<br />
>> t = 0:length(y)-1;<br />
And, to see the result graphically, let’s plot the output:<br />
>> stem(t,y);<br />
What can you observe about the output of this system? Try changing the <strong>time</strong>s the two<br />
impulses occur. What happens to the output of the system? Try changing the order of<br />
convolution, i.e. try conv(x2,x1). Does this change the output?
<strong>EECE</strong><strong>359</strong>: Matlab <strong>Exercise</strong> 2 2<br />
2.2 Convolution of an impulse and a square wave<br />
In this section, we will examine the result of a convolution y[n] = δ[n − 1]∗xs[n], were<br />
xs[n] =<br />
Enter the following code to create this signals<br />
>> x = [0 1 0 0 0 0];<br />
>> xs = [1 1 1 1 1 1];<br />
<br />
1, 0 ≤ n ≤ 5<br />
0, otherwise<br />
Now, convolve the signals and create a <strong>time</strong> vector:<br />
>> y = conv(x,xs);<br />
>> t = 0:length(y)-1;<br />
Try plotting the output of the system using stem. What can you observe about the output?<br />
Try shifting the <strong>time</strong> square wave and/or the impulse. How does this change the<br />
convolution output?<br />
2.3 Convolution of two square waves<br />
In this section, we will try convolving two square waves together. We use again the<br />
function xs[n] defined in Equation 1, and look at the convolution y[n] = xs[n]∗xs[n].<br />
Create xs as we did above. Now, we can perform the convolution:<br />
>> y = conv(xs,xs);<br />
>> t = 0:length(y)-1;<br />
Plot the output using thestem function as we did before. What can you observe about the<br />
convolution of two square waves?<br />
3 Animated <strong>Discrete</strong>-Time Convolution<br />
In this section, you will learn to create Matlab script files (called M-files), and you will write<br />
a Matlab script which shows an animation of the convolution process. The signals we are<br />
convolving are from page 36 of the Lecture Notes.<br />
3.1 Creating an M-file<br />
Create a new M-file (“File” → “New” → “M-File”), and enter the following code into it (the<br />
line numbers at the left margin are only for reference, and should not be entered in the<br />
file):<br />
(1)
<strong>EECE</strong><strong>359</strong>: Matlab <strong>Exercise</strong> 2 3<br />
1 clear all; close all;<br />
2<br />
3 % Example from p. 36 of Lecture Notes<br />
4 t = [-4 -3 -2 -1 0 1 2 3 4];<br />
5 x = [ 0 0 0 0 1 2 3 0 0];<br />
6 h = [ 0 0 0 0 2 1 0 0 0];<br />
7 y = [ 0 0 0 0 0 0 0 0 0];<br />
8<br />
9 yc = 1;<br />
10<br />
11 for n=min(t):max(t),<br />
12 pause(3);<br />
13<br />
14 % flip h<br />
15 ht = fliplr(h);<br />
16<br />
17 if n
<strong>EECE</strong><strong>359</strong>: Matlab <strong>Exercise</strong> 2 4<br />
3.2 Running the M-file<br />
Use the “Current Directory” window in your Matlab session to change the directory to the<br />
one in which you saved your “animate.m” file.<br />
Now, typeanimate into your Matlab Command Window. This will execute the code you<br />
created in the file “animate.m”. Assuming you copied the code from Section 3.1 properly,<br />
you should see a display window appear with two plots, and demonstrate graphically the<br />
process of convolution.<br />
3.3 Explanation of the Code<br />
Below you will find an explanation of some new Matlab concepts used in the M-file given<br />
in Section 3.1.<br />
• Line 1: clear all removes all previously created variables, and close all closes<br />
all open plot windows.<br />
• Line 11–40: this is a Matlab for loop, which ends with end. The value of n takes on<br />
all integer values from min(t) to max(t). The Matlab functions min and max return<br />
the minimum and maximum values in a vector, respectively.<br />
• Line 12: pause(3) causes Matlab to stop and wait for 3 seconds.<br />
• Line 15: fliplr reverses the contents of a vector → v old= [v1, v2, . . .,vn] to be → v new=<br />
[vn, vn−1, . . .,v1]<br />
• Lines 17–23: this is a Matlab if,then,else structure.<br />
• Lines 19,22: we create a new (temporary) vector by adding zeros at the left or the<br />
right (as required)<br />
• Line 25: this is the actual convolution sum. Note that we are doing the convolution<br />
one step at a <strong>time</strong>, so we can’t use the Matlab conv function.<br />
• Lines 28, 37: subplot creates two plots inside one plotting window (see the Matlab<br />
help for more details)<br />
• Lines 30,32: hold allows you to “plot-over” an already-existing plot (this is how we<br />
create the overlay of two different functions in the upper plot)<br />
• Lines 33,34,35,39: xlabel, legend, title are various Matlab commands to add text<br />
to your plot. See the help for more details.