Digital Signal Processing Lab Manual
Experiment # 4
FUNCTIONS AND FUNCTION FILES
PURPOSE:
To learn
1. how to structure, define and call a function file
2. how to access the global and local variables within function file
3. how to save data values from function file
4. how to send and receive data values from function file Personal computer (PC) with windows
operating system and MATLAB software
THEORY:
Many functions are programmed inside MATLAB as built-in functions, and can be used in mathematical
expressions simply by typing their name with an argument examples are sin (x), cos (x), sqrt(x), and exp (x).
Frequently, in computer programs, there is a need to calculate the value of functions that are not built-in.
When a function expression is simple and needs to be calculated only once, it can be typed as part of the
program. However, when a function needs to be evaluated many times for different values of arguments it is
convenient to create a ''user defined" function. Once the new function is created (saved) it can be used just
like the built-in functions.
1) Creating a Function File:-
Function files are created and edited, like script files, in the Editor/Debugger Window. This window
is opened from the Command Window. In the File menu, select New, and then select M-fIle.
2) Structure of a Function File:- The structure of a typical function file is shown in Figure below.
2.1) Function Definition Line:-
The first executable line in a function file must be the function definition line. Otherwise the file is
considered a script file. The function definition line:
• Defines the file as a function file
• Defines the name of the function.
Prepared By: ENGR. FATIMA QAZI (Lecturer)
Digital Signal Processing Lab Manual
• Defines the number and order of the input and output arguments.
Input and Output Arguments:-
The input and output arguments are used to transfer data into and out of the function. The input
arguments are listed inside parentheses following the function name. Usually, there is at least one
input argument, although it is possible to have a function that has no input arguments. If there are
more than one, the input arguments are separated with commas. The following are example of
function definition lines with different combinations of input and output arguments.
2.2) The H1 Line and Help Text Lines:-
Two input arguments, one output argument. Same as above, one output argument can be typed
without the brackets. The H1 line and help text lines are comment lines (lines that begin with the
percent% sign) following the function definition line. They are optional, but frequently used to
provide information about the function. The comment lines that are typed between the function
definition line and the first non-comment line are displayed when the user types help function_name
in the Command Window.
2.3) Function Body:-
The function body contains the computer program (code) that actually performs the computations.
The code can use all MATLAB programming features. This includes calculations, assignments, any
built-in or user-defined functions, flow control, comments, blank lines, and interactive input and
output.
3) Inline Functions Function files can be used for simple mathematical functions, for large and
complicated math functions that require extensive programming, and as subprograms in large
computer programs. In cases when the value of a relatively simple mathematical function has to be
determined many times within a program, MATLAB provides the option of using inline functions.
An inline function is defined within the computer code (not as a separate file like a function file) and
is then used in the code. Inline functions can be defined in any part of MATLAB. Inline functions are
created with the inline command according to the following format:
Prepared By: ENGR. FATIMA QAZI (Lecturer)
Digital Signal Processing Lab Manual
PROCEDURE:
Execute the following example in MATLAB
1) The function:
Example:- Write a function file for the function
the input to the function is x and the output is f(x).
Write the function such that x can be a vector. Use the function to calculate:
a) f(x) for x =6.
b) f(x) for x = 1,3,5,7,9, and 11.
Solution:
Open the Editor/Debugger Window. This window is opened from the Command
Window.
In the File menu, select New, and then select M-file.
Once the Editor/Debugger Window opens write the following function in it
Make sure the m-file location and MATLAB script file location are the same
Figure 1 show the directory for matlab script file
Figure 2: mfile location is the same to run the code without error
1. f(x) for x =6.
The variable [y] represents the output defined in the function, while 'exp4one' is the
name of the function that takes a single input argument.
After defining the input and output, we write an expression and then conclude the
function.
Finally, we call the function in the command window with the given input to calculate
the expression for different input values.
Figure 3 shows the MATLAB code
Figure 4: call function in MATLAB script file
Prepared By: ENGR. FATIMA QAZI (Lecturer)
Digital Signal Processing Lab Manual
2. f(x) for x = 1,3,5,7,9, and 11.
Create the input array and call the function that evaluates the expression at
different values of x
Figure 5 calls the function in the script to get the value at 1,3,5,7,11, respectively
The inline Function:
The function:
can be defined (in the Command Window) as an inline function for x as a scalar by:
Figure 6 shows a function define in the command window and used it again and again
If there are two variables then the f(x, y) = 2x^2- 4xy+y^2 can be defined as an inline function by:
Figure 7 shows MATLAB arranges the arguments in alphabetical order. The function can be used for
different values of x and y.
EXERCISES:
Converting temperature units
Prepared By: ENGR. FATIMA QAZI (Lecturer)
Digital Signal Processing Lab Manual
Write a user-defined function (name it FtoC) that converts temperature in degrees F to temperature in
degrees C. Use the function to convert a) 32 degrees F to degrees C. 32, 35,40,60,80 degrees F to degrees C.
Solution
To create an m-file, click on "New Script" or use the shortcut ‘Ctrl + N’.
In this file, define the input and output variables: the temperature in degrees Fahrenheit (F) will be
the input, and the temperature in degrees Celsius (C) will be the output.
Use the appropriate formula to convert Fahrenheit to Celsius.
Finally, call the function in the command window to calculate the Celsius value for different
Fahrenheit inputs.
Figure 8 shows the conversion of temperature
CONCLUSION:
This experiment demonstrated the effectiveness of creating functions in MATLAB to enhance efficiency
and reduce redundancy in coding. By defining a function, we can execute complex operations multiple times
without rewriting code repeatedly, thus saving time and minimizing errors. We also explored the use of
inline functions directly in the Command Window, allowing us to perform quick calculations without formal
function definitions. It is essential to ensure that the location of the `.m` file matches the Command
Window's working directory for the code to execute properly. Overall, this exercise highlighted the ease of
coding in MATLAB and the advantages of utilizing functions for streamlined programming.
Note
Prepared By: ENGR. FATIMA QAZI (Lecturer)
Digital Signal Processing Lab Manual
Experiment # 5
ELEMENTARY SEQUENCES
PURPOSE:
To learn
1. How to plot a unit sample sequence and its shifted version
2. How to plot the unit step sequence and its shifted version
3. How to plot a real-valued exponential sequence
4. How to plot a complex-valued exponential sequence
5. How to plot sinusoidal sequences
EQUIPMENT:
Personal computer (PC) with windows operating system and MATLAB software
INTRODUCTION:
A discrete time signal is represented as a sequence of numbers, called samples. These samples are
denoted by x(n) where the variable n is integer valued and represents in discrete instances in time.
An example of a discrete-time signal is:
where the up arrow indicates the sample at n = 0
In MATLAB, a finite-duration sequence is represented by a row vector. However, such a vector
does not have any information about sample position n. Therefore, a correct representation of x(n)
would require two vectors, one each for x and n, To represent the sequence defined in eq1, the
following MATLAB command can be used:
PROCEDURE:
1. Unit sample sequence:
Prepared By: ENGR. FATIMA QAZI (Lecturer)
Digital Signal Processing Lab Manual
In MATLAB the function zeros (1, N) generates a row vector of N zeros, which can be used to
implement δ (n) over a finite interval. However, the logical relation n==0 is an elegant way of
implementing δ (n). For example, to implement
over the interval, we will use the following MATLAB function.
1. Define a function named impseq (given below) in the script file.
2. Call the function with the proper input and output arguments in the main window.
3. Title and label the resulting plot properly.
Figure 9 shows a function to create an impulse signal using an m-file
Figure 10 calls impulse sequence function and use stem to create a discrete impulse signal
Figure 11 shows an output of m-file and command window
Task 1:
Prepared By: ENGR. FATIMA QAZI (Lecturer)
Digital Signal Processing Lab Manual
Generate and plot the sequence
δ(n–30) -20 ≤ n ≤ 120
Figure 12 in command window just change the values of n0,n1 and n2
Figure 13 shows impulse signal at δ(n–30)
2. Unit step sequence:
In MATLAB the function ones(1,N) generates a row vector of N ones. It can be used to generate
u( n) over a finite interval. Once again an elegant approach is to use the logical relation n>=0. To
implement
over the interval, we will use the following MATLAB function. 1. Define a function
named stepseq (given below) in script file. 2. Call the function with the proper input and output
arguments in the main window. 3. Title and label the resulting plot properly.
Prepared By: ENGR. FATIMA QAZI (Lecturer)
Digital Signal Processing Lab Manual
Figure 14 shows a step sequence function takes interval and generate unit step sequence in m-file
Figure 15 shows command window to call function and stem is used to generate discrete time unit step
sequence with label and title
Figure 16 shows output of above commands using m file and command window
Task 1:
Generate and plot the sequence
Figure 17 change n0,n1 and n2 value change to create the function
Prepared By: ENGR. FATIMA QAZI (Lecturer)
Digital Signal Processing Lab Manual
Figure 18 shows output of the discrete signal u(n+5)
3. Real-valued exponential sequence:
In MATLAB an array operator “.^” is, required to implement a real exponential sequence.
Example 02
Figure 19 shows implementation of exponential sequence(0.9)^n where n is from 0till10
Figure 20 shows exponential sequence
Task 3:
Generate and plot the sequence
Figure 21 shows code for exponential sequential
Prepared By: ENGR. FATIMA QAZI (Lecturer)
Digital Signal Processing Lab Manual
Figure 22 shows output of exponential sequence
4. Complex-valued exponential sequence:
Where σ is called an attenuation and wo is the frequency in radians. A MATLAB function exp is used to
generate exponential sequences.
Example:-
Figure 23 shows code to implement complex exponential sequence
Figure 24 shows real and imaginary part of complex exponential
Prepared By: ENGR. FATIMA QAZI (Lecturer)
Digital Signal Processing Lab Manual
Figure 25 shows code for discrete sinusoidal sequence
Figure 26 shows an output of sinusoidal sequence
Example
Generate and plot each of the following sequences over the indicated interval.
a.
Figure 27 shows the m-file, command window, and the output of the given impulse signal
Prepared By: ENGR. FATIMA QAZI (Lecturer)
Digital Signal Processing Lab Manual
Figure 28 shows the code of the m-file, the command window, and the output signal for both the a and b
parts. The subplot function is used to represent both on the same figure
EXERCISE:
Generate and plot each of the following sequences over the indicated interval
Figure 29 shows code for exponential expression sequence for interval 0 till 20
Figure 30 shows the output of sequence
Prepared By: ENGR. FATIMA QAZI (Lecturer)
Digital Signal Processing Lab Manual
Figure 31 shows a code for sequence of given signal
Figure 32 shows the output generated by above code
Figure 33 shows a code for discrete sin signal
Figure 34 shows output signal for 0 till 10 time
Prepared By: ENGR. FATIMA QAZI (Lecturer)
Digital Signal Processing Lab Manual
Figure 35 shows a code for given discrete signal at -10 till 10 interval
Figure 36 shows output of given discrete signal
Figure 37 shows code for discrete sin signal expression
Figure 38 shows output discrete signal
Prepared By: ENGR. FATIMA QAZI (Lecturer)
Digital Signal Processing Lab Manual
Figure 39 shows code for discrete exponential sequence
Figure 40 shows output of signal
Figure 41 show code for given complex exponential sequence
Figure 42 show discrete complex exponential sequence
CONCLUSION:
Prepared By: ENGR. FATIMA QAZI (Lecturer)
Digital Signal Processing Lab Manual
This experiment exercise successfully demonstrated the fundamental principles of generating and
visualizing discrete-time signals using MATLAB. By implementing the code for unit sample, unit step, real,
and complex exponential sequences, we gained a practical understanding of their mathematical definitions
and visual representations. The use of the stem function proved essential for accurately plotting these
signals, including their time-shifted versions. This hands-on experience solidified our ability to translate
theoretical signal concepts into practical, visual forms, which is a critical skill for further studies in digital
signal processing.
Prepared By: ENGR. FATIMA QAZI (Lecturer)