0% found this document useful (0 votes)
22 views100 pages

EEE-314 Software Based

The document is a lab manual for a Digital Signal Processing course (EEE-314) that outlines course objectives, outcomes, and a weekly schedule of experiments. It emphasizes the use of MATLAB for signal generation, analysis, and digital filter design, while also providing references and a detailed introduction to MATLAB functionalities. Students are expected to develop practical skills in signal processing through hands-on experiments and teamwork.

Uploaded by

Sabab jamil
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)
22 views100 pages

EEE-314 Software Based

The document is a lab manual for a Digital Signal Processing course (EEE-314) that outlines course objectives, outcomes, and a weekly schedule of experiments. It emphasizes the use of MATLAB for signal generation, analysis, and digital filter design, while also providing references and a detailed introduction to MATLAB functionalities. Students are expected to develop practical skills in signal processing through hands-on experiments and teamwork.

Uploaded by

Sabab jamil
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/ 100

LAB MANUAL

(Software Based)

Program : B.Sc. in EEE


Course Code : EEE-314
Course Title : Digital Signal Processing Lab
Course Credit : 1.50
Contact Hours : 3 hrs

EEE-314-1 | P a g e
Introduction

Signal generation and visualization, transformations on signals. Convolution and correlation and their
application. Z-transformation, analysis systems using z-transformation. Fourier series and
transformation, analysis of signals and systems using Fourier series and transformation. Design of
digital filters.

1. Course Objectives
This course will introduce the basic concepts and techniques for processing signals. Students will be
familiar with the most important methods in DSP, including digital filter design and transform-
domain processing and analyzing. The computer-based experiments extend better understanding and
develop practical problem-solving skills.

2. Course Outcomes
Upon completion of this course students will be able to:

CO1: Use MATLAB to generate signals, describe systems, apply different types of signal
transformations such as shifting, scaling, folding and perform convolution and correlation.

CO2: Analyze discrete time systems in time and frequency domain using signal processing and
symbolic toolboxes of MATLAB.

CO3: Design different discrete time systems to meet specific practical requirements and limitations.

CO4: Work on complex engineering problems individually and in a team

CO5: Communicate and share knowledge, data, information, results etc. with others

3. Text Book
1. John Proakis. G., Dimtris G Manolakis, Digital Signal Processing - Principles, Algorithms and
Application, PHI, 2009.

2.John G Proakis and Vinay K. Ingle, Digital Signal Processing Using MATLAB, PWS Publishing
Company, 1997.

4. References
[1] Michael Weeks, Digital Signal Processing Using MATLAB & Wavelets, Infinity Science
Press, 2007.

EEE-314-2 | P a g e
[2] Jose Maria Giron-Sierra, Digital Signal Processing with MATLAB Examples, Volume 1:
Signals and Data, Filtering, Non-stationary Signals, Modulation, Springer Singapore, 2018.

[3] Samuel D. Stearns, Digital Signal Processing with Examples in MATLAB, CRC Press, 2016.

5. Weekly Schedule
Expt. No. Experiment Name
Week No. Page No.
1 Review of MATLAB Week1
2 Generation and visualization of continuous-time Week2
and discrete-time signals
Generation of basic discrete-time signals and
3 inspection of discrete-time sinusoid frequency Week3
properties.
4 Operations on discrete-time signals Week4
5 Discrete-time convolution summation Week5
6 Discrete-time correlation Week6
Mid Exam
Week7

7 Z-transformation and its application Week8


8 Discrete time Fourier series and its application Week9
9 Discrete time Fourier transform and its application Week10
10 Frequency domain analysis of discrete-time systems Week11
Halfway Project Show Week12

Working with individual team project. Week13


Final Examination Week14

EEE-314-3 | P a g e
Experiment No. 01:
Name of the Experiment: Review of MATLAB

Objective:
The objectives of this experiment are to:
1) Get refamiliarized with the MATLAB environment.
2) learn about important functions needed for digital signal processing lab.

Course Outcomes (COs), Program Outcomes (POs) and Assessment:

Domain /
Delivery
Corresponding level of Assessment
CO Statement methods and
PO learning tools
activities
taxonomy
CO1:Use
MATLAB to Lab tests
generate signals, Simulation Lab reports
describe systems,
apply different Application(PO Experiment Final lab
Affective test
types of signal 3) Practice lab
domain/
transformations Modern Tool Open ended
valuing level Group
such as shifting, Usage (PO5) lab
scaling, folding discussion
and perform Project
Tutorial
convolution and show& project
correlation. presentation

Theory:
Students are required to go through the steps explained below and then complete the exercises given
at the end of the lab. Remember, this is not an introductory course to MATLAB. You already have
been introduced to MATLAB in EEE 204 (Introduction to MATLAB). This is just to reengage you
with the environment.

Refamiliarization with MATLAB:


The following tips will be helpful in the future.
i. To add comment, the symbol is used "%" followed by the comment text.
ii. Help is provided by typing “help” or if you know the topic then “help function name” (for short
description) or “doc function name” for the full reference.
iii. If you don't know the exact name of the topic or command you are looking for, type "look for
keyword" (e.g., "look for regression")
iv. Three dots “...” are used to continue a statement to next line (row).
v. If after a statement “;” is entered, then MATLAB will not display the result of the statement
entered otherwise result would be displayed.
vi. Use the up-arrow to recall commands without retyping them (and down arrow to go forward in
commands).
vii. MATLAB is case sensitive so “taxila” is not same as “TAXILA”.
EEE-314-4 | P a g e
Required Devices & Software:
1. A well configured Laptop or PC.
2. Software: Latest Matlab version. (2014-2020).

Experimental simulation procedure:

Basic functionalities of MATLAB:

Defining a scalar x =1
x=
1
Defining a column vector v = [1;2;3]
v=
1
2
3
Defining a row vector w = [1 0 1]
w=
101
Transpose a vector wT = w’
wT =
1
0
1
Defining a range for a X = 1:.5:5
vector X=
Columns 1 through 7
1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000
Columns 8 through 9
4.5000 5.0000
Empty vector Y = []
Y=
[]
Defining a matrix M = [1 2 3; 3 2 1]
M=
1 2 3
3 2 1
Matrix of zeroes M = zeros(2,3) % 1st parameter is the number of row, 2nd
parameter is the number of column.
M=
0 0 0
0 0 0
Matrix of ones m = ones(2,3)
m=
EEE-314-5 | P a g e
1 1 1
1 1 1
The identity matrix I = eye(3)
I=
1 0 0
0 1 0
0 0 1
Define a random matrix R = rand(1,3)
or vector R=
0.9501 0.2311 0.6068
Access a vector or matrix R(3)
ans=
0.6068

R(1,2)
ans =
0.2311
Access a row or column I(2,:) %2nd row
of matrix ans =
0 1 0

I(:,2) %2nd col


ans =
0
1
0

I(1:2,1:2)
ans =
1 0
0 1
Size and length of matrix size(R)
ans =
13

length(R)
ans =
3

Operations on vector and matrices in MATLAB:


MATLAB utilizes the following arithmetic operators;
+ Addition
- Subtraction
* Multiplication
/ Division
^ Power Operator
EEE-314-6 | P a g e
‘ transpose

Some built in functions in MATLAB


abs magnitude of a number (absolute value for real numbers)
angle angle of a complex number, in radians
cos cosine function, assumes argument is in radians
sin sine function, assumes argument is in radians
exp exponential function

Arithmetic operations
x =[ 1 2 3 4 5]
x=
12345

x=2*x
x=
2 4 6 8 10

x=x/2
x=
12345

y=[12345]
y=
12345

z=x+y
z=
2 4 6 8 10

For point by point or element-wise multiplication/division use “.“


W = x.*y
W=
1 4 9 16 25

Matlab has a large number of built in functions, some operate on each point of avector/matrix:
log([1 2 3 4])
ans =
0 0.69311.0986 1.3863

round([1.5 2; 2.2 3.1])


ans =
2 2
23

a =[1 4 6 3]
EEE-314-7 | P a g e
a=
1 463

sum(a)
ans =
14

mean(a)
ans =
3.5000

std(a)
ans =
2.0817

max(a)
ans =
6

a = [1 2 3; 4 5 6]
mean(a) %mean of each column
max(a) %max of each column =20
max(max(a)) %to obtain max of matrix

Relational operators in MATLAB:

Operator Description
== Equal to
~= Not equal to
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to

Let
a = [1 1 3 4 1]
a=
11341

ind = (a == 1)
ind =
11001

ind = (a < 1)
ind =

EEE-314-8 | P a g e
00000

ind = (a > 1)
ind =
00110

ind = (a <= 1)
ind =
11001

ind = (a >= 1)
ind =
11111

ind = (a ~= 1)
ind =
00110

Control Flow in MATLAB:


To control the flow of commands, the makers of MATLAB supplied four types of commands a
programmer can use while writing his/her computer code.
i. the for loops
ii. the while loops
iii. the if-else-end constructions
iv. the switch-case constructions

Syntax of the for loop is shown below


for k = array
commands
end

The commands between the forand endstatements are executed for all values stored in the array.
Suppose that one-need values of the sine function at eleven evenly spaced pointsn/10, for n = 0, 1,
…, 10. To generate the numbers in question one can use the for loop.

for n=0:10
x(n+1) = sin(pi*n/10);
end

x=
Columns 1 through 7
00.30900.58780.80900.95111.00000.9511
Columns 8 through 11
0.80900.58780.30900.0000

The forloops can be nested

EEE-314-9 | P a g e
H = zeros(5);
for k=1:5
for l=1:5
H(k,l) = 1/(k+l-1);
end
end

H=
1.00000.50000.33330.25000.2000
0.50000.33330.25000.20000.1667
0.33330.25000.20000.16670.1429
0.25000.20000.16670.14290.1250
0.20000.16670.14290.12500.1111

Syntax of the whileloop is


while expression
statements
End
This loop is used when the programmer does not know the number of repetitions a priori.Here is an
almost trivial problem that requires a use of this loop. Suppose that thenumber isdivided by 2. The
resulting quotient is divided by 2 again. This process iscontinued till the current quotient is less than
or equal to 0.01. What is the largestquotient that is greater than0.01?To answer this question, we
write a few lines of code,

q = pi;
while q > 0.01
q = q/2;
end

q=
0.0061

Syntax of the simplest form of the construction under discussion is,


if expression
commands
end

This construction is used if there is one alternative only. Two alternatives require the construction:
if expression
commands (evaluated if expression is true)
else
commands (evaluated if expression is false)
end

If there are several alternatives one should use the following construction:
EEE-314-10 | P a g e
if expression1
commands (evaluated if expression 1 is true)
elseif expression 2
commands (evaluated if expression 2 is true)
elseif
...
...
else
commands (executed if all previous expressions are false)
end

Syntax of the switch-case construction is,


switch expression (scalar or string)
case value1 (executes if expression evaluates to value1)
commands
case value2 (executes if expression evaluates to value2)
commands
...
otherwise
commands
end
Switch compares the input expression to each case value. Once the match is found it executes the
associated commands.
In the following example a random integer number x from the set {1, 2, …, 10} is generated. If x =
1 or x = 2, then the message ‘Probability =20%’ is displayed to the screen. If x = 3 or 4 or 5, then the
message ‘Probability = 30%’ is displayed, otherwise the message ‘Probability = 50%’ is generated.
The script file fswitch.m utilizes a switch as a tool for handling all cases mentioned above.

% Script file fswitch.m.


x = ceil(10*rand); % Generate a random integer in {1, 2, ..., 10}
switch x
case {1,2}
disp('Probability = 20%');
case {3,4,5}
disp('Probability = 30%');
otherwise
disp('Probability = 50%');
end

Note: use of the curly braces after the word case. This creates the so called cell array rather than the
one-dimensional array, which requires use of the square brackets.

Creating functions using m-files:


Files that contain a computer code are called the m-files. There are two kinds of m-files: the script
files and the function files. Script files do not take the input arguments or return the output arguments.
The function files may take input arguments or return output arguments. To make the m-file click on
EEE-314-11 | P a g e
File next select New and click on m-File from the pull-down menu. You will be presented with the
MATLAB Editor/Debugger screen. Here you will type your code, can make changes, etc. Once
you are done with typing, click on File, in the MATLAB Editor/Debugger screen and select Save
As….Chose a name for your file, e.g., firstgraph.mand click on Save. Make sure that yourfile is
saved in the directory that is in MATLAB's search path. If you have at least two files with duplicated
names, then the one that occurs first in MATLAB's search path will be executed.
To open the m-file from within the Command Window type edit firstgraph and thenpress Enter
or Return key.

Here is an example of a small script file


% Script file firstgraph.
x = -10*pi:pi/100:10*pi;
y = sin(x)./x;
plot(x,y)
grid

Enter this code in the MATLAB editor and save it as firstgraph.m. This function call be
called from command line as
firstgraph
Here is an example of a function file
function [b, j] = descsort(a)
% Function descsort sorts, in the descending order, a real array a.
% Second output parameter j holds a permutation used to obtain
% array b from the array a.
[b ,j] = sort(-a);
b = -b;

Enter this code in the MATLAB editor and save it as descsort.m. This function call be called from
command line as
X=1:10
descsort(X)

Graphs in MATLAB
Plotting a single graph
Save the script file as graph1.m and the run it from the command window or clicking Run button
on the editor menu.
Here, we are plotting the graph of the rational function y = x/(1+x^2) using 50 values of x starting
from -2 and ending at 2. The linspace function is used here to generate the values of x.

N = 50;
x = linspace(-2,2,N);
y = x./(1+x.^2);
plot(x,y,'r')
title(sprintf('Graphbased upon %g points.',N))
axis([-2,2,-.8,.8])
xlabel('x')
ylabel('y')

EEE-314-12 | P a g e
grid

You will see the following figure generated by MATLAB,

Fig. 1

Plotting several graphs in one figure


Save the script file as graph2.m and the run it from the command window or clicking Run button
on the editor menu.
Graph of the rational function y = sin(x) for 5, 10, 15 and 20 valuesof x starting from -2 and ending
at 2.

for i = 1:4
N = i*5;
x = linspace(-2,2,N);
y = sin(x);
subplot(2,2,i)
plot(x,y,'r')
title(sprintf('Graph based upon %g points.',N))
axis([-2,2,-1,1])
xlabel('x')
ylabel('y')
grid
end

You will see the following figure generated by MATLAB,

EEE-314-13 | P a g e
Fig. 2

Tasks for lab report:


1. Operate with the vectors
V1 = [1 2 3 4 5 6 7 8 9 0];
V2 = [0.3 1.2 0.5 2.1 0.1 0.4 3.6 4.2 1.7 0.9];
V3 = [4 4 4 4 3 3 2 2 2 1];
a. Calculate, respectively, the sum of all the elements in vectors V1, V2, and V3.
b. How to get the value of the fifth element of each vector?What happens if we execute the
command V1(0) and V1(11)?Remember if a vector has N elements, their subscripts are from
1 to N.
c. Generate a new vector V4 from V2, which is composed of the first five elements of V2.
d. Generate a new vector V5 from V2, which is composed of the last five elements of V2.
e. Derive a new vector V6 from V2, with its 6th element omitted.
f. Derive a new vector V7 from V2, with its 7th element changed to 1.4.
g. Derive a new vector V8 from V2, whose elements are the 1st, 3rd, 5th, 7th, and 9thelements
of V2
h. What are the results of,

9-V1 V1*5 V1+V2 V1-V3 V1.*V2


V1*V2 V1.^V2 V1.^V3 V1^V3 V1==V3
V1>6 V1>V3 V3-(V1>2) (V1>2) & (V1>2) | (V1<6)
(V1<6)
any(V1) all(V1) sqrt(V1) exp(V2)

2. Compare a script and a function


a. Write a script: In the main menu of Matlab, selectfile -> new -> M-file, A new window will
pop up. Input the following commands:
x = 1:5;
y = 6:10;
g = x+y;
and then save the file as myscript.m under the default path matlab/work

EEE-314-14 | P a g e
b. Write a function: Create a new m file following the procedure of above. Type in the
commands:
function g = myfunction(x,y)
g = x + y;
and then save it as myfunction.m

i. Compare their usage by running the commands one by one.


myscript
x
y
g
z = myscript

ii. Run command clear all to remove all variables from memory
iii. Run the commands one by one:
x = 1:5;
y = 6:10;
myfunction
z = myfunction(x,y)
g
a = 1:10;
b = 2:11;
myfunction(a,b)

In each case observe and learn from the errors raised by MATLAB.

Precautions:
1) Follow the software instructions and lab manual for using the software.
2) Be careful while input the variables value before run the code.
3) Always work in the editor environment. This will you to edit the Matlab Code.
4) Do not turn off your pc or laptop while simulating the design.
5) Check your code with the desired input data.
6) Never hurry. Hurry can cause error in simulation.
7) After simulation collect all the data and graphs carefully, take screen shot if necessary.

Data Analysis:

Report:

1. Submit the complete data sheet with the screenshots of Matlab code and Output Graphs.
2. Using various variables value complete the data table, observe the output graphs and write a
report

Results and Discussion:

References
EEE-314-15 | P a g e
1) Li Tan, Purdue University, Jean Jiang, Purdue University “Digital Signal Processing:
Fundamentals and Applications’’.
2) Website: https://www.mathworks.com/products/matlab.html

Pre Lab Viva Sample Question:


1) What are the basic functionalities of Matlab?

Post Lab Viva Sample Questions:


1) How do a user defined function is defined?
2) How many control flows are in Matlab, write a matlab code of for, if-else loop?

EEE-314-16 | P a g e
Experiment No.02
Name of the Experiment: Generation and Visualization of continuous-time and discrete-time
signals.

