Professional Documents
Culture Documents
12/11/2014
A Training Session on
Fundamentals of MATLAB
in connection with the FDP on Digital Signal Processing Tools for Engineering Applications @ CET
PART I
Getting familiarized with MATLAB
90 Minutes (9:30 am 11:00 am)
am)
12 November 2014
Fundamentals of MATLAB
12/11/2014
Outline
Introduction MATLAB and Simulink
Toolboxes & Built-in Functions
Help & Documentation
Matrix Algebra
Polynomials and Roots
Trigonometric Functions
One Dimensional Plots
Developing and running MATLAB scripts
12 November 2014
Fundamentals of MATLAB
About MATLAB
MATLAB MATrix LABoratory a product by MathWorks Inc., USA
High Performance Computing Environment
Completely written using C Language
Unlike C, it is a Column Major Language
Available for both Windows and Linux
Version
V i : R2013a
R2013
or
8 1 0 604
8.1.0.604
R Release
2013 Year
a First half; b Second Half
12 November 2014
Fundamentals of MATLAB
12/11/2014
In addition to MATLAB,
Cleve Moler
Fundamentals of MATLAB
Fundamentals of MATLAB
12/11/2014
Machine Language
12 November 2014
Fundamentals of MATLAB
Fundamentals of MATLAB
12/11/2014
How to Open
Double Clicking the emblem
Version
Product
Company
12 November 2014
Fundamentals of MATLAB
MATLAB Desktop
Workspace
Current
Folder
Command
Window
Command
History
12 November 2014
Fundamentals of MATLAB
10
12/11/2014
Fundamentals of MATLAB
11
All variables are created with double precision unless specified otherwise
Example:
>> x = 5;
>> x1 = 2;
Fundamentals of MATLAB
12
12/11/2014
= [
[1 2 3 4]
] or [
[1,
, 2,
, 3,
, 4]
]
Delimiter is a space or comma
Enclosed in square braces
MATLAB takes the above as
r = [ 1 2 3 4 ]
= [1:10] or 1:10
MATLAB takes the above as
n = [ 1 2 3 4 5 6 7 8 9 10 ]
= [3:2:15] or 3:2:15
MATLAB takes the above as
k = [ 3 5 7 9 11 13 15 ]
12 November 2014
Fundamentals of MATLAB
13
Fundamentals of MATLAB
14
12/11/2014
= [
[1;
; 2;
; 3;
; 4]
]
Delimiter is a semicolon
Enclosed in square braces
MATLAB takes the above as
c=[1
2
3
4 ]
Transposing operation : using
r = 2:3:10 ;
c = r ;
12 November 2014
Fundamentals of MATLAB
15
Creating an M x N Matrix
m
m
a
b
c
= [
[1 2 3;
; 4 5 6;
; 7 8 9];
];
Delimiters are spaces and semicolons
Enclosed in square braces
MATLAB takes the above as
c=[1 2 3
4 5 6
7 8 9]
= [1:3; 4:6; 7:9];
= [1; 2; 3; 4];
= [5; 7; 8; 9]
= [a b];
12 November 2014
Fundamentals of MATLAB
16
12/11/2014
12 November 2014
Fundamentals of MATLAB
17
12 November 2014
Fundamentals of MATLAB
18
12/11/2014
MATLAB Toolboxes
Aerospace Toolbox
Bioinformatics Toolbox
Communications System Toolbox
Computer Vision System Toolbox
Control System Toolbox
Curve Fitting Toolbox
Data Acquisition Toolbox
Database Toolbox
DSP S
System T
Toolbox
lb
Econometrics Toolbox
Financial Toolbox
Fuzzy Logic Toolbox
Image Acquisition Toolbox
12 November 2014
Fundamentals of MATLAB
19
sin()
cos()
()
inv()
eye()
rand()
ones()
zeros()
size()
l
length()
h()
max()
min()
fliplr()
flipud()
12 November 2014
plot()
stem()
t ()
surf()
mesh()
fft()
abs()
eig()
rank()
ceil()
il()
round()
sum()
prod()
linspace()
Fundamentals of MATLAB
20
10
12/11/2014
Help / Documentation
help
p function_name
_
or doc function_name
_
Help Menu
Search
12 November 2014
Fundamentals of MATLAB
21
Fundamentals of MATLAB
22
11
12/11/2014
Matrix Multiplication
Matrix multiplication : *
a = [1 2 3 4; 3 1 2 4];
b = [3 1; 1 2; 0 1; 2 3];
c = a * b
yields c = [ 13 20
18 19 ]
Both a & b should be conformable to multiplication
a should be of dimension M x N and b should be of N x K
The result will be of M x K dimension
Multiplication by a scalar is also done using *
The function equivalent to * is mtimes()
c = mtimes(a, b)
12 November 2014
Fundamentals of MATLAB
23
Signal Product
Signal multiplication is done using .*
.*
* will
ill compute the
h element-by-element
l
b l
product
d
a = 1:5;
b = 2:6;
c = a .* b
will yield c = [ 2 6 12 20 30 ]
Matrix dimensions must match.
Will output a signal of same dimension
Is useful is many signal processing operations
The function equivalent to .* is times()
Check:
pow = a .* a; and pow = a .^ 2;
12 November 2014
Fundamentals of MATLAB
24
12
12/11/2014
2.
12 November 2014
Fundamentals of MATLAB
0.9474
0.6842
0.9474]
25
Fundamentals of MATLAB
26
13
12/11/2014
Fundamentals of MATLAB
27
12 November 2014
Fundamentals of MATLAB
28
14
12/11/2014
Data Input
Generally there are three ways of inputting data into MATLAB
directly entering in the command line
from memory
read an excel file
read a speech file
read an image file
from I/O devices
input from a sensor
input from a microphone
input from a camera
12 November 2014
Fundamentals of MATLAB
29
12 November 2014
Fundamentals of MATLAB
30
15
12/11/2014
Data Output
Generally there are three ways of outputting data from MATLAB
directly flashing to command line
to memory
write an excel file
write a speech file
write an image file
to I/O devices
output to a DAC
output to a loudspeaker
output to a display
12 November 2014
Fundamentals of MATLAB
31
%12.8f\n',y);
fclose(fid);
xlswrite(t.xlsx,m)
wavwrite(s,fs,au.wav)
( , ,
) / sound(s,fs)
( , )
imwrite(y,img.jpg) / imshow(y)
save(filename, var)
save menu
12 November 2014
Fundamentals of MATLAB
32
16
12/11/2014
12 November 2014
Fundamentals of MATLAB
33
12 November 2014
Fundamentals of MATLAB
34
17
12/11/2014
do
pi 3.14..
Inf infinity
NaN not a number
eps spacing of floating point numbers
who prints variables in work space
why answers to such a question. Do chat with MATLAB !
beep produces a beep sound
12 November 2014
Fundamentals of MATLAB
35
Roots of a Polynomial
roots(C) computes the roots of the polynomial whose coefficients
are the elements of the vector C
If C has N+1 components, the polynomial is C(1)*X^N + ... +
C(N)*X + C(N+1).
C = [1
-3
2 ];
r = roots(C)
yields r = [-2
1]
-3
2]
Fundamentals of MATLAB
36
18
12/11/2014
Fundamentals of MATLAB
37
10
8
w = 0.2;
6
4
x = 10*sin(w*t);
plot(t x);
plot(t,
This yields an output figure :
2
0
-2
-4
-6
-8
-10
12 November 2014
20
40
60
Fundamentals of MATLAB
80
100
120
140
160
180
200
38
19
12/11/2014
12 November 2014
Fundamentals of MATLAB
39
Fundamentals of MATLAB
40
20
12/11/2014
12 November 2014
Fundamentals of MATLAB
41
12 November 2014
Fundamentals of MATLAB
42
21
12/11/2014
12 November 2014
Fundamentals of MATLAB
43
Fundamentals of MATLAB
44
22
12/11/2014
Fundamentals of MATLAB
45
Fundamentals of MATLAB
46
23
12/11/2014
Exercise
1)
2)
3)
4)
5)
6)
Pl the
Plot
h ffunction
i f(x)
f( ) = (1/2)
(1/2 ) e (x )
7)
8)
12 November 2014
2 / 22
Fundamentals of MATLAB
47
Part I : Summary
Familiarized with MATLAB desktop, toolboxes, variable classes and
some built-in functions
Performed simple computations using the command prompt, reviewed
fundamentals of linear algebra using MATLAB
Differentiated the matrix product from signal product
Studied how to seek help in the command prompt and how to use the
help menu
Learnt the various methods of data input
p and output
p
Computed the roots of a polynomial and obtained the polynomial from a
root set
Learnt how to plot one dimensional (1-D) curves
Studied how to create and run the MATLAB script files
12 November 2014
Fundamentals of MATLAB
48
24
12/11/2014
PART II
Creating functions, 22-D and 33--D plots
90 Minutes
Mi
((11
(11:15
11 15 am 12:45
11:15
12 45 pm))
12 November 2014
Fundamentals of MATLAB
49
Outline
Logical operations and logical indexing
The for loop and while loop
Commenting and publishing MATLAB script files
Standard AM and FM Simulation
Computing the execution time
Developing user-defined functions
Global variables, current directory & path
Plotting 2-D and 3-D figures in MATLAB
12 November 2014
Fundamentals of MATLAB
50
25
12/11/2014
Logical Operations
Logical operations are :
==
>
<
>=
<=
~=
&
|
12 November 2014
or
eq
or
or
or
ge
le
ne
Fundamentals of MATLAB
51
Logical Indexing
One can use logical indexing to extract portions of data according to a
logical criterion.
Comparisons using relational operators such as < and == return a logical
array.
Eg., id = mark >= 48;
creates a logical variable id of the same size as mark
One can use a logical variable as an index to any other variable The
result will be the array elements where the index is true
t
topranked
k d = mark(id);
k(id)
Note that the result of logical indexing will be a smaller array (possibly
even empty if the index contains no true values).
It is therefore important to be careful to keep dimensions consistent when
indexing to multiple variables.
12 November 2014
Fundamentals of MATLAB
52
26
12/11/2014
Fundamentals of MATLAB
53
if - else
if else also should be closed with end
for i=0:1:10
if rem(i,2) ~= 0
odd = i
else
even = i
end
end
12 November 2014
Fundamentals of MATLAB
54
27
12/11/2014
While
Performs a group of operations when a condition is satisfied
a = 1
1
while length(a) < 10
1 1
a = [0 a] + [a 0]
1 2 1
end
1 3 3 1
1 4 6 4 1
produces the Pascals Triangle :
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
12 November 2014
Fundamentals of MATLAB
55
Break
Is used to terminate the iterations of a loop.
for i=1:1:100
pow = i*i
if pow > 100
break;
end
end
12 November 2014
Fundamentals of MATLAB
56
28
12/11/2014
Fundamentals of MATLAB
57
12 November 2014
Fundamentals of MATLAB
58
29
12/11/2014
Publishing Options
Output file format
Fundamentals of MATLAB
59
%
%
%
%
%
%
Sampling instants
Baseband frequency
Carrier frequency
Baseband Signal
Carrier Signal
Standard AM
Fundamentals of MATLAB
60
30
12/11/2014
FM Another example
Equation : v = V cos (c t + mf sin m t)
Code segment :
t = 1:200;
% Sampling instants
% Baseband frequency
f1 = 0.01;
f2 = 0.1;
% Carrier frequency
mf = 4;
% Modulation Index
vm = cos(2*pi*f1*t);
% Baseband Signal
vc = cos(2*pi*f2*t);
% Carrier Wave
fm = cos(2*pi*f2*t+mf*sin(2*pi*f1*t)); % FM
(Go to the Script File)
12 November 2014
Fundamentals of MATLAB
61
Fundamentals of MATLAB
62
31
12/11/2014
12 November 2014
Fundamentals of MATLAB
63
Fundamentals of MATLAB
64
32
12/11/2014
return values
Fundamentals of MATLAB
65
Fundamentals of MATLAB
66
33
12/11/2014
Fundamentals of MATLAB
67
Current Directory
Current
Directory
12 November 2014
Fundamentals of MATLAB
68
34
12/11/2014
Setting Path
Settingg a ppath
12 November 2014
Fundamentals of MATLAB
69
or
t = linspace(1,200,200)
Here t would serve as the index for the placement of 1-D samples
In 2-D, an extended spatial arrangement is required to initialize a plot
The MATLAB function meshgrid will serve this purpose
12 November 2014
Fundamentals of MATLAB
70
35
12/11/2014
>> x = 1:7;
>> y = 2:2:12;
>> [X,Y] = meshgrid(x,y);
x : 1 x 7 vector
y : 1 x 6 vector
Grid is formed with 6 x 7 points
Here X contains the x coordinates of all points in the 6-by-7 grid and Y
contains the corresponding y coordinates.
These matrices of coordinates can be used in calculations, interpolation
of scattered data and visualization.
12 November 2014
Fundamentals of MATLAB
71
Mesh Grid
(1 2)
(1,2)
(4,2)
(7,8)
(1,8)
(1,12)
12 November 2014
(7,2)
(4,12)
Fundamentals of MATLAB
(7,12)
72
36
12/11/2014
Fundamentals of MATLAB
73
-2
-3
-3
12 November 2014
-2
-1
Fundamentals of MATLAB
74
37
12/11/2014
Fundamentals of MATLAB
75
-2
1
0.5
1
0.5
-0.5
-0.5
-1
12 November 2014
Fundamentals of MATLAB
-1
76
38
12/11/2014
-2
2
-4
2
1
2
1
-1
-1
-2
12 November 2014
-2
Fundamentals of MATLAB
77
-0.5
-1
1
0.5
1
0.5
-0.5
-0.5
-1
12 November 2014
Fundamentals of MATLAB
-1
78
39
12/11/2014
Part II : Summary
Studied about logical operations and logical indexing
Familiarized with for loop, while loop and break
Studied how to insert comments and to publish a MATLAB script
Simulated standard AM and FM
Computed the execution time and learnt to introduce delay
Studied how to create user-defined functions
Earned the notion of global variables, current directory & path
Learnt how to plot 2-D and 3-D figures in MATLAB
12 November 2014
Fundamentals of MATLAB
79
Thanks
aranjithram@gmail.com
12 November 2014
Fundamentals of MATLAB
80
40