صفحه 1:
Iran University of Science and Technology,
Computer Engineering Department,
Introduction to MATLAB
Ehsan Adeli M. (eadeli@iust.ac.ir)
صفحه 2:
Outline
> Introduction
» MATLAB Environment
> Getting Help
» Vectors, Matrices, and Linear Algebra
> Plotting
> User Defined Functions
» Image Manipulation and Processing
» Audio Processing
» Filters and Operations
Multimedia Systems (eadeli@iust.ac.ir)
صفحه 3:
Introduction
» MATLAB Stands for MATrix LABoratory.
» The MATLAB environment allows the user to:
» manage variables
» import and export data
» perform calculations
» generate plots
» develop and manage files for use with MATLAB.
» A script can be made with a list of MATLAB
commands like other programming language.
‘Multimedia Systems (eadeli@tust.ac.ir) َم
صفحه 4:
Environment
‘Multimedia Systems (eadeli@tust.ac.ir)
صفحه 5:
Getting Help
> Type one of following commands in the command
window:
» help - lists all the help topic
help topic - provides help for the specified topic
* help command - provides help for the specified
command
» help help - provides information on use of the
help command
» helpwin - opens a separate help window for
navigation
» lookfor keyword - Search all M-files for keyword
» Or simply press ‘F1’ and use the graphical help
window.
>5 ‘Multimedia Systems (eadeli@tust.ac.ir)
صفحه 6:
Variables
» Variable names:
» Must start with a letter
» May contain only letters, digits, and the underscore
» Matlab is case sensitive, i.e. one & OnE are different
variables.
* Matlab only recognizes the first 31 characters in a
variable name.
» when a semi-colon
» >> tutorial = 1234;
» >> tutorial = 1234
» ”;” is placed at the end of each command, the
result is not displayed.
re ‘Multimedia Systems (eadeli@tust.ac.ir)
صفحه 7:
Variables (Cont’d)
» Special variables:
ans : default variable name for the result
pi: m = 3.1415926..
eps: € = 2.2204e-( 016, smallest amount by which 2 numbers can
differ.
Inf or inf : ©, infinity
NaN or nan: not-a-number
» Commands involving variables:
who: lists the names of defined variables
whos: lists the names and sizes of defined variables
clear: clears all variables, reset the default values of special
variables.
clear name: clears the variable name
clc: clears the command window
clf: clears the current figure and the graph window.
7 Multimedia Systems
صفحه 8:
Vectors
> >> x =[ 0 0.25*pi 0.5*pi 0.75*pi pi ]
gx
O 0.7854 1.5708 2.3562 3.1416
> >>y =[ 0; 0 25*pi; 0 5*pi; 0 75*pi; ]
ys
0
0.7854
1.5708 y
2.3562
3.1416
8 ‘Multimedia Systems (eadeli@tust.ac.ir)
صفحه 9:
Vectors (Cont’d)
» Vector Addressing - A vector element is addressed in
MATLAB with an integer index enclosed in
parentheses.
» >> x(3)
» The colon notation may be used to address a block of
elements (start : increment : end)
start is the starting index, increment is the amount to add to
each successive index, and end is the ending index. A
shortened format (start : end) may be used if increment is 1.
bse (1:3)
ans =
0 0.7854 1.5708
» NOTE: MATLAB index starts at 1.
>9 ‘Multimedia Systems (eadeli@tust.ac.ir)
صفحه 10:
Vectors (Cont’d)
startiend create row vector x starting with start, counting
by one, ending at end
X = startincrementend veate row vector x starting with start, counting
by increment, ending at or before end
linspace(start, end, number) create row vector x starting with start, ending at
end, having number elements
length(x)
y=x ‘Transpose conjugate of vector x
dot (x, y) velurns the sealar dot product of the vector x and
3
10 ‘Multimedia Systems (eadeli@tust.ac.ir)
صفحه 11:
Example:
Array Operations 5 f= [12534]
» Scalar-Array Mathematics: 1 2
8 4
<< عم 9*1
» Element-by-Element Array-Arra دع
Mathematics: 1 8
5 7
ِ >>x=[123];
Addition 5
Subtraction i 5 1:
Multiplication oe eae
Division 8 8 z=
Exponentiation a 4 10 8
11 ‘Multimedia Systems (eadeli@tust.ac.ir)
صفحه 12:
Matrices
> A Matrix array is two-dimensional, << ]12 3: 456
having both multiple rows and 12 123
multiple columns, similar to vector 4 5 8
arrays: << 0 2 ]24 6 1 35[
» It begins with [, and end with ] he
spaces or commas are used to separate 8 3
elements in a row >> (2,3)
» semicolon or enter is used to separate
rows. a
» Matrix Addressing: 6
* matrixname(row, column) >> h(:,1)
» colon may be used in place of a row or
column reference to select the entire تت
row or column. 2
12 ‘Multimedia Systems (eadeli@tust.ac.ir) 1
صفحه 13:
Matrices (Cont’d)
zeros(n) returns a n xn matrix of zeros
Zeros(m,n) returns am x n matrix of zeros
ones(n) returns a n x n matrix of ones
ones(m,n) returns a m x n matrix of ones
size (A) for a m x n matrix A, returns the row vector [m,n]
containing the number of rows and columns in
matrix.
length(A) returns the larger of the number of rows or
columns in A.
13 ‘Multimedia Systems (eadeli@tust.ac.ir)
صفحه 14:
Matrices (Cont’d)
B=‘
eye(n) ® returns an n x n identity matrix
eye(m,n) > returns an m x n matrix with ones on the
main diagonal and zeros elsewhere.
C=A+tB
C=A-B
B=a<A, where a is a scalar.
C=A*B
B= inv(A), A must be a square matrix in this case.
rank (A) > returns the rank of the matrix A.
B=A*2 > squares each element in the matrix
C=A* A> computes A*A, and A must be a square
matrix.
det (A), and A must be a square matrix.
‘Multimedia Systems (eadeli@tust.ac.ir)
Transpose
Identity Matrix
Addition and
subtraction
Multiplication
Matrix Multiplication
Matrix Inverse
Matrix Powers
Determinant
14
صفحه 15:
Example, Linear Equations
3x, 10)
-x b=| 5}
-1)
Solution by Matrix Inverse: — Solution by Matrix Division:
Ax=b The solution to the equation
ATAX = Atb Ax=b
x= Ab can be computed using left division
MATLAB: MATLAB:
دم جد 1-1-1 132-:32-1] عم حد ]3 2-1-1832: 1-1-1:
>> b=[ 10; 5; -1]; << b =[10: 5; -1];
>> x = inv(A)*b >> x =A\b
X= x= ۱
20000 20000 Answer:
5.0000 000 و ,5 2 يك ,2- > بي 2-6
Sapo همه
صفحه 16:
Plotting
Plotting Curves:
plot (x,y) - generates a linear plot of the values of x (horizontal axis) and y (vertical
axis).
semilogx (x,y) - generate a plot of the values of x and y using a logarithmic scale for
x and 4 linear scale for y
semilogy (x,y) - generate a plot of the values of x and y using a linear scale for x and
a logarithmic scale for y.
loglog(x.y) - generate a plot ofthe values of x and y using logarithmic scales for both
xan
Multiple Curves:
plot (x. y..w, 2)- multiple curves can be plotted on the same graph by using multiple
arguments in a plot command, The variables x, , w, and z are vectors, Two curves
be plotted: y vs. x, and z vs. w.
legend (‘string!’, ‘string2’,...) - used to distinguish between plots on the same graph
Multiple Figures:
figure (n) - used in creation of multiple plot windows. place this command before the
plot() command, and the corresponding figure will be labeled as “Figure n”
Elose - closes the figure n window.
close all - closes all the figure windows.
Subplots:
subplot (m, n, p) - m by n grid of windows, with p specifying the current plot as the
pth window
16 ‘Multimedia Systems (eadeli@tust.ac.ir)
>
صفحه 17:
Plotting (Cont’d)
Description
Adds dashed grids lines at the tick marks
removes grid lines (default)
toggles grid status (off to on, or on to off)
labels top of plot with text in quotes
labels horizontal (x) axis with text ia quotes:
labels vertical (y) axis with text is quotes
‘Multimedia Systems (eadeli@tust.ac.ir)
Command
gridon
grid off
grid
title Ctoxt’)
xlabel text’)
ylabel (text’)
صفحه 18:
Flow Control is temperature > 100
disp (‘Too hot — equipment malfunctioning.’)
if logical expression _ elseif temperature > 90 —
commands disp (Normal operating range.’);
end elseif (Below desired operating range.’)
else
disp (Too cold — turn off equipment.’)
end
switch expression
case fest expression 1
commands
case fest expression 2
commands
otherwise
commands
end
18 ‘Multimedia Systems (eadeli@tust.ac.ir)
صفحه 19:
Loops
for variable = expression for t = 1:5000 ۲
commands y(t) = sin (2*pi*t/10);
end
end
EPs = 1;
while expression. while ( 1+EPS) >1
commands EPS = EPS/2;
end
2 EPS = 2*EPS
» break - is used to terminate the
execution of the loop.
‘Multimedia Systems (eadeli@tust.ac.ir)
صفحه 20:
M-Files
» A M-file is a group of MATLAB commands.
» MATLAB can open and execute the
commands exactly as if they were entered at
the MATLAB command window.
۶ To run the M-files, just type the file name in
the command window. (make sure the current
working directory is set correctly)
20 Multimedia Systems (eadeli@iust.ac.ir)
صفحه 21:
User Defined Functions
» Add the following command in the beginning
of your m-file:
> function [output variables] =
function_name (input variables);
» Note that the file name should be the same as
the function name.
21 Multimedia Systems (eadeli@iust.ac.ir)
صفحه 22:
Data types in MATLAB
Double (64-bit double-
precision floating point)
Single (32-bit single-
precision floating point)
Int32 (32-bit signed integer)
Int16 (16-bit signed integer)
Int8 (8-bit signed integer)
Uint32 (32-bit unsigned
integer)
Uint16 (16-bit unsigned
integer)
Uint8 (8-bit unsigned
integer)
Images in MATLAB
< MATLAB can import/export
several image formats
BMP (Microsoft Windows Bitmap)
GIF (Graphics Interchange Files)
HDF (Hierarchical Data Format)
JPEG (Joint Photographic Experts
Group)
PCX (Paintbrush)
PNG (Portable Network Graphics)
TIFF (Tagged Image File Format)
XWD (X Window Dump)
MATLAB can also load raw-data or
other types of image data
صفحه 23:
Images in MATLAB
* Binary images : {0,1}
* Intensity images : [0,1] or uint8, double etc.
* RGB images : m-by-n-by-3
* Indexed images : m-by-3 color map
* Multidimensional images m-by-n-by-p (p is the number of
layers)
صفحه 24:
Image import and export
» Read and write images in Matlab
>> I=imread(‘cells.jpg');
>> imshow(1)
>> size(I)
ans= 479 600 3 (RGB image)
>> Igrey=rgb2gray(1);
>> imshow(Igrey)
>> imwrite(Igrey, ‘cell_gray.tif’, 'tiff)
Alternatives to imshow
>>imagesc(1)
>>imtool(1)
>>image(!)
صفحه 25:
Images and Matrices
> How to build a matrix (or image)?
>>A=[123;456;789];
A=1 2 3
4 5 6
7 8 9
>> B = zeros(3,3)
B= 0 0 0
0 0 0
0 0 0
>> C = ones(3,3)
C= 1 1 1
1 1 1
1 1 1
>>imshow(A) (imshow(A,[]) to get automatic pixel
range)
صفحه 26:
Image format conversion
» Convert between intensity/indexed/RGB to binary
format:
dither()
» Convert between intensity format to indexed format:
gray2ind()
» Convert between indexed format to intensity format:
ind2gray()
» Convert between indexed format to RGB format:
ind2rgb()
» Convert a regular matrix to intensity format by
scaling:
mat2gray()
» Convert between RGB format to intensity format:
rgb2gray()
» Convert between RGB format to indexed format:
rgb2ind()
> 26 ‘Multimedia Systems (eadeli@tust.ac.ir)
صفحه 27:
Colormaps
» I = imread (‘lena.bmp’);
> imshow (I);
» colormap (cool);
» Autumn, bone, colorcube, cool, copper, flag,
gray, hot, hsv, jet, pink, prism, spring,
summer, white, winter
27 ‘Multimedia Systems (eadeli@tust.ac.ir)
صفحه 28:
Video
>» mov = aviread(filename)
>» mov = aviread(filename, index)
> fileinfo = aviinfo(filename)
Multimedia Systems (eadeli@iust.ac.ir)
صفحه 29:
Digital Audio
cP ae
y y Micon
9 Digi Sigua
|
Voltage Wave!
vol vo
My be
1s1g2 conde
> 29 ‘Multimedia Systems (eadeli@tust.ac.ir)
صفحه 30:
Audio
> [road,fs]=wavread('road.wav');
» The array road now contains the stereo
sound data and fs is the sampling frequency.
> This data is sampled at the same rate as that
on a music CD (fs=44,100 samples/second).
> The left and right channel signals are the two
columns of the road array:
> left=road(:,1);
> right=road(:,2);
30
صفحه 31:
> time=(1/44100)*length(left);
» t=linspace(0,time,length(left));
> plot(t,left)
> xlabel('time (sec)');
» ylabel(‘relative signal strength’)
> soundsc(left,fs) % plays left channel as
mono
» soundsc(right,fs) % plays right channel mono
(sound nearly the same)
> soundsc(road,fs) % plays stereo (ahhh...)
31 Multimedia Systems (eadeli@iust.ac.ir)