Objective:
The objectives of this experiment are to:
1. learn how to generate continuous and discrete-time signals.
2. inspect the relationship between continuous and discrete-time signals.
3. get familiarized with the concept of sampling.

Course Outcomes (COs), Program Outcomes (POs) and Assessment:

Domain /
Delivery
Corresponding level of Assessment
CO Statement methods and
PO learning tools
activities
taxonomy
CO1:Use
MATLAB to Lab tests
generate signals, Simulation Lab reports
describe systems,
apply different Application Experiment Final lab
Affective test
types of signal (PO3) Practice lab
domain/
transformations Modern Tool Open ended
valuing level Group
such as shifting, Usage (PO5) lab
scaling, folding discussion
and perform Project
Tutorial
convolution and show & project
correlation. presentation

Theory:

Continuous-time and Discrete-time signals:


Digital Signal Processing must necessarily begin with a signal, and many signals, such as sound,
images, etc., originate as continuous signals, and must be converted into a sequence of samples to be
processed using digital techniques. Sinusoids are one of the most common and important signals.
Fig. 1 depicts continuous-time since wave with a frequency of 2𝐻𝑧.The general equation for
continuous-time sine waves is:

𝑥𝑎 (𝑡) = 𝐴 sin(Ω𝑡 + 𝜃) = 𝐴 sin(2𝜋𝐹𝑡 + 𝜃)

Here, 𝑥𝑎 (𝑡) is the continuous-time signal (which is a function of time, 𝑡), 𝐴 is the amplitude, Ω is the
angular frequency (unit is 𝑟𝑎𝑑𝑖𝑎𝑛/𝑠𝑒𝑐), 𝐹 is the frequency (unit is 𝐻𝑧), 𝑡 is time and 𝜃 is the phase
(unit is 𝑟𝑎𝑑𝑖𝑎𝑛).
The number of cycles or oscillations per second is called the frequency (𝐹). In fig. 1 there are 2 cycles
in 1 𝑠𝑒𝑐, so the frequency is 2𝐻𝑧. The inverse of frequency is time period (𝑇𝑝 ), whose unit is 𝑠𝑒𝑐.
So, 𝑇𝑝 = 1⁄𝐹 . Time period is the time required for one complete cycle. Here in fig. 1 we can see that

EEE-314-17 | P a g e
one cycle takes 0.5 𝑠𝑒𝑐 to complete. So, in this case time period (𝑇𝑝 ) is 0.5 𝑠𝑒𝑐. Alternatively, as the
frequency is 2 𝐻𝑧 time period (𝑇𝑝 ) = 1⁄𝐹 = 1⁄2 = 0.5 𝑠𝑒𝑐.
In continuous-time signals time(𝑡) can take any value from −∞ to +∞. In other words, there will be
a 𝑥𝑎 (𝑡) for every value of 𝑡. You will get a value for 𝑥𝑎 (𝜋), 𝑥𝑎 (0.30874), 𝑥𝑎 (√2), etc.

Discrete-time signals have values only on fixed intervals of time. For example, fig. 2 depicts a
discrete-time signal which has values on 0 𝑠𝑒𝑐, 0.125 𝑠𝑒𝑐, 0.25 𝑠𝑒𝑐, 0.375 𝑠𝑒𝑐, 0.5 𝑠𝑒𝑐, 0.625 𝑠𝑒𝑐,
0.75 𝑠𝑒𝑐, 0.875 𝑠𝑒𝑐, 1 𝑠𝑒𝑐, etc. In this particular example the values are coming after a fixed interval
of 0.125 𝑠𝑒𝑐. It is called the sampling interval (𝑇). The signal in fig. 2 is actually created by taking
values or samples every𝑇 𝑠𝑒𝑐 from the signal in fig. 1.We do not know the value of the discrete-time
signal in fig. 2 at 𝑡 = 0.1 𝑠𝑒𝑐 because we didn’t take the value at that time from the signal in fig. 1.
Alternatively, we can say that we took 8 𝑠𝑎𝑚𝑝𝑙𝑒𝑠 per 𝑠𝑒𝑐 or took samples at the rate of 8 𝐻𝑧 from
the continuous-time signal in fig. 1 to create the discrete-time signal in fig. 2. It is called the sampling
rate or sampling frequency (𝐹𝑠 ). Sampling rate or frequency is the inverse of sampling interval.

The general equation for discrete-time since waves is:

𝑥(𝑛) = 𝐴 sin(𝜔𝑛 + 𝜃) = 𝐴 sin(2𝜋𝑓𝑛 + 𝜃)


Here, 𝑥(𝑛) is the discrete-time signal (which is a function of time index, 𝑛), 𝐴 is the amplitude, 𝜔 is
the angular frequency (unit is 𝑟𝑎𝑑𝑖𝑎𝑛/𝑠𝑎𝑚𝑝𝑙𝑒), 𝑓 is the frequency (unit is 𝑐𝑦𝑐𝑙𝑒𝑠 𝑝𝑒𝑟 𝑠𝑎𝑚𝑝𝑙𝑒), 𝑡
is time and 𝜃 is the phase (unit is 𝑟𝑎𝑑𝑖𝑎𝑛).
The number of samples in one cycle or repetition is called the period (𝑁). In fig. 2 there are 8
𝑠𝑎𝑚𝑝𝑙𝑒𝑠 in 1 𝑐𝑦𝑐𝑙𝑒, so the period is 8 𝑠𝑎𝑚𝑝𝑙𝑒𝑠. Frequency (𝑓) is related to the period by the
equation,
𝑓 = 𝑘⁄𝑁

where,𝑘 is an integer. Unit of 𝑓 is 𝑐𝑦𝑐𝑙𝑒𝑠 𝑝𝑒𝑟 𝑠𝑎𝑚𝑝𝑙𝑒. We can say that there is
1⁄8 𝑐𝑦𝑐𝑙𝑒𝑠 𝑝𝑒𝑟 𝑠𝑎𝑚𝑝𝑙𝑒. So, in this case frequency (𝑓) is 1⁄8. If we compare this to the time
equation above, we get 𝑘 = 1 and 𝑁 = 8.
In discrete-time signals time(𝑡) can take any value from −∞ to +∞. In other words, there will be a
𝑥𝑎 (𝑡) for every value of 𝑡. You will get a value for 𝑥𝑎 (𝜋), 𝑥𝑎 (0.30874), 𝑥𝑎 (√2), etc.

We cannot actually generate continuous-time signals in computers because computer is a digital


device. But we can create a discrete-time signal with a very high sampling frequency to simulate a
continuous-time signal.

with eight samples marked, sequentially obtained every 0.125 second. Alternatively, we can say that
we took 8 samples per second and it is called the sampling frequency 𝐹𝑠 . For this particular example
𝐹𝑠 is 8 Hz.ThesignalvaluesinputtotheADCatsampletimes0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75,
etc., are 0, 0.707, 1, 0.707, 0, -0.707, -1, etc.

The samples within a given sample sequence are normally indexed by the numbers 0, 1, 2, 𝑒𝑡𝑐.which
represent multiples of the sampling period or sampling interval𝑇. For example, in Fig. 1 we note that
the sample period is 0.125 second, and the actual sampling times are therefore 0 sec., 0.125 sec., 0.25
sec., etc. The continuous sine function shown has the value:

EEE-314-18 | P a g e
𝑥(𝑡) = sin(2𝜋𝐹𝑡)

Where 𝑡 is time, 𝐹is the continuous-time frequency, and in this particular case, 𝐹 = 1𝐻𝑧. Sampling
occurs at times 𝑛𝑇where 𝑛 = 0, 1, 2, … and 𝑇 = 0.125 𝑠𝑒𝑐𝑜𝑛𝑑. The sample values of the sequence
would therefore be 𝑠𝑖𝑛(0), 𝑠𝑖𝑛(2𝜋(𝑇)), 𝑠𝑖𝑛(2𝜋(2𝑇)), 𝑠𝑖𝑛(2𝜋(3𝑇)), 𝑒𝑡𝑐., and we would then say
that 𝑥(0) = 0, 𝑥(1) = 0.707, 𝑥(2) = 1.0, 𝑥(3) = 0.707, 𝑒𝑡𝑐. where 𝑥(𝑛)denotes the
𝑛𝑡ℎ sequence value, the amplitude of which is equal to the underlying continuous function at time
𝑛𝑇(note that brackets surrounding a function argument mean that the argument can only assume
discrete values, while parentheses surrounding an argument indicate that the argument’s domain is
continuous).

Required Devices & Software:

1. A well configured Laptop or PC.


2. Software: Latest Matlab version. (2014-2020).

Experimental simulation procedure:

𝑥(𝑛) = sin(2𝜋𝐹𝑛𝑇)

Fig. 1: An analog or continuous-domain sine wave, with eight samples per second marked.

This sequence of values, the samples of the sine wave, can be used to completely reconstruct the
original continuous domain sine wave using a DAC. There are, of course, a number of conditions to
ensure that this is possible, and they are taken up in detail in the next lab. To compute and plot the
sample values of a 2-Hz sine wave sampled every 0.05 second on the time interval 0 to 1.1 second,
write the following MATLAB code in the command window:

t = [0:0.05:1.1];
figure;
stem(t, sin(2*pi*2*t))

where the 𝑡vector contains every sample time 𝑛𝑇with 𝑇 = 0.05. Alternatively, we might write:

EEE-314-19 | P a g e
T = 0.05;
n = 0:1:22;
figure;
stem(n*T, sin(2*pi*2*n*T))

both of which result in Fig. 4.

Fig. 2: A plot of the samples of a sine wave having frequency 2 Hz, sampled every 0.05 second up
to time 1.1 second.

USEFUL SIGNALS, SEQUENCES, AND CONCEPTS

SINE AND COSINE

We saw above that a sine wave of frequency 𝐹 periodically sampled at the sampling interval𝑇 can
be represented as:

𝑥(𝑛) = sin(2𝜋𝐹𝑛𝑇) = sin(2𝜋𝑓𝑛)

where, 𝑓 = 𝐹𝑇 = 𝐹 ⁄𝐹𝑠 and is called the discrete-time frequency. Once we have a sampled sine
wave, we can mathematically express it without reference to the sampling period by defining the
sequence length as𝑁.𝑁 is the discrete-time time period. We would then have, in general,
𝑥(𝑛) = sin(2𝜋𝑛(𝑘⁄𝑛))

where, 𝑓 = 𝑘/𝑁. 𝑛 is the sample index, which runs from 0 to 𝑁 − 1, and 𝑘 is the number of cycles
over the sequence length 𝑁. For the sample sequence marked in Fig. 1, we would have,

𝑥(𝑛) = sin(2𝜋𝑛(2⁄16)) = sin(2𝜋𝑛(1⁄8))

So, here the discrete frequency (𝑓) = 1⁄8 and the discrete time period (𝑁) = 8. Note that there are
two full cycles of the sine over 16 samples (the 17th sample is the start of the third cycle).
EEE-314-20 | P a g e
The correctness of this formula can be verified by noting that for the 17th sample, 𝑛 = 16, and
𝑥(16) = 0, as shown.
Picking another sample, for 𝑛 = 2, we get, 𝑥(2) = sin(2𝜋2(2⁄16)) = sin(𝜋⁄2) = 1, as shown.

A phase angle is sometimes needed, and is denoted 𝜃by in the following expression:

𝑥(𝑛) = sin(2𝜋𝑛(𝑘⁄𝑁) + 𝜃)

Note that, if we shift a sin wave by 𝜋/2 radians we get cosine wave.
We can illustrate this by generating and displaying a sine wave having three cycles over 18 samples
with a phase angle of 𝜋/2 radians, and finally a cosine wave having three cycles over 18 samples
and a zero phase angle. A suitable MATLAB code is given below, which results in Fig. 3.

n = 0:1:17;

x = sin(2*pi*n*(3/18) + pi/2);
subplot(2,1,1);
stem(n,x);

y = cos(2*pi*n*(3/18));
subplot(2,1,2);
stem(n,y);

Fig. 3: (a) sine wave with phase angle of 𝜋/2 radians, (b) cosine wave with phase angle 0 radians
Problem 01:
Write a MATLAB script to plot the signal 𝑥(𝑛) = cos(𝜔𝑜 𝑛) for 𝜔𝑜 = 0, 𝜋⁄16 , 𝜋⁄8, 𝜋⁄4, 𝜋⁄2
and 𝜋. The output should look like the Figure 4.

EEE-314-21 | P a g e
Fig. 4: Signal 𝑥(𝑛) = cos(𝜔𝑜 𝑛) for various values of angular frequency 𝜔𝑜 .
Problem 02:
Write a MATLAB script to plot the signal 𝑥(𝑛) = 𝐶𝑎𝑛 for,
(i) 𝐶 and 𝑎 are real for 𝑎 > 1, 0 < 𝑎 < 1, 𝑎 = 1, 𝑎 < −1,−1 < 𝑎 < 0 and 𝑎 = −1.
(ii) 𝐶iscomplex and 𝑎 is complex with unity magnitude. [𝐶 = 𝐴𝑒 𝑗𝜃 and 𝑎 = 𝑒 𝑗𝜔 ]
(iii) 𝐶 is complex and 𝑎 is complex with unity magnitude. [𝐶 = 𝐴𝑒 𝑗𝜃 and 𝑎 = 𝑒 𝜎+𝑗𝜔 ]
The output for (i) should look like the Figure 5.

Fig. 5: Signal 𝑥(𝑛) = 𝐶𝑎𝑛 for various values of 𝑎.

Try (ii) and (iii) for yourself and comment on the output.

Precautions:
1) Follow the software instructions and lab manual for using the software.
2) Be careful while input the variables value before run the code.
EEE-314-22 | P a g e
3) Always work in the editor environment. This will you to edit the Matlab Code.
4) Do not turn off your pc or laptop while simulating the design.
5) Check your code with the desired input data.
6) Never hurry. Hurry can cause error in simulation.
7) After simulation collect all the data and graphs carefully, take screen shot if necessary.

Data Analysis:

Report:

1) Submit the complete data sheet with the screenshots of Matlab code and Output Graphs.
2) Using various variables value complete the data table, observe the output graphs and write a
report

Results and Discussion:

References
1) Li Tan, Purdue University, Jean Jiang, Purdue University “Digital Signal Processing:
Fundamentals and Applications’’.
2) Website: https://www.mathworks.com/products/matlab.html

Pre Lab Viva Sample Question:


1. What are the main differences between continuous time signals and discrete time signals?

Post Lab Viva Sample Questions:


1. Write a MATLAB script to plot the signal x(n) = Ca^n for,
(i) C and a are real for a > 1, 0< a< 1, a = 1, a < -1,-1 < a < 0 and a = -1.
(ii) Ciscomplex and a is complex with unity magnitude. [C=Ae^jθ and a=e^jω]
(iii) C is complex and a is complex with unity magnitude. [C=Ae^jθ and a=e^(σ+jω)]?
2. Plot a continuous time signal and discrete time signal having frequency 4 KHz.

EEE-314-23 | P a g e
Experiment No. 03:

Name of the Experiment: Generation of basic discrete-time signals and inspection of


discrete-time sinusoid frequency properties.

Objective:
The objectives of this experiment are to:
1. learn how to generate continuous and discrete-time signals.
2. inspect the relationship between continuous and discrete-time signals.
3. get familiarized with the concept of sampling.

Course Outcomes (COs), Program Outcomes (POs) and Assessment:

Domain /
Delivery
Corresponding level of Assessment
CO Statement methods and
PO learning tools
activities
taxonomy
Lab tests
CO1: Analyze
discrete time Simulation Lab reports
systems in time Experiment Final lab
Application
and frequency Affective test
(PO3) Practice lab
domain using domain/
Modern Tool Open ended
signal processing valuing level Group
Usage (PO5) lab
and symbolic discussion
toolboxes of Project
MATLAB Tutorial
show & project
presentation

Theory:

Unit Impulse Signal:


