You are on page 1of 17

Introduction to Signal

Processing Using MATLAB


MATLAB
• MATLAB ( Matrix Laboratory )
• A numerical computing environment and programming
language
• It allows
– Easy matrix manipulation
– Plotting of function and data
– Implementation of algorithms
– Creation of user interfaces (GUIs)
– Interfacing with programs in other languages.
• Simulink
– an additional package that adds graphical multidomain
simulation and Model-Based design for dynamic and embedded
systems.
Variables
– Variables are defined with the assignment operator, =.
– MATLAB is dynamically typed, meaning that variables can be assigned
without declaring their type.
– Values can come from constants, from computation involving values of
other variables, or from the output of a function.
For example:

• >> x = 17
x = 17

• >> x = 'hat'
x = hat

• >> x = [3*4, pi/2]


x = 12.0000 1.5708

• >> y = 3*sin(x)
y = -1.6097 3.0000
Vectors and Matrices
• MATLAB provides many convenient ways for
creating vectors, matrices, and multi-
dimensional arrays.
• A vector refers to a one dimensional (1×N or
N×1) matrix.
• A matrix generally refers to a 2-dimensional
array, i.e. an m×n array where m and n are
greater than 1.
• Arrays with more than two dimensions are
referred to as multidimensional arrays.
Vectors and Matrices
• Matrices can be defined by separating the elements of a row with
blank space or comma and using a semicolon to terminate each row
surrounded by square bracket.
• Syntax : [ init : increment : terminator]
>> array = 1: 2 : 9
array = 1 3 5 7 9

it starts at
1 (the init value),
increments with each step from the previous value by
2 (the increment value )
and stops once it reaches
9.
Vectors and Matrices
• >> A = [1 5]
A=1 5

• >> A = [1 , 5]
A= 1 5

• >> A = [1 : 5]
A=1 2 3 4 5
Vectors and Matrices
• Adding a single quote to a vector gives transpose of the matrix

>> A = [1 : 5]'

A=
1
2
3
4
5

• >> A = [1 ; 5]

A= 1
5
Addition of Matrices
• MATLAB is CASE-SENSITIVE
>> A = [1 1 1 ; 1 2 3 ;1 3 6]
A=
1 1 1
1 2 3
1 3 6

>> b = [1 1 1;1 2 7;1 2 5]


b=
1 1 1
1 2 7
1 2 5
>> A+B
??? Undefined function or variable 'B'.

>> a+B
??? Undefined function or variable 'a'.

>> A+b

ans =
2 2 2
2 4 10
2 5 11
Vector Products
• A row vector and a column vector of the same length can be multiplied in
either order. The result is either a scalar, the inner product, or a matrix, the
outer product.
u = [3; 1; 4];
v = [2 0 -1];
x = v*u

x=
2

X = u*v

X=
6 0 -3
2 0 -1
8 0 -4
Vector Transpose
• Transposition turns a row vector into a column vector.

• For a complex vector or matrix, z, the quantity z' denotes the complex
conjugate transpose, where the sign of the complex part of each element is
reversed. The unconjugated complex transpose, where the complex part of
each element retains its sign, is denoted by z.'. So if

z = [1+2i 3+4i]

then z' is
1-2i
3-4i

while z.' is
1+2i
3+4i
Positive Integer Powers
• If A is a square matrix and p is a positive integer, then
A ^ p effectively multiplies A by itself p-1 times.
For example
>> A = [1 1 1;1 2 3;1 3 6]
A=
1 1 1
1 2 3
1 3 6

>> X = A^2

X=
3 6 10
6 14 25
10 25 46
Operating Element-by-Element

• Element-by-element arithmetic operations are


called array operations. To indicate an array
operation in MATLAB, precede the operator with
a period (.). Addition and subtraction, and matrix
multiplication and division by a scalar, are
already array operations so no period is
necessary.
• When using array operations on two matrices,
the dimensions of the matrices must be the
same.
Dot Operator
• Element-by-Element Powers
• The .^ operator produces element-by-element powers.
For example
>> A = [1 1 1;1 2 3;1 3 6]
A=
1 1 1
1 2 3
1 3 6
>>X = A.^2

A=
1 1 1
1 4 9
1 9 36
The simplest way to execute M-code is to
type it in at the prompt, >> in the
Command Window, one of the elements of
the MATLAB Desktop
M-Files

• You can create your own matrices using M-files, which


are text files containing MATLAB code. Use the MATLAB
Editor or another text editor to create a file containing the
same statements you would type at the MATLAB
command line. Save the file under a name that ends
in .m.
For example, create a file containing these five lines:
A = [ ...
16.0 3.0 2.0 13.0
5.0 10.0 11.0 8.0
9.0 6.0 7.0 12.0
4.0 15.0 14.0 1.0 ];
M-Files
• To write a function in MATLAB , begin the statement with
function
For Example
f(x1,x2) = x12 – 2x1x2+ 6x1+x22- 6x2
• The M-file that computes this function must accept a
row-vector x of length 2 , corresponding to the variable x1
and x2,and return a scalar equal to the value of the
function at x
function z = my_func(x)
z = x(1)^2 – 2*x(1)*x(2) + 6*x(1) + x(2)^2-6 * x(2);

>> my_func([2 3])


THANK YOU !

You might also like