Professional Documents
Culture Documents
BACHELOR OF TECHNOLOGY
IN
ELECTRONICS AND COMMUNICATION
Submitted by
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
SIGNAL X 48
SIGNAL Y 48
CONVOLUTION SUM 48
SEQUENCES - X, Y 49
CROSS-CORRELATION USING 'XCORR' FUNCTION 49
CROSS-CORRELATION WITHOUT 'XCORR' FUNCTION 49
SEQUENCE X 51
TWIDDLE MATRIX FOR N-POINTS 51
DFT 51
IDFT 51
SEQUENCE X 53
TWIDDLE MATRIX FOR N-POINTS 53
DFT 53
MAGNITUDE & PHASE OF DFT 53
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,
General Commands
who List variables in workspace.
who
whos
Page | 1
what List MATLAB files in folder.
what
size([1 2 3; 4 5 6])
ans =
2 3
length([10 20 30])
ans =
3
help sin
dir
save MyFile.mat
load MyFile.mat
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
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
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 =
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
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 =
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
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]
A =
1 2
3 4
B =
10 20 30 40
ans =
37 54
81 118
ans =
-2.0000 1.0000
1.5000 -0.5000
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
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
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
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
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
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
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
x ...
= ...
10
x =
10
% Single-Line Comments.
% This is a comment.
%{ %} Multi-Line Comment.
%{
This is a multi-line comment.
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]
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
% 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.
A = magic(10)
A = magic(3)
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.
% 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
ans =
3.1416
x = 1 + 2i
y = 1 - 2j
x =
1.0000 + 2.0000i
y =
1.0000 - 2.0000i
1 / 0
exp(1000)
log(0)
Inf(1,3)
ans =
Inf
ans =
Inf
ans =
-Inf
ans =
Inf Inf Inf
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(2)
ans =
1.4142
real(3 + 4i)
ans =
3
imag(3 + 4i)
ans =
4
angle(3 + 4i)
ans =
0.9273
conj(3 + 4i)
ans =
3.0000 - 4.0000i
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(-1)
ans =
0.0000 + 3.1416i
log10(-1)
ans =
0.0000 + 1.3644i
Trigonometric Functions
sin, cos, tan, csc, sec, cot All in radians.
ans =
0 -0.9880 0.8509 -0.3048 0.8940
ans =
0 0.5000 0.7071 0.8660 1.0000
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.
ans =
0.0000+0.0000i 1.5708-4.0941i 1.5708-4.4997i 1.5708-4.7874i 1.5708-5.1929i
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.
ans =
0 4.0946 4.4999 4.7876 5.1930
hypot(3, 4)
ans =
5
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
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
X = [3 6 9]
std(X, 0)
std(X, 1)
X =
3 6 9
ans =
3
ans =
2.4495
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
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
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
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
u = [1 3 5 7 9]
v = [2 4 6 8]
c = conv(u, 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
p =
1 -6 -72 -27
ans =
12.1229
-5.7345
-0.3884
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(2)
ones(2, 3)
ans =
1 1
1 1
ans =
1 1 1
1 1 1
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)
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(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.
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
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
X = [2 8 6; 7 3 9; 5 1 4]
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
X = [2 8 6; 7 3 9; 5 1 4]
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('x - axis')
Page | 29
grid Grid lines for 2-D and 3-D plots.
grid on
grid off
clf
close(h)
close all
figure
plot(x)
figure
plot(y)
figure
plot(x, y)
Page | 30
Page | 31
subplot Create axes in tiled positions.
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)
x = -5 : 5;
figure;
bar(x)
Page | 32
hist Histogram plot.
x = -4 : 0.1 : 4;
y = randn(10000,1);
figure
hist(y, x)
t = 0 : .01 : 2*pi;
figure
polar(t, sin(2*t) .* cos(2*t))
Page | 33
stairs Stairstep graph.
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;
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;
Page | 36
loglog Log-log scale plot.
figure;
hold; % Toggles the hold state between adding to graph and replacing the graph.
Page | 37
axis Axis scaling and appearance.
x = 1 : 10;
figure; plot(x)
axis([0 11 0 12])
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
figure
mesh(z)
title('Sinc function');
Page | 39
surf Shaded surface plot.
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.
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
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
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.
n = 5;
nFactorial = 1;
while 1
nFactorial = nFactorial * n;
n = n - 1;
if (n <= 0)
break;
end
end
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;
n = -5 : 5;
x = (n-2) == 0;
subplot(2, 1, 2);
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)
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]')
y = y1 + y2;
subplot(3, 1, 3);
stem(n, y)
ylabel('y[n]')
Page | 47
3. CONVOLUTION SUM OF DISCRETE SIGNALS
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];
z1 =
-5.0000 8.0000 6.0000 -3.0000 -2.0000 -15.0000 7.0000
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;
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);
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;
z =
0
-0.5000
p =
0.5000 + 0.5000i
0.5000 - 0.5000i
k =
1
Page | 52
8. MAGNITUDE & PHASE OF DISCRETE FOURIER TRANSFORM (DFT)
Sequence x
x = [5 3 7 1]; N = length(x);
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
Page | 53
9. TO STUDY THE MAGNITUDE & PHASE RESPONSE OF FIR FILTER
clc;
clear;
close all;
Page | 54
10. TO STUDY THE MAGNITUDE & PHASE RESPONSE OF IIR FILTER
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
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