You are on page 1of 58

(ECE 357)

DIGITAL SIGNAL PROCESSING LAB


A
PRACTICAL FILE
submitted to the Faculty of Engineering of the Department of
Electronics and Communication Engineering, Punjabi University, Patiala

BACHELOR OF TECHNOLOGY
IN
ELECTRONICS AND COMMUNICATION

Submitted by

Roll No: _____________________________ Name: ________________________________

Dr. Reecha Sharma Er. Jaspreet Singh Kaleka


Assistant Professor Assistant Professor
Department of Electronics and Department of Electronics and
Communication Engineering Communication Engineering
Punjabi University, Patiala Punjabi University, Patiala

Department of Electronics and Communication Engineering,


Punjabi University, Patiala-147001, Punjab (India)
(May 2018)
EXPERIMENTS

1. TO STUDY OF BASIC COMMANDS OF MATLAB 1

CREATING VARIABLES 1
GENERAL COMMANDS 1
COMMANDS RELATED TO WORKSPACE 2
ARITHMETIC OPERATORS 3
RELATIONAL OPERATORS 9
LOGICAL OPERATORS 12
SPECIAL CHARACTERS 14
SPECIAL VALUES 18
ELEMENTARY MATHEMATICAL FUNCTIONS 19
TRIGONOMETRIC FUNCTIONS 20
DATA ANALYSIS FUNCTIONS 21
POLYNOMIAL RELATED FUNCTIONS 24
VECTOR OR MATRIX RELATED FUNCTIONS 25
1D AND 2D GRAPHICAL COMMANDS 29
3D GRAPHICAL COMMANDS 39
CONTROL COMMANDS 41

2. UNIT STEP, UNIT RAMP, EXPONENTIAL & ADDITION OF TWO SEQUENCES 43

UNIT SAMPLE SIGNAL 43


UNIT STEP SIGNAL 43
UNIT RAMP SIGNAL 44
REAL-VALUED EXPONENTIAL SIGNAL 45
COMPLEX-VALUED EXPONENTIAL SIGNAL 45
SINUSOIDAL SIGNAL 46
SIGNAL ADDITION. Y[N] = X1[N] + X2[N] 47

3. CONVOLUTION SUM OF DISCRETE SIGNALS 48

SIGNAL X 48
SIGNAL Y 48
CONVOLUTION SUM 48

4. CROSS-CORRELATION OF TWO SEQUENCES 49

SEQUENCES - X, Y 49
CROSS-CORRELATION USING 'XCORR' FUNCTION 49
CROSS-CORRELATION WITHOUT 'XCORR' FUNCTION 49

5. FREQUENCY RESPONSE OF DISCRETE SYSTEM 50

TRANSFER FUNCTION COEFFICIENTS - B, A 50


FREQUENCY RESPONSE AT 101 POINTS SPANNING THE COMPLETE UNIT CIRCLE 50
6. DISCRETE FOURIER TRANSFORM (DFT) OF THE GIVEN SEQUENCE 51

SEQUENCE X 51
TWIDDLE MATRIX FOR N-POINTS 51
DFT 51
IDFT 51

7. POLES, ZEROS AND GAIN OF DISCRETE-TIME TRANSFER FUNCTION 52

TRANSFER FUNCTION COEFFICIENTS - B, A 52


FIND ZEROS, POLES AND GAIN 52
PLOT ZEROS & POLES 52

8. MAGNITUDE & PHASE OF DISCRETE FOURIER TRANSFORM (DFT) 53

SEQUENCE X 53
TWIDDLE MATRIX FOR N-POINTS 53
DFT 53
MAGNITUDE & PHASE OF DFT 53

9. TO STUDY THE MAGNITUDE & PHASE RESPONSE OF FIR FILTER 54

DESIGN A FIR LOWPASS FILTER 54


DISPLAY THE MAGNITUDE AND PHASE RESPONSES OF THE FILTER 54

10. TO STUDY THE MAGNITUDE & PHASE RESPONSE OF IIR FILTER 55

DESIGN AN IIR LOWPASS FILTER 55


DISPLAY THE MAGNITUDE AND PHASE RESPONSES OF THE FILTER 55
FILTER COEFFICIENTS, EXPRESSED AS SECOND-ORDER SECTIONS 55
TRANSFER FUNCTION FILTER COEFFICIENTS 55
1. TO STUDY OF BASIC COMMANDS OF MATLAB

Creating Variables
The MATLAB language lets you construct commands to create and process variables. You can create
variables by entering them in the command window here. For example,

% Default datatype for numbers is 'double'.


A = 10; % A is a VARIABLE of type double.
A = [1 2 3]; % A is a VECTOR of type double.
A = [1 2 ; 3 4]; % A is a MATRIX of type double.
B = 1i; % B is a VARIABLE of type double (complex).
B = [1 2i 3]; % B is a VECTOR of type double (complex).
B = [1 2 ; 3i 4]; % B is a MATRIX of type double (complex).

C = int8(10); % C is a VARIABLE of type int8.


C = int16([1 2 3]); % C is a VECTOR of type int16.
C = int32([1 2 ; 3 4]); % C is a MATRIX of type int32.

D = uint8(10); % D is a VARIABLE of type uint8.


D = uint16([1 2 3]); % D is a VECTOR of type uint16.
D = uint32([1 2 ; 3 4]); % D is a MATRIX of type uint32.

E = single(10); % E is a VARIABLE of type single.


E = single([1 2 3]); % E is a VECTOR of type single.
E = single([1 2 ; 3 4]); % E is a MATRIX of type single.

F = 'Hello World!'; % F is a STRING of type char.


F = ['a' 'b' 'c']; % F is a VECTOR of type char.
F = ['a', 'b'; 'c', 'd']; % F is a MATRIX of type char.

General Commands
who List variables in workspace.

who

Your variables are:


A B C D E F N WN a b x y

whos List variables in workspace, with sizes and types.

whos

Name Size Bytes Class Attributes