Unit impulse signal is defined as:
1, 𝑛 ≥ 0
𝛿(𝑛) = {
0, 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
A time shifted unit impulse signal can be defined as:
1, 𝑛 ≥ 𝑛0
𝛿(𝑛 − 𝑛0 ) = {
0, 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
The relational operators can be used is an elegant way to implement 𝛿(𝑛). For example, to
implement𝛿(𝑛)overthedurationof𝑛1 ≤ 𝑛 ≤ 𝑛2wecanuserelationalequal(==)operatorto
generateavectorwhichis1for𝑛 = 0and0forrest ofthevaluesofn.

EEE-314-24 | P a g e
Required Devices & Software:

1. A well configured Laptop or PC.


2. Software: Latest Matlab version. (2014-2020).

Experimental simulation procedure:

Example 01: Write a MATLAB script to generate and plot a unit impulse sequence over the
duration −𝟓 ≤ 𝒏 ≤ 𝟓.

Execute the code below and observe the generated figure.

n = -5:5;
delta = n==0;
stem(n, delta);

Relational operators produce zero if the expression is false and one if the expression is true. The
expression𝑛 == 0impliesthevalueofnis0.Here,𝑛 = {−5, −4, −3, −2, −1,0,1,2,3,4,5}. So, there
are 11 values for n and the expression 𝑛 == 0is true for only one value of 𝑛and false for the rest
of the values of 𝑛. So, the expression 𝑛 == 0will produce 0 for 𝑛 = −5, 0 for𝑛 = −4,0for𝑛 =
−3,etc.Itwillproduce1onlyfor𝑛 = 0.Inthisway,thevaluesofdelta is{0,0,0,0,0,1,0,0,0,0,0}.

Practice Problem 01: Write a MATLAB script to generate and plot 𝛿(𝑛 − 3)over the
duration −10 ≤ 𝑛 ≤ 10.
[Hint: The value if delta will be 1 only at 𝑛 = 3.]

We can also create a MATLAB function to generate unit impulse sequence as it is a widely used
signal. We will have to use this signal a lot of times in future lab experiments. Creating a function
will allow us to generate impulse signal with a single line ofcode.
Create and save the code below as impseq.m. You can name it anything you want but there are a
few rules for naming i.e. names cannot contain empty spaces and a few special characters, the first
character cannot be a digit. And names should not be the same as any existing MATLAB function
names. For example, you should not name a function plot as there is already a function named
plot in MATLAB. Remember, the name of a function and the name of the file that contains the
function will have to be exactly the same. In this case, the name of the function is impseq. So, the
name of the saved file will have to be impseq.m. “.m” is the extension for MATLAB scripts and
function files.

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


n = n1:n2;
delta = n==n0;

Here, n1 is the starting index and n2 is the ending index. n0 is the index where the value of impulse
EEE-314-25 | P a g e
sequence is 1.

Once saved, you can call the function from the command window or any MATLAB script. The
function file will have to be on the current directory though.
For example, write the code below and observe that it does the same thing as the code in Example
01.

[delta, n] = impseq(-5,5,0);
stem(n, delta)

Unit Stepsequence:
Unit set signal is defined as:
1, 𝑛≥0
𝑢(𝑛) = {
0, 𝑛<0
A time shifted unit step signal can be defined as:
1, 𝑛 ≥ 𝑛0
𝑢(𝑛 − 𝑛0 ) = {
0, 𝑛 < 𝑛0

Example02: WriteaMATLABscripttogenerateandplot𝑢(𝑛 − 3)overtheduration−10 ≤ 𝑛 ≤


10.

Execute the code below and observe the generated figure.

n = -10:10;
u = n>=3;
stem(n, u);

As we created MATLAB function for the unit impulse sequence, similar function for the unit step
signal can also be created. Try it yourself.

Practice Problem 02: Write a MATLAB script to generate and plot the signal provided
belowovertheduration−10≤n≤10. Useimpseqandstepseqfunctionstocompletethe task.
𝑥 (𝑛) = 𝑛(𝑢(𝑛) − 𝑢(𝑛 − 6)) + 3 𝛿(𝑛 − 3)

[Hint: Create and save the stepseq function and then just combine all the codes above.]

Exponential signal:
Any signals having the following general equation is called an exponential signal:

𝑥 (𝑛) = 𝑎 𝑛

a can have any value, real or complex. If a is real then the exponential signal itself will be real. The
shape of exponential signals can vary widely depending on the value of a.

EEE-314-26 | P a g e
Example 03: Write a MATLAB script to generate and plot the exponential signal provided
below over the duration −𝟓 ≤ 𝒏 ≤ 𝟓.

𝑥(𝑛) = 𝑎𝑛 , 𝑤ℎ𝑒𝑟𝑒 𝑎 = 0.9

Execute the code below and observe the generated figure.

n = -5:5;
a = 0.9;
x = a.^n;
stem(n, x);

title("Exponential sequence, a=0.9");


xlabel("Index (n)");
ylabel("Amplitude");

Fig. 1: Exponential sequence with the value of a = 0.9


Practice Problem 03: Write a MATLAB script to plot the signal 𝑥(𝑛) = 𝑎𝑛 for 𝑎 = 1. 1, 𝑎 =
1, 𝑎 = 0. 8, 𝑎 = −0. 8, 𝑎 = −1and 𝑎 = −1. 1. Here, the values of 𝑎are all real.
[Hint: Execute the code above for different values of a and plot them using subplot.]

Practice Problem 04: Write a MATLAB script to plot the signal 𝑥(𝑛) = 𝑎𝑛 for 𝑎 =
𝑒 0.1𝑗 , 𝑎 = 𝑒 0.1 and 𝑎 = 𝑒 0.1+0.1𝑗 .
[Hint: As some of the values of a are complex, we will have to plot the real and imaginary parts
separately. Use the function real and imagin this cause.]

Observe the figures for problem 04 and 05. There is a pattern.

EEE-314-27 | P a g e
Non-sinusoidal periodic sequences:
Sinusoids are periodic sequences and we explored them in the previous lab. There are a lot of non-
sinusoidal periodic sequences too. For example, saw tooth, square, triangular, etc.
There are MATLAB functions for generating these signals. But we should try to code from scratch.
One approach is to create one period of a signal then just repeat it by copying. There is a MATLAB
function named repmatfor this particular task.

Write the following code in the command window and observe the value of B. B will be a matrix
created by copying the matrix A 3 times along the row axis and 2 times along the column axis.

A = [1 2;3 4];
B = repmat(A,3,2);

Example 04: Generate 10 periods of a discrete sawtooth wave with a fundamental frequency
of 1/8 cycles per sample and peaks of -1 and 1.

Frequency (f) is 1/8 so the period (N) must be 8 samples. So, over 8 samples the sample value will
rise from -1 to 1. We can use MATLAB function linspaceto create a linearly spaced vector of
specific length and range.

Execute the code below.

N = 8;
unit = linspace(-1,1,N);
x = repmat(unit,1,10);
n =-5*N:5*N-1;
stem(n, x)

title("Sawtooth wave");
xlabel("Index (n)");
ylabel("Amplitude");

Here, unit is one period of the signal. You can try and plot it for better understanding. Then, we
used repmatto repeat the period 10 times along the column axis. Lastly, index (n) is created from
the fact that one period runs from index 0 to 7 (total 8 samples), so, 10 periods will run from -40
to 39 or from 0 to 79. It is your choice to select the index range as none has been provided. See the
figure on the next page.

EEE-314-28 | P a g e
Fig. 2: Sawtooth signal with frequency 1/8 cycles per sample

Practice Problem 05: Generate 4 periods of a discrete triangular wave with a fundamental
frequency of 1/16 cycles per sample and peaks of 0 and 1.
[Hint: Generate the rising and the falling side of the signal separately. Calculate the number of
samples for each side. Then use linspaceor the colon (:) operator to create the sample values.]

Inspection of Discrete-time sinusoid properties:


We know that the properties of discrete-time sinusoids are:
1. Discrete-time sinusoids are periodic only if the frequency is a rational number (have the
form𝑘 ⁄𝑁 ,where𝑘and𝑁areallintegers).
2. Signals having frequencies separated by integer values are similar. So, signal with frequency
1/8 cycle per sample is same as the signal having frequency 9/8 cycle per sample.
3. Maximum oscillation is attained when frequency is 1/2. That means, if frequency is reduced
or increased slightly from 1/2, the oscillation will decrease in both cases. That defines a unique
range of frequencies (-1/2 to 1/2) outside of which there are only copies from inside. Note that,
property 2 and 3 are actually related.

Practice Problem 06: Generate three discrete-time sinusoids with frequencies𝑡 = √2, 1/8 and 9/8
to verify property 1 and 2. According to property 1, sinusoid with 𝑡 = √2will not be periodic,
which means values will not repeat. And according to property 2, sinusoids with t = 1/8 and 9/8
should be exactly the same because the difference between the frequencies is an integer (1 in this
case).
[Hint: Generation of the signals is very easy as we covered this topic in the previous lab. The main
task here is to observe and interpret the generated signals.]

Example05: Generate three discrete-time cosine wave with frequency t =1/2,1/4 and
4/5toverifyproperty3.Accordingtoproperty3, the maximum oscillation will occur at
EEE-314-29 | P a g e
t = 1/2. Decreasing tfrom 1/2 to 1/4 or increasing tfrom 1/2 to 4/5 will result in decreased
oscillation in both cases.

Execute the code and observe the figures:

n = -15:15;
x1 = cos(2*pi*(1/2)*n);
x2 = cos(2*pi*(1/4)*n);
x3 = cos(2*pi*(4/5)*n);

subplot(3,1,1)
stem(n, x1)
title("Cosine wave with frequency 0.5");
xlabel("Index (n)");
ylabel("Amplitude");

subplot(3,1,2)
stem(n, x2)
title("Cosine wave with frequency 0.25");
xlabel("Index (n)");
ylabel("Amplitude");

subplot(3,1,3)
stem(n, x3)
title("Cosine wave with frequency 0.8");
xlabel("Index (n)");
ylabel("Amplitude");

Fig. 3: Figure generated from the execution of the code above. Observe how the oscillation
varies with frequency.

EEE-314-30 | P a g e
Lab Report:
Solve the practice problems and submit the solutions as the lab report. You do not have to write
down the theory section. The codes should be handwritten. Print the figures. If you face difficulties,
mention it in the discussion section.
Precautions:
8) Follow the software instructions and lab manual for using the software.
9) Be careful while input the variables value before run the code.
10) Always work in the editor environment. This will you to edit the Matlab Code.
11) Do not turn off your pc or laptop while simulating the design.
12) Check your code with the desired input data.
13) Never hurry. Hurry can cause error in simulation.
14) After simulation collect all the data and graphs carefully, take screen shot if necessary.

Data Analysis:

Report:

3) Submit the complete data sheet with the screenshots of Matlab code and Output Graphs.
4) Using various variables value complete the data table, observe the output graphs and write a
report

Results and Discussion:

References
1. Li Tan, Purdue University, Jean Jiang, Purdue University “Digital Signal Processing:
Fundamentals and Applications’’.
2. Website: https://www.mathworks.com/products/matlab.html

Pre Lab Viva Sample Question:


1. Write a MATLAB script to generate and plot the exponential signal provided below over
the duration -5 ≤ n ≤ 5?

Post Lab Viva Sample Questions:


1. Write a MATLAB script to generate and plot the signal provided below over the duration
−10≤n≤10.
2. Generate 4 periods of a discrete triangular wave with a fundamental frequency of 1/16
cycles per sample and peaks of 0 and 1.
3. Generate three discrete-time cosine wave with frequency t =1/2, ¼ and 4/5 to verify
property 3. According to property 3,the maximum oscillation will occur at t = 1/2.
Decreasing tfrom 1/2 to 1/4 or increasing tfrom 1/2 to 4/5 will result in decreased oscillation
in both cases.

EEE-314-31 | P a g e
EEE-314-32 | P a g e
Experiment No. 04
Name of the Experiment: Operations on discrete-time signals

Objective:
The objectives of this experiment are to:
3) Get familiarized with basic signal operations such as shifting, scaling, folding, even-odd
decomposition, etc.
4) Learn to perform basic operations on discrete signals using MATLAB.

Course Outcomes (COs), Program Outcomes (POs) and Assessment:

Domain /
Delivery
Corresponding level of Assessment
CO Statement methods and
PO learning tools
activities
taxonomy
CO1: Use
MATLAB to Lab tests
generate signals, Simulation Lab reports
describe systems,
apply different Application Experiment Final lab
Affective test
types of signal (PO3) Practice lab
domain/
transformations Modern Tool Open ended
valuing level Group
such as shifting, Usage (PO5) lab
scaling, folding discussion
and perform Project
Tutorial
convolution and show& project
correlation. presentation

Theory:

Signal addition, multiplication, subtraction:


Certain operations on two sequences, such as addition and multiplication, require that the
sequences be of equal length, and that their proper positions in time be preserved. Consider the
sequence x1 = [1,2,3,4], which was sampled at sample time indices n1 = [-1,0,1,2], which we
would like to add to sequence x2 = [4,3,2,1], which was sampled at time indices n2 = [2,3,4,5]. To
make these two sequences equal in length, we’ll prepend and post pend zeros as needed to result
in two sequences of equal length that retain the proper time alignment. We see that the minimum
time index is -1 and the maximum time index is 5. Since x1 starts at the minimum time index, we
postponed zeros to it such that we would have x1 = [1,2,3,4,0,0,0], with corresponding time indices
[-1,0,1,2,3,4,5]. Similarly, we prepend zeros so that x2 = [0,0,0,4,3,2,1], with the same total time
or sample index range as the modified version of x1. Figure 2.4 depicts this process. The sum is
then,

x1 + x2 = [1,2,3,4,0,0,0] + [0,0,0,4,3,2,1] = [1,2,3,8,3,2,1]

EEE-314-33 | P a g e
and has time indices [-1,0,1,2,3,4,5].

Experimental simulation procedure:

These two ideas, that sequences to be added or multiplied must be of equal length, but also properly
time-aligned, lead us to write several MATLAB functions that will automatically perform the
needed adjustments and perform the arithmetic operation.

function [y,n] = sigadd(x1,n1,x2,n2)


% implements y(n) = x1(n)+x2(n)
% -----------------------------
% y, n = summed signal (the output) and its index
% x1, n1 = first input signal and its index
% x2, n2 = second input signal and its index
n = min(min(n1),min(n2)):max(max(n1),max(n2));
y1 = zeros(1,length(n));
y2 = y1;
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1;
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2;
y = y1 + y2;

The script above will perform addition of offset sequences y1 and y2 that have respective time
indices n1 and n2 using the method of prepending and post pending zeros. The function [y,n] =
singed(y1, n1, y2, n2)works the same way, with the addition operator (+) in the final statement
being replaced with the operator for multiplying two vectors on a sample-by- sample basis, a period
following by an asterisk (.*). Try to write the functions for signal multiplication and subtraction.

Exercise 01: Let 𝑥(𝑛) = cos(0.05𝜋𝑛) and 𝑦(𝑛) = sin(0.1𝜋𝑛). Generate and plot the following
sequence over the duration −25 ≤ 𝑛 ≤ 25:

𝑧(𝑛) = 5𝑥(𝑛) − 2𝑦(𝑛)

We will have to generate the signals 𝑥(𝑛) and 𝑦(𝑛) separately. Then we add them using the sigadd
function we’ve created earlier. Execute the following MATLAB code:

n = -25:25;
x = cos(0.05*pi*n);
y = sin(0.1*pi*n);
[z,n] = sigadd(5*x,n,-2*y,n);
stem(n,z);
title('Signal: z(n)=5cos(0.05{\pi}n)-2sin(0.1{\pi}n)')
xlabel('Index (n)')
ylabel('Amplitude')

EEE-314-34 | P a g e
Figure 01: Output signal for Exercise 01

Time shifting:
Time shifting of a signal or sequence is defined by the following system equation:

𝑦(𝑛) = 𝑥(𝑛 − 𝑘)

If 𝑛 is positive, the shifting operation is called “Time delaying”, likewise, if 𝑛 is negative, the
shifting operation is called “Time advancing”.

The following MATLAB function will do time shifting operation.

function [y,n] = sigshift(x,m,k)


% implements y(n) = x(n-k)
% ------------------------
n = m+k;
y = x;

Practice problem 01: Let 𝑥(𝑛) = [1,-2 4,6,-5,8,10]. The sample at 𝑛 = 0 has been bolded.
Generate and plot the following sequence:

𝑦(𝑛) = 3𝑥(𝑛 + 2) + 𝑥(𝑛 − 4) − 2𝑥(𝑛)

[Hint: Time shifting and signal addition and subtraction operations are present in this system. So,
combine the codes above. Generate individual sequences by time shifting, then perform the
addition or subtraction.]

EEE-314-35 | P a g e
Exercise 02: Let 𝑥(𝑛) = (0.9)n u(n). Generate and plot the following sequence over the duration
−10 ≤ 𝑛 ≤ 10:

𝑦(𝑛) = 𝑥(𝑛 + 5)

We will use the sigshift function defined above. Here, 𝑘 = −5. Execute the following MATLAB
code and observe the generated figure:

n = -10:10;
x = ((0.9).^n).*stepseq(-10,10,0);
[y,m] = sigshift(x,n,-5);
stem(m,y);
title('Signal: (0.9)^nu(n)')
xlabel('Index (n)')
ylabel('Amplitude')

Figure 02: Output signal for Exercise 02

Practice problem 02:Generate and plot the sequence below over the duration 0 ≤ 𝑛 ≤ 50:

𝑥(𝑛) = cos(0.1𝜋𝑛) (𝑢(𝑛) − 𝑢(𝑛 − 30))

[Hint: Use the knowledge gained on the previous two labs where you learned to generate and plot
sinusoids and basic signals. The rest is same as practice problem 01.]

Time folding/reversing/mirroring:

EEE-314-36 | P a g e
From time to time it is necessary to reverse a sequence in time, i.e., assuming that 𝑥(𝑛) = [1,2,3,4],
the folded sequence would be 𝑥(−𝑛) = [4,3,2,1], defined by the following equation:

𝑦(𝑛) = 𝑥(−𝑛)

The operation is essentially to flip the sequence from left to right around index zero. For example,
the sequence [1,2,3,4] that has corresponding sample indices [3,4,5,6], when folded, results in the
sequence [4,3,2,1] and corresponding indices [-6,-5,-4,-3]. To illustrate the above ideas, we can,
for example, let 𝑥(𝑛)= [1,2,3,4] with corresponding sample indices n = [3,4,5,6], and compute
𝑥(−𝑛)using MATLAB. We can write a simple script to accomplish the folding operation:

function [y,n] = sigfold(x,n)


% implements y(n) = x(-n)
% -----------------------
n = -fliplr(n);
y = fliplr(x);

Even and odd decomposition:


Any real sequence can be decomposed into two components that display even and odd symmetry
about the midpoint of the sequence. A sequence that exhibits even symmetry has its first and last
samples equal, its second and penultimate samples equal, and so on. A sequence that exhibits odd
symmetry has its first sample equal to the negative of the last sample, its second sample equal to
the negative of its penultimate sample, etc. An even decomposition 𝑥𝑒 (𝑛) of a sequence 𝑥(𝑛) can
be obtained as,

1
𝑥𝑒 (𝑛) = (𝑥(𝑛) + 𝑥(−𝑛))
2

and the corresponding odd decomposition 𝑥𝑜 (𝑛) is

1
𝑥𝑜 (𝑛) = (𝑥(𝑛) − 𝑥(−𝑛))
2

Practice problem 03: Write a MATLAB function that will decompose a given signal to its even
and odd components.
[Hint: Combine the signal addition, subtraction and time folding operations discussed earlier
according to the equations shown above.]

