[go: up one dir, main page]

0% found this document useful (0 votes)
67 views13 pages

University of Engineering & Technology Lahore: Experiment

This document provides instructions for an experiment involving functions and function files in MATLAB. It discusses how to create and structure function files, including the function definition line, help text, function body, and assigning output arguments. It also discusses inline functions and provides an example of defining an inline function. Finally, it provides an assignment to write a user-defined function in MATLAB to convert temperatures from Fahrenheit to Celsius.

Uploaded by

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

University of Engineering & Technology Lahore: Experiment

This document provides instructions for an experiment involving functions and function files in MATLAB. It discusses how to create and structure function files, including the function definition line, help text, function body, and assigning output arguments. It also discusses inline functions and provides an example of defining an inline function. Finally, it provides an assignment to write a user-defined function in MATLAB to convert temperatures from Fahrenheit to Celsius.

Uploaded by

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

Digital Signal Processing Lab Manual

University of Engineering & Technology Lahore


Faculty of Engineering
Experiment # 4
Title: Functions and Function Files.
Equipment Required: 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.

27
Page
Digital Signal Processing Lab Manual

Function definition line

The H1 line

Help text

Function body
(Computer program).

Assignment values to output arguments.

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.
 Defines the number and order of the input and output arguments.

function [output arguments] = function_name (input arguments)

The word function must be A list of output The name A list of input
the first word, and must arguments typed of the arguments typed
be typed in lower-case inside brackets. function. inside parentheses.
letters.

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
28

are separated with commas. The following are example of function definition lines with
Page

different combinations of input and output arguments.


Digital Signal Processing Lab Manual

Function definition line Comments

function[mpay,tpay]= loan(amount,rate,years) Three input arguments, two output arguments.

function [A] =RectArea(a,b) Two input arguments, one output argument.

function A = RectArea( a, b) Same as above, one output argument can be


typed without the brackets.

2.2) The H1 Line and Help Text Lines:-

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:

name = inline('math expression typed as a string')


29
Page
Digital Signal Processing Lab Manual

Procedure:-
Execute the following example in MATLAB

1) The function:-

Example:-

Write a function file for the function 4


x 3x  5 the input to the function is x
f ( x) 
( x  1)
2 2

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.

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

function [y] = exp4one(x) Function definition line


y= (x.^4.*sqrt(3*x+5))./(x.^2+1).^2; Assignment to output argument.

a) Calculating the function for x = 6 can be done by typing exp4one(6) in the


Command window
>> exp4one(6)
ans =
4.5401
To calculate the function for several values of x, a vector with the values of x is first
created, and then used for the argument of the function.
>> x = 1:2:11
x=
1 3 5 7 9 11
>> exp4one(x)
ans =
0.7071 3.0307 4.1347 4.8971 5.5197 6.0638
30
Page
Digital Signal Processing Lab Manual

The inline Function:-


x2
The function e can be defined (in the Command Window) as an inline
f ( x) 
x 5
2

function for x as a scalar by:

>> FA = inline('exp(x^2)/sqrt(x^2+5)')
FA =
Inline function:
FA(x) = exp(x^2)/sqrt(x^2+5)

Then the value of f(x) at different value of x can be calculated as


>> FA(2)
ans =
18.1994
>> FA(3)
ans =
2.1656e+003

If there are two variables then the f(x, y) = 2x2- 4xy+y2 can be defined as an inline
function by:
>> HA = inline('2*x^2-4*x*y+y^2')
HA =
Inline function:
HA(x,y) = 2*x^2-4*x*y+y^2

MATLAB arranges the arguments in alphabetical order. The function can be used for
different values of x and y. For example,
HA(2,3)gives:

>> HA(2,3)
ans =
-7 31
Page
Digital Signal Processing Lab Manual

Assignment :- Converting temperature units


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.
b) 32, 35,40,60,80 degrees F to degrees C.

32
Page
Digital Signal Processing Lab Manual

University of Engineering & Technology Lahore


Faculty of Engineering
Experiment # 5
Title: Elementary Sequence.

Equipment Required: 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:

x(n) = {2 ,1 ,-1 ,0 ,1 ,4 ,3 ,7} …(1)

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:

>> n = [-3,-2,-1,0,1,2,3,4] x=[2,1,-1,0,1,4,3,7]

We use several elementary sequences in digital signal processing for analysis purposes.
Their definitions and MATLAB representations are given below.

Procedure:-
1. Unit sample sequence:

1, n  0 ........., 0,0,1,0,0,...... 


 (n)    
33

0, n  0   
Page
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

1, n  no
 ( n  no )  
0, n  no

over the n1  n0  n2 interval, we will use the following MATLAB function.

function [x,n] = impseq(n0,n1,n2)


% Generates x(n) = delta(n-nO); n1 <= n <= n2
% ---------------------------------------------¬
% [x,n] = impseq(n0,n1,n2)
%
n= [n1:n2];
x = [(n-n0) == 0];

MATLAB Script:-

% Generation of a Unit Sample Sequence