A 2x2 32 double
B 2x2 64 double complex
C 2x2 16 int32
D 2x2 16 uint32
E 2x2 16 single
F 2x2 8 char
N 1x1 8 double
WN 4x4 256 double complex
a 1x1 8 double
b 1x1 8 double
x 1x4 32 double
y 4x1 64 double complex

Page | 1
what List MATLAB files in folder.

what

MATLAB Code files in the current folder C:\Code


Exp1 Exp2 Exp4 Exp6 Exp8 Main
Exp10 Exp3 Exp5 Exp7 Exp9

size Provides the size of a data array.

size([1 2 3; 4 5 6])

ans =
2 3

length Provides the size of a data vector.

length([10 20 30])

ans =
3

help Help for functions in Command Window.

help sin

SIN Sine of argument in radians.


SIN(X) is the sine of the elements of X.
See also ASIN, SIND.
Reference page in Doc Center
doc sin

Commands Related to Workspace


dir Lists the files and folders in the MATLAB current folder.

dir

. Exp1.m Exp2.m Exp4.m Exp6.m Exp8.m Main.m


.. Exp10.m Exp3.m Exp5.m Exp7.m Exp9.m html

save Save workspace variables to file.

save MyFile.mat

load Load data from MAT-file into workspace.

load MyFile.mat

delete Remove files or graphics objects.

delete MyFile.mat

Page | 2
Arithmetic Operators
+ Addition or unary plus.

A = [1 2 3; 4 5 6]
B = [10 20 30; 40 50 60]

A + B
A + 10
10 + A

A =
1 2 3
4 5 6
B =
10 20 30
40 50 60
ans =
11 22 33
44 55 66
ans =
11 12 13
14 15 16
ans =
11 12 13
14 15 16

- Subtraction or unary minus.

A = [1 2 3; 4 5 6]
B = [10 20 30; 40 50 60]

A - B
A - 10
10 - A
-A

A =
1 2 3
4 5 6
B =
10 20 30
40 50 60
ans =
-9 -18 -27
-36 -45 -54
ans =
-9 -8 -7
-6 -5 -4
ans =
9 8 7
6 5 4
ans =
-1 -2 -3
-4 -5 -6

Page | 3
* Matrix multiplication.

A = [1 2 3; 4 5 6]
B = [10 20; 30 40; 50 60]

A * B
A * 10
10 * A

A =
1 2 3
4 5 6
B =
10 20
30 40
50 60

ans =
220 280
490 640

ans =
10 20 30
40 50 60

ans =
10 20 30
40 50 60

.* Array multiplication (element-by-element product).

A = [1 2 3; 4 5 6]
B = [10 20 30; 40 50 60]

A .* B
A .* 10
10 .* A

A =
1 2 3
4 5 6
B =
10 20 30
40 50 60

ans =
10 40 90
160 250 360

ans =
10 20 30
40 50 60

ans =
10 20 30
40 50 60

Page | 4
/ Slash or matrix right division.

A = [1 2 3; 4 5 6; 7 8 9]
B = [10 20 30; 40 50 60; 70 80 90]

% A/B is roughly the same as A * inv(B)


A / B
A / 10

% "10/A" is not allowed, unless A is 1x1 matrix.


% 10 / A

A =
1 2 3
4 5 6
7 8 9
B =
10 20 30
40 50 60
70 80 90

ans =
0.1000 0 0.0000
0.0500 0 0.0500
0 0 0.1000
ans =
0.1000 0.2000 0.3000
0.4000 0.5000 0.6000
0.7000 0.8000 0.9000

./ Array right division (element-by-element division).

A = [1 2 3; 4 5 6]
B = [10 20 30; 40 50 60]

A ./ B
A ./ 10
10 ./ A

A =
1 2 3
4 5 6
B =
10 20 30
40 50 60

ans =
0.1000 0.1000 0.1000
0.1000 0.1000 0.1000
ans =
0.1000 0.2000 0.3000
0.4000 0.5000 0.6000
ans =
10.0000 5.0000 3.3333
2.5000 2.0000 1.6667

Page | 5
\ Backslash or matrix left division.

A = [1 2 3; 4 5 6; 7 8 9]
B = [10 20 30; 40 50 60; 70 80 90]

% A\B is roughly the same as inv(A) * B


A \ B
10 \ A

% "A\10" is not allowed, unless A is 1x1 matrix.


% A \ 10

A =
1 2 3
4 5 6
7 8 9
B =
10 20 30
40 50 60
70 80 90

ans =
10.0000 32.0000 -10.0000
0 -54.0000 20.0000
0 32.0000 0
ans =
0.1000 0.2000 0.3000
0.4000 0.5000 0.6000
0.7000 0.8000 0.9000

.\ Array left division (element-by-element division).

A = [1 2 3; 4 5 6]
B = [10 20 30; 40 50 60]

A .\ B
A .\ 10
10 .\ A

A =
1 2 3
4 5 6
B =
10 20 30
40 50 60

ans =
10 10 10
10 10 10
ans =
10.0000 5.0000 3.3333
2.5000 2.0000 1.6667
ans =
0.1000 0.2000 0.3000
0.4000 0.5000 0.6000

Page | 6
^ Matrix power.

A = [1 2; 3 4]
B = [10 20 30 40]

% Not allowed. Both A and B cannot be matrices. One must be scalar.


% A ^ B

% A to the power 3. Equivalent to A*A*A.


A ^ 3

% A to the power -1. Equivalent to inv(A).


A ^ -1

% B to the power 3. Equivalent to B*B*B.


% Error occurs as B is not a square matrix.
% B ^ 3

A =
1 2
3 4
B =
10 20 30 40
ans =
37 54
81 118
ans =
-2.0000 1.0000
1.5000 -0.5000

.^ Array power (element-by-element power).

A = [1 2; 3 4]
B = [5 6; 7 8]

A .^ B
A .^ 3
3 .^ A

A =
1 2
3 4
B =
5 6
7 8
ans =
1 64
2187 65536
ans =
1 8
27 64
ans =
3 9
27 81

Page | 7
' Matrix transpose (complex conjugate transpose).

A = [1 2 3; 4 5 6]
B = [1+i 2 3; -4i 5 6-i]

A'
B'

