Talha - DSP - GCU - Lab1 - 2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Digital Signal Processing Lab Manual

GCU Lahore
Department of Electrical Engineering
Experiment # 1
Title: Starting with MATLAB.

Equipment Required: Personal computer (PC) with windows operating system


and MATLAB software

Theory:-
MATLAB is a powerful computing system for handling the calculations involved in
scientific and engineering problems. The name MATLAB stands for MATrix LABoratory,
because the system was designed to make matrix computations particularly easy.
One of the many things about MATLAB (and which distinguishes it from many
other computer programming systems, such as C++ and Java) is that you can use it
interactively. This means you type some commands at the special MATLAB prompt, and
get the answers immediately. The problems solved in this way can be very simple, like
finding a square root, or they can be much more complicated, like finding the solution to
a system of differential equations. For many technical problems you have to enter only
one or two commands, and you get the answers at once.

MATLAB WINDOWS:-
Window Purpose
Command Window Main window, enters variables, runs programs.
Figure Window Contains output from graphic commands.
Editor Window Creates and debugs script and function files.
Help Window Provides help information.
Launch Pad Window Provides access to tools, demos, and documentation.
Command History Window Logs commands entered in the Command Window
Workspace Window Provides information about the variables that are used.
Current Directory Window Shows the files in the current directory
1
Page
Digital Signal Processing Lab Manual

Workspace Window

Command Window

Command
History Window

Procedure:-
1) To start MATLAB from Windows, double-click the MATLAB icon on your Windows
desktop. When MATLAB starts, the MATLAB desktop opens as shown in Figure
1.1. The window in the desktop that concerns us for this experiment is the
Command Window, where the special >> prompt appears. This prompt means
that MATLAB is waiting for a command. You can quit MATLAB at any time with
one of the following:
➤ Select Exit MATLAB from the desktop File menu.
➤ Enter quit or exit at the Command Window prompt.
2) Once you have started MATLAB, try the following exercises in the Command
Window.
(a) Type 2+3 after the >> prompt, followed by Enter, i.e. press the Enter key, as
indicated by <Enter>, below:
>>2+3 <Enter>
Commands are only carried out when you press Enter. The answer in this
case is, of course, 5.
2

(b) Next try the following:


Page
Digital Signal Processing Lab Manual

>>3-2 <Enter>
>>2*3 <Enter>
>>1/2 <Enter>
>> 2ˆ3 <Enter>
>>2\1 <Enter>
symbols *, / and ˆ, are multiplication, division and exponentiation. The backslash
means the denominator is to the left of the symbol and the numerator is to the
right of the symbol; the result for the last command is 0.5. This operation is
equivalent to 1/2.
3) Assign values to variables to do arithmetical operations with the variables.
(a) Enter the command a = 2, i.e. the MATLAB command line should look
like this:
>>a = 2 <Enter>
The symbol a is called a variable. This statement assigns the value of 2 to a.
(b)Now enter the statement
>>b = 3; <Enter>
The semicolon (;) prevents the value of b from being displayed.
4) The output format is fixed-point with 4 decimal digits (called short), which is the
default format for numerical values. The format can be changed with the
formatcommand. Once the format command is entered, all the output that
follows is displayed in the specified format. Several of the available formats are
listed and described in Table below.

Display formats
Command Description Example
format short Fixed-point with 4 decimal digits for: >> format short
0.001 ≤ number ≤1000 Otherwise display >> 290/7
format short e. ans = 41.4286
format long Fixed-point with 14decimal digits. >> format long
>> 290/7
ans = 41.428571428571431
format short e Scientific notation with 4 decimal digits. >> format short e
>> 290/7
ans = 4.1429e+001
format long e Scientific notation with 15 decimal digits. >> format long e
>> 290/7
ans =
4.142857142857143e+001
3

format short g Best of 5-digit fixed or floating point. >> format short g
Page

>> 290/7
Digital Signal Processing Lab Manual

