Eee30004: Digital Signal Processing Lab 1: Introduction To Matlab
Eee30004: Digital Signal Processing Lab 1: Introduction To Matlab
ID: 101219467
Date: 11/9/2019
The main purpose of this lab is to introduce and familiarize MATLAB. It involves learning the syntaxes
and grammar, basic commands, working with script files as well as venture into certain advanced
commands.
1.0 Pre-lab
The first command I used was help , which provides a window into MATLAB’s documentation:
1|Page
The help colon command:
Page |2
The help ones command:
Question 1.3 e
The ‘ans’ variable automatically holds the last result of the performed calculations, making it
convenient to use the result for further calculations, as shown above.
Question 1.3 f
The use of the semi-colon at the end of the first command stops the command window from
displaying the output for x. Here, the ans variable stores the value of cos(pi/5).
Page |3
Question 1.3 g
z= 3 + 4i, w= -3 +4j
real(z),imag(z)
ans = 3
ans = 4
abs([z,w])
ans = 5 5
conj([z+w])
angle(z)
ans = 0.9273
exp(j*pi)
exp(j*[pi/4, 0, -pi/4])
2.0 Warm-UP
Q 2.1a
jkl = 0 : 6
jkl =
0 1 2 3 4 5 6
jkl = 2 : 4 : 17
jkl =
2 6 10 14
jkl = 99 : -1 : 88
jkl =
99 98 97 96 95 94 93 92 91 90 89 88
ttt = 2 : (1/9) : 4
ttt =
Columns 1 through 10
Page |4
Columns 11 through 19
Columns 11 through 20
Column 21
6.2832
Page |5
The colon notation can be used to create a linearly spaced list or vector. It is also used to pick out
selected rows, columns or elements of a particular vector.
Q 2.1b
xx = [ zeros(1,3), linspace(0,1,5), ones(1,4)]
xx =
Columns 1 through 10
Columns 11 through 12
1.0000 1.0000
xx(4:6)
ans =
0 0.2500 0.5000
size(xx)
ans =
1 12
length(xx)
ans =
12
xx(2:2:length(xx))
ans =
0 0 0.5000 1.0000 1.0000 1.0000
The first line inserts zeros into the first 3 elements of x, 5 linearly spaced points from 0 to 1 for the
3rd to 8th elements of x. The last 4 elements are set as ones.
The next line displays the elements of x with the index from 4 to 6.
The last line outputs all the elements from index 2 to 12 in steps of 2. Thus elements with
index(2,4,6..12) are shown.
Page |6
Q 2.1c
yy = xx; yy(4:6) = pi*(1:3)
yy =
Columns 1 through 10
Columns 11 through 12
1.0000 1.0000
The variable xx is assigned to yy, and the elements 4-6 of yy are set to pi, as a vector with size 3 is set
with the values of pi.
xx(2:2:length(xx)) = pi^pi
xx =
Columns 1 through 10
Columns 11 through 12
1.0000 36.4622
Vector replacement is used here to replace the even indexed elements of x with the constant pi^2.
Q 2.2a
xk = cos( pi*(0:11)/4)
xk =
Columns 1 through 10
Columns 11 through 12
0.0 -0.7071
xk(1)
ans =
xk(0)
Page |7
Array indices must be positive integers or
logical values.
The above error message indicates that an index value of 0 is invalid, since they can be positive
integers only and thus start from 1. The element of index 1 stores the value of 1.0000.
Q 2.2b
yy = [ ];
for k=-5:5
yy(k+6) = cos(k*pi/3)
end
yy =
0.5000
yy =
0.5000 -0.5000
yy =
yy =
yy =
yy =
Columns 1 through 5
Column 6
1.0000
yy =
Columns 1 through 5
Columns 6 through 7
1.0000 0.5000
Page |8
yy =
Columns 1 through 5
Columns 6 through 8
yy =
Columns 1 through 5
Columns 6 through 9
yy =
Columns 1 through 5
Columns 6 through 10
yy =
Columns 1 through 5
Columns 6 through 10
Column 11
0.5000
As the index of a vector cannot be zero or negative, yy(k) is invalid. This is since the ‘for’ loop starts
from k=-5 to k=5. To overcome this issue, yy(k+6) is used to ensure that the index values used are
always above zero.
Page |9
Q2.2c
x = [-3 -1 0 1 3];
y = x.*x - 3*x;
plot(x,y)
z = x + y*sqrt(-1)
plot( z )
z =
x,y plot:
z plot:
P a g e | 10
Q2.2d and e
tt = -1 : 0.01 : 1;
xx = cos(5*pi*tt);
zz = 1.4*exp(j*pi/2)*exp(j*5*pi*tt);
plot(tt , xx, 'b-', tt, real (zz), 'r--'), grid on
title('TEST PLOT of a SINUSOID')
xlabel('TIME (sec)')
The formula breaking down complex exponentials into sines and cosines is;
Thus the real part of zz is a sinusoid and plotted as shown in the figure.
P a g e | 11
3.0 Lab Exercise : Manipulating Sinusoids with MATLAB
Code:
t=1/4000;
tt=(-t:1e-5:t);
A1=20;
A2= 1.2*A1;
D=14;
M=9;
tm1=(37.2/M)*t;
tm2=-(41.3/D)*t;
x1=A1*cos((2*pi*4000*(tt-tm1)));
x2=A2*cos((2*pi*4000*(tt-tm2)));
x3=x1+x2;
subplot(3,1,1);
plot(tt,x1,'b-'),grid on;
title('Milinda x1 plot');
xlabel('Time(s)');
ylabel('Amplitude');
subplot(3,1,2);
plot(tt,x2,'r-'),grid on;
title('x2 plot');
xlabel('Time(s)');
ylabel('Amplitude');
subplot(3,1,3);
plot(tt,x3,'g-'),grid on;
title('x3 plot');
xlabel('Time(s)');
ylabel('Amplitude');
P a g e | 12
3.1 Theoretical Calculations
Q 3.1a
𝝋 = 𝝎𝒕m
𝝎 = 𝟐𝝅𝒇 = 𝟐 ∗ 𝝅 ∗ 𝟒𝟎𝟎𝟎 = 𝟖𝟎𝟎𝟎𝝅 𝒓𝒂𝒅/𝒔
For x1:
Q 3.1b
P a g e | 13
Q 3.1c
X = 20;
t = -1/4000 : 1e-5 : 1/4000;
f = real( X * exp((j*8000*pi*t)));
plot(t,f), grid on
title('Complex amplitude');
P a g e | 14
Conclusion
Overall, this lab gives a thorough introduction to MATLAB. It started with an outline of the
basic prompts with the help commands to get information regarding functions. I familiarised
myself with the command line as well as writing script files to run Matlab code. Next, basic
calculations were carried out with complex numbers and vectors. Plotting graphs was a
straightforward process, where I learned the various commands and tools involving plots.
Finally, I learned to observe the behaviour of sinusoidal graphs when the parameters used
are adjusted, such as time-shifts and amplitude.
P a g e | 15