Professional Documents
Culture Documents
Talha - DSP - GCU - Lab1 - 2
Talha - DSP - GCU - Lab1 - 2
Talha - DSP - GCU - Lab1 - 2
GCU Lahore
Department of Electrical Engineering
Experiment # 1
Title: Starting with MATLAB.
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
>>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
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
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
( 453 ) 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
abcd
Page
Digital Signal Processing Lab Manual
GCU Lahore
Department of Electrical Engineering
Experiment # 2
Title: Arrays.
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
2) Creating a vector with constant spacing by specifying the first term, the spacing,
and the last term
Example:-
>>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
Example:
Digital Signal Processing Lab Manual
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
>> 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
-9
Digital Signal Processing Lab Manual
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
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
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
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:-
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