ans = 41.429
format long g Best of I5-digit fixed or floating point. >> format long g
>> 290/7
ans = 41.4285714285714
format bank Two decimal digits. >> format bank
>> 290/7
ans = 41.43

5) ELEMENTARY MATH BUILT-IN FUNCTIONS


In addition to basic arithmetic operations, expressions in MATLAB can
include functions. MATLAB has a very large library of built-in functions. A function
has a name and an argument in parentheses. For example, the function that
calculates the square root of a number is sqrt(x). Its name is sqrt, and the
argument is x.

Elementary math functions

Function Description Example


sqrt(x) Square root. >> sqrt(81)
ans = 9.00
exp(x) Exponential (ex). >> exp(5)
ans = 148.41
abs (x) Absolute value. >> abs (-24)
ans = 24.00
log (x) Natural logarithm. >> log(1000)
Base e logarithm (In). ans = 6.91
log10(x) Base 10 logarithm. >> log10(1000)
ans = 3.00
factorial (x) The factorial function x! >> factorial (5)
(x must be a positive ans = 120.00
integer.)

Function Description Example


sin(x) Sine of angle x (x in >> sin(pi/6)
radians). ans = 0.5000
cos(x) Cosine of angle x (x in >> cos(pi/6)
4

radians). ans = 0.8660


Page
Digital Signal Processing Lab Manual

tan (x) Tangent of angle x (x in >> tan(pi/6)


radians). ans = 0.5774
cot (x) Cotangent of angle x (.x in >> cot(pi/6)
radians). ans = 1.7321

Rounding functions
Function Description Example
round (x) Round to the nearest >> round (17/5)
integer. ans = 3
fix(x) Round towards zero. >> fix (13/5)
ans = 2
ceil(x) Round towards infinity. >> ceil (11/5)
ans = 3
floor (x) Round towards minus >> floor(-9/4)
infinity. ans = -3
rem(x,y) Returns the remainder after >> rem(13,5)
x is divided by y. ans = 3
sign(x) Signum function. Returns 1 >> sign(5)
if ans = 1
x > 0 , -1 if x < 0, and 0 if x =
0

Rules About VariableNames


Variablenames:
 Can be up to 63 characters long
 Can contain letters, digits, and the underscore character
 Must begin With a letter.
 MATLAB is case sensitive; it distinguishes between uppercase and lowercase
letters. For example, AA,Aa, aA, and aa are the names of four different variables.
 Avoid using the names of a built-in function for a variable (i.e. avoid using: cos,
sin, exp, sqrt, etc.). Once a function name is used to define a variable, the
function cannot be used.
Problems:-
Solve the following problems in the Command Window.
1) Calculate:
35.7 * 64  7 3
5

a)
45  5 2
Page
Digital Signal Processing Lab Manual

37
b) 5 * 7 * 6 2 
4 (93  652)
2732 / 3 552
c) (2  7) 3  
2 3
2733
d) 23  7 3   553 / 2
2
e) 37 log(76) 3
 910
7 3  546
4
 23
f) 43* 250
( 453 ) 3
e
 
tan ln 8 
5 7
2

g) cos2   sin    6 
 6   8  7
  ln 8 
tan 
 5  2  7 
2

h) cos  sin     6 
 6   8  7*
5
2
2) Define the variable x as x = 13.5, then evaluate:
a) x 3  5 x 2  26.7 x  52
14 x 3
b)
e3x
c) log x 2  x 3
3) Define the variable x and z as x = 9.6, and z=8.1, then evaluate
3
 2z  5
a) xz   2

 3x 
 xz
b) 4433z  e
2x ( x  z)
4) Define the variable a, b, c, and d as:
a = 15.62, b =-7.08, c = 62.5 and d = 0.5(ab-c)
evaluate:
ab (a  d ) 2
a) a  *
c ab
ad  cd
20 30
d
 

6

b) de 
2 a b
abcd
Page
Digital Signal Processing Lab Manual