A =
1 2 3
4 5 6

B =
1.0000 + 1.0000i 2.0000 + 0.0000i 3.0000 + 0.0000i
0.0000 - 4.0000i 5.0000 + 0.0000i 6.0000 - 1.0000i

ans =
1 4
2 5
3 6

ans =
1.0000 - 1.0000i 0.0000 + 4.0000i
2.0000 + 0.0000i 5.0000 + 0.0000i
3.0000 + 0.0000i 6.0000 + 1.0000i

.' Array transpose (only transpose).

A = [1 2 3; 4 5 6]
B = [1+i 2 3; -4i 5 6-i]

A.'
B.'

A =
1 2 3
4 5 6

B =
1.0000 + 1.0000i 2.0000 + 0.0000i 3.0000 + 0.0000i
0.0000 - 4.0000i 5.0000 + 0.0000i 6.0000 - 1.0000i

ans =
1 4
2 5
3 6

ans =
1.0000 + 1.0000i 0.0000 - 4.0000i
2.0000 + 0.0000i 5.0000 + 0.0000i
3.0000 + 0.0000i 6.0000 - 1.0000i

Page | 8
Relational Operators
< Less than.

X = [1 2 3; 4 5 6]
Y = [6 5 4; 3 2 1]

X < Y
X < 3
3 < X

X =
1 2 3
4 5 6
Y =
6 5 4
3 2 1

ans =
1 1 1
0 0 0
ans =
1 1 0
0 0 0
ans =
0 0 0
1 1 1

<= Less than or equal to.

X = [1 2 3; 4 5 6]
Y = [6 5 4; 3 2 1]

X <= Y
X <= 3
3 <= X

X =
1 2 3
4 5 6
Y =
6 5 4
3 2 1

ans =
1 1 1
0 0 0
ans =
1 1 1
0 0 0
ans =
0 0 1
1 1 1

Page | 9
> Greator than.

X = [1 2 3; 4 5 6]
Y = [6 5 4; 3 2 1]

X > Y
X > 3
3 > X

X =
1 2 3
4 5 6
Y =
6 5 4
3 2 1

ans =
0 0 0
1 1 1
ans =
0 0 0
1 1 1
ans =
1 1 0
0 0 0

>= Greator than or equal to.

X = [1 2 3; 4 5 6]
Y = [6 5 4; 3 2 1]

X >= Y
X >= 3
3 >= X

X =
1 2 3
4 5 6
Y =
6 5 4
3 2 1

ans =
0 0 0
1 1 1
ans =
0 0 1
1 1 1
ans =
1 1 1
0 0 0

Page | 10
== Equal to.

X = [1 2 3; 4 5 6]
Y = [6 5 4; 3 2 1]

X == Y
X == 3
3 == X

X =
1 2 3
4 5 6
Y =
6 5 4
3 2 1

ans =
0 0 0
0 0 0
ans =
0 0 1
0 0 0
ans =
0 0 1
0 0 0

~= Not equal to.

X = [1 2 3; 4 5 6]
Y = [6 5 4; 3 2 1]

X ~= Y
X ~= 3
3 ~= X

X =
1 2 3
4 5 6
Y =
6 5 4
3 2 1

ans =
1 1 1
1 1 1
ans =
1 1 0
1 1 1
ans =
1 1 0
1 1 1

Page | 11
Logical Operators
&& Logical AND.

X = [1 2 3; 4 5 6]
Y = [6 5 4; 3 2 1]

% Error. Both X and Y must be scalar values or any expression yielding scalar values.
% X && Y

2 && 5
-1 && 7
0 && 9

ismatrix(X) && isvector(Y)

X =
1 2 3
4 5 6
Y =
6 5 4
3 2 1
ans =
1
ans =
1
ans =
0
ans =
0

|| Logical OR.

X = [1 2 3; 4 5 6]
Y = [6 5 4; 3 2 1]

% Error. Both X and Y must be scalar values or any expression yielding scalar values.
% X || Y

2 || 5
-1 || 7
0 || 0
ismatrix(X) || isvector(Y)

X =
1 2 3
4 5 6
Y =
6 5 4
3 2 1
ans =
1
ans =
1
ans =
0
ans =
1

Page | 12
& Element-wise logical AND.

X = [1 2 -3; 4 5 0]
Y = [0 5 0; -1 2 0]

X & Y
X & 7
7 & X

X =
1 2 -3
4 5 0

Y =
0 5 0
-1 2 0

ans =
0 1 0
1 1 0
ans =
1 1 1
1 1 0
ans =
1 1 1
1 1 0

| Element-wise logical OR.

X = [1 2 -3; 4 5 0]
Y = [0 5 0; -1 2 0]

X | Y
X | 0
0 | X

X =
1 2 -3
4 5 0

Y =
0 5 0
-1 2 0

ans =
1 1 1
1 1 0
ans =
1 1 1
1 1 0
ans =
1 1 1
1 1 0

Page | 13
~ Logical NOT.

X = [1 2 -3; 4 5 0]

~X
~1
~0

X =
1 2 -3
4 5 0

ans =
0 0 0
0 0 1
ans =
0
ans =
1

Special Characters
= Variable assignment operator.

x = 10

x =
10

... Ellipsis (line continuation operator).

x ...
= ...
10

x =
10

% Single-Line Comments.

% This is a comment.

%{ %} Multi-Line Comment.

%{
This is a multi-line comment.

The %{ and %} symbols enclose a block


of comments that extend beyond one line.
%}

Page | 14
[] Array construction and concatenation. Declaring and capturing values returned by a function.

% Array Constructor.
A = [1 2 3]
B = [4 5 6]
X = [1 2; 3 4]

% Concatenation.
C = [A , B]
C = [A ; B]

% Function Declarations and Calls.


[Val, Vec] = eig(X)

A =
1 2 3
B =
4 5 6
X =
1 2
3 4
C =
1 2 3 4 5 6
C =
1 2 3
4 5 6
Val =
-0.8246 -0.4160
0.5658 -0.9094
Vec =
-0.3723 0
0 5.3723

, A comma is used to separate the following types of elements.

