# m2

September 13, 2017 | Author: Jorge Ríos | Category: Matlab, Convolution, Computing, Technology, Computer Programming

#### Description

EECE359: Matlab Exercise 2

1

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 find 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, let’s 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

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] defined 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 files (called M-files), 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-file

Create a new M-file (“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 file):

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

3

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