DSP Lab 2
DSP Lab 2
(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.
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
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
For m=1:j
For n=1:k
<statements>;
end
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
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.
Comments:
6
NATIONAL UNIVERSITY OF TECHNOLOGY
(NUTECH)
DEPARTMENT OF ELECTRICAL ENGINEERING