Vous êtes sur la page 1sur 5

EECE359: Matlab Exercise 1

EECE 359
MATLAB Exercise 1
1 Introduction
This assignment will provide you with a tutorial introduction to Matlab and some of its more
useful features. All the commands given in this exercise should be typed in the Matlab
Command Window (indicated by the >> ), and are given in this monospaced font.
The Matlab built-in help command provides a lot of useful information. For example,
to find out how to use the Matlab plot command, just type help plot in the Matlab
Command Window.

2 Discrete-time Variables
2.1

Creating and Plotting Variables

MATLAB has built-in support for vectors (arrays) and matrices. All indices start from 1,
i.e. y(1) is the first element of the vector y. You will find it benefical to create a vector
to represent the indices of your signal as well as a vector containing the signal itself. For
example, given the discrete-time signal
x[n] =

2n, 3 n 3
0, otherwise

you could use the following MATLAB commands to create vectors representing the indices
and signal
>> n = [-3:3];
>> x = 2*n;
Note that the : is a Matlab shortcut for all the integers between, i.e. in this case we
obtain {3, 2, 1, 0, 1, 2, 3}. The ; at the end of each line tells Matlab to be quiet. If you
leave off the semicolon, Matlab will echo to the screen what it is doing, for example
>> n = [-3:3]
n =
-3 -2 -1

Now, try plotting this signal using the stem command to plot discrete-time sequences:
>> stem(n,x)
A window will appear showing the signal for n = [3, 2, . . . , 2, 3]. If you would like to see
the signal in a larger range, try the following

EECE359: Matlab Exercise 1

>> x = [zeros(1,3) n zeros(1,3)];


>> n2 = [-6:6];
>> stem(n2,x)
The command zeros(N,M) creates an NxM matrix of zeros in this case a vector [0, 0, 0].

2.2

Signal Energy

We know (see Lecture Notes 1.1.2) that the energy of a discrete-time signal is given by
E =

n=

|x[n]|2

We can easily calculate the energy of signals in Matlab using a combination of the sum,
abs, and ^ commands, as follows
>> n = [-3:3];
>> x = 2*n;
>> energy = sum(abs(x).^2)
energy =
112
A few notes are in order here. First, the command .^ tells Matlab to apply the exponentiation operation (the ^) to each element of the vector (or matrix) individually this is
referred to as element-by-element exponentiation. The command abs works element-byelement as well, e.g.
>> abs([-2 -3 5 -1 9])
ans =
2
3
5
1

while, on the other hand, the sum operation returns the total of the elements in a vector,
e.g.
>> sum([1:10])
ans =
55
Consult the Matlab help for the details of the sum command when dealing with matrices.

2.3

Periodic Signals

We can easily create periodic signals in Matlab. For example, to create the periodic signal
on Page 9 of the Lecture Notes, we can do

EECE359: Matlab Exercise 1

>>
>>
>>
>>
>>

N = 5;
x = [1 1 -1 -1];
xr = repmat(x,1,N);
n = 0:length(xr)-1;
stem(n,xr);

Note that the repmat command creates copies of a matrix (or vector), and the length
command returns the number of elements in the vector. Note also that we could have
equivalently used the command
>> xr = [x x x x x];
but this becomes cumbersome as the number of periods gets large.

3 Continuous-time Variables
3.1

Creating and Plotting

Matlab does not have support for true continuous-time functions e.g. sin(t). However,
we can easily create such functions in an approximate way by choosing discrete-time
functions with a very small step size. For example
>> t = 0:0.01:10;
>> y = sin(t);
creates an approximation of a continuous-time function. Note the use of the : operator
with three arguments this creates a vector t = {0.00, 0.01, 0.02, . . . , 4.98, 4.99, 5.00}.
We will refer to functions of this sort as continuous from now on even though, strictly
speaking, they are discrete.
To plot a continuous function, use the plot command, e.g.
>> t = 0:0.01:10;
>> y = sin(t);
>> plot(t,y)
Try modifying the example above to calculate and plot:
sin(2t)
sin(5t /4)
cos(3t + /2)
(hint: look at the help for cos and pi).

EECE359: Matlab Exercise 1

4 Complex Exponentials
Matlab fully supports complex numbers. Both i and j refer to the imaginary unit
the exercises below, we will create and plot some complex signals.

4.1

1. In

Creating Complex Signals

Creating complex signals in Matlab follows the same procedure as creating real signals.
For example, if we want to create the complex exponential x1 (t) = 2ej(t+/4) u(t) from
Problem 1.6.a of the textbook, and recalling that u(t 0) = 1, we can do
>> t = 0:0.01:10;
>> x1 = 2*exp(j*(t+pi/4));
Now, we know from 1.2.1 of the lecture notes that Re{x1 (t)} = 2 cos(t + /4) and that
Im{x1 (t)} = 2 sin(t + /4), which we can verify by
>>
>>
>>
>>
>>

t = 0:0.01:10;
x1 = 2*exp(j*(t+pi/4));
xc = real(x1);
xs = imag(x1);
plot(t,xc,r,t,2*cos(t+pi/4),d);

The last command plots Re{x1 (t)} as a solid red line (specified by r), and 2 cos(t + /4)
as a series of diamond characters (, specified by d). As we expect, the two curves are
identical and thus totally overlap each other. Try comparing Im{x1 (t)} and 2 sin(t + /4)
yourself. Also have a look at help plot to see what other line colours, styles, and symbols
are available.

4.2

Plotting Complex Signals

As you might expect, plotting complex signals is slightly more complicated than plotting
real-valued signals, since we require 3 axes to represent the real, imaginary, and time
components of the signal. Fortunately, Matlab has built-in support for 3-D plotting using
the plot3 command. For example,
>> t = 0:0.01:10;
>> x1 = 2*exp(j*(t+pi/4));
>> plot3(t,real(x1),imag(x1));
produces a 3-D plot of the spiralling function x1 (t). Try using the zoom and rotate tools in
the toolbar of the plot window in order to look at the plot from different angles.
Now try plotting the exponentially-decaying complex function x2 (t) = 2e0.25t ej(t+/4) u(t)
as shown below

EECE359: Matlab Exercise 1

>> t = 0:0.01:10;
>> x2 = 2*exp(-0.25*t).*exp(j*(t+pi/4));
>> plot3(t,real(x2),imag(x2));
Again, the .* operation represents element-by-element multiplication. If you leave out the
., Matlab will try do do a matrix multiplication and will give you an error due to a dimension
mismatch.
Try creating and plotting the following complex signals (from Problem 1.9 in your textbook)
x1 (t) = jej10t
x2 (t) = e(1+j)t
x3 [n] = ej7n
x4 [n] = 3ej3(n+1/2)/5
x5 [n] = 3ej3/5(n+1/2)
(remember to choose a suitable values for t and n in order to see several periods of the
function).

Vous aimerez peut-être aussi