% Row Element Separator.


A = [10, 20, 30]

% Array Index Separator. Used to separate the indices into each dimension.
X = A(1, 2)

% Function Input and Output Separator. Used to separate output and input arguments.
% function [oArg1, oArg2] = AnyName(iArg1, iArg2, iArg1)

% Command or Statement Separator. To enter more than one MATLAB command in same line.
x = 10, y = 20, z = 30

A =
10 20 30
X =
20
x =
10
y =
20
z =
30

Page | 15
: The colon operator generates a sequence of numbers that you can use in creating or indexing into
arrays.

% Numeric Sequence Range


% Generate a sequential series of regularly spaced numbers from
% first to last using the syntax "first:last".
N = 6:17

% Numeric Sequence Step


% Generate a sequential series of numbers, each number
% separated by a step value, using the syntax first:step:last.
% For a sequence from 2 through 38, stepping by 4 between each entry, use:-
N = 2:4:38

% Indexing Range Specifier.


% Use to specify a range of indices.

A = magic(10)

% Read columns 1-5 of row 7.


B = A(7, 1:5)

% Read columns 1-5 of rows 4, 6, and 8.


B = A(4:2:8, 1:5)

% Read columns 1-5 of all rows.


B = A(:, 1:5)

A = magic(3)

% Conversion to Column Vector.


B = A(:)

% Preserving Array Shape on Assignment.


% Using the colon operator on the left side of an assignment
% statement, we can assign new values to array elements without
% changing the shape of the array.
A(:) = 1:9

N =
6 7 8 9 10 11 12 13 14 15 16 17
N =
2 6 10 14 18 22 26 30 34 38

A =
92 99 1 8 15 67 74 51 58 40
98 80 7 14 16 73 55 57 64 41
4 81 88 20 22 54 56 63 70 47
85 87 19 21 3 60 62 69 71 28
86 93 25 2 9 61 68 75 52 34
17 24 76 83 90 42 49 26 33 65
23 5 82 89 91 48 30 32 39 66
79 6 13 95 97 29 31 38 45 72
10 12 94 96 78 35 37 44 46 53
11 18 100 77 84 36 43 50 27 59

B =
23 5 82 89 91

Page | 16
B =
85 87 19 21 3
17 24 76 83 90
79 6 13 95 97
B =
92 99 1 8 15
98 80 7 14 16
4 81 88 20 22
85 87 19 21 3
86 93 25 2 9
17 24 76 83 90
23 5 82 89 91
79 6 13 95 97
10 12 94 96 78
11 18 100 77 84
A =
8 1 6
3 5 7
4 9 2
B =
8
3
4
1
5
9
6
7
2
A =
1 4 7
2 5 8
3 6 9

; The semicolon can be used to construct arrays, suppress output from a MATLAB command, or to
separate commands entered on the same line.

% Array Row Separator (creates a new row in the array).


A = [5, 8; 3, 4]

% Output Suppression. When placed at the end of a command, the semicolon tells MATLAB
% not to display any output from that command.
A = [10, 20; 30, 40]

% Command or Statement Separator. Like the comma operator, you can enter more than one MATLAB
% command in one line by separating each command with a semicolon. MATLAB suppresses output
for those commands terminated with a semicolon, and displays the output for commands
terminated with a comma.
x = 10; y = 20, z = 30;

A =
5 8
3 4
A =
10 20
30 40
y =
20

Page | 17
Special Values
ans Most recent answer. In no output variable is present, MATLAB by default stores the result in ans.

10 + 20

ans =
30

pi Ratio of circle's circumference to its diameter.

pi

ans =
3.1416

i, j Imaginary unit (Value of square root of –1).

x = 1 + 2i
y = 1 - 2j

x =
1.0000 + 2.0000i
y =
1.0000 - 2.0000i

Inf IEEE arithmetic representation for positive infinity.

1 / 0
exp(1000)
log(0)
Inf(1,3)

ans =
Inf
ans =
Inf
ans =
-Inf
ans =
Inf Inf Inf

NaN IEEE arithmetic representation for Not-a-Number.

0 / 0
Inf / Inf
NaN(2,3)

ans =
NaN
ans =
NaN
ans =
NaN NaN NaN
NaN NaN NaN

Page | 18
Elementary Mathematical Functions
abs Absolute value and complex magnitude.

abs(-5)
abs(3 + 4i)

ans =
5
ans =
5

sqrt Square root.

sqrt(2)

ans =
1.4142

real Real part of complex number.

real(3 + 4i)

ans =
3

imag Imaginary part of complex number.

imag(3 + 4i)

ans =
4

angle Phase angle of a complex number. (In Radians)

angle(3 + 4i)

ans =
0.9273

conj Complex conjugate.

conj(3 + 4i)

ans =
3.0000 - 4.0000i

sign Signum function.

sign([-2 -3 1 0 5 7])

ans =
-1 -1 1 0 1 1

Page | 19
rem Remainder after division.

rem(101, 10)

ans =
1

exp Exponential.

exp(-pi/2)

ans =
0.2079

log Natural logarithm.

log(-1)

ans =
0.0000 + 3.1416i

log10 Common (base 10) logarithm.

log10(-1)

ans =
0.0000 + 1.3644i

Trigonometric Functions
sin, cos, tan, csc, sec, cot All in radians.

X = [0, 30, 45, 60, 90];


sin(X)

ans =
0 -0.9880 0.8509 -0.3048 0.8940

sind, cosd, tand, cscd, secd, cotd All in degrees.

X = [0, 30, 45, 60, 90];


sind(X)

ans =
0 0.5000 0.7071 0.8660 1.0000

sinh, cosh, tanh, csch, sech, coth Hyperbolic. All in radians.

X = [0, 30, 45, 60, 90];


sinh(X)

ans =
1.0e+38 *
0 0.0000 0.0000 0.0000 6.1020

Page | 20
asin, acos, atan, acsc, asec, acot Inverse. All in radians.

X = [0, 30, 45, 60, 90];


asin(X)

ans =
0.0000+0.0000i 1.5708-4.0941i 1.5708-4.4997i 1.5708-4.7874i 1.5708-5.1929i