% Generate a vector from -10 to 20
[x,n]=impseq(1,-10,20)
%plot the unit sample sequence
stem(n,u);
xlabel(‘time index n’);ylabel(‘Amplitude’);
title(‘Unit Sample Sequence’);
axis([-10 20 0 1.2]);

Task 1:
Generate and plot the sequence

δ(n–30) -20≤n≤120
34

MATLAB CODE:-
Page
Digital Signal Processing Lab Manual

2. Unit step sequence:


1, n  0 ....., 0,0,1,1,1,....... 
u (n)    
0, n  0   
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
1, n  n o
u ( n  no )  
0, n  n o
over the n1  n0  n2 interval, we will use the following MATLAB function.

function [x,n] = stepseq(n0,n1,n2)


% Generates x(n)= u(n-nO); n1 <= n <= n2
%---------------------------------------------------------------
% [x,n] = stepseq(n0,n1,n2)
%
n = [n1:n2]; x = [(n-n0) >= 0];

Exmaple:- Generate and plot the sequence


u(n-5) -20≤n≤10

Scrip File:-

% Generation of a Unit Step Sequence


% Generate a vector from -20 to 10
[x,n]=stepseq(5,-20,10);
%plot the unit sample sequence
stem(n,x);
xlabel('time index n');ylabel('Amplitude');
title('Unit Step Sequence');
axis([-20 10 0 1.2]);
35
Page
Digital Signal Processing Lab Manual

Task 2:
Generate and plot the sequence
u(n+5) -20≤n≤20

MATLAB CODE:-

3. Real-valued exponential sequence:-

x(n)  a n , n; a  
In MATLAB an array operator “.^” is, required to implement a real exponential
sequence.
Example:-
Generate x(n)  0.9 0  n  10 ,
n

MATLAB script:
>>n = [0:10]; x = (0.9).^n;
>>stem(n,x);

Task 3:
Generate and plot the sequence
x( n)   10  10  n  10
n

MATLAB CODE:-
36
Page

4. Complex-valued exponential sequence:


Digital Signal Processing Lab Manual

x(n)  e (  jwo ) n
Where σ is called an attenuation and wo is the frequency in radians. A MATLAB function
exp is used to generate exponential sequences.
Example:-
Generate x(n) = exp [(2 + j3) n] , 0  n  10 ,

MATLAB script:-
n = [0:10]; x = exp((2+3j)*n);
subplot(2,1,1); .
stem(n,real(x));
xlabel('Time index n');ylabel('Amplitude');
title('Real part');
subplot(2,1,2);
stem(n,imag(x));
xlabel('Time index n');ylabel('Amplitude');
title('Imaginary part');

5. Sinusoidal sequence:

x(n)  cos(w n   ), n
o

where θ is the phase in radians. A MATLAB function cos (or sin) is used to generate
sinusoidal sequences.
Example,
Generate x(n)  cos(0.1n   / 3)  2 sin( 0.5n) 0  n  10 ,

MATLAB script:
n = [0:10]; x = 3*cos(0.1*pi*n+pi/3) + 2*sin(0.5*pi*n);
37
Page

EXAMPLE
Digital Signal Processing Lab Manual

Generate and plot each of the following sequences over the indicated interval.
a. x(n)  2 (n  2)   (n  4), 5  n  5
MATLAB script:

n = [-5 : 5];
x = 2*impseq(-2,-5,5) - impseq(4,-5,5);
subplot(2,1,1);stem(n,x); title('Sequence in example a')
xlabel ('n'); ylabel('x(n)');

The plot of the sequence is shown in Figure a

.03( n 10 )
b. x(n)  n[u (n)  u (n  10)]  10e [u (n  10)  u (n  20)] 0  n  20
MATLAB script:

n = [0:20];
x1 = n.*(stepseq(0,0,20)-stepseq(10,0,20));
x2 = 10*exp(-0.3*(n-10)).*(stepseq(10,0,20)-stepseq(20,0,20));
x = x1+x2;
subplot(2,1,2); stem(n ,x); title('Sequence in example b');
xlabel(' n '); ylabel('x (n)');
The plot of the sequence is shown in Figure b.
Sequence in example a
2

1.5

1
x(n)

0.5

-0.5

-1
-5 -4 -3 -2 -1 0 1 2 3 4 5
n

Sequence in example b
10

6
x (n)

0
0 2 4 6 8 10 12 14 16 18 20
n
38
Page
Digital Signal Processing Lab Manual

Assignments:

Generate and plot each of the following sequences over the indicated interval

1. x[n] = cos πn/3 + sin πn/3 0 ≤ n ≤ 20

2. y[n] = nx[n] 0 ≤ n ≤ 20 x[n] is given in qs.1

3. x[n] = sin πn/4 0 ≤ n ≤ 10

4. y[n] = x[n]/(πn/4) -10 ≤ n ≤ 10 x[n] is given in qs.3

5. x[n] = (sin πn/4)/(sin π/4) -20 ≤ n ≤ 20

39
Page

You might also like