GCU Lahore
Department of Electrical Engineering
Experiment # 2
Title: Arrays.

Equipment Required: Personal computer (PC) with windows operating system


and MATLAB software

Theory:-
Arrays: vectors and matrices:-
As mentioned earlier, the name MATLAB stands for MATrix LABoratory because
MATLAB has been designed to work with matrices. A matrix is a rectangular object (e.g.
a table) consisting of rows and columns. A vector is a special type of matrix, having only
one row, or one column.
MATLAB handles vectors and matrices in the same way, but since vectors are easier to
think about than matrices.

Procedure:-
1) One-Dimensional array:-
A one dimensional array is a list of number that is placed in a row or a column.
The vector is created by typing the elements inside the square brackets [ ]
Variable_name = [ type vector elements]
1.1)Row vector:-
To create a row vector type the elements with a space or a comma
between the elements inside the square brackets
>> yr = [1999 2000 2001 2003 2004]
yr =
1999 2000 2001 2003 2004

>> yr = [1999, 2000, 2001, 2003, 2004]


yr =
1999 2000 2001 2003 2004
1.2)Column vector :-
To create column vector type the left square brackets [and then enter the
elements with a semicolon between then, or press the enter key after each element.
7

Type the right square bracket]


Page
Digital Signal Processing Lab Manual

>>Pop = [127; 130; 136; 145; 158; 178 ]


Pop =
127
130
136
145
158
178
Assignment 1:-
Create a row vector that has the elements: 32, 4, 81, 63, cos(pi/3) and 14.12
Assignment 2:-
Create a column vector that has the elements: 55, 14, ln(51), 987, 0 and 5
sin(2.5pi)

2) Creating a vector with constant spacing by specifying the first term, the spacing,
and the last term

Variable_name = [firstterm : spacing : lastterm]

Example:-

>> x = [1:2:13] First element 1, spacing 2, last element 13


x=
1 3 5 7 9 11 13

>>y = [-3:7] First element -3, last term 7. If space is omitted, the default
is 1

Y = -3 -2 -1 0 1 2 3 4 5 6 7

Assignment 3 : Create a row vector in which the first element is 1, the last element is
33, with an increment of 2 between the elements (1,3,5,…….,33)
Assignment 4: Create a row vector in which the first element is 15, the elements
decrease with increments of -5 and the last elements is -25
8
Page
Digital Signal Processing Lab Manual

3) Creating a vector with constant spacing by specifying the first and last terms
and the number of terms:
A vector in which the first element is xi, the last element is xf, and the number of
elements is n is created by typing the linspace command:
variable_name =linspace (xi, xi, n)
Example:-
>> va = linspace(0,8,6) 6 elements, first element 0, last element8
va =
0 1.6000 3.2000 4.8000 6.4000 8.0000
>> va = linspace(30,10,11) 11 elements, first element 30, last element10
va =
30 28 26 24 22 20 18 16 14 12 10

4) Creating a two-dimensional array (matrix)


A matrix is created by assigning the elements of the matrix to a variable. This is
done by typing the elements, row by row, inside square brackets [ ]. First type the
left bracket [ , then type the first row separating the elements with spaces or
commas. To type the next row type a semicolon or press Enter. Type the right
bracket] at the end of the last row.
variable_name = [ 1st row elements; 2nd row elements; 3rd row elements; ………;
last row elements]
Example:-
>> a = [5 35 43; 4 76 81; 21 32 40]
a=
5 35 43
4 76 81 A semicolon is typed before a
21 32 40 new line is entered.
>> a = [5 35 43
4 76 81
21 32 40] A Enter key is pressed before a
a= new line is entered.
5 35 43
4 76 81
21 32 40
Rows of a matrix can also be entered as vectors using the notation for creating vectors
with constant spacing, or the linspace command.
9
Page

Example:
Digital Signal Processing Lab Manual

