Lab 0: Introduction To MATLAB: (LABE 410) Dr. Jad Abou Chaaya
Lab 0: Introduction To MATLAB: (LABE 410) Dr. Jad Abou Chaaya
0.1 Overview
The goal of this lab is to get familiar with MATLAB and to learn how to represent, manipulate, and
analyze discrete time signals in MATLAB. We will also look at some tools and commands to generate
and simulate signals in MATLAB.
1.
2. At the prompt, type help followed by the command to get information about the command, try the
following :
>>help plot
>>help stem
>>help cos
>>help sin
>>help exp
>>help for
>>help ones
>>help zeros
3. What if you do not know a command name ? A keyword search can be done by using the lookfor
command.
>>lookfor frequency
4. Variables in MATLAB can hold numbers, vectors or matrices :
>>x = randn(1,1)
>>x = randn(1,10)
>>x = randn(10,1)
>>x = randn(10,10)
>>x = rand(10,10)-0.5
You can see what rand and randn do using the help command !
5. Putting a semicolon after a variable assignment prevents unnecessary echoing :
>>x = randn(10,10);
>>x = rand(10,10)-0.5;
6. Result of a computation can be assigned to another variable. If the result is not assigned to a
variable, MATLAB stores it in the default variable ans
>>x = 0.25*0.25;
>>x
x=
0.0625
>>0.25*0.25
ans =
0.0625
7. It is very easy to manipulate complex numbers in MATLAB. You can assign a complex number of
the form a + bi directly to a variable and perform arithmetic operations :
>>x = 2+3i
>>y = 3+4i
>>x+y
ans =
5.000 + 7.000i
Use help to see what the following functions do : conj, abs, real, imag.
8. Loops in MATLAB : A simple for loop can be written as,
>> for cnt = 1 : 10
cnt
end
The loop prints out the values of cnt. Remember you can suppress the echo using semicolon. You
can also use while loop instead of for. In general MATLAB is not very efficient with loops. We will
see later that one can avoid loops by using the semicolon operator.
1. The colon operator : The colon operator can be used to define a vector. Let us say we want to create
a vector x which holds the integers from 0-100. One way is to use a loop. Another way to do this is
shown below,
>> x = [0:100];
2. Try the following,
>> x = [-0.5:0.1:0.5];
>> x = [0.5:-0.1:-0.5];
Use help to check what linspace does.
3. Discrete-time complex exponentials form an important class of functions in the analysis of discrete-
time signals and systems. A discrete-time complex exponential has the form αn , where α is a com-
plex scalar. The discrete-time sine and cosine signals can be built from complex exponential signals
by setting α = e±iω
1 +iωn
cos(ωn) = e + e−iωn
2
1 +iωn
sin(ωn) = e − e−iωn
2j
MATLAB has functions cos, sin and exp to manipulate these signals.
4. In general, signals are represented by row or column vectors, depending on the context. All vectors
in MATLAB are indexed starting with 1. One may have to create an additional vector to properly
keep track of signal index. Consider the following example,
(
2n, −3 ≤ n ≤ 3
x[n] =
0, otherwise
6. One can also extend the range of the sequences. For instance, if you want to plot the signal over
the range -5 to 5, you can extend the index vector n and add additional elements to x,
>>n = [-5:5];
>>x = [0 0 x 0 0];
7. What if want to extend the range by a large value ? Use zeros :
>>n = [-100:100];
>>x = [zeros(1,97) x zeros(1,97)];
8. We can define x1 [n] to be the discrete-time unit impulse function and x2 [n] to be the time-advances
version of x1 [n], i.e., x1 [n] = δ[n] and x2 [n] = δ[n + 2]. We can do this by,
>>nx1 = [0:10];
>>x1 = [1 zeros(1,10)];
>>nx2 = [-5:5];
>>x2 = [zeros(1,3) 1 zeros(1,7)];
9. Let us now plot the signals,
>>stem(nx1,x1)
>>stem(nx2,x2)
10. Discrete-time sinusoids and exponents can also be generated using cos, sin, and exp,
>>n = [0:32];
>>x = exp(i*(pi/8)*n)
Note the variable x is a vector of complex values now ! Try the following commands,
>>stem(n,real(x))
>>stem(n,imag(x))
>>stem(n,abs(x))
>>stem(n,angle(x))
Note angle returns the phase in radians.
11. MATLAB also allows you to add, subtract, multiply, divide, scale and exponentiate signals. let us
define the signals x1 and x2,
>>x1 = sin((pi/4)*[0:15]);
>>x2 = cos((pi/7)*[0:15]);
Try the following
>>y1 = x1 + x2
>>y2 = x1 - x2
>>y3 = x1 .* x2
>>y4 = x1 ./ x2
>>y5 = 2*x1
>>y6 = x1 .^2
>>y7 = x1 * x2
>>y8 = x1 * x2’
12. For multiplying, dividing, and exponentiating on a term by term basis, you must precede the opera-
tor with a period .* instead of *. Also note x2’ converts the row vector x2 into a column vector, i.e .it
computes the hermitian transpose (conjugate transpose) of the argument. If you want to transpose
x2 without conjugating it use x2.’
13. MATLAB has several commands to help you label the plots appropriately, as well as to print them
out. title places its argument over the current plot as the title. xlabel and ylabel allow you to label
the axes. Every plot or graph you generate must have a title and the axes must be labeled clearly.
>>n = [0:32];
>>x = exp(i*(pi/8)*n);
>>stem(n,angle(x))
>>title(’Phase of exp(i*(pi/8)*n)’)
>>xlabel(’n (Samples)’)
>>ylabel(’Phase of x[n] (radians)’)
1. MATLAB allows us to create m-files. There are two types of m-files : scripts and functions
2. A command script is a text file of MATLAB commands whose filename ends in a .m in the current
working directory or elsewhere in your MATLAB path. If you type the name of the file (without .m)
at the command prompt the commands contained in the script file will be executed.
3. The following script file generates a discrete time cosine signal. It then computes the mean of the
signal and plots the signal. You can create a script file by using the MATLAB editor.
%example1.m
n = [0:16];
x1 = cos(pi*n/4);
y1 = mean(x1);
stem(n,x1)
title(’x1 = cos(pi*n/4)’)
xlabel(’n (samples)’)
ylabel(’x1[n]’)
Note % is used to comment in MATLAB. To execute the script file,
>>example1
4. An m-file implementing a function is a text file with a title ending .m whose first word is a function.
The rest of the line specifies the input and output arguments.
5. The following m-file is a function called foo. It accepts input x and returns y and z which are equal
to 2*x and 5/9*(x-32) respectively
function [y,z] = foo(x)
%[y,z] = foo(x) accepts a numerical argument x and
% returns two arguments y and z, where y is 2*x and z is (5/9)*(x-32)
y = 2*x;
z = (5/9)*(x-32);
Try the following
>> help foo
>>[y,z] = foo(-40)
>>[y,z] = foo(225)
1. (a) Plot using the complex plane (on the same graph) :
i. The complex number z = 1 + 0.3j (mark this location with an asterisk, i.e. ’*’). The com-
mands figure, plot, real,and imag may be useful. Also give the magnitude and phase of
this number (the commands sqrt, abs, angle, exp, and/or hold could come in handy).
ii. The complex number e−(1+0.2j) (mark the location with ’o’).
iii. The unit circle, by plotting 1001, (e.g., using the vector [0 : 1000]), complex points ejθk for
θk taking on 1001 values from 0 to 2π (The MATLAB command axis equal can make the
circle).
(b) On the same graph, plot 100 samples, over n = [0 : 99] of the signal e0.025(1−j)n , marking each
location with an ’x’.
(c) In two new figures, plot the real and imaginary parts of these signal samples, respectively,
using digital stem plot using the stem command. Label the x-axis as ’samples’ (using xlabel)
and the y-axis as ’real amplitude’ or ’imaginary amplitude’ (using ylabel) as appropriate. What
similarities and differences do you note between the real and imaginary parts ?
(d) Create a 100 × 100 matrix containing the magnitude of the complex function :
z − 0.3
f (z) =
z − 0.85ejπ/4
for values of z in the range 0 to 1 for both real and imaginary parts (that is, for all combinations
of <z = (0, 0.01, 0.02, ..., 0.99) and =z = (0, 0.01, 0.02, ..., 0.99)) and plot that using either the
command mesh or surf. (The for and zeros commands may be useful.)
2. Consider the following discrete-time signal :
" #
2πM n
xM [n] = sin ,
N
and assume that N = 12. Plot xM [n] in the time interval 0 ≤ n ≤ 2N − 1 for allM = 4, 5, 7, 10. Use
stem to create your plots, and be sure to appropriately label the axes. What is fundamental period
of each signal ? In general, how can the fundamental period be determined from arbitrary integer
values of M and N ?
3. Consider the following signal :
xk [n] = sin ωk n ,
where ωk = 2πk5
. Plot xk [n] for k = 1, 2, 4, 6 using stem for 0 ≤ n ≤ 9. The signals should be plotted
on one figure but on separate axes using subplot. How many unique signals have you plotted ? If
two signals are identical, explain how different values of ωk can yield the same signal.
4. Plot each of the following signals on the interval 0 ≤ n ≤ 31 :
πn πn
x1 [n] = sin cos
4 4
πn 2
x2 [n] = cos
πn 4 πn
x3 [n] = sin cos
4 8
What is the fundamental period of each signal ? For each of the three above signals, how you can
determine the fundamental period without relying on MATLAB ?
5. (a) Define a vector nx as the time index −3 ≤ n ≤ 7 and the vector x as the values of the signal
x[n] given by :
2, n=0
1, n=2
x[n] = −1, n=3
3, n=4
otherwise
0,
Plot this discrete-time sequence by typing stem(nx,x).
(b) Define vectors y1 through y4 to represent the following discrete-time signals :
y1 [n] = x[n − 2]
y2 [n] = x[n + 1]
y3 [n] = x[−n]
y4 [n] = x[−n + 1]
To do this, you should define y1 through y4 to be equal to x. The key is to define correctly the
corresponding index vectors ny1 through ny4. First, you should figure out how the index of a
given sample of x[n] changes when transforming to yi [n]. The index vectors need not span the
same set of indices as nx, but they should all be at least 11 samples long and include the indices
of all nonzero samples of the associated signal.
(c) Generate plots of y1 [n] to y4 [n] using stem. Based on your plots, state how each signal is related
to the original x[n], e.g. “delayed by 4” or “flipped and advanced by 3”.