asind, acosd, atand, acscd, asecd, acotd Inverse. All in degrees.

X = [0, 30, 45, 60, 90];


asind(X)

ans =
1.0e+02 *
0.0000+0.0000i 0.9000-2.3457i 0.9000-2.5781i 0.9000-2.7430i 0.9000-2.9753i

asinh, acosh, atanh, acsch, asech, acoth Hyperbolic inverse. All in radians.

X = [0, 30, 45, 60, 90];


asinh(X)

ans =
0 4.0946 4.4999 4.7876 5.1930

hypot Square root of sum of squares.

hypot(3, 4)

ans =
5

Data Analysis Functions


max Largest elements in array.

X = [2 8 6]
max(X)

X = [2 8 6; 7 3 9; 5 1 4]
max(X)

X =
2 8 6
ans =
8

X =
2 8 6
7 3 9
5 1 4
ans =
7 8 9

Page | 21
min Smallest elements in array.

X = [2 8 6]
min(X)
X = [2 8 6; 7 3 9; 5 1 4]
min(X)

X =
2 8 6
ans =
2
X =
2 8 6
7 3 9
5 1 4
ans =
2 1 4

mean Average or mean value of array.

X = [3 6 9]
mean(X)
X = [3 2 5; 6 4 10; 9 6 15]
mean(X)

X =
3 6 9
ans =
6
X =
3 2 5
6 4 10
9 6 15
ans =
6 4 10

std Standard deviation.

X = [3 6 9]
std(X, 0)
std(X, 1)

X =
3 6 9
ans =
3
ans =
2.4495

sum Sum of array elements.

X = [3 6 9]
sum(X)

X = [3 2 5; 6 4 10; 9 6 15]
sum(X)

Page | 22
X =
3 6 9
ans =
18
X =
3 2 5
6 4 10
9 6 15
ans =
18 12 30

cumsum Cumulative sum.

X = [3 6 9]
cumsum(X)
X = [3 2 5; 6 4 10; 9 6 15]
cumsum(X)

X =
3 6 9
ans =
3 9 18
X =
3 2 5
6 4 10
9 6 15
ans =
3 2 5
9 6 15
18 12 30

prod Product of array elements.

X = [3 6 9]
prod(X)
X = [3 2 5; 6 4 10; 9 6 15]
prod(X)

X =
3 6 9
ans =
162
X =
3 2 5
6 4 10
9 6 15
ans =
162 48 750

cumprod Cumulative product.

X = [3 6 9]
cumprod(X)

X =
3 6 9
ans =
3 18 162

Page | 23
Polynomial Related Functions
conv Convolution and polynomial multiplication.

u = [1 3 5 7 9]
v = [2 4 6 8]
conv(u, v)

u =
1 3 5 7 9
v =
2 4 6 8
ans =
2 10 28 60 100 118 110 72

deconv Deconvolution and polynomial division.

u = [1 3 5 7 9]
v = [2 4 6 8]
c = conv(u, v)

deconv(c, v) % Use deconvolution to recover u.


deconv(c, u) % Use deconvolution to recover v.

u =
1 3 5 7 9
v =
2 4 6 8
c =
2 10 28 60 100 118 110 72
ans =
1 3 5 7 9
ans =
2 4 6 8

roots Polynomial roots.

p = [1 -6 -72 -27] % Polynomial: s^3 – 6s^2 – 72s – 27


roots(p)

p =
1 -6 -72 -27
ans =
12.1229
-5.7345
-0.3884

polyval Polynomial evaluation.

p = [1 -6 -7 5] % Polynomial: s^3 – 6s^2 – 7s + 5


polyval(p, [-1 0 1]) % p is evaluated at s = -1, 0 and 1.

p =
1 -6 -7 5
ans =
5 5 -7

Page | 24
Vector or Matrix Related Functions
zeros Create array of all zeros.

zeros(2)
zeros(2, 3)

ans =
0 0
0 0
ans =
0 0 0
0 0 0

ones Create array of all ones.

ones(2)
ones(2, 3)

ans =
1 1
1 1
ans =
1 1 1
1 1 1

eye Identity matrix.

eye(3)
eye(2, 3)

ans =
1 0 0
0 1 0
0 0 1

ans =
1 0 0
0 1 0

rand Uniformly distributed pseudorandom numbers drawn from the standard uniform distribution
on the open interval (0,1).

rand(2)
rand(2, 3)

a = 1; b = 10;
a + (b-a) .* rand(1, 5)

ans =
0.8147 0.1270
0.9058 0.9134
ans =
0.6324 0.2785 0.9575
0.0975 0.5469 0.9649
ans =
4.7959 9.2416 8.1299 9.6354 6.9017

Page | 25
randn Normally distributed pseudorandom numbers drawn from the standard Normal (Gaussian)
distribution.

randn(2)
randn(2, 3)

% Values from a normal distribution with mean 1 and standard deviation 2.


1 + 2.* randn(1, 5)

ans =
-1.2075 1.6302
0.7172 0.4889
ans =
1.0347 -0.3034 -0.7873
0.7269 0.2939 0.8884
ans =
-0.5099 3.7406 -2.4230 0.7955 0.5171

linspace Generate linearly spaced vectors.

linspace(10, 20) % Generates 100 points LINEARLY spaced between and including 10 and 20.

linspace(11, 20, 10) % Generates 10 points LINEARLY spaced between and including 11 and 20.

