0% found this document useful (0 votes)
14 views

DSP Lab 2

Uploaded by

Hafiz Muhammad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

DSP Lab 2

Uploaded by

Hafiz Muhammad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

NATIONAL UNIVERSITY OF TECHNOLOGY

(NUTECH)
DEPARTMENT OF ELECTRICAL ENGINEERING

Experiment No: 02
To Construct Functions, Control Flow, Relational and Logical
Statements and sequence generators
OBJECTIVE:
The basic objective of this Lab is to familiarize the students with MATLAB function
files. How to use different functions and commands in MATLAB. Students will be
able to generate basic DT sequences & their operations. After performing this lab,
students will be able to do following tasks in Matlab:
● How to make function files
● How to use loops in Matlab
● Generating different sequences.

1. FUNCTION M-FILES:
Most of the M-files that one ultimately uses will be function M-files. These files have the .m
extension, but they are used in a different way than scripts. Function files have input and
output arguments, and behave like C-functions. In order to create a function M-file in
MATLAB, click:
File 🡺 New🡺 M-file
The structure of a typical function file, say my_fun.m, is as follows:

Note that the word “function” appears at the start of the file, and in lower case letters. In
addition, the outputs, inputs, and name of the function should be listed.

For example, if we wish to plot 𝑥𝑠𝑖𝑛(3𝑥 )e 𝜋, 𝜋]


2
−x
2
4 between the interval [

Suppose that instead of giving the vector x, we want to make it a variable. At the same time,
we want to have the data that we plotted. Here is a function file that would do this.

1
NATIONAL UNIVERSITY OF TECHNOLOGY
(NUTECH)
DEPARTMENT OF ELECTRICAL ENGINEERING

Now, in order to execute the function M-file, there should be some input to the function. For
example: in the above case if we write my_fun on the command terminal an error will occur
that the input arguments are not available. So, in order to execute the function, input
arguments must be given to the function, following command should be written on command
terminal.
>> x=-pi:pi/40:pi;
>>my_fun(x)

Function files are normally used to combine functions in MATLAB to get new functions.
For example, suppose that we want to have at our disposal a function that computes the
inverse of the square of a matrix, and returns an error message if the matrix is close to
singular or singular. Call this file inv_sq.m. One more thing, in the code below, note that A^2
is used, not A.^2. This is because we are computing the square of a matrix, not a matrix with
the entries of A squared.

2. INTRODUCTION TO CONTROL FLOW STATEMENTS


MATLAB has the following flow control constructs:
a. if statements
b. switch statements
c. for loops
d. while loops
e. break statement

2
NATIONAL UNIVERSITY OF TECHNOLOGY
(NUTECH)
DEPARTMENT OF ELECTRICAL ENGINEERING

The if, for, switch and while statements need to terminate with an end statement.
2.1: WHILE LOOP
while loop is used to repeat a specific argument when condition is true
Syntax:
while expression
statements
end
2.1.1: NESTED WHILE LOOP
Syntax:
while <expression1>
while <expression2>
<statements>
end
end
2.2: IF LOOP
Execute statements if condition is true
Syntax:
if expression
statements
else if expression
statements
else
statements
end
2.3: FOR LOOP
Syntax:
for index = values
statements
end

3
NATIONAL UNIVERSITY OF TECHNOLOGY
(NUTECH)
DEPARTMENT OF ELECTRICAL ENGINEERING

2.3.1: NESTED FOR LOOP


MATLAB allows to use one loop inside another loop.
Syntax:
The syntax for a nested for loop statement in MATLAB is as follows:

For m=1:j

For n=1:k

<statements>;

end

end

2.4: SWITCH STATEMENTS


Execute several groups of statements
Syntax:
switch switch_expression
case case_expression
statements
case case_expression
statements
...
otherwise
statements
end

2.5: Break
The break statement lets you exit early from a for or a while loop.
Syntax:
while expression
statements
break expression
break
end

4
NATIONAL UNIVERSITY OF TECHNOLOGY
(NUTECH)
DEPARTMENT OF ELECTRICAL ENGINEERING

3. SOME FUNDAMENTAL SIGNAL SEQUENCE


3.1: UNIT STEP
The Heaviside step function, or the unit step function, usually denoted by u, is a
discontinuous function whose value is zero for negative argument and one for positive
argument.
µ[n] = {0 ,n< 01 ,n ≥ 0

Unit Step Sequence


3.2: UNIT IMPULSE SEQUENCE
Unit Impulse sequence is very important to characterize the impulse response of the
system. Mathematically an impulse sequence is represented as:
µ [n] = {0 ,n ≠ 01 , n=0

4. SINUSOIDAL SEQUENCE
A matlab function “cos” (or sin) is used to generate sinusoidal sequences.
To generate x(n) = 3cos(0.1πn + π/3) + 2sin(0.5 πn), 0<= n<=10, we will need the following
script:
n = [0:10];

5
NATIONAL UNIVERSITY OF TECHNOLOGY
(NUTECH)
DEPARTMENT OF ELECTRICAL ENGINEERING

x = 3*cos(0.1*pi*n+pi/3) + 2*sin(0.5*pi*n);

In-Lab Tasks
1. Run the Matlab code described in section 1.
2. Run the Matlab code for all control flow statements described in section 2.
3. Determine a value falls within a specified range using IF Else statements.
4. Initialize a vector A having 20 rows. Replace the values of this vector from row 5 to row
15 by this equation 5*(n-1) using for loop.
5. Use switch-case statements to categorize the 12 months into 4 seasons i.e summer, spring,
winter, autumn.
6. Implement a unit step sequence in MATLAB and plot it, provide the snapshot.
7. Provide the snapshot of the following pulses using built-in MATLAB commands.
Rectangular pulse, Saw tooth, Triangular, Square.

Post Lab Tasks


1. Perform Q3 of In-Lab Task using function file.
2. Implement a unit step sequence in MATLAB using function file and plot it. Provide
the snapshot.
3. Implement a unit impulse sequence in MATLAB using function file and plot it.
Provide the snapshot.

Comments:

6
NATIONAL UNIVERSITY OF TECHNOLOGY
(NUTECH)
DEPARTMENT OF ELECTRICAL ENGINEERING

You might also like