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 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.