ans =
Columns 1 through 7
10.0000 10.1010 10.2020 10.3030 10.4040 10.5051 10.6061
Columns 8 through 14
10.7071 10.8081 10.9091 11.0101 11.1111 11.2121 11.3131
Columns 15 through 21
11.4141 11.5152 11.6162 11.7172 11.8182 11.9192 12.0202
Columns 22 through 28
12.1212 12.2222 12.3232 12.4242 12.5253 12.6263 12.7273
Columns 29 through 35
12.8283 12.9293 13.0303 13.1313 13.2323 13.3333 13.4343
Columns 36 through 42
13.5354 13.6364 13.7374 13.8384 13.9394 14.0404 14.1414
Columns 43 through 49
14.2424 14.3434 14.4444 14.5455 14.6465 14.7475 14.8485
Columns 50 through 56
14.9495 15.0505 15.1515 15.2525 15.3535 15.4545 15.5556
Columns 57 through 63
15.6566 15.7576 15.8586 15.9596 16.0606 16.1616 16.2626
Columns 64 through 70
16.3636 16.4646 16.5657 16.6667 16.7677 16.8687 16.9697
Columns 71 through 77
17.0707 17.1717 17.2727 17.3737 17.4747 17.5758 17.6768
Columns 78 through 84
17.7778 17.8788 17.9798 18.0808 18.1818 18.2828 18.3838
Columns 85 through 91
18.4848 18.5859 18.6869 18.7879 18.8889 18.9899 19.0909
Columns 92 through 98
19.1919 19.2929 19.3939 19.4949 19.5960 19.6970 19.7980
Columns 99 through 100
19.8990 20.0000

ans =
11 12 13 14 15 16 17 18 19 20

Page | 26
logspace Generate logarithmically spaced vectors.

logspace(1, 2) % Generates 50 LOGARITHMICALLY spaced points between 10^1 and 10^2.


logspace(1, 2, 10) % Generates 10 LOGARITHMICALLY spaced points between 10^1 and 10^2.

ans =
Columns 1 through 8
10.0000 10.4811 10.9854 11.5140 12.0679 12.6486 13.2571 13.8950
Columns 9 through 16
14.5635 15.2642 15.9986 16.7683 17.5751 18.4207 19.3070 20.2359
Columns 17 through 24
21.2095 22.2300 23.2995 24.4205 25.5955 26.8270 28.1177 29.4705
Columns 25 through 32
30.8884 32.3746 33.9322 35.5648 37.2759 39.0694 40.9492 42.9193
Columns 33 through 40
44.9843 47.1487 49.4171 51.7947 54.2868 56.8987 59.6362 62.5055
Columns 41 through 48
65.5129 68.6649 71.9686 75.4312 79.0604 82.8643 86.8511 91.0298
Columns 49 through 50
95.4095 100.0000

ans =
Columns 1 through 7
10.0000 12.9155 16.6810 21.5443 27.8256 35.9381 46.4159
Columns 8 through 10
59.9484 77.4264 100.0000

det Matrix determinant.

X = [2 8 6; 7 3 9; 5 1 4]
det(X)

X =
2 8 6
7 3 9
5 1 4
ans =
94.0000

norm Vector and matrix norms.

X = [2 8 6; 7 3 9; 5 1 4]

norm(X, 1) % 1st norm of X.


norm(X, 2) % 2nd norm of X.
norm(X, Inf) % Infinite norm of X.

X =
2 8 6
7 3 9
5 1 4
ans =
19
ans =
15.7859
ans =
19

Page | 27
inv Matrix inverse.

X = [2 8 6; 7 3 9; 5 1 4]
inv(X)
X*inv(X)

X =
2 8 6
7 3 9
5 1 4

ans =
0.0319 -0.2766 0.5745
0.1809 -0.2340 0.2553
-0.0851 0.4043 -0.5319

ans =
1.0000 0 -0.0000
0 1.0000 -0.0000
-0.0000 0.0000 1.0000

eig Eigenvalues and eigenvectors of matrix.

X = [2 8 6; 7 3 9; 5 1 4]

% Produces matrices of eigenvalues (D) and eigenvectors (V) of matrix X.


[Val, Vec] = eig(X)

X*Val
Val*Vec

X =
2 8 6
7 3 9
5 1 4

Val =
0.6271 0.8256 -0.7548
0.6848 -0.1350 -0.1078
0.3713 -0.5478 0.6470

Vec =
14.2892 0 0
0 -3.2892 0
0 0 -2.0000

ans =
8.9602 -2.7157 1.5097
9.7853 0.4441 0.2157
5.3052 1.8019 -1.2940

ans =
8.9602 -2.7157 1.5097
9.7853 0.4441 0.2157
5.3052 1.8019 -1.2940

Page | 28
1D and 2D Graphical Commands
figure Create figure graphics object.

h = figure; % 'h' is figure handle, used to modify figure afterwards (if required).

xlabel, ylabel Label x-axis, y-axis.

xlabel('x - axis')

title Add title to current axes.

title('Title goes here.');

Page | 29
grid Grid lines for 2-D and 3-D plots.

grid on
grid off

% Toggles the major grid visibility state.


grid

clf Clear current figure window.

clf

close Remove specified figure.

close(h)
close all

plot 2-D line plot.

x = -pi : 0.1 : pi;


y = sin(x);

figure
plot(x)

figure
plot(y)

figure
plot(x, y)

Page | 30
Page | 31
subplot Create axes in tiled positions.

x = -pi : 0.1 : pi;


y = sin(x);

figure;
subplot(2, 2, 1); plot(x)
subplot(2, 2, 2); plot(y)
subplot(2, 2, 3); plot(x, y)
subplot(2, 2, 4); plot(y, x)

bar Plot bar graph.

x = -5 : 5;
figure;
bar(x)

Page | 32
hist Histogram plot.

x = -4 : 0.1 : 4;
y = randn(10000,1);

figure
hist(y, x)

polar Polar coordinate plot.

t = 0 : .01 : 2*pi;

figure
polar(t, sin(2*t) .* cos(2*t))

Page | 33
stairs Stairstep graph.

x = linspace(-2*pi, 2*pi, 40);


figure; stairs(x)

stem Plot discrete sequence data.

x = -4 : 4;
figure; stem(x)

Page | 34
semilogx Plots data as LOGARITHMIC scales for the x-axis, and LINEAR scale for the y-axis.

x = 0 : 0.1 : 10;
y = 10 .^ x;

figure; semilogx(x); grid on


figure; semilogx(y, x); grid on

Page | 35
semilogy Plots data as LOGARITHMIC scales for the y-axis, and LINEAR scale for the x-axis.

x = 0 : 0.1 : 10;
y = 10 .^ x;

