University of Engineering & Technology Lahore: Experiment
University of Engineering & Technology Lahore: Experiment
27
Page
Digital Signal Processing Lab Manual
The H1 line
Help text
Function body
(Computer program).
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:
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.
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
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.
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:
Procedure:-
Execute the following example in MATLAB
1) The function:-
Example:-
and the output is f(x). Write the function such that x can be a vector. Use the function to
calculate:
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
>> FA = inline('exp(x^2)/sqrt(x^2+5)')
FA =
Inline function:
FA(x) = exp(x^2)/sqrt(x^2+5)
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
32
Page
Digital Signal Processing Lab Manual
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:
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:
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
MATLAB Script:-
Task 1:
Generate and plot the sequence
δ(n–30) -20≤n≤120
34
MATLAB CODE:-
Page
Digital Signal Processing Lab Manual
Scrip File:-
Task 2:
Generate and plot the sequence
u(n+5) -20≤n≤20
MATLAB CODE:-
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
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.1n / 3) 2 sin( 0.5n) 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)');
.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
39
Page