Lab_1
Lab_1
CLO5: USE MODERN TOOL (E.G. MATLAB) FOR SIGNAL REPRESENTATION , VISUALIZATION AND
PROCESSING IN BOTH TIME AND FREQUENCY DOMAIN .
Objective:
Most of the Lab work will be covered using MATLAB. The main objective of this lab manual is to
introduce the basics of MATLAB.
SECTION-A
1. Arithmetic Operations:
All basic arithmetic operations can be performed using MATLAB. The symbols are listed in table
below:
2. Display Formats:
MATLAB has several screen display formats for displaying numbers. They are listed below. You
can try them in command window like >>format short and the format will be changed to short.
Test Yourself
Function Description
abs(x) Computes the absolute value of x
sqrt(x) Computes the square root of x
round(x) Rounds x to the nearest integer
fix(x) Rounds (or truncate) x to the nearest integer 0
floor(x) Rounds x to the nearest integer toward –
ceil(x) Rounds x to the nearest integer toward +
sign(x) Returns a value of –1 if x is less than 0, a value of 0 if x equals 0 and a value of 1
otherwise
rem(x, y) Returns the remainder of x/y. This function is also called modulus function.
exp(x) Computes ex, where e is the base for natural logarithms (or approximately 2.718282)
log(x) Computes ln x, the natural logarithm of x to the base e
log10(x) Computes log10 x, the common logarithm of x to the base 10
log2(x) Computes log2 x, the logarithm of x to the base 2
Trigonometric Functions
Function Description
sin(x) Computes the sine of x, where x is in radians
cos(x) Computes the cosine of x, where x is in radians
tan(x) Computes the tangent of x, where x is in radians
asin(x) Computes the arc-sine or inverse sine of x, where x must be between –1 and 1. The
function returns and angle in radian between –π/2 and π/2
acos(x) Computes the arc-cosine or inverse cosine of x, where x must be between –1 and
1. The function returns and angle in radian between 0 and π
Function Description
conj(x) Computes the complex conjugate of the complex number x. thus if x is equal to a
+ ib, then conj(x) will be equal to a – ib
real(x) Computes the real part of the complex number x
imag(x) Computes the imaginary part of the complex number x
abs(x) Computes the absolute value of magnitude of the complex number x
angle(x) Computes the angle using the value of atan2(imag(x), real(x)); thus the angle
value is between –π and π
4. Variable names:
Variable names are case sensitive in MATLAB. So xy, Xy, xY and XY are four different variable
names. Names of built-in functions should not be used as variable name e.g. sin, abs, ceil etc. Also
predefined variable (Mentioned below) names cannot be used for your own variable.
5. Predefined Variables:
Many variables are pre-defined in MATLAB. Some are listed below:
Predefined variables Description
in MATLAB
inf Represents infinity which for instance occurs as a result of a division by zero.
A warning message will be displayed or the value will be printed as ∞
clock Represents the current time in a six-element row vector containing year,
month, day, hour, minute and seconds
Test Yourself
17(√5−1) 57 log10 (𝑒 3 )
Evaluate + + (ln(4))3 using single line MATLAB statement:
152 −132 𝜋√120
7. General Commands:
Following tables give some general commands that can be required many times:
Online – Help
Function Description
help Lists topics on which help is available
helpwin Opens the interactive help window
helpdesk Opens the web browser based help facility
help topic Provides help on topic
lookfor string Lists help topics containing string
demo Runs the demo program
Workspace Information
Function Description
who Lists variables currently in the workspace
whos Lists variables currently in the workspace with their size
what Lists .m, .mat and .mex files on the disk
clear Clears the workspace, all the variables are removed
clear x, y, z Clears only variables x, y and z
clear all Clears all variables and functions from workspace
8. Arrays:
An array is a list of numbers arranged in rows and/or columns. A one-dimensional array is a
row or a column of numbers and a two-dimensional array has a set of numbers arranged in rows
and columns. An array operation is performed element-by-element. In a row vector the elements
are entered with a space or a comma between the elements inside the square brackets. For
example,
>>x = [7 – 1 2 – 5 8]
In a column vector the elements are entered with a semicolon between the elements inside
the square brackets. For example,
7
−1
>>y = [7 ; – 1 ; 2 ; – 5 ; 8] will generate a column vector y = 2
−5
[ 8]
On the other hand, a row vector is converted to a column vector using the transpose operator.
The transpose operation is denoted by an apostrophe or a single quote (').
7
−1
>>y=x’ will generate the same column vector y = 2
−5
[ 8]
MATLAB also provides shortcuts for elements entry. For example if you want to create a vector v
as:
v= [1 2 3 4 5 6 7 8 9 10]
then instead of writing all numbers from 1 to 10, you can use colon (:) operator like:
>>v = [1:10]
What if you want to enter all odd numbers from 1 to 99 in a vector w? You can use colon operator
as:
>>w=[1:2:99];
The first value i.e. 1 is that starting value, then 2 is the amount of increment and 99 is the last
value of the vector.
Test Yourself
Define a vector z=[1000 997 994 991 … 100]
Editing/Adding/Removing elements:
Element(s) of matrix can be changed by accessing them like
Test Yourself
Without using MATLAB find out the elements of matrix e:
>>a=[5 3];
>>b=[a;a];
>>c=[4;6];
>>d=[8 9 7 1];
>>e=[b c c; d]
Verify the output by executing the code on MATLAB.
Once you get the matrix e, write MATLAB command to delete the 3rd column.
MATLAB Statement Output
MATLAB provides functions that generates elementary matrices. The matrix of zeros, the matrix of
ones, and the identity matrix are returned by the functions zeros, ones, and eye, respectively.
Test Yourself
Without using MATLAB, write down the output of following commands:
>> a = [1 2 3 ;4 5 6 ;7 8 9] ;
>> b = [a, zeros (3,2); ones(2,3),eye(2)]
Test Yourself
Enter following matrix in MATLAB:
1 2 3 4 5 6 7 8
9 10 11 12 13 14 15 16
𝐴 = 17 18 19 20 21 22 23 24
25 26 27 28 29 30 31 32
[33 34 35 36 37 38 39 40]
Create a 3 × 4 matrix B from the 1st , 3rd , and the 5th rows, and the 1st , 2nd , 4th , and 8th columns
of the matrix A.
MATLAB Statement Output
Test Yourself
Define following Matrices in MATLAB
2 1 2 3
5 3
A = [0 5] B = [ ] C = [ −5 −2 ] D = [1 2]
−2 −4
7 4 0 3
Compute following matrices and determine if they exist:
a) (ACT )−1 b) |B| c) |ACT |
Script M-File:
It consists of sequence of normal MATLAB statements written in a file with extension .m and saved
in MATLAB Directory. If file is saved with name test.m then executing the following command on
main Command Window:
>>test
It will execute instructions in that file one by one. Any variable defined in script file are global which
means that they can be accessed by Command Window.
Function M-File:
You can create new function according to your problem using Function M-File. Variables defined in
Function M-File are local. So they will not be defined in command window. You can create one
function in one Function M-File, however you can use as many existing (built-in or made by yourself)
function as you wish. M-File name should be function name. Function M-File’s first line defines
function name and input/output variables, for example for two input and two output function it is:
function [output_1 output_2] = name(input_1, input_2)
For example if you want to make a very simple function that converts radians to degree, M-file will
be:
function d=r2d(r) %%r2d is name of function, r is input, d is
output
d=r*180/pi
>>r2d(pi)
ans = 180
Test Yourself
Make a function file with name tri which will take three inputs a, b and c as sides of triangle and will
return its area A and Perimeter P. Show output for some set of sides.
P
P=a+b+c s= A = √s(s − a)(s − b)(s − c)
2
1. Programming in MATLAB:
Programming can also be done using MATLAB as in other programming languages. Basic
operators used in programming are:
Relational Operators
Relational Operator Interpretation
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal to
~= Not equal to
Logical Operators
Control Flow:
To control the flow of command in MATLAB , you can use
for loops
while loops
if-else-end constructions
switch-case constructions
If-else-end constructions:
Syntax of simple if condition is:
This construction is used if there is one alternative only. Two alternatives require the construction
if expression
commands (evaluated if expression is true)
else
commands (evaluated if expression is false)
end
If there are several alternatives one should use the following construction
if expression1
commands (evaluated if expression 1 is true)
elseif expression 2
commands (evaluated if expression 2 is true)
elseif …
.
.
.
else
commands (executed if all previous expressions evaluate to
false)
end
Test Yourself
Create an M-File function with name car2pol which will convert Cartesian co-ordinates to Polar
co-ordinates. For a Cartesian point (x,y), Polar co-ordinate (r,θ) can be found using
𝑦
𝑟 = √𝑥 2 + 𝑦 2 𝜗 = tan−1 (𝑥 )
Angle should be in degrees at output. Show what is output for (1,1), (-1,-3) and (-2,2)
𝑦 𝜋 𝜋
(Recall from your previous knowledge that 𝑡𝑎𝑛−1 (𝑥 ) will give result only from − 2 𝑡𝑜 2 . It is true
if θ is in 1st or 4th quadrant. And if θ is in 2nd or 3rd quadrant you have to add π in the result. So if
x is +ve (1st and 4th Quadrant), result is correct and if x is –ve (2nd and 3rd Quadrant), you have to
add π to it. Use if-else condition to handle it )
For Loop:
You people are well familiar with for loop in programming languages. Its syntax is
for expression
statements
end
Suppose that one-need values of the sine function at eleven evenly spaced points πn/10, for n =
0, 1, … 10. To generate the numbers in question for loop can be used as:
for n=0:10
x(n+1) = sin(pi*n/10);
end
Test Yourself
Run following nested for loop and write down its output:
H = zeros(5);
H=
for m=1:5
for n=1:5
H(m,n) = 1/(m+n-1);
end
end
H