Practice problem 04: Decompose the signal 𝑦(𝑛) in the practice problem 01 into its even and odd
components. Plot the components and the original signal on the same figure.
[Hint: Use the function you wrote for practice problem 03 to generate the components.]

Time scaling:

EEE-314-37 | P a g e
Time scalingof a signal or sequence is defined by the following system equation:

𝑦(𝑛) = 𝑥(𝑎𝑛)

If 𝑎 > 1, the scaling operation is called “Downsampling” and 𝑎 will be an integer. If 𝑎 < 1, the
scaling operation is called “Upsampling” and 𝑎 = 1⁄𝑏, where 𝑏 is an integer.

If 𝑎 > 1, the number of samples is reduced by the factor 𝑎 and the signal is compressed. This is
why it is called “Down sampling”. Likewise, if 𝑎 < 1, the number of samples is increased by the
factor 𝑎 and the signal is expanded. Hence the name “Upsampling”.

In case of down sampling, every 𝑎 samples from the original signal is taken. This is why the
number of sample is reduced and the signal gets compressed. For example, if 𝑦(𝑛) = 𝑥(2𝑛), then
𝑦(0) = 𝑥(0), 𝑦(1) = 𝑥(2), 𝑦(2) = 𝑥(4), 𝑦(3) = 𝑥(6). So the samples of the output signal at
𝑛 = 0,1,2,3, 𝑒𝑡𝑐.are created by taking the samples from the input signal at 𝑛 = 0,2,4,6, 𝑒𝑡𝑐.

We can write a simple script to accomplish the down sampling operation.

function [y,m] = sigdown(x,n,a)


% implements y(n) = x(an)
% -----------------------
m = n(mod(n,a)==0)/a;
y=zeros(1,length(m));
fori=1:length(m)
y(i)=x(n==a*m(i));
end

In case of upsampling, extra samples are produced. This is why the signal gets expanded. For
example, if 𝑦(𝑛) = 𝑥(0.5𝑛), then 𝑦(0) = 𝑥(0), 𝑦(1) = 𝑥(0.5), 𝑦(2) = 𝑥(1), 𝑦(3) = 𝑥(1.5),
𝑦(4) = 𝑥(2). So the samples of the output signal at 𝑛 = 0,1,2,3,4,5,6, 𝑒𝑡𝑐.are created by taking
the samples from the input signal at 𝑛 = 0,0.5,1,1.5,2,2.5,3, 𝑒𝑡𝑐. But how can one get samples at
𝑛 = 0.5,1.5,2.5 for a discrete signal? It is actually not possible as 𝑛 is always integer. For this
reason we can simply put 0 at the samples at the corresponding indices of the output signal. A
better option is to interpolate the sample values from the surrounding samples. But that is for
another day.

We can write a simple script to accomplish the upsampling operation.

function [y,m] = sigup(x,n,a)


% implements y(n) = x(an)
% -----------------------
m = (min(n)*(1/a)):(max(n)*(1/a));
y=zeros(1,length(m));
fori=1:length(n)
y(m==(n(i)/a))=x(i);

EEE-314-38 | P a g e
end

Exercise 03: Let 𝑥(𝑛) = cos(0.05𝜋𝑛)over the duration −25 ≤ 𝑛 ≤ 25. Generate and plot the
following signals on the same figure:

𝑦(𝑛) = 𝑥(2𝑛) and 𝑧(𝑛) = 𝑥(0.5𝑛)

Here, 𝑦(𝑛) is a downsampled version of 𝑥(𝑛) (as 𝑎 = 2 > 1) and 𝑧(𝑛) is an upsampled version
of 𝑥(𝑛) (as 𝑎 = 0.5 < 1). Execute the following MATLAB code:

n = -25:25;
x = cos(0.05*pi*n);
subplot(3,1,1)
stem(n,x);
title('Signal: x(n) = cos(0.05{\pi}n)')
xlabel('Index (n)')
ylabel('Amplitude')

[y,m] = sigdown(x,n,2);
subplot(3,1,2)
stem(m,y);
title('Signal: y(n) = x(2n)')
xlabel('Index (n)')
ylabel('Amplitude')

[z,m] = sigup(x,n,0.5);
subplot(3,1,3)
stem(m,z);
title('Signal: z(n) = x(0.5n)')
xlabel('Index (n)')
ylabel('Amplitude')

EEE-314-39 | P a g e
Figure 03: Output figure for Exercise 03

Practice problem 05: For the signal 𝑥(𝑛) = cos(0.1𝜋𝑛) over the duration 0 ≤ 𝑛 ≤ 10 generate
and plot the following sequence:

𝑦(𝑛) = 3𝑥(3𝑛 − 2) + 𝑥(2𝑛 + 4) − 2𝑥(0.5𝑛)

Precautions:
1. Follow the software instructions and lab manual for using the software.
2. Be careful while input the variables value before run the code.
3. Always work in the editor environment. This will you to edit the Matlab Code.
4. Do not turn off your pc or laptop while simulating the design.
5. Check your code with the desired input data.
6. Never hurry. Hurry can cause error in simulation.
7. After simulation collect all the data and graphs carefully, take screen shot if necessary.

Data Analysis:

Report:

1) Submit the complete data sheet with the screenshots of Matlab code and Output
Graphs.
2) Using various variables value complete the data table, observe the output graphs and
write a report

EEE-314-40 | P a g e
Results and Discussion:

References
1. Li Tan, Purdue University, Jean Jiang, Purdue University “Digital Signal Processing:
Fundamentals and Applications’’.
2. Website: https://www.mathworks.com/products/matlab.html

Pre Lab Viva Sample Question:


1. What do you mean by time shifting, time scaling and time folding ?

Post Lab Viva Sample Questions:


1. Write a MATLAB script for time shifting, time scaling and time folding?

EEE-314-41 | P a g e
Experiment No. 05
Name of the Experiment: Discrete-time Convolution summation

Objective:
The objectives of this experiment are to:
5) Learn about convolution operation and its purpose.
6) Perform convolution summation on signals using MATLAB.

Course Outcomes (COs), Program Outcomes (POs) and Assessment:

Domain /
Delivery
Corresponding level of Assessment
CO Statement methods and
PO learning tools
activities
taxonomy
CO1:Use
MATLAB to Lab tests
generate signals, Simulation Lab reports
describe systems,
apply different Application Experiment Final lab
Affective test
types of signal (PO3) Practice lab
domain/
transformations Modern Tool Open ended
valuing level Group
such as shifting, Usage (PO5) lab
scaling, folding discussion
and perform Project
Tutorial
convolution and show& project
correlation. presentation

Theory:
Convolution is a mathematical operation used to express the relation between input and output of
an LTI system. It relates input, output and impulse response of anLTI system as:

𝑦(𝑛) = 𝑥(𝑛) ∗ ℎ(𝑛)

where, 𝑦(𝑛) = output of LTI


𝑥(𝑛) = input of LTI
ℎ(𝑛) = impulse response of LTI

Discrete Convolution:

𝑦(𝑛) = 𝑥(𝑛) ∗ ℎ(𝑛) = ∑ 𝑥(𝑘)ℎ(𝑛 − 𝑘)


𝑘=−∞
By using convolution we can find zero state response of the system.

If 𝑥(𝑛) and ℎ(𝑛) are provided then one can find the output 𝑦(𝑛) using MATLAB in-built function
conv. The function conv takes two inputs: one is the input signal and the other is the impulse

EEE-314-42 | P a g e
response. No time information is taken as input nor given as output. We will discuss about the time
information in the next section.

Required Devices & Software:

1. A well configured Laptop or PC.


2. Software: Latest Matlab version. (2014-2020).

Experimental simulation procedure:

Exercise 01:The input signal and the impulse response of a system are given as following:
𝑥(𝑛) = {3,11,7,0, −1,4,2} , − 3 ≤ 𝑛 ≤ 3

ℎ(𝑛) = {2,3,0, −5,2,1} , − 1 ≤ 𝑛 ≤ 4

Determine the output signal using convolution.

Solution: Create the signals first. Then use the conv function to generate the output. Finally, plot
the three signals on the same figure using subplots. Plot the output without the time information
as we don’t have it yet. Execute the code below and observe the plot.

n1 = -3:3;
x = [3,11,7,0,-1,4,2];
n2 = -1:4;
h = [2,3,0,-5,2,1];
y = conv(x,h);

subplot(3,1,1)
stem(n1,x);
title('Input signal, x(n)')
xlabel('Index (n)')
ylabel('Amplitude')

subplot(3,1,2)
stem(n2,h);
title('Impulse response, h(n)')
xlabel('Index (n)')
ylabel('Amplitude')

subplot(3,1,3)
stem(y);
title('Output signal, y(n)')
xlabel('Index (n)')
ylabel('Amplitude')

EEE-314-43 | P a g e
Output waveform:

Figure 01: Output figure for exercise 01

Examine the x-axis of the output signal in the figure above. The values are not time index (n),
rather they are actually the MATLAB array indices. We need to figure out the time information.
What is needed is the beginning and ending points of the output signal time index. Then we can
create the output time index using the colon (:) operator. Given the time index of 𝑥(𝑛) and ℎ(𝑛),
these points are very easy to determine.

Suppose, for 𝑥(𝑛), 𝑛𝑥𝑏 ≤ 𝑛 ≤ 𝑛𝑥𝑒 and for ℎ(𝑛), 𝑛ℎ𝑏 ≤ 𝑛 ≤ 𝑛ℎ𝑒 . So, 𝑛𝑥𝑏 and 𝑛𝑥𝑒 are the
beginning and ending points of the input signal 𝑥(𝑛) and 𝑛ℎ𝑏 respectively and 𝑛ℎ𝑒 are the
beginning and ending points of the impulse response ℎ(𝑛) respectively. Now, if 𝑛𝑦𝑏 and 𝑛𝑦𝑒 are
the beginning and ending points of the output signal 𝑦(𝑛), then,

𝑛𝑦𝑏 = 𝑛𝑥𝑏 + 𝑛ℎ𝑏


𝑛𝑦𝑒 = 𝑛𝑥𝑒 + 𝑛ℎ𝑒

Respectively. We can create a new MATLAB function conv_with_time to incorporate these time
information.

function [y,ny] = conv_with_time(x,nx,h,nh)


nyb = nx(1)+nh(1);
nye = nx(end) + nh(end);
ny = nyb:nye;
y = conv(x,h);

EEE-314-44 | P a g e
end

Exercise 02:Consider an integrator-like filter defined by the I/O equation:

𝑦(𝑛) = 0.1{𝑥(𝑛) + 𝑥(𝑛 − 1) + 𝑥(𝑛 − 2) +··· + 𝑥(𝑛 − 14)}

Such a filter accumulates (integrates) the present and past 14 samples of the input signal. The
factor0.1 represents only a convenient scale factor for this experiment. It follows that the impulse
responseof this filter will be,
0.1, 0 ≤ 𝑛 ≤ 14
ℎ(𝑛) = {
0, 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒

To observe the steady-state response of this filter, as well as the input-on and input-off transients,
consider a square wave input signal 𝑥(𝑛) of length L = 200 and period of N = 50 samples.

Solution: First, we need to generate the input signal 𝑥(𝑛) and the impulse response ℎ(𝑛). This part
will be very easy as signal generation has been covered in the previous labs. Then we will use the
function we created (conv_with_time) to generate the output and will also get its time index.

nx = 0:199;
x = repmat([ones(1,25) zeros(1,25)],[1,4]);
subplot(3,1,1)
stem(nx,x)
title('Input signal, x(n)')
xlabel('Index (n)')
ylabel('Amplitude')

nh = 0:14;
h = 0.1*ones(1,15);
subplot(3,1,2)
stem(nh,h)
title('Impulse response, h(n)')
xlabel('Index (n)')
ylabel('Amplitude')

[y,ny] = conv_with_time(x,nx,h,nh);
subplot(3,1,3)
stem(ny,y)
title('Output signal, y(n)')
xlabel('Index (n)')
ylabel('Amplitude')

Output waveform:

EEE-314-45 | P a g e
Figure 02: Output figure for exercise 02

Practice problem01:Repeat Exercise 02 for the filter:

0.25(0.75)𝑛 , 0 ≤ 𝑛 ≤ 14
ℎ(𝑛) = {
0, 𝑒𝑙𝑠𝑒𝑤ℎ𝑒𝑟𝑒

This filter acts more like an RC-type integrator than an accumulator.

Practice problem02: To demonstrate the concepts of impulse response, linearity, and time-
invariance, consider a filter with finite impulse response,ℎ(𝑛) = (0.95)𝑛 , 𝑓𝑜𝑟 0 ≤ 𝑛 ≤ 24.The
input signal,

𝑥(𝑛) = 𝛿(𝑛) + 2𝛿(𝑛 − 40) + 2𝛿(𝑛 − 70) + 𝛿(𝑛 − 80), 𝑛 = 0, 1, . . . , 120

consists of four impulses of the indicated strengths occurring at the indicated time instances. Note
that the first two impulses are separated by more than the duration of the filter, whereas the last
two are separated by less. Using your function conv_with_time, compute the filter output
𝑦(𝑛)andplot it on the same graph with 𝑥(𝑛). Comment on the resulting output with regard to
linearity and time invariance.

Filtering using convolution:


A signal 𝑥(𝑛) is the sum of a desired signal 𝑠(𝑛) and interference𝑤(𝑛):

𝑥(𝑛) = 𝑠(𝑛) + 𝑤(𝑛)

where, 𝑠(𝑛) = 𝐴1 𝑠𝑖𝑛(2𝜋𝑓1 𝑛)and 𝑤(𝑛) = 𝐴2 𝑠𝑖𝑛(2𝜋𝑓2 𝑛).


Suppose, 𝐴1 = 5, 𝑓1 = 1𝐻𝑧, 𝐴2 = 3 and 𝑓2 = 30𝐻𝑧

EEE-314-46 | P a g e
In order to remove 𝑤(𝑛), the signal 𝑥(𝑛) is filtered through a filter that isdesigned to pass the
frequency 𝑓1 and reject the interfering frequency 𝑓2 .

The actual designing of the filter is beyond scope of this lab. For now, say we know that a filter
with the following system equation will be able to remove the noise 𝑤(𝑛):
1
𝑦(𝑛) = {𝑥(𝑛) + 𝑥(𝑛 − 1) + 𝑥(𝑛 − 2)}
3

Practice problem 03: Implement the filter discussed above. Use a sampling frequency of 90
Hzand signal length of 200to generate the signals.Plot the original signal 𝑥(𝑛), the desired signal
𝑠(𝑛) and the output signal 𝑦(𝑛). Observe the output and see if the filter hasreally been able to
remove the noise. You can use the plot function instead of the stem function for better
visualization.

Practice problem 04: A simple digital differentiator is given by,

𝑦(𝑛) = 𝑥(𝑛) − 𝑥(𝑛 − 1)

which computes a backward first-order difference of the input sequence. Implement this
differentiator on the following sequences and plot the results. Use suitable signal length. Comment
on the appropriateness of this simple differentiator.

a) 𝑥(𝑛) = 5{𝑢(𝑛) − 𝑢(𝑛 − 10)}


𝜋𝑛
b) 𝑥(𝑛) = sin( 25 ){𝑢(𝑛) − 𝑢(𝑛 − 100)}

Precautions:
1) Follow the software instructions and lab manual for using the software.
2) Be careful while input the variables value before run the code.
3) Always work in the editor environment. This will you to edit the Matlab Code.
4) Do not turn off your pc or laptop while simulating the design.
5) Check your code with the desired input data.
6) Never hurry. Hurry can cause error in simulation.
7) After simulation collect all the data and graphs carefully, take screen shot if necessary.

Data Analysis:

Report:

1) Submit the complete data sheet with the screenshots of Matlab code and Output
Graphs.
2) Using various variables value complete the data table, observe the output graphs and
write a report

EEE-314-47 | P a g e
Results and Discussion:

References
1) Li Tan, Purdue University, Jean Jiang, Purdue University “Digital Signal Processing:
Fundamentals and Applications’’.
2) Website: https://www.mathworks.com/products/matlab.html

Pre Lab Viva Sample Question:


1. What is convolution? What is the usefulness of convolution in DSP?

Post Lab Viva Sample Questions:


1. The input signal and the impulse response of a system are given as following:
𝑥(𝑛) = {3,11,7,0, −1,4,2} , − 3 ≤ 𝑛 ≤ 3

ℎ(𝑛) = {2,3,0, −5,2,1} , − 1 ≤ 𝑛 ≤ 4

Determine the output signal using convolution

EEE-314-48 | P a g e
Experiment No. 06
Name of the Experiment: Discrete-time Correlation

Objective:
The objectives of this experiment are to:
1) Learn about correlation operations and their properties and purposes.
2) Calculate autocorrelation and cross-correlation of signals using MATLAB.
3) learn about some real-world application of correlation and simulate them in the LAB.

Course Outcomes (COs), Program Outcomes (POs) and Assessment:

Domain /
Delivery
Corresponding level of Assessment
CO Statement methods and
PO learning tools
activities
taxonomy
CO1:Use
MATLAB to Lab tests
generate signals, Simulation Lab reports
describe systems,
apply different Application Experiment Final lab
Affective test
types of signal (PO3) Practice lab
domain/
transformations Modern Tool Open ended
valuing level Group
such as shifting, Usage (PO5) lab
scaling, folding discussion
and perform Project
Tutorial
convolution and show & project
correlation. presentation

Theory:
Correlation: Correlation determines the degree of similarity between two signals. If the signals
are identical, then the correlation coefficient is 1; if they are totally different, the correlation
coefficient is 0, and if they are identical except that the phase is shifted by exactly 180° (i.e.
mirrored), then the correlation coefficient is -1.

