0% found this document useful (0 votes)
131 views17 pages

Eee30004: Digital Signal Processing Lab 1: Introduction To Matlab

Here is the breakdown of the complex exponential formula used in the code: zz = 1.4*exp(j*pi/2)*exp(j*5*pi*tt) 1.4 is a real scaling factor exp(j*pi/2) = i, since j*pi/2 = pi/2 radians = 90 degrees exp(j*5*pi*tt) is the complex exponential of the sinusoid with frequency 5*pi radians/sec So together, zz calculates a scaled complex sinusoid with amplitude 1.4, phase shifted by pi/2 radians (90 degrees). The real part of zz is then plotted separately using real(zz) to show the

Uploaded by

Shelby
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
131 views17 pages

Eee30004: Digital Signal Processing Lab 1: Introduction To Matlab

Here is the breakdown of the complex exponential formula used in the code: zz = 1.4*exp(j*pi/2)*exp(j*5*pi*tt) 1.4 is a real scaling factor exp(j*pi/2) = i, since j*pi/2 = pi/2 radians = 90 degrees exp(j*5*pi*tt) is the complex exponential of the sinusoid with frequency 5*pi radians/sec So together, zz calculates a scaled complex sinusoid with amplitude 1.4, phase shifted by pi/2 radians (90 degrees). The real part of zz is then plotted separately using real(zz) to show the

Uploaded by

Shelby
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

EEE30004: DIGITAL SIGNAL PROCESSING

LAB 1: INTRODUCTION TO MATLAB

Name: Don Jude Milinda Jayamaha

ID: 101219467

Lab Group: Wednesday 10:30 am

Date: 11/9/2019

Lab Supervisor: Ling Ting Soon


Contents
Objectives ............................................................................................................................................... 1
1.0 Pre-lab ............................................................................................................................................... 1
Question 1.3 e ..................................................................................................................................... 3
Question 1.3 f...................................................................................................................................... 3
Question 1.3 g ..................................................................................................................................... 4
2.0 Warm-UP........................................................................................................................................... 4
Q 2.1a .................................................................................................................................................. 4
Q 2.1b .................................................................................................................................................. 6
Q 2.1c .................................................................................................................................................. 7
Q 2.2a .................................................................................................................................................. 7
Q 2.2b .................................................................................................................................................. 8
Q2.2c ................................................................................................................................................. 10
Q2.2d and e ....................................................................................................................................... 11
3.0 Lab Exercise : Manipulating Sinusoids with MATLAB..................................................................... 12
3.1 Theoretical Calculations .............................................................................................................. 13
Q 3.1a ............................................................................................................................................ 13
Q 3.1b ............................................................................................................................................ 13
Q 3.1c ............................................................................................................................................ 14
3.2 Complex amplitude ..................................................................................................................... 14
Conclusion ............................................................................................................................................. 15
Objectives

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:

The plot function:

1|Page
The help colon command:

The help ops command:

The help zeros 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

z = 3.0000 + 4.0000i w = -3.0000 + 4.0000i

real(z),imag(z)

ans = 3

ans = 4

abs([z,w])

ans = 5 5

conj([z+w])

ans = 0.0000 - 8.0000i

angle(z)

ans = 0.9273

exp(j*pi)

ans = -1.0000 + 0.0000i

exp(j*[pi/4, 0, -pi/4])

ans = 0.7071 + 0.7071i 1.0000 + 0.0000i 0.7071 - 0.7071i

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

2.0000 2.1111 2.2222 2.3333 2.4444 2.5556 2.6667


2.7778 2.8889 3.0000

Page |4
Columns 11 through 19

3.1111 3.2222 3.3333 3.4444 3.5556 3.6667 3.7778


3.8889 4.0000

tpi = pi*[0 : 0.1 : 2]


tpi =
Columns 1 through 10

0 0.3142 0.6283 0.9425 1.2566 1.5708 1.8850


2.1991 2.5133 2.8274

Columns 11 through 20

3.1416 3.4558 3.7699 4.0841 4.3982 4.7124 5.0265


5.3407 5.6549 5.9690

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

0 0 0 0 0.2500 0.5000 0.7500


1.0000 1.0000 1.0000

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 3rd line outputs the size of the vector x.

Line 4 shows the length of vector x.

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

0 0 0 3.1416 6.2832 9.4248 0.7500


1.0000 1.0000 1.0000

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

0 36.4622 0 36.4622 0.2500 36.4622 0.7500


36.4622 1.0000 36.4622

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

1.0000 0.7071 0.0000 -0.7071 -1.0000 -0.7071 -0.0000


0.7071 1.0000 0.7071

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 =

0.5000 -0.5000 -1.0000

yy =

0.5000 -0.5000 -1.0000 -0.5000

yy =

0.5000 -0.5000 -1.0000 -0.5000 0.5000

yy =

Columns 1 through 5

0.5000 -0.5000 -1.0000 -0.5000 0.5000

Column 6

1.0000

yy =

Columns 1 through 5

0.5000 -0.5000 -1.0000 -0.5000 0.5000

Columns 6 through 7

1.0000 0.5000

Page |8
yy =

Columns 1 through 5

0.5000 -0.5000 -1.0000 -0.5000 0.5000

Columns 6 through 8

1.0000 0.5000 -0.5000

yy =

Columns 1 through 5

0.5000 -0.5000 -1.0000 -0.5000 0.5000

Columns 6 through 9

1.0000 0.5000 -0.5000 -1.0000

yy =

Columns 1 through 5

0.5000 -0.5000 -1.0000 -0.5000 0.5000

Columns 6 through 10

1.0000 0.5000 -0.5000 -1.0000 -0.5000

yy =

Columns 1 through 5

0.5000 -0.5000 -1.0000 -0.5000 0.5000

Columns 6 through 10

1.0000 0.5000 -0.5000 -1.0000 -0.5000

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 =

-3.0000 +18.0000i -1.0000 + 4.0000i 0.0000 + 0.0000i 1.0000 -


2.0000i 3.0000 + 0.0000i

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;

𝑒 𝑗(∅) = cos(∅) + 𝑗𝑠𝑖𝑛(∅)


𝜋
𝑅𝑒(𝑧) = cos(5𝜋𝑡 + )
2

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

Equation to find the phase angle:

𝝋 = 𝝎𝒕m
𝝎 = 𝟐𝝅𝒇 = 𝟐 ∗ 𝝅 ∗ 𝟒𝟎𝟎𝟎 = 𝟖𝟎𝟎𝟎𝝅 𝒓𝒂𝒅/𝒔
For x1:

𝜑1 = 8000𝜋 ∗ 3𝑒 − 05 = 0.24𝜋 𝑟𝑎𝑑


For x2:

𝜑2 = 8000𝜋 ∗ 1𝑒 − 05 = 0.08𝜋 𝑟𝑎𝑑

Q 3.1b

𝜑3 = 8000𝜋 ∗ 2𝑒 − 05 = 0.16𝜋 𝑟𝑎𝑑

P a g e | 13
Q 3.1c

X1 = 20*𝑒 𝑗(0.24𝜋) =20(cos(0.24*pi)+ j*sin(0.24*pi)

X2 = 24*𝑒 𝑗(0.08𝜋) = 24(cos(0.08*pi)+ j*sin(0.08*pi)

X3= 37.82 +j19.66= 42.62𝑒 𝑗(0.153𝜋)


Phasor addition to get x3 results matches with the values of amplitude and phase for x3 in
part b.

3.2 Complex amplitude

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

You might also like