>> A = [1:2:11; 0:5:25; linspace(10,60,6); 67 2 43 68 4 13]


A=
1 3 5 7 9 11
0 5 10 15 20 25
10 20 30 40 50 60
67 2 43 68 4 13
In this example the first two rows were entered as vectors using the notation of
constant spacing, the third row was entered using the linspacecommand, and in the last
row the elements were entered individually.
5) The zeros, ones and eye Commands
The zeros (m, n), the ones(m, n), and eye (n) commands can be used to create
matrices that have elements with special values. The zeros (m, n) and the ones (m, n)
commands create a matrix with m rows and n columns, in which all the elements are the
numbers 0 and 1, respectively. The eye (n) command creates a square matrix with n
rows and n columns in which the diagonal elements are equal to 1, and the rest of the
elements are 0.This matrix is called the identity matrix.
Examples:
>> zr = zeros(3,4)
zr =
0 0 0 0
0 0 0 0
0 0 0 0
>> ne=ones(3,5)
ne =
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
>> idn = eye(5)
idn =
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1

6) The Transpose Operator:-


The transpose operator is applied by typing a single quote' following the variable to
be transposed.
10
Page

Examples:
Digital Signal Processing Lab Manual

>> aa = [3 8 1]
aa =
3 8 1
>> bb = aa'
bb =
3
8
1
>> C = [2 55 14 8 ; 21 5 32 11; 1 2 3 4]
C=
2 55 14 8
21 5 32 11
1 2 3 4
>> D = C'
D=
2 21 1
55 5 2
14 32 3
8 11 4
7) Array Addressing
Elements in an array (either vector or matrix) can be addressed individually or in
subgroups.
7.1) Vector:-
The address of an element in a vector is its position in the row (or column). For a
vector named ve, ve (k) refers to the element in position k. The first position is 1. For
example, if the vector ve has nine elements:
ve=35 46 78 2351481355
then
ve(4) = 23, ve(7) = 81, and ve(1) = 35.
Example:-
>> VCT = [35 46 78 23 5 14 81 3 55] Define a Vector
VCT =
35 46 78 23 5 14 81 3 55
>> VCT(4) Display the fourth element
ans =
23
>> VCT(6)=273 Assign a new value to the sixth element
VCT =
11

35 46 78 23 5 273 81 3 55 The whole vector is displayed


Page

>> VCT(2)+VCT(8)
Digital Signal Processing Lab Manual

ans =
49 Use the vector elements in
>> VCT(5)^VCT(8)+sqrt(VCT(7)) mathematical expressions
ans =
134
7.2) Matrix:-
The address of an element in a matrix is its position, defined by the row
number and the column number where it is located. For a matrix assigned to a
variable ma, ma(k,p)refers to the element in row k and column p.
For example, if the matrix is:
 3 11 6 5
ma   4 7 10 2
 
13 9 0 8
then, ma(1,1) = 3, and ma(2,3) = 10.
As with vectors, it is possible to change the value of just one element of a
matrix by assigning a new value to that element. Also, single elements can be
used
Like variables in mathematical expressions and functions.
Examples:
>> MAT = [3 11 6 5; 4 7 10 2; 13 9 0 8]
MAT = Create a 3 X 4 matrix
3 11 6 5
4 7 10 2
13 9 0 8
>> MAT(3,1)
ans =
13
>> MAT = [3 11 6 5; 4 7 10 2; 13 9 0 8]
MAT =
3 11 6 5
4 7 10 2
13 9 0 8
>> MAT(3,1)=20 Assign a new value to the (3,1) element.
MAT =
3 11 6 5
4 7 10 2
20 9 0 8
12

>> MAT(2,4)-MAT(1,2) Use the elements in a mathematical expression


ans =
Page

-9
Digital Signal Processing Lab Manual

7.3) Using A Colon: In Addressing Arrays


A colon can be used to address a range of elements in a vector or a matrix.
For a vector:
va(:) Refers to all the elements of the vector va (either a row or a column vector).
va(m:n) Refers to elements m through n of the vector va.
Example:

>> v = [4 15 8 12 34 2 50 23 11] A vector v is created


v=
4 15 8 12 34 2 50 23 11
>> u = v(3:7)
A vector u is created from
u=
the elements 3 through 7
8 12 34 2 50 of vector v.

For a matrix:
A( :,n) Refers to the elements in all the rows of column n of the matrix A.
A(n,:) Refers to the elements in all the columns of row n of the matrix A.
A(:,m:n) Refers to the elements in all the rows between columns m and n of the
matrix A.
A(m:n,:) Refers to the elements in all the columns between rows m and n of the
matrix A.
A(m:1},p:q) Refers to the elements in rows m through n and columns p through q
of the matrix A.
Example:-
>> A = [1 3 5 7 9 11; 2 4 6 8 10 12; 3 6 9 12 15 18]
A=
1 3 5 7 9 11
2 4 6 8 10 12 Define a matrix A with 3
3 6 9 12 15 18 rows and 6 columns.
>> B = A(:,3)
B= Define a column vector B from the
5 elements in all the rows of column 3
6 in matrix A.
9
>> C = A(2,:)
Define a row vector C from the elements in
C=
all the columns of row 2 in matrix A.
2 4 6 8 10 12
>> E = A(1:3,:)
13

E= Define a column matrix E from the


elements in the rows 1 through 3 and
Page

1 3 5 7 9 11
all the columns in matrix A.
Digital Signal Processing Lab Manual

2 4 6 8 10 12
3 6 9 12 15 18
>> F = A(1:3,2:4)
F= Define a column matrix F from the elements in
3 5 7 the rows 1 through 3 and the columns 2
4 6 8 through 4 in matrix A.
6 9 12
It is possible, to select only specific elements, or specific rows and columns
of existing variables to create new variables. This is done by typing the selected
elements or rows or columns inside brackets, as shown below:
>> v = 4:3:34 Create a vector v with 11 elements.
v=
4 7 10 13 16 19 22 25 28 31 34
>> u = v([3,5,7:10])
Create a vector u from the 3rd, the 5th , and
u= 7th through 10th elements of v
10 16 22 25 28 31
>> A = [10:-1:4;ones(1,7);2:2:14;zeros(1,7)]
A=
10 9 8 7 6 5 4 Create a 4 X 7 matrix A.
1 1 1 1 1 1 1
2 4 6 8 10 12 14
0 0 0 0 0 0 0
>> B = A([1,3],[1,3,5:7]) Create a matrix B from the 1st and
B= 3rd rows and 1st,3rd , and 5th through
10 8 6 5 4 7th columns of A.
2 6 10 12 14
8) Adding Elements to Existing Variables
A variable that exists as a vector, or a matrix, can be changed by adding elements
to it.
8.1) Adding elements to a vector:
Elements can be added to an existing vector by assigning values to the
new elements
Example:- Define vector DF with 4 elements
>> DF = 1:4
DF =
1 2 3 4
>> DF(5:10)=10:5:35 Adding 6 elements starting with the 5th.
DF =
14

1 2 3 4 10 15 20 25 30 35
Page

>> AD = [5 7 2] Define vector AD with 3 elements


Digital Signal Processing Lab Manual

AD =
5 7 2
Assign a value to the 8th element.
>> AD(8)=4
AD =
5 7 2 0 0 0 0 4 MATLAB assignd zeros to the 4th
>> AR(5)=24 through 7th elements,
AR =
0 0 0 0 24
8.2) Adding elements to a matrix:
Rows and/or columns can be added, to an existing matrix by assigning values to
the new rows or columns. This can be done by assigning new values, or by appending
existing variables. This must be done carefully since the size of the added rows or
columns must fit the existing matrix.
Examples:
>> E = [1 2 3 4; 5 6 7 8] Define a 2X4 matrix E.
E=
1 2 3 4
5 6 7 8
>> E(3,:)=[10:4:22] Adding the vector 10 14 18 22 as
E= the third row of E.
1 2 3 4
5 6 7 8
10 14 18 22
>> K = eye(3) Define a 3X3 matrix K.
K=
1 0 0
0 1 0
0 0 1
>> G = [E K] Append the matrix K to matrix E. the
G= number of rows in E and K must be the
1 2 3 4 1 0 0 same.
5 6 7 8 0 1 0
10 14 18 22 0 0 1
9) Built-in Functions for Handling Arrays:-
MATLAB has many built-in functions for managing and handling arrays. Some of
these are listed below:
15
Page