There are two types of correlations. i) Autocorrelation and ii) cross-correlation

Autocorrelation: The Autocorrelation of a sequence is correlation of a sequence with itself. The


autocorrelation of a sequence 𝑥(𝑛) is defined by,

𝑟𝑥𝑥 (𝑙) = ∑ 𝑥(𝑛)𝑥(𝑛 − 𝑙) 𝑙 = 0, ±1, ±2, ±3, …


𝑛=−∞
Properties of Autocorrelation:
1. 𝑟𝑥𝑥 (0) = 𝑒𝑛𝑒𝑟𝑔𝑦(𝑥)
2. Autocorrelation function is symmetric. i.e. 𝑟𝑥𝑥 (𝑙) = 𝑟𝑥𝑥 (−𝑙)

EEE-314-49 | P a g e
Algorithm for calculating Autocorrelation:
1. Read the input sequence x[n].
2. Auto correlate the signal using xcorr(x,x). [xcorr is the built in MATLAB function for
performing correlation.]
3. Display the auto correlation result on a suitable axis.

You can additionally verify the correlation property 𝑟𝑥𝑥 (0) = 𝑒𝑛𝑒𝑟𝑔𝑦(𝑥) and the symmetric
property to get a better understanding.

Required Devices & Software:

1. A well configured Laptop or PC.


2. Software: Latest Matlab version. (2014-2020).

Experimental simulation procedure:

Example 01: For the following signal 𝑥(𝑛) calculate the autocorrelation sequence 𝑟𝑥𝑥 (𝑙).

1, −5≤𝑛 ≤5
𝑥(𝑛) = {
0, 𝑒𝑙𝑠𝑒𝑤ℎ𝑒𝑟𝑒

Also verify the properties of autocorrelation by inspection.

Solution:
Execute the following MATLAB code:

n = -5:5; % define the x axis for input sequence


x = ones(1,11); % define the amplitude for the input
[Rxx,lag] = xcorr(x, x); % calculate the autocorrelation
subplot(2,1,1); % plot the input and output sequences
stem (n, x);
xlabel('n');
ylabel('x(n)');
title('Plot of x(n)');
subplot(2,1,2);
stem(lag,Rxx);
title('Autocorrelation output');
xlabel('n');
ylabel('r(n)');

% Verifying property 1
Energy = sum(x.^2); % calculate the energy of input signal
fprintf("Energy: %2.f\n",Energy);

EEE-314-50 | P a g e
Output waveform:

Fig. 1: Output of Example 01

Using Data Tips on the MATLAB figure, we can clearly see that that,

𝑟𝑥𝑥 (0) = 𝑒𝑛𝑒𝑟𝑔𝑦(𝑥) = 11

So, the first property is verified. Also, we can see that 𝑟𝑥𝑥 (𝑙) is symmetric about the y axis. So, the
second property is also verified.

Cross-correlation: When two independent signals are compared, the procedure is known as cross-
correlation. The cross-correlation between two signals 𝑥(𝑛) and 𝑦(𝑛) is given by,

𝑟𝑥𝑦 (𝑙) = ∑ 𝑥(𝑛)𝑦(𝑛 − 𝑙) 𝑙 = 0, ±1, ±2, ±3, …


𝑛=−∞
or

𝑟𝑦𝑥 (𝑙) = ∑ 𝑦(𝑛)𝑥(𝑛 − 𝑙) 𝑙 = 0, ±1, ±2, ±3, …


𝑛=−∞

Properties of cross correlation:


1. 𝑟𝑥𝑦 (𝑙) is always a real valued function with positive or negative values if 𝑥(𝑛) and 𝑦(𝑛) are
real.
2. 𝑟𝑥𝑦 (𝑙) = 𝑟𝑦𝑥 (−𝑙), that means 𝑟𝑥𝑦 (𝑙) and 𝑟𝑦𝑥 (𝑙) are folded version of each other about the y-
axis.
2
3. |𝑟𝑥𝑦 (𝑙)| ≤ 𝑟𝑥𝑥 (0)𝑟𝑥𝑥 (0)
4. When 𝑟𝑥𝑦 (𝑙) = 0, x(n) and y(n) are said to be uncorrelated or they said tobe statistically

EEE-314-51 | P a g e
independent.
5. 𝑟𝑥𝑦 (𝑙) may not be necessarily have a maximum at 𝑙 = 0 nor 𝑟𝑥𝑦 (𝑙) an even function.
Example 02: For the following signals 𝑥(𝑛) and𝑦(𝑛), calculate the cross-correlation sequence
𝑟𝑥𝑦 (𝑙).

𝑥(𝑛) = {1, −2, 2, 3, 4} 𝑦(𝑛) = {−1, 1, −1, 1, −1}

Solution:
Execute the following MATLAB code:

nx = [0:4]; % define index for x(n)


ny = [-1:3]; % define index for x(n)
x = [1,-2,2,3,4]; % define x(n)
y = [-1,1,-1,1,-1]; % define y(n)
[Rxy,lag] = xcorr(x, y); % calculate the cross-correlation

subplot(2,2,1); % plot x(n)


stem (nx, x);
xlabel('n');
ylabel('x(n)');
title('Plot of x(n)');

subplot(2,2,2); % plot y(n)


stem (ny, y);
xlabel('n');
ylabel('y(n)');
title('Plot of y(n)');

subplot(2,2,[3 4]); % plot cross-correlation


stem(lag,Rxy);
title('Cross-correlation output');
xlabel('n');
ylabel('r(n)');

EEE-314-52 | P a g e
Output waveform:

Fig. 2: Output of Example 02

Practice Problem 01: Verify the properties of cross-correlation for Example 02.

Application of cross-correlation in radar/sonar:


Let us suppose that we have two signal sequences 𝑥(𝑛) and𝑦(𝑛)that we wish to compare. In radar
and active sonar applications, 𝑥(𝑛) can represent the sampled version of the transmitted signal and
𝑦(𝑛) can represent the sampled version of the received signal at the output of the analog-to-digital
(A/D) converter. If a target is present in the space being searched by the radar or sonar, the received
signal 𝑦(𝑛)consists of a delayed version of the transmitted signal, reflected from the target, and
corrupted by additive noise. Fig. 3 depicts the radar signal reception problem.

We can represent the received signal sequence as

𝑦(𝑛) = 𝑎𝑥(𝑛 — 𝐷) + 𝑤(𝑛)

where a is some attenuation factor representing the signal loss involved in the round-trip
transmission of the signal 𝑥(𝑛), 𝐷 is the round-trip delay, which is assumed to be an integer
multiple of the sampling interval, and 𝑤(𝑛) represents the additive noise that is picked up by the
antenna and any noise generated by the electronic components and amplifiers contained in the
front end of the receiver. On the other hand, if there is no target in the space searched by the radar
and sonar, the received signal 𝑦(𝑛) consists of noise alone.

Cross-correlation between 𝑥(𝑛) and𝑦(𝑛) can be used to determine if the received signal contains

EEE-314-53 | P a g e
the transmitted signal or not and also the delay between transmission and reception. The cross-
correlation when 𝑦(𝑛) contains delayed 𝑥(𝑛) is distinctly different than the cross-correlation when
there is only noise is 𝑦(𝑛). The reason is simple. As cross-correlation measures the similarity
between signals, when 𝑥(𝑛) and 𝑦(𝑛) matches the output is very high at that time. But if 𝑦(𝑛) is
only noise then there is no match and the output is low.

Fig. 3: Radar target detection

Suppose, 𝑥(𝑛) = 5cos(𝜋𝑛/10) and the noise is a uniformly distributed random signalhaving
values in the range of 0 to 1. We will explore both the cases (when 𝑦(𝑛) contains 𝑥(𝑛) and when
𝑦(𝑛) do not contains 𝑥(𝑛)) and observe the output of the cross-correlation in the following
example.

Example 03:
Execute and try to understand the following MATLAB code:

n = [0:100]; % define index for x(n)


x = 5*cos(pi*n/10); % define transmitted signal x(n)
w = rand(1,length(n)); % define noise w(n)

subplot(2,2,1); % plot x(n)


stem (n, x);
xlabel('n');
ylabel('x(n)');
title('Transmitted signal x(n)');
EEE-314-54 | P a g e
% Case 01: Received signal contains transmitted signal
% We will use no delay and attenuation for this basic example
y = x + w; % define received singal
subplot(2,2,2); % plot x(n)
stem (n, y);
xlabel('n');
ylabel('y(n)');
title('Received signal y(n) = x(n) + w(n)');

[Rxy,lag] = xcorr(y, x); % calculate the cross-correlation


subplot(2,2,3); % plot cross-correlation
stem (lag, Rxy);
xlabel('n');
ylabel('r(n)');
title('Cross-correlation: CASE 01');

% Case 02: Received signal contains only noise


y = w; % define received singal
[Rxy,lag] = xcorr(y, x); % calculate the cross-correlation
subplot(2,2,4); % plot cross-correlation
stem (lag, Rxy);
xlabel('n');
ylabel('r(n)');
title('Cross-correlation: CASE 02');

EEE-314-55 | P a g e
Output waveform:

Fig. 4: Output of Example 03

Observe the difference between the two cases. In the first case, the output of correlation is very
high. The highest value is more than 1000 and is occurred at 𝑛 = 0. If we had a delay of 20 samples
the highest value would have been located at 𝑛 = 20. This is how we can calculate the delay. In
the second case, the highest value is only close to 20. This reflects the fact that there is not as much
similarity as in the first case.

Application of autocorrelation in detecting periodic signals buried in noise:


correlation is used to identify periodicities in an observed physical signal which may becorrupted
by random interference. For example, consider a signal sequence 𝑦(𝑛) of the form,

𝑦(𝑛) = 𝑥(𝑛) + 𝑤(𝑛)

where,𝑥(𝑛) is a periodic sequence of some unknown period 𝑁 and 𝑤(𝑛) represents an additive
random interference. Suppose, we observe 𝑀 samples of 𝑦(𝑛). Now, we can write a normalized
version of autocorrelation sequence of 𝑦(𝑛) as given bellow:

𝑀−1
1
𝑟𝑦𝑦 (𝑙) = ∑ 𝑦(𝑛)𝑦(𝑛 − 𝑙)
𝑀
𝑛=0

Following the calculation done in “Digital Signal Processing” (Proakis and Manolakis, Chapter 2,

EEE-314-56 | P a g e
Topic 2.6, Page 124) we can write,

𝑟𝑦𝑦 (𝑙) = 𝑟𝑥𝑥 (𝑙) + 𝑟𝑤𝑥 (𝑙) + 𝑟𝑥𝑤 (𝑙) + 𝑟𝑤𝑤 (𝑙)

The cross-correlation term of 𝑟𝑤𝑥 (𝑙) 𝑎𝑛𝑑 𝑟𝑥𝑤 (𝑙) will be relatively small as there is no similarity
between the periodic signal 𝑥(𝑛) and random noise 𝑤(𝑛). The autocorrelation term 𝑟𝑤𝑤 (𝑙) will be
large at 𝑙 = 0 but decay rapidly toward zero because there is no similarity between a random signal
and its delayed version. So, only the autocorrelation term 𝑟𝑥𝑥 (𝑙) will have large peaks at a fixed
interval. If there is no periodic signal in 𝑦(𝑛), 𝑟𝑦𝑦 (𝑙) will only have the term 𝑟𝑤𝑤 (𝑙), which will
not have periodic peaks. This behavior slows us to detect a small periodic signal buried in a
relatively large random interference.

Practice Problem 02:Suppose, you want to work on a signal 𝑦(𝑛) which consists of two parts, a
periodic signal 𝑥(𝑛) = 0.2cos(𝜋𝑛/10) and a noise𝑤(𝑛), which is a uniformly distributed random
signal having values in the range of 0 to 1. But you don’t know about the periodic signal. How can
you check if there is any? Write a MATLAB program which can serve this cause.

Precautions:
1. Follow the software instructions and lab manual for using the software.
2. Be careful while input the variables value before run the code.
3. Always work in the editor environment. This will you to edit the Matlab Code.
4. Do not turn off your pc or laptop while simulating the design.
5. Check your code with the desired input data.
6. Never hurry. Hurry can cause error in simulation.
7. After simulation collect all the data and graphs carefully, take screen shot if necessary.

Data Analysis:

Report:

1. Submit the complete data sheet with the screenshots of Matlab code and Output
Graphs.
2. Using various variables value complete the data table, observe the output graphs and
write a report

Results and Discussion:

References
1. Li Tan, Purdue University, Jean Jiang, Purdue University “Digital Signal Processing:
Fundamentals and Applications’’.
2. Website: https://www.mathworks.com/products/matlab.html

EEE-314-57 | P a g e
Pre Lab Viva Sample Question:
1. Discrete-time Correlation and what are the applications of correlation?

Post Lab Viva Sample Questions:


1. Suppose, you want to work on a signal 𝑦(𝑛) which consists of two parts, a periodic signal
𝑥(𝑛) = 0.2cos(𝜋𝑛/10) and a noise𝑤(𝑛), which is a uniformly distributed random signal
having values in the range of 0 to 1. But you don’t know about the periodic signal. How
can you check if there is any? Write a MATLAB program which can serve this cause.

EEE-314-58 | P a g e
Experiment No. 07:
Name of the Experiment: Z-transformation and its application.

Objective:
The objectives of this experiment are to:
4) Analyze signals and systems using Z-transform.
5) Learn usage of some signal processing and symbolic toolbox functions related to Z-
transform.
6) Create simple discrete time systems from given parameters.

Course Outcomes (COs), Program Outcomes (POs) and Assessment:

Domain /
Delivery
Corresponding level of Assessment
CO Statement methods and
PO learning tools
activities
taxonomy
Lab tests
CO2:Analyze Simulation Lab reports
discrete time Investigation(P
systems using O4) Experiment Final lab
Affective test
signal processing Modern Tool Practice lab
domain/
and symbolic Usage: (PO5) Open ended
valuing level Group
toolboxes of lab
MATLAB. discussion
Project
Tutorial
show & project
presentation
Lab tests
 CO3: Design
different discrete Simulation Lab reports
time systems to Experiment Final lab
Investigation Affective
meet specific test
(PO4) domain/char Practice lab
practical
Modern Tool acterizing Open ended
requirements and Group
Usage: (PO5) level lab
limitations. discussion
 Project
 Tutorial
show & project
presentation

Theory:
The Z-transform plays the same role in the analysis of discrete time signals and LTI systems as
the Laplace transform does in the analysis of continuous-time signals and LTI systems. The use of
z-transform techniques permits simple algebraic manipulations. The Z-transform has become an

EEE-314-59 | P a g e
important tool in the analysis and design of digital filters. In this experiment, different features of
Z-transform have been discussed through various examples and pre-lab work exercises. At the end
of this experiment, one will be able to perform Z transformation, inverse Z transform, system
analysis through stability and causality.

New functions introduced:

ztrans(), iztrans(), residuez(), sos2tf(),zplane(), roots(), poly(), impz()

Z-transform:
The Z-transform of a discrete time signal 𝑥(𝑛) is –

𝑋(𝑧) = ∑ 𝑥(𝑛)𝑧 −𝑛
𝑛=−∞
where, z is a complex variable. Since Z-transform is an infinite series, it exists only for those values
of z for which this series converges. The region of convergence (ROC) of 𝑋(𝑧) is the set of all
values of z for which 𝑋(𝑧) attains a finite value.

If 𝑥(𝑛) is a finite duration signal like -


𝑥(𝑛) = {1, 2, 5, 7, 0, 1}
Its Z-transformed signal,
𝑋(𝑧) = 1 + 2𝑧 −1 + 5𝑧 −2 + 7𝑧 −3 + 𝑧 −5
And its ROC is entire Z-plane except 𝑧 = 0 as at 𝑧 = 0, 𝑋(𝑧) becomes infinite.

If 𝑥(𝑛) is -
𝑥(𝑛) = {1, 2, 5, 7, 0, 1}
Its Z-transformed signal,
𝑋(𝑧) = 𝑧 2 + 2𝑧 + 5 + 7𝑧 −1 + 𝑧 −3
And its ROC is entire Z-plane except 𝑧 = 0 and 𝑧 = ∞.

Converting a Z-domain function back to its time-domain form is called inverse Z-transform.

For Z-transform pairs and ROC of common functions, look up Table 3.1 (Chapter 3, Digital signal
processing - principles, algorithms, and applications, Proakis and Manolakis).

Required Devices & Software:

1. A well configured Laptop or PC.


2. Software: Latest Matlab version. (2014-2020).

EEE-314-60 | P a g e
Experimental simulation procedure:

Performing Z-transformation and invers Z-transform using MATLAB:


For representing a signal like this:
𝑋(𝑧) = 1 + 2𝑧 −1 + 5𝑧 −2 + 7𝑧 −3 + 𝑧 −5
in MATLAB, we justhave to put the coefficients of the polynomial like this:

x = [1, 2, 5, 7, 0, 1];

For converting the time domain signal into equivalent Z domain ztrans() function can be used
which is part of the symbolic toolbox. It evaluates signals of the form 𝑥[𝑛]𝑢[𝑛], i.e. for non-
negative values of 𝑛.

Example 01: Consider the infinite duration time domain signal below:
𝑥(𝑛) = 𝑎𝑛 𝑢(𝑛)
Determine the Z domain signal.

We have to declare some symbolic variables, declare the function and the ztrans() function on it.
Write down the following code and execute it.

syms a n;
x = a^n;
X = ztrans(x)

Compare the result with the result found solving by hand.Note that there is also the iztrans()
function which performs inverse Z-transform.

Practice Problem 01:Determine the Z-transform of the time domain signals below:
i. 𝑥(𝑛) = sin(𝑤0 𝑛) 𝑢(𝑛)
ii. 𝑥(𝑛) = 𝑢(𝑛)
iii. 𝑥(𝑛) = 𝛿(𝑛)
iv. 𝑥(𝑛) = 𝑛𝑎𝑛 𝑢(𝑛)

