Date post: | 02-Jun-2018 |
Category: |
Documents |
Upload: | ibra-nazla |
View: | 228 times |
Download: | 2 times |
of 20
8/10/2019 Workshop Number 1- Matlab Tutorial
1/20
Signals & System - A Practical Approach Using Matlab 1999 -S.R. Taghizadeh
1
Workshop Number 1
Matlab Tutorial
Introduction
The aim of this workshop is to introduce the concept of Matlab. The following topics
are included:
Simple arithmetic operations
Generation of signals, Manipulation and visualisation
Defining Systems and plotting frequency and phase responses
System responses
Spectral Analysis
Filter designing
Tutorial 1: Simple Arithmetic Operations
Once in Matlab environment, you can perform simple arithmetical operations similar to a
hand-held calculator. Some examples are shown below:
8/10/2019 Workshop Number 1- Matlab Tutorial
2/20
Signals & System - A Practical Approach Using Matlab 1999 -S.R. Taghizadeh
2
4+5
ans =
9
123-76
ans =
47
4^2
ans =
16
7*(5+2)/5
ans =
9.8000
Consider the following example:
You type this.
This is Matlab Prompt.
Matlab generates this
Matlab prompt. Ready for more Command
To raise a number toa power: 4 2
Evaluation of anexpression
8/10/2019 Workshop Number 1- Matlab Tutorial
3/20
Signals & System - A Practical Approach Using Matlab 1999 -S.R. Taghizadeh
3
x=156-87
x =
69
Once a variable is defined (like x above), Matlab stores this in the memory. The variable
x may be used to evaluate other expressions. For example:
y=x+1
y =
70
Now there are two variables and their values stored in the memory. To find out how
many variables are currently in the memory, type the command and press enter on
the prompt:
who
Your variables are:
ans x y
Here, a variable x has been defined asx=156-87. Matlab generates the answer as x=69.
Here we create another variable called, 'y' andassign the value of x in the memory plus 1.
8/10/2019 Workshop Number 1- Matlab Tutorial
4/20
Signals & System - A Practical Approach Using Matlab 1999 -S.R. Taghizadeh
4
A similar command called performs the same operation, but also gives the size
of each variable:
whos
Name Size Elements Bytes Density Complex
ans 1 by 1 1 8 Full No
x 1 by 1 1 8 Full No
y 1 by 1 1 8 Full No
Grand total is 3 elements using 24 bytes
Note that x and y have size of 1 by 1. The reason for this is that Matlab treats all
variables as a Matrix (Hence the name Matlab which stands for MATrix LABoratory).
This means that all the arithmetical operations are performed as Matrix arithmetic.
Consider the following examples:
x=[1,2,3,4] % This creates a matrix x having one row and 4 columns (i.e. a vector )
x = % and initialises it to 1,2,3 and 4.
1 2 3 4
y=[5,6,7,8]
y =
5 6 7 8
Now lets execute the command :
8/10/2019 Workshop Number 1- Matlab Tutorial
5/20
Signals & System - A Practical Approach Using Matlab 1999 -S.R. Taghizadeh
5
whos
Name Size Elements Bytes Density Complex
ans 1 by 1 1 8 Full No
x 1 by 4 4 32 Full No
y 1 by 4 4 32 Full No
Grand total is 9 elements using 72 bytes
As it can be seen, the variables x and y have dimensions of 1 by 4. This means that x
and y are matrices of 1 row and 4 columns each. When multiplying such variables,
matrix rules for multiplication is applied. For example:
z=x*y
??? Error using ==> *
Inner matrix dimensions must agree.
To multiply each member of x by a corresponding member of y, we perform a sample-
by-sample multiplication using the array operator ".*" as follows:
z=x.*y
z =
5 12 21 32
8/10/2019 Workshop Number 1- Matlab Tutorial
6/20
Signals & System - A Practical Approach Using Matlab 1999 -S.R. Taghizadeh
6
Size & Length of a variable
Two commands, 'size' and 'length' may be used to determine its size as follows:
size(x)
ans =
1 4
length(x)
ans =
4
Solving Simultaneous Equations
Consider solving the following simultaneous equation:
2x1+6x 2=3
5x1-2x 2=12
Lets put this in matrix form:
123
2562
y x
12
3,
2
1 and
25
62 Q
x
x X P Let
Hence
PX=Q , Therefore X=P -1Q
In Matlab, This is defined and solved as followed:
'size' returns the number of rowsand columns of a variable.
'length' returns the number of elements of a variable.
8/10/2019 Workshop Number 1- Matlab Tutorial
7/20
Signals & System - A Practical Approach Using Matlab 1999 -S.R. Taghizadeh
7
clear
P=[2,6;5,-2];
Q=[3;12];
X=inv(P)*Q
X =
2.2941
-0.2647
Question 1
Use Matlab to solve the following linear system:
0325
2
532
321
321
321
x x x
x x x
x x x
Here are the sequence of commands:
P=[2 3 -1;1 -1 1;5 2 -3];
Q=[5;2;0];
X=P\Q
X =
1.0000
2.0000
3.0000
This clears all previously declared variables
';' separates rows from columns
Matlab doesnot echo when a ';' at the end of adeclaration
'inv' inverts a matrix
This creates thematrix:
325
111132
P
This creates the columnvector:
02
5
QThis solves thesimultaneous equationusing: X=P -1Q
8/10/2019 Workshop Number 1- Matlab Tutorial
8/20
Signals & System - A Practical Approach Using Matlab 1999 -S.R. Taghizadeh
8
Complex Numbers
In Matlab complex numbers/variables such as:
Z=a+jb may be defined as follows:
a=2;
b=3;
z=a+b*j
z =
2.0000 + 3.0000i
Examples of complex number arithmetic are given below:
z1=2+3*j;
z2=4-5*j;
za=z1+z1za =
4.0000 + 6.0000i
zd=z1-z2
zd =
-2.0000 + 8.0000i
zm=z1*z2
zm =
23.0000 + 2.0000i
zdiv=z1/z2
zdiv =
-0.1707 + 0.5366i
'j' or 'i' may be used to indicate 1 andwhen defining a complex number.
Matlab always uses i to representacomplex numbers.
Defining complex numbers z1 and z2
Addition
Subtraction
Multiplication
Division
8/10/2019 Workshop Number 1- Matlab Tutorial
9/20
Signals & System - A Practical Approach Using Matlab 1999 -S.R. Taghizadeh
9
Separating real and imaginary part of a complex number
z=2+3*j;
R=real(z)
R =
2
I=imag(z)
I =
3
Magnitude and argument(phase) of a complex number
z=6+3*j;
magz=abs(z)
magz =
6.7082
phasez=angle(z)
phasez =
0.4636
Note: Given z=a+jb , then
)(tan 1
22
ab
phasez
bamagz
real(z) extracts the real part of z
imag(z) extracts the imaginary part of z
abs(z) returns the magnitude of thecomplex number z.
angle(z) returns the argument (phase) of the complex number z.
8/10/2019 Workshop Number 1- Matlab Tutorial
10/20
Signals & System - A Practical Approach Using Matlab 1999 -S.R. Taghizadeh
10
Vector representation of complex numbers
z=2+3*j;
compass(z);
grid;
title('Vector representation of z=2+3j');
xlabel('Real Part');
ylabel('Imaginary Part');
The result of the command compass(z) is shown below:
-2 -1 0 1 2 3-1
-0.5
0
0.5
1
1.5
2
2.5
3
Real Part
I
agi
nar yPar t
Vector representation of z=2+3j
title('Vector representation of z=2+3j'); grid; ylabel('Imaginary Part');
xlabel('Real Part');
angle
magZ
8/10/2019 Workshop Number 1- Matlab Tutorial
11/20
Signals & System - A Practical Approach Using Matlab 1999 -S.R. Taghizadeh
11
Trigonometric Functions
Matlab includes the following trigonometric functions
sin/asin: sine/arcsine
cos/acos: cosine/arccosine
tan/atan: tangent/arctan
sinh / asinh: hyperbolic sine/arcsine
cosh/acosh: hyperbolic cosine/arccosine
tanh/atanh:hyperbolic tangent/arcttang
Vector and matrix generation
Vectors may be generated in Matlab using the colon notation (:). For example to
generate a vector x having values from 1 to 8 may be performed as follows:
x=1:8
x =
1 2 3 4 5 6 7 8
Notice the following examples:
8/10/2019 Workshop Number 1- Matlab Tutorial
12/20
Signals & System - A Practical Approach Using Matlab 1999 -S.R. Taghizadeh
12
t=-0.2:0.1:0.2
t =
-0.2000 -0.1000 0 0.1000 0.2000
x=10:-1:0
x =
10 9 8 7 6 5 4 3 2 1 0
Subscripting vectors and matrices
Consider vector x:
x=[1.2, 3.4, 5.6, 6.7,3.7,8.7]
Here x has 6 elements. Each element of x may be referenced using the followingnotation:
y=x(1) y has the same value as the first element of vector x.
z=x(4) z has the same value as the 4 th elements of vector x.
Sometimes it may necessary to assign a group of elements of a vector to another variable.
For example:
w=x(1:3) w becomes a three column vector having values [1.2,3.4,5.6].
q=x(5:6) q=[3.7,8.7]
Matrices may also be referenced in the same way. Consider the following matrix:
Here, t takes on values from -0.2 to 0.2 in stepsof 0.1. the general format is as follows:Variable=Starting value:Steps:Final value
Here, values of x start form 10 down to 0 insteps of -1:
Starting ValueSteps
Final Value
8/10/2019 Workshop Number 1- Matlab Tutorial
13/20
Signals & System - A Practical Approach Using Matlab 1999 -S.R. Taghizadeh
13
1211109
8765
4321
A
A has 3 rows and 4 coulmns. For example the element in row 2 and column3 is 7, we
reference this as A(2,3).
Consider the following special references which makes Matlab a powerful way of
manipulating matrices.
Suppose A is a 10x10 matrix.
Then
B=A(1:5,3)
B becomes a 5x1 matrix that consists of the first five elements in the third column of A.
This is shown below:
19876543210
2198765439
3219876548
4321987657
5432198766
6543219875
7654321984
8765432193
9876543212
10987654321
A
B=A(1:5,3)
8/10/2019 Workshop Number 1- Matlab Tutorial
14/20
Signals & System - A Practical Approach Using Matlab 1999 -S.R. Taghizadeh
14
B=A(:,5)
B becomes the entire 5 th column of A as shown below:
19876543210
2198765439
3219876548
4321987657
5432198766
65432198757654321984
8765432193
9876543212
10987654321
A
B=A(1:5,:)B becomes the first five rows of A as shown below.
19876543210
2198765439
3219876548
4321987657
54321987666543219875
7654321984
8765432193
9876543212
10987654321
A
B=A(1:5,3)
B=A(1:5,:)
8/10/2019 Workshop Number 1- Matlab Tutorial
15/20
Signals & System - A Practical Approach Using Matlab 1999 -S.R. Taghizadeh
15
Assume B is a 5x5 matrix and A is a 10x10 matrix as before. Then complex indexing
may be achieved. For example:
A(:,[3 5 9])=B(:,2:4)
This replaces the third, fifth and ninth columns of A with the second third and fourth
columns of B as shown below:
19876543210
2198765439
3219876548
4321987657
5432198766
65432198757654321984
8765432193
9876543212
10987654321
A
6.07.08.09.00.1
5.06.07.08.09.0
4.05.06.07.08.0
3.04.05.06.07.0
2.03.04.05.06.0
1.02.03.04.05.02.01.02.03.04.0
3.02.01.02.03.0
4.03.02.01.02.0
5.04.03.02.01.0
B
Rational Operations
Six relational operators are available for comparing two variables of the same
dimensions.
< Less than
Greater than
>= Greater than or equal
== Equal
~= Not equal
8/10/2019 Workshop Number 1- Matlab Tutorial
16/20
Signals & System - A Practical Approach Using Matlab 1999 -S.R. Taghizadeh
16
Some Useful Functions
A group of functions provides basic data analysis capabilities:
max: Maximum value
min: Minimum value
std: standard deviation (square root of variance)
sum: Sum of elements
sort: Sorting
prod: Product of elements
var: Variance
sqrt: Square root
Consider the following examples.
x=[1 2 4 6 9 10 12 3 7]; % placing a ; at the end of a line, causes matlab no to echo the answer.
max(x)
ans =
12
min(x)
ans = 1
sum(x)
ans =
54
sort(x)
ans =
1 2 3 4 6 7 9 10 12
std(x)
ans =
3.8079
8/10/2019 Workshop Number 1- Matlab Tutorial
17/20
Signals & System - A Practical Approach Using Matlab 1999 -S.R. Taghizadeh
17
var(x)
ans =
14.5000
sqrt(var(x)) % this evaluates: x
ans =
3.8079
8/10/2019 Workshop Number 1- Matlab Tutorial
18/20
Signals & System - A Practical Approach Using Matlab 1999 -S.R. Taghizadeh
18
Generation and Visualisation of Signals
Consider generating and plotting a sinusoidal signal from -2 to +2 in steps of /10.
The function is x(t)=sin(t). Here is the sequences of commands to generate and plot x(t):
t=-2*pi:pi/10:+2*pi;
x=sin(t);
plot(t,x);
grid;
title('x(t)=sin(t)');
xlabel('Time');
ylabel('Amplitude');
Here is the output:
Create a vector of values of t from -2 to +2 insteps of /10. Note that pi is a predefinedconstant in Matlab.
Now generate the samples of x(t).
Now plot samples of x against t
-8 -6 -4 -2 0 2 4 6 8-1
-0.8
-0.6
-0.4
-0.2
0
0 .2
0 .4
0 .6
0 .8
1
Time
AmplItude
x(t)=sin(t)
8/10/2019 Workshop Number 1- Matlab Tutorial
19/20
Signals & System - A Practical Approach Using Matlab 1999 -S.R. Taghizadeh
19
Plotting more than one signal on the same set of axis
Consider plotting x1(t) and x2(t) defined as:
x1(t)=10sin(2 f 1t)
x2(t)=8cos(2
f 2t)Assume a sampling frequency of f s=4000Hz with f 1=100Hz and f 2=250Hz.
The discrete version of these signals are:
x1(n)=10sin(2 (f 1/ f s )n)
x2(t)=8cos(2 (f 2/ f s )n)
We will generate 100 samples of each as shown below:
fs=4000; % Defining Sampling Frequency
A1=10; % Amplitude of the first sinewave
A2=8; % Amplitude of the second sinewave
f1=100; % Frequency of the first sinewave
f2=250; % Frequency of the second sinewave
N=100; % Number of samples to generate
n=0:N-1; % time index
x1=A1*sin(2*pi*(f1/fs)*n); % Generating x1(t)
x2=A2*sin(2*pi*(f2/fs)*n); % Genertaing x2(t)
plot(n,x1,'y',n,x2,'c'); % Plot the two signals
grid; % Add a grid to the figure
title('Two Sinewaves'); % Add title
xlabel('Sample Number'); % Label x-axis
ylabel('Amplitude'); % Label y-axis
Here is the output.
8/10/2019 Workshop Number 1- Matlab Tutorial
20/20
Signals & System - A Practical Approach Using Matlab 20
Two Useful Functions
Two functions which are often used in creating vecotrs and matrices are:'ones' and 'zeros'. Their usage is illustrated below:
x=ones(n,m)
This creates a matrix of size 'n' rows and 'm' columns with their initial values set
to 1.
Y=zeros(n,m)
This creates a matrix of size 'n' rows and 'm' columns with their initial values set
to 1.
Generating 32 samples of an impulse signal: x=[1, zeros(1,31)];
Generating 32 samples of a step signal: x=ones(1,32);
Generating a single pulse:
x1=[zeros(1,8),ones(1,16),zeros(1,8)];
0 10 20 30 40 50 60 70 80 90 100-10
-8
-6
-4
-2
0
2
4
6
8
10Two Sinewaves
Sample Number
AmpLITUDe