Built-in functions for handling arrays


Digital Signal Processing Lab Manual

Function Description Example


length (A) Returns the number of elements in >> A = [5 9 2 4];
the vector A. >> length(A)
ans =
4
size(A) Returns a row vector [m, n] , >> A = [6 1 4 0 12; 5 19 6 8 2];
where m and n are the size m X n >> size(A)
of the array A. ans =
2 5
reshape(A,m,n) Rearrange a matrix A that has r >> A = [5 1 6; 8 0 2]
rows and s columns to have m A=
rows and n columns. r times s 5 1 6
must be equal to m times n. 8 0 2
>> B = reshape(A,3,2)
B=
5 0
8 6
1 2
diag(v) When v is a vector, creates a >> v = [7 4 2]
square matrix with the elements of v =
v in the diagonal. 7 4 2
>> A = diag(v)
A=
7 0 0
0 4 0
0 0 2
diag(A) When A is a matrix, creates a >> A = [1 2 3; 4 5 6; 7 8 9]
vector from the diagonal elements A =
of A. 1 2 3
4 5 6
7 8 9
>> vec = diag(A)
vec =
1
5
9
16
Page

Assignment 5:-
Digital Signal Processing Lab Manual

Create a row vector with 15 equally spaced elements in which the first element is
7 and the last element is 40.
Assignment 6:-
Create a column vector with 12 equally spaced elements in which the first
element is -1 and the last element is -15.
Assignment 7:-
Create the matrix shown below by using the vector notation for creating vectors
with constant spacing and/or the linspace command when entering the rows.

1 4 7 10 13 16 19 22 25 
B  72 66 60 54 48 42 36 30 24 
 
 0 0.125 0.250 0.375 0.500 0.625 0.750 0.875 1.000
Assignment 8:-

Create the following matrix A:


 6 43 2 11 87
A   12 6 34 0 5 
 
 34 18 7 41 9 
Use the matrix A to:
a) Create a five-element row vector named va that contains the elements of the
second row of A.
b) Create a three-element row vector named vb that contains the elements of the
fourth column ofA.
c) Create a ten-element row vector named vc that contains the elements of the
first and second rows of A.
d) Create a six-element row vector named vd that contains the elements of the
second and fifth columns of A.
Assignment 9:-
Create the following matrix c:
2 4 6 8 10 
A  3 6 9 12 15 
 
7 14 21 28 35
Use the matrix C to:
17

a) Create a three-element column vector named ua that contains the elements of


Page

the third column of C.


Digital Signal Processing Lab Manual

b) Create a five-element column vector named ub that contains the elements of


the second row of c:
c) Create a nine-element column vector named uc that contains the elements of
the first, third and fifth columns of C.
d) Create a ten-element column vector named ud that contains the elements of
the first and second rows of C.
Assignment 10:-
Create the following matrix A:

I 2 3 4 5 6 7
 2 4 6 8 10 12 14 
A 
211815 12 9 6 3 
 5 1015 20 25 30 35
 
a) Create a 3 x 4 matrix B from the 1st, 3rd, and 4th rows, and the 1st, 3rd through
5th, and 7th columns of the matrixA.
b) Create a 15 elements-long row vector u from the elements of the third row,
and the 5th and 7th columns of the matrixA.

18
Page

You might also like