figure; semilogy(x); grid on

figure; semilogy(x, y); grid on

Page | 36
loglog Log-log scale plot.

x = logspace(-1, 2); y = exp(x);


figure; loglog(x, y); grid on

hold Retain current graph when adding new graphs.

figure;
hold; % Toggles the hold state between adding to graph and replacing the graph.

x = -pi: pi/20 : pi;


plot(sin(x)); hold on
plot(cos(x)); hold off

Current plot held

Page | 37
axis Axis scaling and appearance.

x = 1 : 10;

figure; plot(x)
axis([0 11 0 12])

legend Graph legend for lines and patches.

x = -pi : pi/20 : pi;

figure; plot(x, cos(x), '-ro', x, sin(x), '-.b')


legend('cos_x','sin_x');

Page | 38
3D Graphical Commands
plot3 3-D line plot.

t = 0 : pi/50 : 10*pi;
x = sin(t);
y = cos(t);

figure
plot3(x, y, t);

xlabel('sin(t)')
ylabel('cos(t)')
zlabel('time (t)')

grid on
axis square

mesh Mesh plot. Draws a coloured wireframe mesh.

[x, y] = meshgrid(-8 : .5 : 8);


r = sqrt(x.^2 + y.^2) + eps;
z = sin(r) ./ r;

figure
mesh(z)

title('Sinc function');

Page | 39
surf Shaded surface plot.

[x, y] = meshgrid(-8 : .5 : 8);


r = sqrt(x.^2 + y.^2) + eps;
z = sin(r) ./ r;

figure
surf(z)

title('Sinc function');

Page | 40
Control Commands
if / elseif / else Execute statements if condition is true.

x = 10;
y = 20;

if (x > y)
disp('x is greator than y.');
elseif (x < y)
disp('x is less than y.');
else
disp('x is equal to y.');
end

x is less than y.

for Execute statements specified number of times.

n = 0;
for k = 0 : 3
n = n + k;
end
n

n = 0;
for k = [1 3 7 -4 10]
n = n + k;
end
n

% Successively set k to unit vectors:


for k = eye(3)
disp('Current value of k :-')
disp(k)
end

n =
6

n =
17

Current value of k :-
1
0
0

Current value of k :-
0
1
0

Current value of k :-
0
0
1

Page | 41
while Repeatedly execute statements while condition is true.

n = 5;
nFactorial = 1;

while (n > 0)
nFactorial = nFactorial * n;
n = n - 1;
end

nFactorial

nFactorial =
120

switch / case / otherwise Switch among several cases based on expression

n = 0;

switch (n)
case -1
disp('n is equal to -1.');
case 0
disp('n is equal to 0.');
case +1
disp('n is equal to 1.');
otherwise
disp('n is any othe number.');
end

n is equal to 0.

break Terminate execution of for or while loop.

n = 5;
nFactorial = 1;

while 1
nFactorial = nFactorial * n;
n = n - 1;
if (n <= 0)
break;
end
end

continue Pass control to next iteration of for or while loop.

for k = 1 : 5
if(k == 1 || k == 3 || k == 5)
continue;
end
disp(k)
end

2
4

Page | 42
2. UNIT STEP, UNIT RAMP, EXPONENTIAL & ADDITION OF TWO SEQUENCES

clc;
clear;
close all;

Unit Sample Signal


n = -5 : 5;
x = n == 0;
figure;
subplot(2, 1, 1);
stem(n, x)

n = -5 : 5;
x = (n-2) == 0;
subplot(2, 1, 2);
stem(n, x)

Unit Step Signal


n = -5 : 5;
x = n >= 0;
figure;
subplot(2, 1, 1);
stem(n, x)

n = -5 : 5;
x = (n-2) >= 0;
subplot(2, 1, 2);
stem(n, x)

Page | 43
Unit Ramp Signal
n = -5 : 5;
x = n .* (n >= 0);
figure;
subplot(2, 1, 1);
stem(n, x)

n = -5 : 5;
x = (n-2) .* ((n-2) >= 0);
subplot(2, 1, 2);
stem(n, x)

Page | 44
Real-valued Exponential Signal
n = -20:20;
x = (0.9 .^ n) .* (n >= 0);
figure;
subplot(2, 1, 1);
stem(n, x)

n = -20:20;
x = (0.9.^(n-5)) .* ((n-5) >= 0);
subplot(2, 1, 2);
stem(n, x)

Complex-valued Exponential Signal


n = -5:40;
x = exp((3*4j)*n) .* (n >= 0);

y = real(x);
z = imag(x);

figure;
subplot(2, 1, 1);
stem(n, y)
ylabel('Real')

subplot(2, 1, 2);
stem(n, z)
ylabel('Imag')

Page | 45
Sinusoidal Signal
n = -5 : 40;
x = 4*cos(0.1*pi*n+pi/3);
figure;
subplot(2, 1, 1);
stem(n, x)

x = 4*cos(0.1*pi*n+pi/3)+3*sin(0.3*pi*n+pi);
subplot(2, 1, 2);
stem(n, x)

Page | 46
Signal Addition. y[n] = x1[n] + x2[n]
n1 = 0 : 4;
x1 = [0 1 2 3 4];
figure;
subplot(3, 1, 1);
stem(n1, x1)
ylabel('x_1[n]')

n2 = -2 : 2;
x2 = [2 2 2 2 2];
subplot(3, 1, 2);
stem(n2, x2)
ylabel('x_2[n]')

n = min(min(n1),min(n2)) : max(max(n1),max(n2)); % Duration of y(n)


y1 = zeros(1,length(n));
y2 = zeros(1,length(n));

y1((n >= min(n1)) & (n <= max(n1)) == 1) = x1; % x1 with duration of y


y2((n >= min(n2)) & (n <= max(n2)) == 1) = x2; % x2 with duration of y

y = y1 + y2;
subplot(3, 1, 3);
stem(n, y)
ylabel('y[n]')

Page | 47
3. CONVOLUTION SUM OF DISCRETE SIGNALS

clc; clear; close all;