[Hint: (i), (ii) and (iv) are quite straight forward. For (iii), use the kronecker Delta function to
create the delta function. See the MATLAB help page on kronecker Delta if you have
difficulties.]

Properties of Z-transform:
There are many properties of Z-transform which dictates how a change in one domain affects the
other domain. For example, the time-shifting property states the effect on Z-domain function if the
time-domain function is shifted. Complete collection of the properties are listed in Table 3.2
(Chapter 3, Digital signal processing - principles, algorithms, and applications, Proakis and
Manolakis).

Among different properties of Z-transform, convolution property is an important one. It removes

EEE-314-61 | P a g e
the complexity of doing convolution, because this property transforms the time domain
convolution into a multiplication of two functions.

𝑥(𝑛) ∗ ℎ(𝑛) ≪=≫ 𝑋(𝑧)𝐻(𝑧)


Practice Problem 02: Consider the infinite duration time domain signals below:

𝑥(𝑛) = 0.9𝑛 𝑢(𝑛)


ℎ(𝑛) = 𝑢(𝑛)

Here, 𝑥(𝑛) is the system input and ℎ(𝑛) is the impulse response. Determine the system response
𝑦(𝑛).

[Hint: In time domain we will have to perform convolution to find the output. But in Z-domain we
will have to just multiply. So, transform both the signals to Z-domain, multiply and then inverse
transform back to the time domain.]

Inverse Z-transform:
In previous sections it was shown that the iztrans() function can be used to perform the inverse Z-
transform operation. But iztrans()normally returns a very long and complicated symbolic
expressions. There are other methods to perform inverse Z_transform.MATLAB functions
deconv() and residuez() can be used in this purpose. The deconv() function is used to perform the
long division required in the power series method. The residuez() function is used to find the
partial fraction coefficients (residues) and poles of the Z-transform.

Power series expansion with MATLAB


In the power series method, the key operation is polynomial division. The MATLAB function
deconv() performs the deconvolution operation. In the power series method, we exploit the fact
that the deconvolution operation is equivalent to polynomial division. Thus, given a Z-transform
𝑋(𝑧), of the form:

𝑏0 + 𝑏1 𝑧 −1 + 𝑏2 𝑧 −2 + ⋯ + 𝑏𝑚 𝑧 −𝑚 𝐵(𝑧)
𝑋(𝑧) = =
𝑎0 + 𝑎1 𝑧 −1 + 𝑎2 𝑧 −2 + ⋯ + 𝑎𝑛 𝑧 −𝑛 𝐴(𝑧)

the format of the command for deconvolution is:

[q,r] = deconv(B,A)

where Band A are vectors representing the numerator and denominator polynomials, 𝐵(𝑧) and
𝐴(𝑧), respectively, in increasing negative powers of 𝑧. The quotient of the polynomial division is
returned in the vector 𝑞 and the remainder is contained in 𝑟. To implement the power series
method, the long division operation is applied successively depending on the number of points
required in the inverse operation.

Example 03: Find the first five terms of the inverse Z-transform, 𝑥(𝑛), using the power series

EEE-314-62 | P a g e
(polynomial division) method and MATLAB. Assume that the Z-transform, 𝑋(𝑧), hasthe
following form:

1 + 2𝑧 −1 + 𝑧 −2
𝑋(𝑧) =
1 − 𝑧 −1 + 0.3561𝑧 −2

Solution:
The coefficient for the numerator and denominator polynomials are formed, zeros are appended to
the coefficient vector b to ensure the correct dimension for MATLAB, and then the command
deconv() is used to compute the inverse Z-transform.

B=[1 2 1];
A=[1 -1 0.3561];
n=5;% number of power series points
B=[B zeros(1, n-1)];
[x, r]=deconv(B,A);
disp(x)

The result will be:

1.0000 3.0000 3.6439 2.5756 1.2780

That means, 𝑥(0) = 1, 𝑥(1) = 3, 𝑥(2) = 3.6439, 𝑥(3) = 2.5756, 𝑥(4) = 1.2780.

Example 04:
Find the first five values of the inverse z-transform of the following using the power series
(polynomial division) method and MATLAB:

𝑁1 (𝑧)𝑁2 (𝑧)𝑁3 (𝑧)


𝑋(𝑧) =
𝐷1 (𝑧)𝐷2 (𝑧)𝐷3 (𝑧)
where,
𝑁1 (𝑧) = 1 − 1.22346𝑧 −1 + 𝑧 −2
𝑁2 (𝑧) = 1 − 0.437833𝑧 −1 + 𝑧 −2
𝑁3 (𝑧) = 1 + 𝑧 −1
𝐷1 (𝑧) = 1 − 1.4334509𝑧 −1 + 0.85811𝑧 −2
𝐷2 (𝑧) = 1 − 1.293601𝑧 −1 + 0.556929𝑧 −2
𝐷3 (𝑧) = 1 – 0.612159𝑧 −1

Solution:
The Z-transform has three pairs of numerator and denominator polynomials. In the MATLAB
implementation (Example 03), the vectors containing the polynomial coefficient are first formed.
The MATLAB function sos2tf() (second order sections – to- transfer function) is then used to
convert the three pairs of polynomials into a transfer function with a pair of rational polynomials,
like𝐵(𝑧)/𝐴(𝑧):

EEE-314-63 | P a g e
𝐵(𝑧) 𝑏0 + 𝑏1 𝑧 −1 + 𝑏2 𝑧 −2 + ⋯ + 𝑏𝑚 𝑧 −𝑚
𝑋(𝑧) = =
𝐴(𝑧) 𝑎0 + 𝑎1 𝑧 −1 + 𝑎2 𝑧 −2 + ⋯ + 𝑎𝑛 𝑧 −𝑛

The deconv() function is used to generate the inverse Z-transform coefficients. The firstfive values
of the inverse Z-transform are

𝑥(0) = 1.0000, 𝑥(1) = 4.6915, 𝑥(2) = 11.4246, 𝑥(3) = 19.5863, 𝑥(4) = 27.0284

n = 5;% number of power series points


N1 = [1 -1.122346 1];
D1 = [1 -1.433509 0.858111];
N2 = [1 1.474597 1];
D2 = [1 -1.293601 0.556929];
N3 = [1 1 0];
D3 = [1 -0.612159 0];

b = [N1; N2; N3];


a = [D1; D2; D3];

[B,A] = sos2tf([ba]);
B = [B zeros(1,n-1 )];
[x,r] = deconv(B,A); %perform long division
disp(x);

Partial fraction expansion with MATLAB


The MATLAB function residuez() may be used to perform partial fraction expansion of a Z-
transform, 𝑋(𝑧), expressed as a ratio of two polynomials. The syntax for the residuez() command
is

[r, p, k] = residuez(B, A)

where B and A are vectors representing the numerator and denominator polynomials,
𝐵(𝑧) and 𝐴(𝑧), respectively, in increasing negative powers of 𝑧 as follows:

𝑏0 + 𝑏1 𝑧 −1 + 𝑏2 𝑧 −2 + ⋯ + 𝑏𝑚 𝑧 −𝑚 𝐵(𝑧)
𝑋(𝑧) = =
𝑎0 + 𝑎1 𝑧 −1 + 𝑎2 𝑧 −2 + ⋯ + 𝑎𝑛 𝑧 −𝑛 𝐴(𝑧)

If the poles of 𝐻(𝑧) are distinct, its partial fraction expansion has the form

𝐵(𝑧) 𝑟1 𝑟𝑛
𝑋(𝑧) = = −1
+ ⋯ + −1
+ 𝑘1 + 𝑘2 𝑧 −1 + ⋯ + 𝑘𝑚−𝑛 𝑧 −(𝑚−𝑛)
𝐴(𝑧) 1 − 𝑝1 𝑧 1 − 𝑝𝑛 𝑧

After calculating the partial fraction, Table 3.1 and 3.2 can be used to get the time domain signal.

EEE-314-64 | P a g e
The residuez() function returns the residues of the rational polynomial 𝐵(𝑧)/𝐴(𝑧) in the vector 𝑟,
the pole positions in 𝑝, and the constant terms in 𝑘.

Example 05:Find the partial fraction expansion of the following Z-transform,

1 + 2𝑧 −1 + 𝑧 −2
𝐻(𝑧) =
1 − 𝑧 −1 + 0.3561𝑧 −2

Solution:

[r, p, k] = residuez([1,2,1], [1, -1, 0.3561])

The result is:

r=
-0.9041 - 5.9928i
-0.9041 + 5.9928i
p=
0.5000 + 0.3257i
0.5000 - 0.3257i
k=
2.8082

Thus, the Z-transform, expressed as a partial fraction expansion, becomes

−0.9041 − 5.9928j −0.9041 + 5.9928j


𝐻(𝑧) = −1
+ + 2.8082
1 − (0.5000 + 0.3257j)𝑧 1 − (0.5000 − 0.3257j)𝑧 −1

Practice problem 03:


Find the first five values of the inverse Z-transform of the function in Example 04 using thepartial
fraction expansion method and MATLAB.

[Hint: Mix the procedure used in Example 04 and 05.]

Pole-zero diagram:
The MATLAB function, zplane(), allows the computation and display of the pole-zero diagram.
The syntax for the command is:

zplane(B,A)

where B and A are the coefficient vectors of the numerator and denominator polynomials,
𝐵(𝑧)/𝐴(𝑧). In this format, the command first finds the locations of the poles and zeros (i.e. the
roots of 𝐵(𝑧) and 𝐴(𝑧), respectively) and then plots the Z-plane diagram.

EEE-314-65 | P a g e
Example 06:
A discrete-time system is characterized by the following transfer function:

1 + 1.16180𝑧 −1 + 𝑧 −2
𝐻(𝑧) =
1 − 1.5161𝑧 −1 + 0.878𝑧 −2

Obtain and plot its pole-zero diagram.

Solution:

B = [1 -1.6180 1];
A = [1 -1.5161 0.878];

zplane(B, A) % compute and plot the pole-zero diagram

Fig. 1: Pole-Zero diagram (plot) of function in Example 06.

If the locations of the poles and zeros are known, these can be used as inputs to the zplane()
command. The syntax of the command in this case is,

zplane(z, p)

where z and p are the zeros and poles.

The locations of the poles and zeros can be found directly using the roots() command. This is

EEE-314-66 | P a g e
useful for converting between pole and zero and the transfer function representations. For example,
an IIR filter is represented by,

1 + 1.16180𝑧 −1 + 𝑧 −2
𝐻(𝑧) =
1 − 1.5161𝑧 −1 + 0.878𝑧 −2

B = [1 -1.618 11];
A = [1 -1 .5161 0.878];
zk = roots(B);
pk = roots(A);

On the other hand, the numerator and denominator polynomials, b(z) and a(z), can beobtained
using thepoly() function:

B = poly(zk);
A = poly(pk);

Impulse response:
We can use the impz() function to calculate the impulse response of a given system in the form:
𝑏0 + 𝑏1 𝑧 −1 + 𝑏2 𝑧 −2 + ⋯ + 𝑏𝑚 𝑧 −𝑚 𝐵(𝑧)
𝑋(𝑧) = =
𝑎0 + 𝑎1 𝑧 −1 + 𝑎2 𝑧 −2 + ⋯ + 𝑎𝑛 𝑧 −𝑛 𝐴(𝑧)

The syntax of the impz() function is,

h = impz(B, A, n)

where the variables B and A are the vectors of the numerator and denominator polynomials’ is the
number of samples of the impulse response. If n is 50, first 50 samples of the impulse response
will be calculated and returned.

Example 07:A discrete-time system is characterized by the following transfer function:

1 + 1.16180𝑧 −1 + 𝑧 −2
𝐻(𝑧) =
1 − 1.5161𝑧 −1 + 0.878𝑧 −2

Plot the first 50 samples of the impulse response.

Solution:

B = [1 -1.6180 1];
A = [1 -1.5161 0.878];
n = 50;
impz(B,A,n); % compute and plot the impulse response

EEE-314-67 | P a g e
Fig. 2: Impulse response of the system in Example 07.
Practice problem 04:
For the following system,
𝑦(𝑛) = 0.9𝑦(𝑛 − 1) + 𝑥(𝑛)

I. Find 𝐻(𝑧) and plot the pole-zero diagram.


II. Plot the first 100 samples of the impulse response.

Practice problem 05:


i. Make transfer functions of two stable and two unstable systems.
ii. Plot the pole-zero diagrams.
iii. Calculate and plot the impulse responses and verify the stability of the systems.

Precautions:
1) Follow the software instructions and lab manual for using the software.
2) Be careful while input the variables value before run the code.
3) Always work in the editor environment. This will you to edit the Matlab Code.
4) Do not turn off your pc or laptop while simulating the design.
5) Check your code with the desired input data.
6) Never hurry. Hurry can cause error in simulation.
7) After simulation collect all the data and graphs carefully, take screen shot if necessary.

Data Analysis:

Report:

EEE-314-68 | P a g e
1. Submit the complete data sheet with the screenshots of Matlab code and Output
Graphs.
2. Using various variables value complete the data table, observe the output graphs and
write a report

Results and Discussion:

References
1. Li Tan, Purdue University, Jean Jiang, Purdue University “Digital Signal Processing:
Fundamentals and Applications’’.
2. Website: https://www.mathworks.com/products/matlab.html

Pre Lab Viva Sample Question:


1. What is Z-transformation and its application?

Post Lab Viva Sample Questions:


1. For the following system,
𝑦(𝑛) = 0.9𝑦(𝑛 − 1) + 𝑥(𝑛)

I. Find 𝐻(𝑧) and plot the pole-zero diagram.


II. Plot the first 100 samples of the impulse response.
2.
iv. Make transfer functions of two stable and two unstable systems.
v. Plot the pole-zero diagrams.
vi. Calculate and plot the impulse responses and verify the stability of the systems.

EEE-314-69 | P a g e
Experiment No. 08:
Name of the Experiment: Discrete Time Fourier Series and its application

Objective:
The objectives of this experiment are to:
1) Learn how Discrete Time Fourier Series works.
2) Analyze signals using Discrete Time Fourier Series.
3) Learn usage of some signal processing functions related to Discrete Time Fourier Series.

Course Outcomes (COs), Program Outcomes (POs) and Assessment:

Domain /
Delivery
Corresponding level of Assessment
CO Statement methods and
PO learning tools
activities
taxonomy
Lab tests
CO2: Analyze Simulation Lab reports
discrete time Investigation(P
systems using O4) Experiment Final lab
Affective test
signal processing Modern Tool Practice lab
domain/
and symbolic Usage: (PO5) Open ended
valuing level Group
toolboxes of lab
MATLAB. discussion
Project
Tutorial
show & project
presentation

Theory:

Discrete Time Fourier Series (DTFS):


Let us consider a sequence 𝑥(𝑛) with period N, that is 𝑥(𝑛) = 𝑥(𝑛 + 𝑁) for all 𝑛.The Fourier
series representation of 𝑥(𝑛) consists of 𝑁 harmonically related exponential functions

𝑗2𝜋𝑘𝑛
𝑒 𝑁 , 𝑤ℎ𝑒𝑟𝑒 𝑘 = 0, 1, 2, … , 𝑁 − 1

And expressed as,


𝑁−1
𝑗2𝜋𝑘𝑛
𝑥(𝑛) = ∑ 𝐶𝑘 𝑒 𝑁
𝑘=0

This equation is often called the discrete-time Fourier series (DTFS). Fourier
coefficients,𝐶𝑘 𝑓𝑜𝑟 𝑘 = 0, 1, 2, … , 𝑁 − 1 provide the description of 𝑥(𝑛) is in frequency

EEE-314-70 | P a g e
domain.𝐶𝑘 can be computed as,

𝑁−1
1 𝑗2𝜋𝑘𝑛
𝐶𝑘 = ∑ 𝑥(𝑛)𝑒 𝑁
𝑁
𝑛=0

Note that,𝐶𝑘+𝑁 = 𝐶𝑘 , that is, 𝐶𝑘 is a periodic sequence with fundamental period 𝑁. Thus, the
spectrum of a signal 𝑥(𝑛),which is periodic with period 𝑁, is a periodic sequence with period 𝑁.

Average power can be given as

𝑁−1 𝑁−1
1
𝑃𝑥 = ∑|𝐶𝑘 |2 = ∑|𝑥(𝑛)|2
𝑁
𝑘=0 𝑛=0

The sequence|𝐶𝑘 |2 𝑓𝑜𝑟 𝑘 = 0, 1, 2, … , 𝑁 − 1is the distribution of power as a function offrequency


and is called the power density spectrum of the periodic signal.

If the signal 𝑥(𝑛) is real [𝑥(𝑛) = 𝑥 ∗ (𝑛)] then it can be shown that,
𝐶𝑘 ∗ = 𝐶−𝑘

Again,the following symmetry relationship holds,


|𝐶𝑘 | = |𝐶𝑁−𝑘 | 𝑎𝑛𝑑 ∠𝐶𝑘 = −∠𝐶𝑁−𝑘
and
|𝐶𝑁/2 | = |𝐶𝑁/2 | 𝑎𝑛𝑑 ∠𝐶𝑁/2 = 0
if 𝑁 is even and
|𝐶(𝑁−1)/2 | = |𝐶(𝑁+1)/2 | 𝑎𝑛𝑑 ∠𝐶(𝑁−1)/2 = −∠𝐶(𝑁+1)/2
if 𝑁 is odd.

Required Devices & Software:

1. A well configured Laptop or PC.


2. Software: Latest Matlab version. (2014-2020).

Experimental simulation procedure:

Example 01:
Calculate Fourier Series Coefficients of a continuous rectangular pulse sequence of 1ms period
and pulse width 0.1ms.The signal is sampled at 100kHz and sampled discrete with 𝑛 its index is
shown below in Fig. 1.

Solution:
The following MATLAB code calculates the Fourier series coefficients from the first principle
and from the Fourier series coefficients, again reconstructs the original periodic sequence.

EEE-314-71 | P a g e
Code (part 1: Generation of the signal)

clear all;
echo on;
Fs = 100e3;
dt = 1/Fs;

%GENERATING THE RECTANGULAR PULSE TRAIN


T = 1e-3; %PERIOD OF THE PULSE TRAIN
D = 0.1; %DUTY CYCLE
PW = D*T; %PULSE WIDTH
f = 1/T; %ANALOG FREQUENCY
t = -T/2:dt:T/2; %TIME INTERVAL FOR A PERIOD
n = t/dt; %INDEX FOR DATA POINTS IN A PERIOD
L = PW/dt; %DATA POINTS IN THE HIGH TIME

x = zeros(1,length(t)); %INITIALIZING A SINGLE RECTANGULAR PULSE


x(find(abs(n)<=L/2)) = 1.1; %GENERATION OF A SINGLE RECTANGULAR PULSE

%END OF THE RENTANGULAR PULSE TRAIN


figure(1);
subplot(211);
plot(t,x)
xlabel('Time (Seconds)');
ylabel('x(t)');
title('Continuous signal');
subplot(212)
stem(n,x)
xlabel('n');
ylabel('x(n)');
title('Discrete signal');

EEE-314-72 | P a g e
Plot for code part 1:

Fig. 1: A rectangular pulse in continuous domain and its sampled version.

Code (Part 2: Calculating the Fourier Series Coefficients)


N = length(x); % TOTAL NO OF DATA POINTS IN A PERIOD
Nc = N; %TOTAL NO COEFFICIENTS
if mod(Nc,2) == 0
k = -(Nc/2):(Nc/2)-1;
else
k = -(Nc-1)/2:(Nc-1)/2;
end
c = zeros(1,length(k)); %INITIALIZING FOURIER COEFFICIENTS
for i1=1:length(k)
for i2=1:length(x)
c(i1)=c(i1)+1/N*x(i2)*exp(-1i*2*pi*k(i1)*n(i2)/N);
end
end
figure(2);subplot(211);
stem(k,abs(c));
xlabel('k');ylabel('|c_k|');
title('Fourier series coefficients c_k');
subplot(212);
stem(k,angle(c)*130/pi);
xlabel('k');ylabel('angle(c_k)');

EEE-314-73 | P a g e
Plot for code part 2:

Fig. 2: Magnitude and phase spectrum of the discrete time signal. Note that the phase spectrum is
not skew-symmetric with respect to 𝑘 = 0. It is an artifact of MATLAB and 𝜃 =
+180o and −180o are in fact synonymous.

Code (Part 3: Reconstruction of the signal from Fourier Series Coefficients)


t_remax = T/2;
t_re = -t_remax:dt:t_remax;
n_re=t_re/dt;
x_re=zeros(1,length(n_re));

for i1=1:length(k)
for i2=1:length(x_re)
x_re(i2)= x_re(i2)+c(i1)*exp(1i*2*pi*k(i1)*n_re(i2)/N);
end
end

%END OF RECONSTRUCTION OF SIGNAL


figure(3); subplot(211);
stem(n_re, real(x_re));
xlabel('n'); ylabel('x_{reconstructed}');
title('Reconstructed signal');
subplot(212);
plot(t_re, real(x_re));
xlabel('t'); ylabel('x_{reconstructed}');
title('Reconstructed signal');

EEE-314-74 | P a g e
Plot for code part 3:

Fig. 3: Signals reconstructed from Fourier coefficients

Now, we can see that the Fourier Coefficients are plotted with respect to the variable 𝑘. We can
easily convert this unintuitive variable to continuous time frequency as the relationship between 𝑘
and the continuous time frequency can be represented by the following equation:

𝑘
𝑓= 𝐹
𝑁 𝑠
where,
𝑓 = Continuous time frequency
𝑁 = Period of the discrete time signal
𝐹𝑠 = Sampling frequency

Practice Problem01:
Modify code part 2 in Example 01 so that the x-axis of the plot of Fourier Coefficients represents
the continuous frequency instead of the variable 𝑘.

Practice Problem02:
Plot the power density spectrum of the signal in Example 01 with respect to the continuous
frequency.

[Hint: In Fig. 2 you can see that |𝐶𝑘 | has been plotted. For the power density spectrum you will
have to just plot |𝐶𝑘 |2 instead of |𝐶𝑘 | as evident from the equation of the average power in theory
section.]

EEE-314-75 | P a g e
In Example 01, we have used 𝑁𝑐 = 𝑁 or in other words, the number of coefficients is same as the
number of samples in one period of the original signal.
If we take less coefficients than 𝑁 (i.e. 𝑁 = 101 and 𝑁𝑐 = 50), we will not be able to perfectly
reconstruct the signal which is intuitive because we have essentially left out some information.
If we take more coefficients than 𝑁 and 𝑁𝑐 is integer multiple of 𝑁 (i.e. 𝑁 = 101 and 𝑁𝑐 = 303),
there will be no extra benefit. That means we will get the same result as when we used 𝑁𝑐 = 𝑁.
This is also intuitive because we are actually getting no extra information. We are just providing
more information than needed.
If we take more coefficients than 𝑁 and 𝑁𝑐 is not integer multiple of 𝑁 (i.e. 𝑁 = 101 and 𝑁𝑐 =
300), there will be some errors because of disturbance in symmetry.

Practice Problem03:
For the signal in Example 01, plot the Fourier coefficients and the reconstructed signals for each
of the case below:

𝑁𝑐 = 𝑓𝑎𝑐𝑡𝑜𝑟 ∗ 𝑁

1. 𝑓𝑎𝑐𝑡𝑜𝑟 = 0.1
2. 𝑓𝑎𝑐𝑡𝑜𝑟 = 0.5
3. 𝑓𝑎𝑐𝑡𝑜𝑟 = 2.5
4. 𝑓𝑎𝑐𝑡𝑜𝑟 =3

Comment on the results. Can you find out the errors in each case?

[Hint: To calculate the error, first, calculate the difference between the original and the
reconstructed signal. Then, square the differences (errors) and lastly, get the summation of squared
errors and find the mean value. This type of error calculation is called the mean squared error
(MSE).]
Practice Problem04:
Repeat Example 01 to Practice Problem 03 for the triangular pulse depicted in Fig. 4. Assume, the
sampling frequency is 500 Hz.

EEE-314-76 | P a g e
Fig. 4: Signal for practice problem 04.

Precautions:
15) Follow the software instructions and lab manual for using the software.
16) Be careful while input the variables value before run the code.
17) Always work in the editor environment. This will you to edit the Matlab Code.
18) Do not turn off your pc or laptop while simulating the design.
19) Check your code with the desired input data.
20) Never hurry. Hurry can cause error in simulation.
21) After simulation collect all the data and graphs carefully, take screen shot if necessary.

Data Analysis:

Report:

5) Submit the complete data sheet with the screenshots of Matlab code and Output
Graphs.
6) Using various variables value complete the data table, observe the output graphs and
write a report

Results and Discussion:

EEE-314-77 | P a g e
References
1. Li Tan, Purdue University, Jean Jiang, Purdue University “Digital Signal Processing:
Fundamentals and Applications’’.
2. Website: https://www.mathworks.com/products/matlab.html

Pre Lab Viva Sample Question:


I. What is Discrete Time Fourier Series and its application?

Post Lab Viva Sample Questions:


1. For the signal in Example 01, plot the Fourier coefficients and the reconstructed signals for
each of the case below:

𝑁𝑐 = 𝑓𝑎𝑐𝑡𝑜𝑟 ∗ 𝑁

I. 𝑓𝑎𝑐𝑡𝑜𝑟 = 0.1
II. 𝑓𝑎𝑐𝑡𝑜𝑟 = 0.5
III. 𝑓𝑎𝑐𝑡𝑜𝑟 = 2.5
IV. 𝑓𝑎𝑐𝑡𝑜𝑟 =3

Comment on the results. Can you find out the errors in each case?

[Hint: To calculate the error, first, calculate the difference between the original and the
reconstructed signal. Then, square the differences (errors) and lastly, get the summation of squared
errors and find the mean value. This type of error calculation is called the mean squared error
(MSE).]
2. Repeat Example 01 to Practice Problem 03 for the triangular pulse depicted in Fig. 4.
Assume, the sampling frequency is 500 Hz

EEE-314-78 | P a g e
Experiment No. 09:
Experiment Name: Discrete Time Fourier Transform and its application

Objective:
The objectives of this experiment are to:
1) Learn how Discrete Time Fourier Transform works.
2) Analyze signals using Discrete Time Fourier Transform.
3) Learn usage of some signal processing functions related to Discrete Time Fourier
Transform.

Course Outcomes (COs), Program Outcomes (POs) and Assessment:

Domain /
Delivery
Corresponding level of Assessment
CO Statement methods and
PO learning tools
activities
taxonomy
Lab tests
CO2: Analyze Simulation Lab reports
discrete time Investigation(P
systems using O4) Experiment Final lab
Affective test
signal processing Modern Tool Practice lab
domain/
and symbolic Usage: (PO5) Open ended
valuing level Group
toolboxes of lab
MATLAB. discussion
Project
Tutorial
show& project
presentation

Theory:

Discrete Time Fourier Transform (DTFT):


If 𝑥(𝑛) is absolutely summable that is if,

∑ |𝑥(𝑛)| < ∞
𝑛=−∞

then its discrete time Fourier transform (DTFT)is given by,


𝑋(𝜔) = ∑ 𝑥(𝑛)𝑒 −𝑗𝜔𝑛


𝑛=−∞

Now 𝜔 is a real variable between − ∞ to + ∞ but one important property of 𝑋(𝜔) is itsperiodicity
in 𝜔 with period 2𝜋. So, we only need one period of 𝑋(𝜔) i.e. 𝜔 ∈ [0, 2𝜋] or[−𝜋, 𝜋].

EEE-314-79 | P a g e
The inverse DTFT equation, in other words the equation for reconstructing the signal from𝑋(𝜔)
is given by,

1 𝜋
𝑥(𝑛) = ∫ 𝑋(𝜔)𝑒 𝑗𝜔𝑛 𝑑𝜔
2𝜋 −𝜋
Required Devices & Software:

1. A well configured Laptop or PC.


2. Software: Latest Matlab version. (2014-2020).

Experimental simulation procedure:

Calculation of Fourier transform from first principle:

Since the frequency is continuous variable, DTFT cannot be implemented in digital hardware in
rigorous sense. By defining a fine grid of frequency in the range [−𝜋, 𝜋]the frequency variable is
usually handled in DTFT implementation. In MATLAB we evaluate 𝑋(𝜔)atequidistant
frequencies between [0, 2𝜋]and then interpolate using plot()function.

Say we want 𝑀 equidistant samples between [0, 2𝜋].

2𝜋𝑘
𝜔𝑘 = , 𝑤ℎ𝑒𝑟𝑒 𝑘 = 0, 1, 2, … , 𝑀 − 1
𝑀

Then, the equation for DTFT can be written as,

𝑛𝑁
𝑗2𝜋𝑘𝑛
𝑋(𝜔𝑘 ) = ∑ 𝑥(𝑛)𝑒 − 𝑀
𝑛=𝑛1

Although matrix method may be an easier approach for DTFT in some cases, we first examine
calculation DTFT of an aperiodic signal from first principles in the following example.

Example 01:
Calculate the Fourier transform of a discrete time sinc signal with a frequency of 1⁄8.Plot the
magnitude spectra and reconstruct the signal back to the time domain.

Solution:
The following MATLAB code calculates the Fourier transform of the given signal using the first
principle. And then, from the Fourier transform, reconstructs the original signal. For better
understanding the code has been organized in three parts.

Code (part 1: Generation of the signal)


%GENERATING A SINC PULSE

EEE-314-80 | P a g e
f_c=1/8; %DEFINING FREQUENCY
n=-40:40; %DEFINING THE TIME INDEX
x=sinc(2*f_c*n);
%END OF SINC PULSE
figure(1);
stem(n, x);
xlabel('n');
ylabel('x(n)');
title('Discrete time signal');

Output of Code Part 1:

Code (Part 2: Calculating the Fourier transform)


M=101; %NUMBER OF POINTS IN DIGITAL FREQUENCY GRID
w=linspace(-pi, pi, M); %DEFINING THE DIGITAL FREQUENCY GRID
dw=w(2)-w(1); %RESOLUTION OF DIGITAL FREQUENCY
X=zeros(1,M); %INITIALIZING THE DTFT OF x(n)
for i1=1:M
for i2=1:length(x)
X(i1)=X(i1)+x(i2)*exp(-1i*w(i1)*n(i2));
end
end
figure(2);
plot(w,abs(X));
xlabel('Frequency (rad/sec)');
ylabel('X(w)');
title('Magnitude spectra');

EEE-314-81 | P a g e
Output of Code Part 2:

Code (Part 3: Reconstruction of the signal from Fourier transform)


%RECONSTRUCTION OF SIGNAL
n_re=-40:40;
x_re=zeros(1,length(n_re)); %INITIALIZING RECONSTRUCTED SIGNAL
for i1=1:M
for i2=1:length(x_re)
x_re(i2)=x_re(i2)+1/(2*pi)*X(i1)*exp(-1i*w(i1)*n_re(i2))*dw;
end
end
figure(3);
stem(n_re, real(x_re));%THE CALCULATED SIGNAL IS COMPLEX
xlabel('t');
ylabel('x_{reconstructed}');
title('Reconstructed signal');

EEE-314-82 | P a g e
Output of Code Part 3:

Practice problem01:
Calculate the Fourier transform of a 𝑠𝑖𝑛𝑐 2 pulse with a frequency of 1⁄8.The signal is given as,

sin(2𝜋𝑓𝑛) 2
𝑥(𝑛) = 𝑠𝑖𝑛𝑐 2 (2𝜋𝑓𝑛) = ( )
2𝜋𝑓𝑛

where, 𝑓 = 1⁄8. Plot the magnitude spectra and reconstruct the signal back to the time domain.

Practice problem02:
Do the following for the signal in Example 01:
1. Increase the range of the frequency grid to [0, 2𝜋]. Observe the magnitude spectra and explain
the effects.
2. Change the range of the frequency range to [−2𝜋, 2𝜋]. Observe the magnitude spectra and
explain the effects.
3. Verify the effect of changing index (𝑛) to -40:120 and -40:80. In each case, observe the
reconstructed signal and explain the result.
4. If you reconstruct the signal in n_re=-240:240, why do you get a repetition of theaperiodic
signal?
5. Verify the duality property of DTFT with rectangular pulse and sinc function.

DTFT equations in matrix form:


We can arrange DTFT equations in Matrix form.
Let,
𝑊 be the vector containing 𝑀 frequency points between [0, 2𝜋].
𝑥 be the data sequence having 𝑁 data points
𝑛 be the time index vector

We can calculate the DTFT (𝑋) using the following equation,

EEE-314-83 | P a g e
𝑋 = 𝑊𝑥 𝑇

here, 𝑇 means transpose of a matrix and 𝑊 is an 𝑀 × 𝑁 matrix defined as,

𝑒 −𝑗𝜔(0)𝑛(0) 𝑒 −𝑗𝜔(0)𝑛(1) ⋯ 𝑒 −𝑗𝜔(0)𝑛(𝑁−1)


𝑊=[ 𝑒 −𝑗𝜔(1)𝑛(0) 𝑒 −𝑗𝜔(1)𝑛(1) ⋯ 𝑒 −𝑗𝜔(1)𝑛(𝑁−1) ]
⋮ ⋮ ⋱ ⋮
𝑒 −𝑗𝜔(𝑀−1)𝑛(0) 𝑒 −𝑗𝜔(𝑀−1)𝑛(1)
⋯ 𝑒 −𝑗𝜔(𝑀−1)𝑛(𝑁−1)

The advantage of using the matrix form is that we don’t have to use for loops here. So, the
calculation will be faster.

Example 02:
Calculate the Fourier transform of the following signal,

𝑥(𝑛) = [1, 3 , −9, 5, 10]

Solution:

%MATRIX IMPLEMENTATION OF DTFT


x=[1, 3 , -9, 5, 10];
n1=-1; %DEFINING THE INDEX OF FIRST ELEMENT OF x
n2=3; %DEFINING THE INDEX OF THE LAST ELEMENT OF x
n=n1:n2; %INDEX OF x
M=500; %TOTAL NUMBER OF POINTS IN THE FREQUENCY RANGE
w=(-M/2:M/2)*2*pi/M;%FREQUENCY GRID
W=exp(-1j*w'*n); % Matrix formation
X=W*x';
figure(1);
subplot(211);
plot(w/(2*pi),abs(X));
xlabel('Digital frequency, f');
ylabel('|X(f)|');
title('Magnitude Spectrum');
subplot(212);
plot(w/(2*pi),angle(X)*180/pi);
xlabel('Digital frequency, f');
ylabel('angle(X(f))');
title('Phase Spectrum');

EEE-314-84 | P a g e
Output:

DTFT using built-in MATLAB function:


Built-in MATLAB function fft() and ifft() can be used to calculate the discrete-time Fourier
transform (actually, it is called discrete Fourier Transform or DFT). The fft() function takes the
time domain signal as input but you can additionally specify the number of uniformly sampled
frequency points in the range of [0, 2𝜋]. There is another function fftshift() which can shift the
range of frequency from [0, 2𝜋] to [−𝜋, 𝜋].
The following example will clarify how to use the above-mentioned functions to calculate the
Fourier Transform.

Example 03:
Calculate the Fourier transform of the signal in Example 02 using MATLAB built-in functions.

Solution:

% DTF using FFT algorithm


x = [1, 3 , -9, 5, 10];
M = 500;
w=(-M/2:M/2-1)*2*pi/M; %FREQUENCY GRID
X = fftshift(fft(x,M));
figure(1);
subplot(211);
plot(w/(2*pi),abs(X));
xlabel('Digital frequency, f');
ylabel('|X(f)|');
title('Magnitude Spectrum');
subplot(212);

EEE-314-85 | P a g e
plot(w/(2*pi),angle(X)*180/pi);
xlabel('Digital frequency, f');
ylabel('angle(X(f))');
title('Phase Spectrum');

This code will generate the same magnitude spectra as Example 02. The phase spectra might be a
little different but it would be due to the behavior of MATLAB function angle (). For example,
instead of an angle of 185° it could be −175°. Though it distorts the graph and makes it
discontinuous but the values are actually same.

Precautions:
1. Follow the software instructions and lab manual for using the software.
2. Be careful while input the variables value before run the code.
3. Always work in the editor environment. This will you to edit the Matlab Code.
4. Do not turn off your pc or laptop while simulating the design.
5. Check your code with the desired input data.
6. Never hurry. Hurry can cause error in simulation.
7. After simulation collect all the data and graphs carefully, take screen shot if necessary.

Data Analysis:

Report:

1) Submit the complete data sheet with the screenshots of Matlab code and Output
Graphs.
2) Using various variables value complete the data table, observe the output graphs and
write a report

Results and Discussion:

References
1) Li Tan, Purdue University, Jean Jiang, Purdue University “Digital Signal Processing:
Fundamentals and Applications’’.
2) Website: https://www.mathworks.com/products/matlab.html

Pre Lab Viva Sample Question:


1. Calculate the Fourier transform of the following signal,
𝑥(𝑛) = [1, 3 , −9, 5, 10]?

Post Lab Viva Sample Questions:


1. Calculate the Fourier transform of a 𝑠𝑖𝑛𝑐 2 pulse with a frequency of 1⁄8.The signal is
given as,

EEE-314-86 | P a g e
2 (2𝜋𝑓𝑛)
sin(2𝜋𝑓𝑛) 2
𝑥(𝑛) = 𝑠𝑖𝑛𝑐 =( )
2𝜋𝑓𝑛

where, 𝑓 = 1⁄8. Plot the magnitude spectra and reconstruct the signal back to the time domain.

1) Do the following for the signal in Example 01:


2) Increase the range of the frequency grid to [0, 2𝜋]. Observe the magnitude spectra and
explain the effects.
3) Change the range of the frequency range to [−2𝜋, 2𝜋]. Observe the magnitude spectra and
explain the effects.
4) Verify the effect of changing index (𝑛) to -40:120 and -40:80. In each case, observe the
reconstructed signal and explain the result.
5) If you reconstruct the signal in n_re=-240:240, why do you get a repetition of the aperiodic
signal?
6) Verify the duality property of DTFT with rectangular pulse and sinc function.

EEE-314-87 | P a g e
Experiment No. 10:
Name of the Experiment: Frequency domain analysis of discrete-time systems

Objective:
The objectives of this experiment are to:
2. Learn to analyze discrete-time systems in frequency domain.
3. Compute the magnitude and phase response of systems and interpret them.
4. Apply filters or systems to some real world audio signals and observe the response.

Course Outcomes (COs), Program Outcomes (POs) and Assessment:

Domain /
Delivery
Corresponding level of Assessment
CO Statement methods and
PO learning tools
activities
taxonomy
Lab tests
CO2:Analyze Simulation Lab reports
discrete time Investigation(P
systems using O4) Experiment Final lab
Affective test
signal processing Modern Tool Practice lab
domain/
and symbolic Usage: (PO5) Open ended
valuing level Group
toolboxes of lab
MATLAB. discussion
Project
Tutorial
show & project
presentation

Theory:

Calculating frequency response from transfer functions:


The Signal Processing Toolbox contains many useful functions for computing and displaying the
frequency response of discrete-time systems. The most widely used is the freqz() function. Given
the transfer function of a system in the following form:

𝑏0 + 𝑏1 𝑧 −1 + 𝑏2 𝑧 −2 + ⋯ + 𝑏𝑚 𝑧 −𝑚 𝐵(𝑧)
𝐻(𝑧) = =
𝑎0 + 𝑎1 𝑧 −1 + 𝑎2 𝑧 −2 + ⋯ + 𝑎𝑛 𝑧 −𝑛 𝐴(𝑧)

the freqz() function uses an FFT-based approach to compute the frequency response. The function
has a variety of formats. A useful format is,

[h,f] = freqz(B, A, npt, Fs)

where the variables B and A are the vectors of the numerator and denominator polynomials. Fs is
the sampling frequency and npt the number of frequency points between 0 and Fs/2. In the

EEE-314-88 | P a g e
MATLAB Toolbox, the Nyquist frequency (i.e. Fs/2) is the unit of normalized frequency, Using
the freqz() command without output arguments plots the magnitude and phase responses
automatically.

Experimental simulation procedure:

Example 01:A discrete-time system is characterized by the following transfer function:

1 + 1.16180𝑧 −1 + 𝑧 −2
𝐻(𝑧) =
1 − 1.5161𝑧 −1 + 0.878𝑧 −2

Plot the frequency response of the system using MATLAB. Assume a sampling frequency of 500
Hz and a resolution of <1 Hz for the frequency response.

Solution:
Here, sampling frequency, Fs is 500 Hz. So, we will have to choose few points between 0 and 250
Hz. The more point we choose the sharper the plot will be. As the resolution should be less than 1
Hz we can choose any number of points more than 250.

B = [1 -1.6180 1];
A = [1 -1.5161 0.878];
Fs = 500;
npt = 256;
freqz(B,A,npt,Fs); % compute and plot the frequency response

Fig. 1: Frequency response of the system in Example 01.

EEE-314-89 | P a g e
Calculating response for a given input signal:
Given the transfer function of a system in the following form:

𝑏0 + 𝑏1 𝑧 −1 + 𝑏2 𝑧 −2 + ⋯ + 𝑏𝑚 𝑧 −𝑚 𝐵(𝑧)
𝐻(𝑧) = =
𝑎0 + 𝑎1 𝑧 −1 + 𝑎2 𝑧 −2 + ⋯ + 𝑎𝑛 𝑧 −𝑛 𝐴(𝑧)

and a time domain input signal 𝑥(𝑛), we can calculate the response of the system using MATLAB
built-in function filter().

The syntax for filter() function is,

y = filter(B, A, x)

Example 02: For the system in Example 01, if the sampling rate is 500 Hz, plot the response of
the system for the following signals,

1. 𝑥(𝑡) = 5cos(100𝜋𝑡)
2. 𝑥(𝑡) = 5cos(200𝜋𝑡)

Solution:

Fs = 500;
B = [1 -1.6180 1]; % System coefficients B
A = [1 -1.5161 0.878]; % System coefficients A

% Signal generation
F1 = 50; % Frequency of the first signal
F2 = 100; % Frequency of the second signal
t = 0:(1/Fs):0.2; % 0s to 200ms
x1 = 5*cos(2*pi*F1*t);
x2 = 5*cos(2*pi*F2*t);

% Plotting inputs
subplot(221)
plot(t,x1);
xlabel('time (s)');
ylabel('Amplitude');
title('First signal, F = 50 Hz');
subplot(222)
plot(t,x2);
xlabel('time (s)');
ylabel('Amplitude');
title('Second signal, F = 100 Hz');

% Applying the filter

EEE-314-90 | P a g e
y1 = filter(B,A,x1);
y2 = filter(B,A,x2);
% End of applying filter. Easy, isn't it?

% Plotting outputs
subplot(223)
plot(t,y1);
xlabel('time (s)');
ylabel('Amplitude');
title('Output for first (50 Hz) signal');
subplot(224)
plot(t,y2);
xlabel('time (s)');
ylabel('Amplitude');
title('Output for second (100 Hz) signal');

Fig. 2: Output of Example 02.

Observe how the 50 Hz signal is rejected by the filter. Its amplitude has rapidly declined to zero.
From the output of Example 01 (Fig. 01), we can see that it was bound to happen as at 50 Hz the
magnitude response is zero. On the other hand, the magnitude of the 100 Hz signal is nearly
unchanged which can also be explained from Fig. 01.

Practice Problem 01:A discrete-time system is characterized by the following transfer function:
𝑁1 (𝑧)𝑁2 (𝑧)𝑁3 (𝑧)
𝑋(𝑧) =
𝐷1 (𝑧)𝐷2 (𝑧)𝐷3 (𝑧)
where,
𝑁1 (𝑧) = 1 − 1.22346𝑧 −1 + 𝑧 −2

EEE-314-91 | P a g e
𝑁2 (𝑧) = 1 − 0.437833𝑧 −1 + 𝑧 −2
𝑁3 (𝑧) = 1 + 𝑧 −1
𝐷1 (𝑧) = 1 − 1.4334509𝑧 −1 + 0.85811𝑧 −2
𝐷2 (𝑧) = 1 − 1.293601𝑧 −1 + 0.556929𝑧 −2
𝐷3 (𝑧) = 1 – 0.612159𝑧 −1

Plot the frequency response of the system using MATLAB. Assume a sampling frequency of 10
kHz and a resolution of < 10 Hz for the frequency response.

Loading audio files to the MATLAB environment:


There are some built-in MATLAB audio files. You can load them to the MATLAB environment
using function load (),given the name of the sample file. This function will return a MATLAB
structure containing both the audio sequence and the sampling frequency that was used to record
the sequence. Actually, this function is not exclusive for audio files. You can use this to load raw
variable data from saved files to the MATLAB environment.

Code for loading audio files using the load() function is,

data = load('gong'); %'gong'is the sample file name


audio = data.y;% audio sequence will stored in “audio”
Fs = data.Fs;% Sampling frequency

Some of vaild sampl names: gong, handle, laughter, train. You can also play the ausio using the
sound() function.

For any other audio files (.wav, .mp3, .flac, .ogg, etc) that isn’t built into MATLAByou can use
the audioread() function. The syntax is given below,

[y,Fs] = audioread(filename);

Example 03:Do the following tasks step by step:


Part 1:
1. Load the ‘gong’ sample audio file.
2. Calculate the magnitude response of the audio signal.
3. Plot the time domain signal and the magnitude spectra using proper x-axis values.
Part 2:
1. Load the coefficients of a low pass filter from the file ‘coeff. mat’ provided to you.
2. Apply the filter to the input audio signal and determine the output.
3. Calculate the magnitude response of the output signal.
4. Plot the time domain output signal and its magnitude spectra using proper x-axis values.
5. Observe the effect of the filter.

Solution:

Code Part 1: Loading the input and calculating the magnitude spectra:

EEE-314-92 | P a g e
data = load('gong'); % Load audio
audio = data.y;
L = length(audio); % Length of audio
Fs = data.Fs;
t = (0:L-1)*(1/Fs); % Generate time sequence
N = 1025; % Define number of points for DFT
F = (-(N-1)/2:(N-1)/2)*Fs/(N-1);% Generate frequency sequence
H = fft(audio,N); % Calculate DFT using FFT algorithm
H = fftshift(H); % Shift frequency range

% Plot the time domain signal


subplot(211)
plot(t,audio);
axis([t(1) t(end) -Inf Inf]);
xlabel('Time in sec');
ylabel('Amplitude');
title('Audio signal');
% Plot the magnitude spectra
subplot(212)
plot(F,abs(H/L));
axis([F(1) F(end) -Inf Inf]);
xlabel('Frequency in Hz');
ylabel('Magnitude');
title('Magnitude spectra');

Output of Part 1:

Fig. 3: Output of Example 03 Part 1.

EEE-314-93 | P a g e
Code Part 2: Calculating the output of the filter and its magnitude spectra:

data = load('coeff'); % Load coeff.mat file


B = data.B;% define coefficient B
A = data.A; % define coefficient A
y = filter(B,A,audio);% Calculate output
H = fft(y,N); % Calculate DFT using FFT algorithm
H = fftshift(H); % Shift frequency range

% Plot the time domain output signal


subplot(211)
plot(t,y);
axis([t(1) t(end) -Inf Inf]);
xlabel('Time in sec');
ylabel('Amplitude');
title('Audio signal');
% Plot the magnitude spectra of the output
subplot(212)
plot(F,abs(H/L));
axis([F(1) F(end) -Inf Inf]);
xlabel('Frequency in Hz');
ylabel('Magnitude');
title('Magnitude spectra');
title('Magnitude spectra');

Output of Part 2:

Fig. 4: Output of Example 03 Part 2.

EEE-314-94 | P a g e
Observe how the filter removed all the frequency components beyond 1000 Hz. Can you explain
this result from the frequency response of the filter?

Precautions:
1) Follow the software instructions and lab manual for using the software.
2) Be careful while input the variables value before run the code.
3) Always work in the editor environment. This will you to edit the Matlab Code.
4) Do not turn off your pc or laptop while simulating the design.
5) Check your code with the desired input data.
6) Never hurry. Hurry can cause error in simulation.
7) After simulation collect all the data and graphs carefully, take screen shot if necessary.

Data Analysis:

Report:

1) Submit the complete data sheet with the screenshots of Matlab code and Output
Graphs.
2) Using various variables value complete the data table, observe the output graphs and
write a report

Results and Discussion:

References
1. Li Tan, Purdue University, Jean Jiang, Purdue University “Digital Signal Processing:
Fundamentals and Applications’’.
2. Website: https://www.mathworks.com/products/matlab.html

Pre Lab Viva Sample Question:


1. A discrete-time system is characterized by the following transfer function:

1 + 1.16180𝑧 −1 + 𝑧 −2
𝐻(𝑧) =
1 − 1.5161𝑧 −1 + 0.878𝑧 −2

Plot the frequency response of the system using MATLAB. Assume a sampling frequency
of 500 Hz and a resolution of <1 Hz for the frequency response.?

Post Lab Viva Sample Questions:


1. Plot the frequency response of the filter used in Example 03. What is the cutoff frequency
of the filter? Do the same tasks in Example 03 for the audio samples ‘train’ and ‘laughter’.

2. The coefficients of a FIR filter are stored in the file ‘coeff2.mat’. Plot the frequency

EEE-314-95 | P a g e
response this filter. What type of filter is it? What is the cutoff frequency of the filter? Do
the same tasks in Example 03 for the audio samples ‘train’ and ‘handle’ using the provided
filter.

EEE-314-96 | P a g e
Experiment No. 11:
Name of the Experiment: Design a LP FIR filter in MATLAB
Objective:
The objectives of this experiment are to:
1) Learn to analyze a LP FIR filter.
2) To implement LP FIR filter for a given sequence.
3) Apply filters or systems to some real world and observe the response.

Course Outcomes (COs), Program Outcomes (POs) and Assessment:

Domain /
Delivery
Corresponding level of Assessment
CO Statement methods and
PO learning tools
activities
taxonomy
Lab tests
CO2:Analyze Lab
discrete time Simulation
Investigation( reports
systems using Affective Experiment
PO4) Final lab
signal
ModernTool domain/ Practice lab test
processing and
Usage: (PO5) valuing
symbolic Group Open
level
toolboxes of discussion ended lab
MATLAB.
Tutorial Project
show & project
presentation

Theory:

FIR filters are digital filters with finite impulse response. They are also known asnon-recursive
digital filters as they do not have the feedback. An FIR filter has two important advantages over
an IIR design: Firstly, there is no feedback loop in the structure of an FIR filter. Due to not having
a feedback loop, an FIR filter is inherently stable. Meanwhile, for an IIR filter, we need to check
the stability. Secondly, an FIR filter can provide a linear-phase response. As a matter of fact, a
linear-phase response is the main advantage of an FIR filter over an IIR design otherwise, for the
same filtering specifications; an IIR filter will lead to a lower order. FIR FILTER DESIGN An
FIR filter is designed by finding the coefficients and filter order that meet certain specifications,
which can be in the time-domain (e.g. a matched filter) and/or the frequency domain (most
common). Matched filters perform a cross-correlation between the input signal and a known pulse-
shape. The FIR convolution is a cross-correlation between the input signal and a time-reversed
copy of the impulse-response. Therefore, the matched-filter’s impulse response is “designed” by
sampling the known pulse-shape and using those samples in reverse order as the coefficients of
the filter.

EEE-314-97 | P a g e
Output Graph:

EEE-314-98 | P a g e
Required Devices & Software:
3. A well configured Laptop or PC.
4. Software: Latest Matlab version. (2014-2020).

Precautions:
1) Follow the software instructions and lab manual for using the software.
2) Be careful while input the variables value before run the code.
3) Always work in the editor environment. This will you to edit the Matlab Code.
4) Do not turn off your pc or laptop while simulating the design.
5) Check your code with the desired input data.

EEE-314-99 | P a g e
6) Never hurry. Hurry can cause error in simulation.
7) After simulation collect all the data and graphs carefully, take screen shot if necessary.
Data Analysis:

Report:

3. Submit the complete data sheet with the screenshots of Matlab code and Output
Graphs.
4. Using various variables value complete the data table, observe the output graphs and
write a report

Results and Discussion:

References
3) Li Tan, Purdue University,Jean Jiang, Purdue University “Digital Signal Processing:
Fundamentals and Applications’’.
4) Website: https://www.mathworks.com/products/matlab.html

Pre Lab Viva Sample Question:


2) Difference between FIR low pass filter and high pass filter?

Post Lab Viva Sample Questions:


3) Differentiate ideal filter and practical filter responses.
4) What is the filter specifications required to design the analog filters??

EEE-314-100 | P a g e

You might also like