Signal x
x = [0 0 -1 2 3 -2 0 1 0 0]; % Values
dtx = -2 : 7; % Time, t
subplot(3,1,1);
stem(dtx, x)

Signal y
y = [1 -1 2 4]; % Values
dty = 8 : 11; % Time, t
subplot(3,1,2);
stem(dtx, x)

Convolution Sum
z = conv(x, y);
dtz = (min(dtx) + min(dty)) : (max(dtx) + max(dty));
z
subplot(3,1,3);
stem(dtz, z)

z =
0 0 -1 3 -1 -5 16 9 -9 2 4 0 0

Page | 48
4. CROSS-CORRELATION OF TWO SEQUENCES

clc;
clear;
close all;

Sequences - x, y
x = [5 -3 1 -7];
y = [-1 2 1 -1];

Cross-correlation using 'xcorr' function


z1 = xcorr(x,y)

z1 =
-5.0000 8.0000 6.0000 -3.0000 -2.0000 -15.0000 7.0000

Cross-correlation without 'xcorr' function


xLen = length(x);
yLen = length(y);

z2 = zeros(1, xLen + yLen - 1);


zLen = length(z2);

for m = 1 : zLen
arg = (m - xLen);

if(arg < 0)
negativeCondition = 1;
limit = xLen + arg;
else
negativeCondition = 0;
limit = xLen - arg;
end

for n = 1:limit
if (negativeCondition)
z2(m) = z2(m) + x(n) * y(n - arg);
else
z2(m) = z2(m) + x(arg + n) * y(n);
end
end
end

z2

z2 =
-5 8 6 -3 -2 -15 7

Page | 49
5. FREQUENCY RESPONSE OF DISCRETE SYSTEM

clc;
clear;
close all;

Transfer Function Coefficients - b, a


b = [1 0.5 0];
a = [1 -1 0.5];

Frequency response at 101 points spanning the complete unit circle


[h, w] = freqz(b, a, 101, 'whole');

f = w/(2*pi);

plot(f,20*log10(abs(h)))
xlabel('Normalized Frequency (\times\pi rad/sample)')
ylabel('Magnitude (dB)')

Page | 50
6. DISCRETE FOURIER TRANSFORM (DFT) OF THE GIVEN SEQUENCE

clc;
clear;

Sequence x
x = [5 3 7 1];
N = length(x);

Twiddle Matrix for N-Points


WN = zeros(N,N);
for a = 1:N
for b = 1:N
WN(a,b) = exp(-2i*((a-1)*(b-1))*pi/N);
end
end

DFT
y = WN * x.';
disp(' ');
disp('DFT X(k) = ');
disp(y);

DFT X(k) =
16.0000 + 0.0000i
-2.0000 - 2.0000i
8.0000 + 0.0000i
-2.0000 + 2.0000i

IDFT
x = (1/N) * (WN') * y;
disp('IDFT x(n) = ');
disp(x);

IDFT x(n) =
5.0000 - 0.0000i
3.0000 + 0.0000i
7.0000 + 0.0000i
1.0000 + 0.0000i

Page | 51
7. POLES, ZEROS AND GAIN OF DISCRETE-TIME TRANSFER FUNCTION

clc;
clear;
close all;

Transfer Function Coefficients - b, a


b = [1 0.5 0];
a = [1 -1 0.5];

Find Zeros, Poles and Gain


[z, p, k] = tf2zpk(b,a)

z =
0
-0.5000

p =
0.5000 + 0.5000i
0.5000 - 0.5000i

k =
1

Plot Zeros & Poles


zplane(z,p)

Page | 52
8. MAGNITUDE & PHASE OF DISCRETE FOURIER TRANSFORM (DFT)

clc; clear; close all;

Sequence x
x = [5 3 7 1]; N = length(x);

Twiddle Matrix for N-Points


WN = zeros(N,N);
for a = 1:N
for b = 1:N
WN(a,b) = exp(-2i*((a-1)*(b-1))*pi/N);
end
end

DFT
y = WN * x.';
disp('DFT X(k) = '); disp(y);

DFT X(k) =
16.0000 + 0.0000i
-2.0000 - 2.0000i
8.0000 + 0.0000i
-2.0000 + 2.0000i

Magnitude & Phase of DFT


figure;
subplot(2, 1, 1); stem(0:N-1, abs(y)); title('Magnitude');
subplot(2, 1, 2); stem(0:N-1, angle(y)); title('Phase');

Page | 53
9. TO STUDY THE MAGNITUDE & PHASE RESPONSE OF FIR FILTER

clc;
clear;
close all;

Design a FIR lowpass filter


Order = 80 Window = Kaiser with 𝛽 = 8. Normalized cut-off frequency = 0.5𝜋 rad/sample.

b = fir1(80, 0.5, kaiser(81, 8));

Display the magnitude and phase responses of the filter


freqz(b, 1)

Page | 54
10. TO STUDY THE MAGNITUDE & PHASE RESPONSE OF IIR FILTER

clc; clear; close all;

Design an IIR lowpass filter


Order = 8 Passband frequency = 35 kHz Passband ripple = 0.2 dB Sample rate = 200 kHz.

lpFilt = designfilt('lowpassiir','FilterOrder',8, ...


'PassbandFrequency',35e3,'PassbandRipple',0.2, ...
'SampleRate',200e3);

Display the magnitude and phase responses of the filter


fvtool(lpFilt)

Filter coefficients, expressed as second-order sections


sos = lpFilt.Coefficients

sos =
0.2666 0.5333 0.2666 1.0000 -0.8346 0.9073
0.1943 0.3886 0.1943 1.0000 -0.9586 0.7403
0.1012 0.2023 0.1012 1.0000 -1.1912 0.5983
0.0318 0.0636 0.0318 1.0000 -1.3810 0.5090

Transfer function filter coefficients


[b, a] = sos2tf(sos)

b =
0.0002 0.0013 0.0047 0.0093 0.0117 0.0093 0.0047 0.0013 0.0002

a =
1.0000 -4.3655 9.8127 -14.1520 14.1088 -9.8750 4.7253 -1.4153 0.2046

Page | 55

You might also like