You are on page 1of 94

Sri AdichunchanagiriShikshana Trust (R)

SJB Institute of Technology


(Affiliated to Visvesvaraya Technological University, Belagavi& Approved by AICTE, New Delhi.)

Department of Electrical & Electronics Engineering

VI SEMESTER

DIGITAL SIGNAL PROCESSING LABORATORY (18EEL67)

NAME: .................................

USN: ...................................

SEM: ....................................

BATCH: ...............................

Prepared by:

Dr. Sridhar J P Mr. Dwarakanath S K


Associate Professor Assistant Professor
Dept. of EEE, SJBIT Dept. of EEE, SJBIT
Institution

To provide learning opportunities that fosters


To become a recognized students ethical values, intelligent development in
technical education center with science & technology and social responsibility so
global perspective. that they become sensible and contributing members
of the society.

Department

To become one among the best departments in M1: To provide learner-centric environment through
engineering and research arena through quality education and training
professional faculty and state of art laboratories M2: To lay the foundation for research by fortifying
and to make the students successful engineers peers & establishing incubation center.
with good ethics. M3: To develop the overall personality of the students to
face the challenges of the real world.

Program Educational Objectives (PEO)


Graduates be able to apply their knowledge of engineering to analyze, design,
PEO1 evaluate and solve complex problems using fundamental principles in combination
with modern engineering tools and methods.
Graduates be able to productively function as members of multidisciplinary team
PEO2 and lead to demonstrate sustained learning for constantly changing field through
graduate work, professional development and self-study.
Graduates, to demonstrate collaborative, entrepreneurial and leadership qualities to
PEO3
address socio-economic and environmental concerns.

Program Specific Outcome (PSO)


The Electrical and Electronics Engineering program must demonstrate that
PSO1 graduates can design and analyze systems that efficiently generate, transmit &
distribute, measurement, protection and utilization of electric power.
The Electrical and Electronics Engineering program must demonstrate that
graduates can understand the Operation of Electrical machines, Drives and their
PSO2
Control using Electronic Circuits and applying to the ever evolving societal and
Industrial needs.
The Electrical and Electronics Engineering program must demonstrate design,
PSO3 analyze, implement and test the analog & digital circuits, signal processing,
microcontrollers & computer programming.
PROGRAM OUTCOMES (POs)

Engineering Graduates will be able to:


1.Engineering knowledge: Apply the knowledge of mathematics, science, engineering fundamentals,
and an engineering specialization to the solution of complex engineering problems.
2.Problem analysis: Identify, formulate, review research literature, and analyze complex engineering
problems reaching substantiated conclusions using first principles of mathematics, natural sciences,
and engineering sciences.
3.Design/development of solutions: Design solutions for complex engineering problems and design
system components or processes that meet the specified needs with appropriate consideration for the public
health and safety, and the cultural, societal, and environmental considerations.
4.Conduct investigations of complex problems: Use research-based knowledge and research methods
including design of experiments, analysis and interpretation of data, and synthesis of the information to
provide valid conclusions.
5.Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modeling to complex engineering activities with an
understanding of the limitations.
6.The engineer and society: Apply reasoning informed by the contextual knowledge to assess societal,
health, safety, legal and cultural issues and the consequent responsibilities relevant to the professional
engineering practice.
7.Environment and sustainability: Understand the impact of the professional engineering solutions in
societal and environmental contexts, and demonstrate the knowledge of, and need for sustainable
development.
8.Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms of the
engineering practice.
9.Individual and team work: Function effectively as an individual, and as a member or leader in-
diverse teams, and in multidisciplinary settings. 10.Communication: Communicate effectively on
complex engineering activities with the engineering community and with society at large, such as,
being able to comprehend and write effective reports and design documentation, make effective
presentations, and give and receive clear instructions.
11.Project management and finance: Demonstrate knowledge and understanding of the engineering
and management principles and apply these to one’s own work, as a member and leader in a team, to manage
projects and in multidisciplinary environments.
12.Life-long learning: Recognize the need for, and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change.
RUBRICS

Evaluation Of Laboratory Activities

SL
Criteria Marks Allocation Marks
No.
1 Conduction/Execution of Experiment(C) (30% of Total Max..Mks) 9
2 Analysis & Interpretation of Results(A) (30% of Total Max..Mks) 9
3 Report writing & Documentation ( R ) (20% of Total Max..Mks) 6
4 Viva (V) (20% of Total Max..Mks) 6

Weightage : Excellent = 80-100%, Satisfactory = 40-60%,

Unsatisfactory = <40% Total Maximum Marks: As Prescribed by the University

SL
Criteria Marks Allocation Marks
No.
1 Procedure write-up on given Experiment (20% of Total Max..Mks) 2
2 Conduction / Execution of Experiment (30% of Total Max. Mks) 3
3 Analysis & Interpretation of Results (30% of Total Max. Mks) 3
4 Viva (20% of Total Max. Mks) 2

Evaluation Of Internal Assessment

Weightage : Excellent = 80-100%, Satisfactory = 40-60%,

Unsatisfactory = <40% Total Maximum Marks: As Prescribed by the University


B.E ELECTRICAL AND ELECTRONICS ENGINEERING (EEE)
CHOICE BASED CREDIT SYSTEM (CBCS) SEMESTER - VI
DIGITAL SIGNAL PROCESSING LABORATORY
Subject Code 18EEL67 CIE Marks 40
Number of Practical
0:2:2 SEE Marks 60
Hours/Week(L/T/P)
Credits 02 Exam Hours 03
Course objectives:
➢ To explain the use of MATLAB software in evaluating the DFT and IDFT of given
sequence
➢ To verify the convolution property of the DFT
➢ To design and implementation of IIR and FIR filters for given frequency specifications.
➢ To realize IIR and FIR filters.
➢ To help the students in developing software skills
Course outcomes: At the end of the course the student will be able to:
• Utilize MATLAB platform to perform interpretation of sampling theorem in time and
frequency domains.
• Perform the impulse response, Linear and circular convolution of given sequences.
• Compute DFT and IDFT of a given sequence using the basic definition and also using
FFT algorithms.
• Design and implementation of IIR and FIR filters for the given specifications.
Graduate Attributes (As per NBA)
Engineering Knowledge, Problem Analysis, Individual and Team work, Communication
Conduct of Practical Examination:
1. All laboratory experiments are to be included for practical examination.
2. Breakup of marks and the instructions printed on the cover page of answer script to be strictly
adhered by the examiners.
3. Students can pick one experiment from the questions lot prepared by the examiners.
4. Change of experiment is allowed only once and 15% Marks allotted to the procedure part to
be made zero.
CO-PO-PSO Mapping Details

PO 1 2 3 4 5 6 7 8 9 10 11 12
CO1 3 2 3 2 2 1
CO2 3 2 3 2 2 1
CO3 3 2 3 2 2 1
CO4 3 2 3 2 2 1

PSO's Mapping
CO-NO
1 2 3 4
CO1 3
CO2 3
CO3 3
CO4 3
Table of Contents
Sl. No. Particulars Page No.
1 Introduction to MATLAB 1
2 Sampling Theorem 13
3 Impulse Response 17
4 Linear Convolution 21
5 Circular convolution 25
6 N point DFT 32
7 Linear and circular convolution 36
8 Difference equation 41
9 Implement FFT for given sequence 47
10 Design & implementation of IIR filter Design. 50
11 Design & implementation of FIR filter using window function 60

12 Design & implementation of FIR filter using frequency sampling method 68

13 Design & implementation of IIR filter 71

14 Viva Questions and answers 75

15 Annexure – Additional Experiments 77


DSP Laboratory - 18EEL67 2020-21
Introduction to MATLAB
MATLAB stands for MATrix LABoratory. It is a technical computing environment for high
performance numeric computation and visualization which has application in almost all the areas.
MATLAB is one of the few languages in which each variable is a matrix. Moreover, the
fundamental operators (e.g. addition, multiplication) are programmed to deal with matrices when
required.
In the MATLAB environment, a matrix is a rectangular array of numbers. Unlike languages such as
C and C++, there is no much syntax in the MATLAB. Anyone having knowledge of simple
programming language can work with the MATLAB. So, learning MATLAB programming is made
easier using certain applications.
MATLAB is very rich with toolboxes, which consist of various functions depending on the
applications. These functions can be used as a command to perform particular operation.
MATLAB has a very good help support for all it’s built-in functions and programming language
constructs.
MATLAB has a demonstration program that shows many of its features. The program includes a
tutorial introduction. Type demo at the matlab prompt to invoke the demonstration program and
follow the instruction on the screen.

How to Start MATLAB

You can find a MATLAB icon on the desktop or from the start menu. By double clicking the
icon you can invoke the MATLAB command window.

MATLAB works through three basic windows,

1. COMMAND window
2. GRAPHICS or FIGURE window
3. EDITOR window

Department of EEE, SJBIT Page 1


DSP Laboratory - 18EEL67 2020-21
1. COMMAND WINDOW:
• This is the main window. It is characterized by the MATLAB command prompt ‘>>’.
• When you launch an application program, MATLAB puts you in this window. All commands
including those for running user written programs are typed in this window at the MATLAB
prompt.

2. GRAPHICS or FIGURE WINDOW:

• The output of all the graphics commands typed in the command window are flushed to the
graphics or figure window.
• The user can create as many
• figure windows as the system memory will allow.

Department of EEE, SJBIT Page 2


DSP Laboratory - 18EEL67 2020-21

3. EDITOR WINDOW:
• This is where you write, edit, create, and save your own programs in files called ‘m-Files’.
• MATLAB provides its own built in editor.

Department of EEE, SJBIT Page 3


DSP Laboratory - 18EEL67 2020-21

MATLAB FILE TYPES

MATLAB has three types of files for storing information:

1. M-files.
2. MAT-files.
3. MEX-files.

1. M-FILES:
They are standard ASCII text files with an .m extension to the file name. There are two types of m-files
namely script file and function file.

2. MAT-FILES:
These are binary data files, with a .mat extension to the file name. MAT files are created by MATLAB
when you save the data with the save command. The data is written in a special format that only
MATLAB can read.

3. MEX_FILES:
These are MATLAB callable FORTRAN and c programs with an .mex extension to the filename.

Department of EEE, SJBIT Page 4


DSP Laboratory - 18EEL67 2020-21
Basic Instructions in Matlab.
1. T = 0: 1:10
This instruction indicates a vector T which as initial value 0 and final value 10 with an increment of 1
Therefore T = [0 1 2 3 4 5 6 7 8 9 10]
2. F= 20: 1: 100
Therefore F = [20 21 22 23 24 ……… 100]
3. T= 0:1/pi: 1
Therefore T= [0, 0.3183, 0.6366, 0.9549]
4. zeros (1, 3)
The above instruction creates a vector of one row and three columns whose values are zero
Output= [0 0 0]
5. zeros( 2,4)
Output = 0 0 0 0
0000
6. ones (5,2)
The above instruction creates a vector of five rows and two columns.
Output = 1 1
11
11
11
11
7. a = [ 1 2 3]
b = [4 5 6]
a.*b = [4 10 18]
Which is multiplication of individual elements?
i.e. [4X1 5X2 6X3]
8 if C= [2 2 2]
b.*C results in [8 10 12]
9. plot (t, x)
If x = [6 7 8 9]
t = [1 2 3 4]
This instruction will display a figure window which indicates the plot of x versus t

Department of EEE, SJBIT Page 5


DSP Laboratory - 18EEL67 2020-21

10. stem (t,x):- This instruction will display a figure in discrete form.

11. Subplot: This function divides the figure window into rows and columns Subplot (2 2 1) divides the
figure window into 2 rows and 2 columns 1 represent number of the figure.

1 2
(2 2 1) (2 2 2)

3 4
(2 2 3) (2 2 4)
Subplot (3,1,3)

1 (3,1,1)

2 (3,1,2)

3(3,1,3)
12 Filter

Syntax: y = filter(b,a,X)

Description: y = filter(b,a,X) filters the data in vector X with the filter described by numerator
coefficient vector b and denominator coefficient vector a. If a(1) is not equal to 1, filter normalizes the
filter coefficients by a(1). If a(1) equals 0, filter returns an error.

13. Impz

Syntax: [h,t] = impz(b,a,n)

Description: [h,t] = impz(b,a,n) computes the impulse response of the filter with numerator coefficients
b and denominator coefficients a and computes n samples of the impulse response when n is an integer
(t = [0:n-1]'). If n is a vector of integers, impz computes the impulse response at those integer locations,
starting the response computation from 0 (and t = n or t = [0 n]).If, instead of n, you include the empty
vector for the second argument, the number of samples is computed automatically by default.

Department of EEE, SJBIT Page 6


DSP Laboratory - 18EEL67 2020-21
14. Conv

Syntax: w = conv(u,v)

Description: w = conv(u,v) convolves vectors u and v. Algebraically, convolution is the same operation
as multiplying the polynomials whose coefficients are the elements of u and v.

15.Disp

Syntax: disp(X)
Description: disp(X) displays an array, without printing the array name. If X contains a text string, the
string is displayed. Another way to display an array on the screen is to type its name, but this prints a
leading "X=," which is not always desirable. Note that disp does not display empty arrays.
16.xlabel

Syntax: xlabel('string')

Description: xlabel('string') labels the x-axis of the current axes.

17. ylabel

Syntax : ylabel('string')

Description: ylabel('string') labels the y-axis of the current axes.

18.Title

Syntax : title('string')

Description: title('string') outputs the string at the top and in the center of the current axes.

19. grid on

Syntax : grid on

Description: grid on adds major grid lines to the current axes.

Department of EEE, SJBIT Page 7


DSP Laboratory - 18EEL67 2020-21
Prerequisite experiments (Not appeared in SEE study purpose only)
Generation of different signals in MATLAB
1. Generation of sine wave
Program Waveform

clc;
clear all;
close all;
fs = 2000;
f = 50;
t = 0:1/fs:(1/f);
x = sin(2*pi*f*t);
plot(t,x);
title('sine wave');
xlabel('time in sec');
ylabel('amplitude');

Sample Explanation
clc; % clears the command window
clear all; % clears all variables
close all; % existing figure windows will be closed
fs = 2000; % sampling frequency
f = 100; % signal frequency
t = 0:1/fs:(2/f); % generation of the time sequence for sine wave
x = sin(2*pi*f*t); % sine wave generation using sine function
plot(t,x); % the sine wave is displayed in a separate plot window
title('sine function')

Department of EEE, SJBIT Page 8


DSP Laboratory - 18EEL67 2020-21
2. Operation on discrete time step signal
Program Waveform
clc;
clear all;
close all;
n = -10:20; % Define time vector
u = [(n)>=0]; % Unit Step
u5 = [(n-10)>=0]; % Delayed step
subplot(211), stem(n,u), title('Unit-
step Function');
xlabel('Number of samples');
ylabel('amplitude');
subplot(212), stem(n,u5),
title('Delayed Unit-step Function');
xlabel('Number of samples');
ylabel('amplitude');
3. Generation of unit step function
Program Waveform
clc;
clear all;
close all;
t = -5:0.01:10;
x1=1;
x2=0;
x=x1.*(t>=0)+x2.*(t<0)
plot(t,x);
title('Continuous time Unit-
step signal');
xlabel('time in sec');
ylabel('amplitude');

Department of EEE, SJBIT Page 9


DSP Laboratory - 18EEL67 2020-21
4. Generation of multiple sine wave plots in single window

Program Waveform

clc;
clear all;
close all;
fs = 2000;
f = 50;
t = 0:1/fs:(1/f);
x = sin(2*pi*f*t);
subplot(1,3,1)
plot(t,x);
title('sine function')
xlabel('time');
ylabel('magnitude');
y1= 2*x;
subplot(1,3,2);
plot(t,y1);
title('2x(t)')
xlabel('time');
ylabel('magnitude');
y2= 0.5*x;
subplot(1,3,3);
plot(t,y2);
title('x(t)/2')
xlabel('time');
ylabel('magnitude');

Department of EEE, SJBIT Page 10


DSP Laboratory - 18EEL67 2020-21
5. Generation of unit step function
Program Waveform
clc;
clear all;
close all;
t = -5:0.01:10;
x1=1;
x2=0;
x=x1.*(t>=0)+x2.*(t<0)
plot(t,x);
title('Continuous time Unit-step
signal');
xlabel('time in sec');
ylabel('amplitude');

6. Generation of unit triangle function


Program Waveform
clc;
clear all;
close all;
t = -5:0.01:5;
a=3;
x1= 1-abs(t)/a;
x2=0;
x=x1.*(abs(t)<=a)+x2.*(abs(t)>a);
plot(t,x);
title('triangle signal');
xlabel('time in sec');
ylabel('amplitude');

Department of EEE, SJBIT Page 11


DSP Laboratory - 18EEL67 2020-21
7. Generation of parabolic function
clc;
clear all;
close all;
t = -5:0.01:10;
a=0.4;
x1=(a*(t.^2))/2;
x2=0;
x=x1.*(t>=0)+x2.*(t<0)
%x=x1.*(t>=1 & t<=50);
plot(t,x);
title('parabolic signal');
xlabel('time in sec');
ylabel('amplitude');

8. A mixed frequency sine signal having the frequencies 10, 30 and 60Hz.

• clc; Signal with frequencies 10, 30 and 60Hz


• clear all; 2.5

• close all; 2
• t = 0:.001:0.5;
1.5
• x=sin(2*pi*t*10)+sin(2*pi*t*30)
+sin(2*pi*t*60); 1

• plot(t,x); 0.5

Amplitude

title('Signal with frequencies 10,


30 and 60Hz'); 0

• xlabel('Time'), -0.5
ylabel('Amplitude');
-1

-1.5

-2

-2.5
0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5
Time

Department of EEE, SJBIT Page 12


DSP Laboratory - 18EEL67 2020-21
EXPERIMENT 01
SAMPLING THEOREM
AIM: Verification of sampling theorem
THEORY: After completing this experiment, student will be able to verify sampling theorem the
sampling theorem using MATLAB.
Sampling theorm statement:
A continuous time signal can be represented in its samples and can be recovered back when sampling
frequency fs is greater than or equal to the twice the highest frequency component of message signal. i.e.
Fs ≥ 2fm.
THEORY:
Signal can be of continuous time or discrete time. A continuous signal is one which is defined
for all values of time where as a discrete time signal is one which is defined over a discrete set of points
in a time. Most of the signals that we encounter in practice are continuous time (analog) signals. We can
process the signal in the analog domain or digital domain. For processing the signals digitally we need
to transform analog signal into digital signal.
The process of converting a continuous time signal into a discrete time signal is called Sampling. After
sampling, the signal is defined at discrete instants of time and the time interval between two successive
sampling instants is called sampling period of sampling interval.

Sampling: Is the process of converting a continuous time signal into a discrete time signal. It is the first
step in conversion from analog signal to digital signal.

The sampling operation can be represented by a switch as shown in Fig. 1. The switch is closed
for a very short interval of time τ (ideally, τ = 0), once every T seconds during which the signal is
available at the output. For the input x(t), the output xs(t) = x(nT), n = 0,±1, ±2, ±3, …… and x(nT) is a
sampled sequence of x(t), where T is called the sampling period or sampling interval. It is the time
interval between successive samples and the sampling frequency is given by fs = (1/T) Hz. The switch
used for sampling purpose will be an electronic switch.

x(t) x(n)

x(t) xs(t) = x(nT)


t nT
fs = 1/T= x(n)

NYQUIST RATE OF SAMPLING: Nyquist rate of sampling is the theoretical minimum sampling
rate at which a signal can be sampled and still be reconstructed from its samples without any distortion.

Department of EEE, SJBIT Page 13


DSP Laboratory - 18EEL67 2020-21
It is the theoretical minimum because when the Nyquist rate of sampling is used, only an ideal LPF can
be used to extract X(ω) from Xs(ω), i.e. to recover x(t) from xs(t).

Nyquist rate = 2 fm, where fm is the maximum frequency present in the signal. A signal sampled at
greater than Nyquist rate is said to be over sampled and a signal sampled at less than its Nyquist rate is
said to be under sampled.

Sampling is a process of converting a continuous time signal (analog signal) x(t) into a discrete
time signal x[n], which is represented as a sequence of numbers. (A/D converter)

Converting back x[n] into analog is the process of reconstruction. (D/A converter)

Example 1: Determine the Nyquist rate for the analog signal x(t) is sampled using a sampling rate
of 300Hz. x(t) = 3 cos 100πt − 6 sin 400πt + 4 cos 60πt.

Frequencies present in x(t) are f1 = 50 Hz; f2 = 200 Hz and f3 = 30 Hz

Department of EEE, SJBIT Page 14


DSP Laboratory - 18EEL67 2020-21
(Compare cos 100πt to cos ωt to get ω = 2πf1 = 100π i.e., f1 = 100π/2π = 50 Hz)
Maximum freq component fmax = f2 = 200 Hz.
According to sampling theorem;
Nyquist rate is fN = 2fmax = 400 Hz
Program code:

tfinal=0.05;
t=0:0.00005:tfinal; % define time vector .05/.00005=1000 sample pts
fd=input('Enter desired analog frequency'); %200 Hz
%Generate analog signal
xt=sin(2*pi*fd*t);
%simulate condition for under sampling i.e., fs1<2*fd
fs1=1.3*fd;
%define the time vector for under sampling
n1=0:1/fs1:tfinal; % define time vector .05/.00384=13 sample pts
xn=sin(2*pi*n1*fd); %Signal reconstruction
%plot the analog & reconstructed signals
subplot(3,1,1);
plot(t,xt,'b',n1,xn,'r*-');
title('undersampling plot');
%condition for Nyquist sampling fs2=2*fd
fs2=2*fd;
n2=0:1/fs2:tfinal; %define the time vector 05/.00384=13 sample pts
xn=sin(2*pi*fd*n2); %Signal reconstruction
%plot the analog & reconstructed signals
subplot(3,1,2);
plot(t,xt,'b',n2,xn,'r*-');
title('Nyquist plot');
%condition for oversampling
fs3=5*fd;
n3=0:1/fs3:tfinal; %define the time vector 05/.001=50 sample pts
xn=sin(2*pi*fd*n3); %Signal reconstruction
%plot the analog & reconstructed signals
subplot(3,1,3);
plot(t,xt,'b',n3,xn,'r*-');
title('Oversampling plot');
xlabel('time');
ylabel('amplitude');
legend('analog','discrete')

Department of EEE, SJBIT Page 15


DSP Laboratory - 18EEL67 2020-21
OUTPUTS:

Output:
A/D converter operates at a specified sampling rate, termed as fs .
This fs determines the upper limit of the input signal frequency, fmax.
From Nyquist sampling theorem the requirement of sampling frequency is given by fs ≥ 2fmax
where fs = sampling frequency, fmax = Highest frequency component of analog signal.
Say if fs = 20 kHz, then fmax should be ≤10 KHz.
If the input signal has frequency components greater than fmax, then these components are converted
into low-frequency ones, resulting in a noise called aliasing noise.
The signal in Example 1 is sampled using a sampling rate of 300Hz.
After sampling the signal is reconstructed from the samples. Determine the frequency components in the
reconstructed analog signal. Comment on the aliasing error.
Sol:
x(t) with freq components of 30 Hz, 50 Hz & 200 Hz is sampled at 300 Hz (fs).
30 Hz & 50 Hz are less than fs/2 (i.e., 150 Hz) not affected by aliasing present as 30 Hz and 50Hz
respectively in the reconstructed analog signal.
200Hz component of x(t) is above fs/2 (folding frequency) and is changed by the aliasing effect.
In the reconstructed analog signal the 200 Hz component will be aliased into
200-fs = 200 − 300 = −100 Hz. (the minus sign represents a phase shift of 180◦)
In summary the reconstructed analog signal has the frequency components 30 Hz, 50 Hz and 100 Hz.

Result: Sampling theorem is verified for under, Nyquist rate and over sampling using MATLAB.

Staff Signature:

Department of EEE, SJBIT Page 16


DSP Laboratory - 18EEL67 2020-21
EXPERIMENT 02

Impulse Response of a system

AIM: To find Impulse response of a first order and second order system.

Question: Write the Matlab code to find the impulse response of a given second order system whose
difference equation is given below.

y (n) = x(n) + 0.2 x(n-1) – 1.5 x(n-2) – 3 y(n-1) + 0.12 y(n-2)

THEORY: A discrete time system performs an operation on an input signal based on predefined criteria
to produce a modified output signal. The input signal x(n) is the system excitation, and y(n) is the
system response. The transform operation is shown as,

A discrete time LTI system (also called digital filters) as shown in Fig above represented by a linear
constant coefficient difference equation,

y[n] + a1 y[n −1] − a2 y[n − 2] = b0 x[n] + b1 x[n −1] + b2 x[n − 2];

The general form of difference equation is,


M M
y(n) = Σak y(n-k) + Σbk x(n-k)
k=1 k=0
A system function H(z) (obtained by applying Z transform to the difference equation). Find out the
impulse response of second order difference equation.
Y ( z ) b0 + b1 z −1 + b2 z −2
H ( z) = =
X ( z ) 1 + a1 z −1 + a2 z −2
Inverse Z transform of H(z) yields h(n) – impulse response
LTI system characterized in time domain by impulse response h(n), which is obtained by applying an
impulse input to the LTI system as shown in Fig below. Where the LTI system is a black box about
which nothing is known.

Department of EEE, SJBIT Page 17


DSP Laboratory - 18EEL67 2020-21

Once the impulse response h(n) is obtained, the response y(n) of the LTI system for any input x[n] can
be obtained by applying the convolution sum.

Infinite Impulse Response Filter (IIR Filter)


If the impulse response h[n] is infinite, Then the LTI systems are termed as Infinite Impulse Response
(IIR) systems or IIR filters.
In general IIR filters are recursive, where the present output sample (y(n)) depends not only on the input
(x(n), x(n−1), etc.) but also on the past output samples y(n − 1), etc
General form of difference equation of an IIR filter is

Given the difference equation or H(z), impulse response of LTI system is found using filter or impz
MATLAB functions.

If the difference equation contains past samples of output, i.e., y[n-1], y[n-2], etc , then its impulse
response is of infinite duration (IIR). For such systems the impulse response is computed for a large
value of n, say n=100 (to approximate n=∞).

Finite Impulse Response Filter (FIR Filter)

If both y[n] and x[n] are finite then the impulse response is finite (FIR).
Given only the input sequence x[n] and the output sequence y[n], we can find the impulse function h[n]
by using the inverse operation deconv.
conv operation convolves 2 sequences x[n] and h[n] to obtain the output y[n]. The deconvolution
operation is valid only if the LTI system is ‘invertible’.

Department of EEE, SJBIT Page 18


DSP Laboratory - 18EEL67 2020-21
PROGRAM CODE: ( By using Conv and deconv Commands)
y= input('The output sequence y(n) of the system=');
%Plot y
n1=0:1:length(y)-1; %define the time axis
subplot(3,1,1);
stem(n1,y);
x=input('The input sequence of the system=');
%Plot x
N2=0:1:length(x)-1; %define the time axis
subplot(3,1,2);
stem(N2,x);

%find the impulse response given x and y


h =deconv(y,x); %find h
disp('The impulse response of the system is=');
disp(h);
%plot h
n=0:1: length(h)-1;
subplot(3,1,3);
stem(n,h);
xlabel('Time index n');
ylabel('Amplitude');
title('impulse response of a system')

%Verify by convolving x and h to get y


yv=conv(x,h);
disp('the verified output sequence is');
disp(yv)

Output:
The given difference equation is y(n) - y(n-1) + 0.9y(n-2) = x(n)
The output sequence y(n) of the system=[1 -1 0.9]
The input sequence of the system=[1]
The impulse response of the system is=
1.0000 -1.0000 0.9000
the verified output sequence is
1.0000 -1.0000 0.9000

Case 2: By using impulse Response command impz


y (n) = x(n) + 0.2 x(n-1) – 1.5 x(n-2) – 3 y(n-1) + 0.12 y(n-2)
the above equation can be written as

y (n) + 3 y(n-1) - 0.12 y(n-2) = x(n) + 0.2 x(n-1) – 1.5 x(n-2) take Z transform on both the sides
Y(Z) + 3Z-1Y(Z) - 0.12Z-2Y(Z) = X(Z) +0.2 Z-1X(Z) – 1.5 Z-2 X(Z)
Y(Z) [ 1 + 3Z-1 - 0.12 Z-2 ] = X(Z) [1 +0.2 Z-1 – 1.5 Z-2]

Department of EEE, SJBIT Page 19


DSP Laboratory - 18EEL67 2020-21
The transfer function of the system is defined as
Y ( z ) 1 + 3 z −1 − 0.12 z −2
H ( z) = =
By long division we get X ( z ) 1 + 0.2 z −1 − 1.5 z − 2

H(Z) = 1- 2.8 Z-1 + 7.02 Z-2- 21.4Z-3 + 65.03 Z-4


By taking the Inverse Z-Transform we can obtain the impulse response as
h(n) = [ 1 -2.8 7.02 -21.4 65.03]
Experiment 2 : Impulse Response of a Given Second order System

clc;
% enter the input and output coefficients
b=input('Enter the coefficients of x(n): ');
a=input('Enter the coefficients of y(n): ');
N=input('Enter the number of samples of impulse response desired: ');
% Calculate Impulse Response using/IMpZ futption:
% [1.1,T] = IMPZ(B,A,N) computes N leMples:9f the impulse response
% coefficients B and A from difference egeation representation.
[h,t] = impz(b,a,N);
%Plot and Display impulse response co-efficients:
stem(t,h);
title('Impulse Response Plot');
ylabel('h(n)');
xlabel('n');
disp('Impulse response coefficients:');
disp(h);

Output:
Enter the coefficients of x(n):[1 0.2 -1.5]
Enter the coefficients of y(n): [1 3 -0.12]
Enter the number of samples of impulse response desired:5
Impulse Response coefficients are
1.0000
-2.8000
7.0200
-21.3960
65.0304

Results: Verify the impulse response of the given system for the given LTI System.

Staff Signature:

Department of EEE, SJBIT Page 20


DSP Laboratory - 18EEL67 2020-21
EXPERIMENT 03

LINEAR CONVOLUTION

AIM: To perform linear convolution of two given sequences.

THEORY:
Convolution is a formal mathematical operation, just as multiplication, addition, and integration.
Addition takes two numbers and produces a third number, while convolution takes two signals and
produces a third signal.
Convolution is used in the mathematics of many fields, such as probability and statistics. In
linear systems, convolution is used to describe the relationship between three signals of interest: the
input signal, the impulse response, and the output signal.
A discrete-time system performs an operation on an input signal based on a predefined criterion to
produce a modified output signal. The input signal x(n) is the system excitation and the output signal
y(n) is the system response. The transform operation is as shown below:

If the input to the system is a unit impulse, i.e. x(n) = δ(n), then the output of the system is known as
impulse response denoted by h(n) where h(n) = T{δ(n)}.
The system is assumed to be initially relaxed, i.e. the system has zero initial conditions.
For any arbitrary sequence x(n) can be represented as a weighted sum of discrete impulses as

The system response is given by

For a linear system y(n) reduces to

The response due to shifted impulse sequence δ(n – k) can be denoted by


h(n, k) = T[δ(n – k)]
For shift – invariant system,
Delayed output = output to delayed input

Department of EEE, SJBIT Page 21


DSP Laboratory - 18EEL67 2020-21
i.e. h(n – k) = h(n, k)
Therefore, the output equation for y(n) reduces to

For a linear time invariant system, if the input sequence x(n) and the impulse response h(n) are given,
the output sequence y(n) is given by

This is known as convolution summation, represented by


y(n) = x(n)*h(n) = h(n)*x(n)
This is an extremely power full tool and useful result that allows us to compute the system output for
any input signal excitation.

Department of EEE, SJBIT Page 22


DSP Laboratory - 18EEL67 2020-21
Question:
To perform the linear convolution of the two given sequences
x1(n) = { 1 2 -1 3} and x2(n) = {2 3 -2} by using CONV command.
Matlab Code:
% enter the coefficients of x1 and x2
x1=input('Enter the input signal of sequence x1(n): ');
x2=input('Enter the input signal of sequence x2(n): ');
% perform linear convolution using CONV Command
y = conv(x1,x2);
% plots the input and convolved signals
subplot (3,1,1);
stem(x1)
title('Input signal x1(n)');
xlabel('n');
ylabel('x1(n)');
subplot (3,1,2);
stem(x2)
title('Input signal x2(n)');
xlabel('n');
ylabel('x2(n)');
subplot (3,1,3);
stem(y);
title('Linear convolved signal y(n) = x1(n)* x2(n)');
xlabel('n');
ylabel('y(n)');
% Display the convolved sequence in command window
disp('Linear Convolved sequence:');
disp(y);

Output of exp3:

Enter the input signal of sequence x1(n): [1 2 -1 3]

Enter the input signal of sequence x2(n): [2 3 -2]

Linear Convolved sequence:

2 7 2 -1 11 -6

Department of EEE, SJBIT Page 23


DSP Laboratory - 18EEL67 2020-21

Result: Linear convolution of the sequences is found and the results are verified in MATLAB.

Staff Signature:

Department of EEE, SJBIT Page 24


DSP Laboratory - 18EEL67 2020-21
EXPERIMENT 04

CIRCULAR CONVOLUTION

AIM: To perform circular convolution of given sequences using (a) the convolution summation formula
(b) the matrix method and (c) Linear convolution from circular convolution with zero padding.

THEORY: The convolution property of DFT says that, the multiplication of DFTs of two sequences is
equivalent to the DFT of the circular convolution of the two sequences.
Let DFT [x1(n)] = X1(k) and DFT [x2(n)] = X2(k), then by the convolution property
X1(k)X2(k) = DFT{x1(n) x2(n)}.
Circular convolution: Let x1(n) and x2(n) are finite duration sequences both of length N with DFT’s
X1(k) and X2(k). Convolution of two given sequences x1(n) and x2(n) is given by the equation,

x3(n) = IDFT[X3(k)]

X3(k) = X1(k) X2(k)

N-1
x3(n) = ∑ x1(m) x2((n-m))N
m=0

Department of EEE, SJBIT Page 25


DSP Laboratory - 18EEL67 2020-21
EXAMPLE:

Let’s take x1(n) = {1, 1, 2, 1} and


x2(n) = {1, 2, 3, 4}
x3(0) = x1(m) x2(-m) = x1(0) x2(0) + x1(1) x2(3) + x1(2) x2(2) + x1(3) x2(1)
= 1 + 4 + 6 +2
x3(0) = 13
x3(1) = x1(m) x2(1-m)
= x1(0) x2(1) + x1(1) x2(0) + x1(2) x2(3) + x1(3) x2(2)
=2+1+8+3
x3(1) = 14
x3(2) = x1(m) x2(2-m)
= x1(0) x2(2) + x1(1) x2(1) + x1(2) x2(0) + x1(3) x2(3)
= 3 + 2 + 2+ 4
x3(2) = 11
x3(3) = x1(m) x2(3-m)
= x1(0) x2(3) + x1(1) x2(2) + x1(2) x2(1) + x1(3) x2(0)
=4+3+4+1
x3(3) = 12
The convoluted signal is,
x3(n) = {13, 14, 11, 12}

Department of EEE, SJBIT Page 26


DSP Laboratory - 18EEL67 2020-21
Case a: Perform circular convolution of the given sequences using Summation formula
% Circular convolution using summation formula
clc;
clear all;
close all;
x=input('enter the input sequence x(n)=');
h=input('enter the impulse response sequence h(n)=');
N1=length(x);
N2=length(h);
N=max(N1,N2);
%% Using built-in function
y1= cconv(x,h,N)
disp('N=');
disp(N);
%% Using summation formula
x=[x zeros(1, N-N1)]; % Zero padding
h=[h zeros(1, N-N2)]; % Zero padding
for n=0:(N-1);
y(n+1)=0;
for i=0:(N-1);
j= mod(n-i,N);
y(n+1)= y(n+1)+(x(i+1)*h(j+1));
end
end
disp('Given sequences');
disp('x(n)=');
disp(x);
disp('h(n)=');
disp(h);
disp('Circular convolution y(n)=');
disp(y);

Output:

enter the input sequence x(n)=[1 1 1]


enter the impulse response sequence h(n)=[1 1]
y1 = 2 2 2
N=3
Given sequence x(n) = 1 1 1
h(n) = 1 1 0
Circular convolution y(n) = 2 2 2

Department of EEE, SJBIT Page 27


DSP Laboratory - 18EEL67 2020-21
Case b: Circular Convolution by using Matrix Method

clc;
close all;
clear all;
x=input('enter the 1st sequence x(n)=');
h=input('enter the 1st sequence h(n)=');
N1=length(x);
N2=length(h);
N=max(N1,N2);
h=[h,zeros(1,N-N2)]
x=[x,zeros(1,N-N1)]
x1=transpose(x);
h1=transpose(h);
temp=h1;
for i= 1:(N-1);
temp=circshift(temp,1);
h1=horzcat(h1,temp);
end
y=h1*x1; % Matrix multiplication
disp('convolution y(n)= x(n)*h(n)=');
disp(y);

%% Plotting of input sequences


n=0:1:(N-1);
subplot(3,1,1);
stem(n,x);
title('First sequence');
xlabel('n');
ylabel('x(n)');
subplot(3,1,2);
stem(n,h);
title('Second sequence');
xlabel('n');
ylabel('h(n)');

%% Plotting of output sequence


subplot(3,1,3);
stem(n,y);
title('Circular convolution sequence');
xlabel('n');
ylabel('y(n)');

Department of EEE, SJBIT Page 28


DSP Laboratory - 18EEL67 2020-21
Output:

enter the 1st sequence x(n)=[1 2 3 4]


enter the 1st sequence h(n)=[2 1 2 1]
h=
2 1 2 1
x=
1 2 3 4
convolution y(n)= x(n)*h(n)=
14
16
14
16

Department of EEE, SJBIT Page 29


DSP Laboratory - 18EEL67 2020-21
Case c: To perform linear convolution using circular convolution

clc;
clear all;
close all;
x=input('enter the input sequence x(n)=');
h=input('enter the impulse response sequence h(n)=');
N1=length(x);
N2=length(h);
N=N1+N2-1; %% Length of sequence for linear convolution
h=[h,zeros(1,N-N2)] %% Zero padding
x=[x,zeros(1,N-N1)]
y=cconv(x,h,N); %% Circular convolution function
disp('Given sequence');
disp('x(n)=');
disp(x);
disp('h(n)=');
disp(h);
disp('Linear convolution y(n)= x(n)*h(n)=');
disp(y);

%% Plotting of input sequences


subplot(3,1,1);
stem(n,x);
title('First sequence');
xlabel('n');
ylabel('x(n)');
subplot(3,1,2);
stem(n,h);
title('Second sequence');
xlabel('n');
ylabel('h(n)');

%% Plotting of output sequence


subplot(3,1,3);
stem(n,y);
title('Circular convolution sequence');
xlabel('n');
ylabel('y(n)');

Department of EEE, SJBIT Page 30


DSP Laboratory - 18EEL67 2020-21
Output:

enter the input sequence x(n)=[1 2]


enter the impulse response sequence h(n)=[3 4]
h=
3 4 0
x=
1 2 0
Given sequence x(n) = 1 2 0
h(n) = 3 4 0
Linear convolution y(n) = x(n) * h(n) = 3.0000 10.0000 8.0000

Results: Perform the circular convolution for the given sequences for all the three cases successfully
and verify both the theoretical and practical results

Staff Signature:

Department of EEE, SJBIT Page 31


DSP Laboratory - 18EEL67 2020-21
EXPERIMENT 05

Computation of N-point DFT

AIM: To compute n-point DFT of a given sequence and to plot magnitude and phase spectrum.

THEORY: DISCRETE FOURIER TRANSFORM (DFT)


Any periodic function can be expressed in a Fourier series representation. The discrete-time Fourier
transform (DTFT) X(w) of a discrete-time sequence x(n) is a periodic continuous function of ω with a
period of 2π. So it cannot be processed by a digital system. For processing by a digital system it should
be converted into discrete form. The DFT converts the continuous function of ω to a discrete function of
ω. Thus, DFT allows us to perform frequency analysis on a digital computer.
The DFT of a discrete-time signal x(n) is a finite duration discrete frequency sequence. The DFT
sequence is denoted by X(k). The DFT is obtained by sampling one period of the Fourier transform
X(ω) of the signal x(n) at a finite number of frequency points. This sampling is conventionally
performed at N equally spaced points in the period 0 ≤ ω ≤ 2π or ωk = 2 π k/N; 0 ≤ ω ≤ 2π . We can say
that DFT is used for transforming discrete-time sequence x(n) of finite length into discrete frequency
sequence X(k) of finite length.
The Discrete Fourier Transform is a powerful computation tool which allows us to evaluate the Fourier
Transform X(ejω) on a digital computer or specially designed digital hardware. Since X(ejω) is
continuous and periodic, the DFT is obtained by sampling one period of the Fourier Transform at a
finite number of frequency points. Apart from determining the frequency content of a signal, DFT is
used to perform linear filtering operations in the frequency domain.

The DFT is important for two reasons. First it allows us to determine the frequency content of a signal
that is to perform spectral analysis. The second application of the DFT is to perform filtering operation
in the frequency domain.
The sequence of N complex numbers x0,..., xN−1 is transformed into the sequence of N complex numbers
X0, ..., XN−1 by the DFT according to the formula:

N-1
X(k) = ∑x(n)e-j2πnk/N k = 0,1, …. N-1
n=0

Department of EEE, SJBIT Page 32


DSP Laboratory - 18EEL67 2020-21
Example:
Let us assume the input sequence x[n] = [1 1 0 0]
We have,
N-1
X(k) = ∑ x(n)e-j2πnk/N k = 0,1, …. N-1
n=0
For k = 0,
3
X(0) = ∑ x(n) = x(0) + x(1) + x(2) + x(3)
n=0
X(0) = 1+1+0+0 = 2
For k = 1,
3
X(1) = ∑ x(n)e-jπn/2 = x(0) + x(1) e-jπ/2 + x(2) e-jπ + x(3) e-j3π/2
n=0
= 1 + cos(π/2) - jsin(π/2) = X(1) = 1 – j
For k = 2,
3
X(2) = ∑ x(n)e-jπn = x(0) + x(1) e-jπ + x(2) e-j2π + x(3) e-j3π = = 1 + cos π – jsin π
n=0
X(2) = 1-1 = 0
For k = 3,
3
X(3) = ∑ x(n)e-j3nπ/2 = x(0) + x(1) e-j3π/2 + x(2) e-j3π + x(3) e-j9π/2 =
n=0
= 1 + cos(3π/2) - jsin(3π/2) = X(3) = 1 + j
The DFT of the given sequence is,
X(k) = { 2, 1-j, 0, 1+j }
To find Magnitude of X(k):
Magnitude= (a2+b2)1/2
Where a and b are real and imaginary parts respectively
To fine Phase of X (k):
Phase=tan-1(b/a)

Department of EEE, SJBIT Page 33


DSP Laboratory - 18EEL67 2020-21
PROGRAM: computation of N point DFT also to plot magnitude and phase spectrum.

close all;
clear;
x=input('enter the time sequence');
N=input('enter the no.of points of DFT');
for k=0:N-1
X(k+1)=0;
for n=0:length(x)-1
X(k+1)= X(k+1) + x(n+1)* exp (-j*2*pi*n*k/N);
end
end
n=0:length(x)-1;
stem(n,x);
title('time sequence');
xlabel('n');
ylabel('x(n)');
k=0:N-1;
figure
subplot(2,1,1);
stem(k,abs(X));
title('magnitude response');
xlabel('k');
ylabel('mag(X(k))');
subplot(2,1,2);
stem(k,angle(X));
title('phase response');
xlabel('k');
ylabel('phase(X(k))');
abs(X)
angle(X)

OUTPUT:
enter the time sequence [1 2 3 4]
enter the no.of points of DFT 4

ans =

10.0000 2.8284 2.0000 2.8284

ans =

0 2.3562 -3.1416 -2.3562

Department of EEE, SJBIT Page 34


DSP Laboratory - 18EEL67 2020-21

Result: DFT of the given sequence is found and the results are verified using MATLAB.

Staff Signature:

Department of EEE, SJBIT Page 35


DSP Laboratory - 18EEL67 2020-21
EXPERIMENT 06
LINEAR AND CIRCULAR CONVOLUTION
AIM: To compute linear and circular convolution using DFT and IDFT methodology

THEORY: Linear convolution is the basic operation to calculate the output for any linear time invariant
system given its input and its impulse response. Circular convolution is the same thing but considering
that the support of the signal is periodic (as in a circular).

The circular convolution, also known as cyclic convolution, of two aperiodic functions (i.e.
Schwartz functions) occurs when one of them is convolved in the normal way with a periodic
summation of the other function. That situation arises in the context of the Circular
convolution theorem.

The DFT supports only circular convolution. When two numbers of N-point sequence are circularly
convolved, it produces another N-point sequence. For circular convolution, one of the sequence should
be periodically extended. Also the resultant sequence is periodic with period N.
The linear convolution of two sequences of length N1 and N2 produces an output sequence of
length N1 + N2 – 1. To perform linear convolution using DFT, both the sequences should be converted
to N1 + N2 – 1 sequences by padding with zeros. Then take N1 + N2 – 1-point DFT of both the
sequences and determine the product of their DFTs. The resultant sequence is given by the IDFT of the
product of DFTs. [Actually the response is given by the circular convolution of the N1 + N2 – 1
sequences].
Let x(n) be an N1-point sequence and h(n) be an N2-point sequence. The linear convolution of x(n) and
h(n) produces a sequence y(n) of length N1 + N2 – 1. So pad x(n) with N2 – 1 zeros and h(n) with N1 –
1 zeros and make both of them of length N1 + N2 – 1.
Let X(k) be an N1 + N2 – 1-point DFT of x(n), and H(k) be an N1 + N2 – 1-point DFT of h(n). Now,
the sequence y(n) is given by the inverse DFT of the product X(k) H(k).
y(n) = IDFT {X(k)H(k)}
This technique of convolving two finite duration sequences using DFT techniques is called fast
convolution. The convolution of two sequences by convolution sum formula is called direct convolution
or slow convolution.

The term fast is used because the DFT can be evaluated rapidly and
efficiently using any of a large class of algorithms called Fast Fourier Transform (FFT).

Department of EEE, SJBIT Page 36


DSP Laboratory - 18EEL67 2020-21
Program Code: Linear Convolution

close all;
clear;
x= input('enter the first sequence');
h= input('enter the second sequence');
L1=length(x);
L2=length(h);
N= L1 +L2 -1;
X=fft(x,N);
H=fft(h,N);
Y= X.*H;
y=ifft(Y,N)
n=0:L1-1;
subplot(3,1,1);
stem(n,x);
title('First sequence');
xlabel('n');
ylabel('x(n)');
n=0:L2-1;
subplot(3,1,2);
stem(n,h);
title('second sequence');
xlabel('n');
ylabel('h(n)');
n=0:N-1;
subplot(3,1,3);
stem(n,y);
title('linear convolution sequence');
xlabel('n');
ylabel('y(n)');

Department of EEE, SJBIT Page 37


DSP Laboratory - 18EEL67 2020-21
Output:

enter the first sequence[ 1 2 2 1]

enter the second sequence[ 3 1 1 2 3]

y=

3.0000 7.0000 9.0000 9.0000 10.0000 11.0000 8.0000 3.0000

Department of EEE, SJBIT Page 38


DSP Laboratory - 18EEL67 2020-21
Program Code: Circular Convolution

close all;
clear;
x= input('enter the first sequence');
h= input('enter the second sequence');
L1=length(x);
L2=length(h);
N= max(L1,L2);
X=fft(x,N);
H=fft(h,N);
Y= X.*H;
y=ifft(Y,N)
n=0:N-1;
subplot(3,1,1);
stem(n,x);
title('First sequence');
xlabel('n');
ylabel('x(n)');
subplot(3,1,2);stem(n,h);
title('second sequence');
xlabel('n');
ylabel('h(n)');
subplot(3,1,3);
stem(n,y);
title('linear convolution sequence');
xlabel('n');
ylabel('y(n)');

Department of EEE, SJBIT Page 39


DSP Laboratory - 18EEL67 2020-21
Output:

enter the first sequence [1 2 2 1]


enter the second sequence [3 1 1 2]
y=
10 12 11 9

Result: The result for given sequence for circular and linear convolution using DFT and IDFT was
computed and verified.

Staff Signature:

Department of EEE, SJBIT Page 40


DSP Laboratory - 18EEL67 2020-21
EXPERIMENT 07

SOLUTION OF A DIFFERENCE EQUATION

AIM: To obtain the solution for a given difference equation

THEORY:
To solve the difference equation, first it is converted into algebraic equation by taking its Z-
transform. The solution is obtained in z-domain and the time domain solution is obtained by taking its
inverse Z-transform.
The system response has two components. The source free response and the forced response. The
response of the system due to input alone when the initial conditions are neglected is called the forced
response of the system. It is also called the steady state response of the system. It represents the
component of the response due to the driving force. The response of the system due to initial conditions
alone when the input is neglected is called the free or natural response of the system. It is also called the
transient response of the system. It represents the component of the response when the driving function
is made zero. The response due to input and initial conditions considered simultaneously is called the
total response of the system.
For a stable system, the source free component always decays with time. In fact a stable system
is one whose source free component decays with time. For this reason the source free component is also
designated as the transient component and the component due to source is called the steady state
component.
When input is a unit impulse input, the response is called the impulse response of the system and
when the input is a unit step input, the response is called the step response of the system.
Systems may be continuous-time systems or discrete-time systems. Discrete-time systems may be FIR
(Finite Impulse Response) systems or IIR (Infinite Impulse Response) systems.
FIR systems are the systems whose impulse response has finite number of samples and IIR systems are
systems whose impulse response has infinite number of samples. Realization of a discrete-time system
means obtaining a network corresponding to the difference equation or transfer function of the system.
If the discrete-time system is described by difference equation, the time invariance can be found by
observing the coefficients of the difference equation.
If all the coefficients of the difference equation are constants, then the system is time-invariant. If even
one of the coefficient is function of time, then the system is time-variant.

Department of EEE, SJBIT Page 41


DSP Laboratory - 18EEL67 2020-21
The system described by y(n) + 3y(n – 1) + 5y(n – 2) = 2x(n) is time-invariant system because all the
coefficients are constants.
The system described by y(n) – 2ny(n – 1) + 3n2y(n – 2) = x(n) + x(n – 1)
is time-varying system because all the coefficients are not constant (Two are functions of time).
The systems satisfying both linearity and time-invariant conditions are called linear, time-invariant
systems, or simply LTI systems.

Relationship between Transfer function and Difference equation


In terms of a difference equation, an nth order discrete-time LTI system is specified as:

expanding it, we have

Now, Y(z) /X(z) = H(z) is called the transfer function of the system or the system function. The frequency response of a

system is obtained by substituting z = e in H(z).

Department of EEE, SJBIT Page 42


DSP Laboratory - 18EEL67 2020-21
Find the impulse and step response of the system y(n) = 2x(n) - 3x(n - 1) + x(n - 2) - 4x(n - 3)
Solution: For impulse response, x(n) = δ(n)
The impulse response of the system is: y(n) = 2 δ (n) - 3 δ (n -1) + δ (n -2) - 4 δ (n -3)
For step response, x(n) = u(n)
The step response of the system is: y(n) = 2u(n) - 3u(n - 1) + u(n - 2) - 4u(n -3)
PROGRAM 1: To Find the Impulse response

x(n) = y(n) – y(n-1) + 0.9 y(n-2)

%To find Impulse Response


N=input('Length of response required=');
b=[1]; %x[n] coefficient
a=[1,-1,0.9]; %y[n] coefficients
%impulse input
x=[1,zeros(1,N-1)];
%time vector for plotting
n=0:1:N-1;
%impulse response
h=filter(b,a,x)
%plot the waveforms
subplot(2,1,1);
stem(n,x);
title('impulse input');
xlabel('n');
ylabel('?(n)');
subplot(2,1,2);
stem(n,h);
title('impulse response');
xlabel('n');
ylabel('h(n)');

Department of EEE, SJBIT Page 43


DSP Laboratory - 18EEL67 2020-21
Output:
Length of response required=25
h=
Columns 1 through 11
1.0000 1.0000 0.1000 -0.8000 -0.8900 -0.1700 0.6310 0.7840 0.2161 -0.4895
-0.6840
Columns 12 through 22
-0.2434 0.3722 0.5912 0.2563 -0.2758 -0.5065 -0.2583 0.1976 0.4300 0.2522
-0.1348
Columns 23 through 25
-0.3618 -0.2405 0.0852

Department of EEE, SJBIT Page 44


DSP Laboratory - 18EEL67 2020-21
Program2: To find step response

%To find step response


N=input('Length of response required=');
b=[1]; %x[n] coefficient
a=[1,-1,0.9]; %y[n] coefficients
x=[ones(1,N)]; %step input
n=0:1:N-1; %time vector for plotting
y=filter(b,a,x); %step response
%plot the waveforms
subplot(2,1,1);
stem(n,x);
title('step input');
xlabel('n');
ylabel('u(n)');
subplot(2,1,2);
stem(n,y);
title('step response');
xlabel('n');
ylabel('y(n)');

Output: exp7b
Length of response required=25

Department of EEE, SJBIT Page 45


DSP Laboratory - 18EEL67 2020-21
Program2: To find the steady state response
%To find steady state response
%y[n]-0.8y[n-1]=x[n]
N=input('Length of response required=');
b=[1]; %x[n] coefficient
a=[1,-0.8]; %y[n] coefficients
n=0:1:N-1; %time vector
x=cos(0.05*pi*n); % sinusoidal input
y=filter(b,a,x); %steady state response
%plot the waveforms
subplot(2,1,1);
stem(n,x);
title('sinusoidal input');
xlabel('n');
ylabel('u(n)');
subplot(2,1,2);
stem(n,y);
title('steady state response');
xlabel('n');
ylabel('y(n)');
Output: exp7c
Length of response required=100

Result: Solution of the difference equation is found and obtained the various responses.

Staff Signature:

Department of EEE, SJBIT Page 46


DSP Laboratory - 18EEL67 2020-21
EXPERIMENT 08

Calculation of DFT and IDFT by using FFT

AIM: To obtain the DFT and IDFT of a given sequence by using FFT

THEORY:
The N-point DFT of a sequence x(n) converts the time domain N-point sequence x(n) to a frequency
domain N-point sequence X(k). The direct computation of an N-point DFT requires N x N complex
multiplications and N(N – 1) complex additions. Many methods were developed for reducing the
number of calculations involved. The most popular of these is the Fast Fourier Transform (FFT), a
method developed by Cooley and Turkey. The FFT may be defined as an algorithm (or a method) for
computing the DFT efficiently (with reduced number of calculations). The computational efficiency is
achieved by adopting a divide and conquer approach. This approach is based on the decomposition of an
N-point DFT into successively smaller DFTs and then combining them to give the total transform.
Based on this basic approach, a family of computational algorithms was developed and they are
collectively known as FFT algorithms. Basically there are two FFT algorithms; Decimation in- time
(DIT) FFT algorithm and Decimation-in-frequency (DIF) FFT algorithm.

From the above equations for X(k) and x(n), it is clear that for each value of k, the direct computation of
X(k) involves N complex multiplications (4N real multiplications) and N – 1 complex additions (4N – 2

Department of EEE, SJBIT Page 47


DSP Laboratory - 18EEL67 2020-21
real additions). Therefore, to compute all N values of DFT, N complex multiplications and N(N – 1)
2

complex additions are required. In fact the DFT and IDFT involve the same type of computations.
Program Code:

%Computation of N-point DFT using FFT


clc;
close all;
clear;
x= input('enter the given time domain sequence x(n)=');
N= input('enter the length of DFT = ');
%% Using built-in functions
X= fft(x,N)
x1=ifft(X,N)
%% Plotting magnitude spectrum
k=0:N-1;
figure
subplot(3,1,1);
stem(k,abs(X));
title('magnitude response');
xlabel('k');
ylabel('mag(X(k))');
%% Plotting phase spectrum
subplot(3,1,2);
stem(k,angle(X));
title('phase response');
xlabel('k');
ylabel('phase(X(k))');
disp('The N-point DFT of the given sequence is');
disp(X);
disp('The IDFT of X is given by');
disp(x1);

Output:
enter the given time domain sequence x(n)=[1 1 1 1]
enter the length of DFT = 8

X = Columns 1 through 5
4.0000 + 0.0000i 1.0000 - 2.4142i 0.0000 + 0.0000i 1.0000 - 0.4142i 0.0000 + 0.0000i

Columns 6 through 8
1.0000 + 0.4142i 0.0000 + 0.0000i 1.0000 + 2.4142i

x1 = 1.0000 1.0000 1.0000 1.0000 0 0 0.0000 0

Department of EEE, SJBIT Page 48


DSP Laboratory - 18EEL67 2020-21
The N-point DFT of the given sequence is

Columns 1 through 5
4.0000 + 0.0000i 1.0000 - 2.4142i 0.0000 + 0.0000i 1.0000 - 0.4142i 0.0000 + 0.0000i

Columns 6 through 8
1.0000 + 0.4142i 0.0000 + 0.0000i 1.0000 + 2.4142i

The IDFT of X is given by


1.0000 1.0000 1.0000 1.0000 0 0 0.0000 0

Result: Obtain the DFT and IDFT of a given sequence by using FFT and verify the results successfully.

Staff Signature:

Department of EEE, SJBIT Page 49


DSP Laboratory - 18EEL67 2020-21
EXPERIMENT 09

DESIGN AND IMPLEMENTATION OF IIR FILTERS

AIM: Design of FIR filters of Low pass and high pass filter using Matlab commands

DESCRIPTION: Digital filters refers to the hardware and software implementation of the mathematical
algorithm which accepts a digital signal as input and produces another digital signal as output whose
wave shape, amplitude and phase response has been modified in a specified manner. Digital filter play
very important role in DSP. Compare with analog filters they are preferred in number of application due
to following advantages.
1. Truly linear phase response
2. Better frequency response
3. Filtered and unfiltered data remains saved for further use.
There are two types of digital filters.
1. FIR (finite impulse response) filter
2. IIR (infinite impulse response) filter
Filters are of two types—FIR and IIR.: The type of filters which make use of feedback connection to get
the desired filter implementation are known as recursive filters. Their impulse response is of infinite
duration. So they are called IIR filters. The types of filters which do not employ any kind of feedback
connection are known as non-recursive filters. Their impulse response is of finite duration. So they are
called FIR filters. IIR filters are designed by considering all the infinite samples of the impulse response.
The impulse response is obtained by taking inverse Fourier transform of ideal frequency response. There
are several techniques available for the design of digital filters having an infinite duration unit impulse
response. The popular methods for such filter design use the technique of first designing the digital filter
in analog domain and then transforming the analog filter into an equivalent digital filter because the
analog filter design techniques are well developed.
Advantages of Digital Filters:
1. The values of resistors, capacitors and inductors used in analog filters change with temperature. Since
the digital filters do not have these components, they have high thermal stability.
2. In digital filters, the precision of the filter depends on the length (or size) of the registers used to store
the filter coefficients. Hence by increasing the register bit length (in hardware) the performance
characteristics of the filter like accuracy, dynamic range, stability and frequency response tolerance, can
be enhanced.

Department of EEE, SJBIT Page 50


DSP Laboratory - 18EEL67 2020-21
3. The digital filters are programmable. Hence the filter coefficients can be changed any time to
implement adaptive features.
4. A single filter can be used to process multiple signals by using the techniques of multiplexing.
Disadvantages of Digital Filters
1. The bandwidth of the discrete signal is limited by the sampling frequency. The bandwidth of real
discrete signal is half the sampling frequency.
2. The performance of the digital filter depends on the hardware (i.e., depends on the bit length of the
registers in the hardware) used to implement the filter.
The popular methods of designing IIR digital filter involve the design of equivalent analog filter
and then converting the analog filter to digital filter. Hence to design a Butterworth IIR digital filter,
first an analog Butterworth filter transfer function is determined using the given specifications. Then the
analog filter transfer function is converted to a digital filter transfer function using either impulse
invariant transformation or bilinear transformation.

Specifications of the low pass filter:


The magnitude response of low-pass filter in terms of gain and attenuation are shown in Figure below.

Department of EEE, SJBIT Page 51


DSP Laboratory - 18EEL67 2020-21
Matlab Code:

% Using BUTTERWORTH Low Pass FILTER


clc;
clear all;
close all;
Ap= input('enter the passband attenuation in dB=');
As= input('enter the stopband attenuation in dB=');
Fp= input('enter the passband frequency in Hz=');
Fs= input('enter the stopband frequency in Hz=');
F= input('enter the sampling frequency in Hz=');
Wp= 2*Fp/F;
Ws= 2*Fs/F;
[N,Wn]= buttord(Wp,Ws,Ap,As)
%% low-pass filter
[b,a]=butter(N,Wn,'low');
[h,W]= freqz(b,a);
subplot(2,1,1);
plot(W/pi,20*log10(abs(h)));
xlabel('Normalized Frequency')
ylabel('gain in db')
grid on;
title('Magnitude Response')
subplot(2,1,2);
plot(W/pi,20*angle(h));
xlabel('Normalized Frequency')
ylabel('Phase in Radians')
grid on;
title('Phase Response')

Department of EEE, SJBIT Page 52


DSP Laboratory - 18EEL67 2020-21
Output:
enter the passband attenuation in dB=0.5
enter the stopband attenuation in dB=30
enter the passband frequency in Hz=1000
enter the stopband frequency in Hz=1500
enter the sampling frequency in Hz=5000
N= 8
Wn = 0.4644

Department of EEE, SJBIT Page 53


DSP Laboratory - 18EEL67 2020-21
% Using BUTTERWORTH HIGHPASS FILTER
clc;
clear all;
close all;
Ap= input('enter the passband attenuation in dB=');
As= input('enter the stopband attenuation in dB=');
Fp= input('enter the passband frequency in Hz=');
Fs= input('enter the stopband frequency in Hz=');
F= input('enter the sampling frequency in Hz=');
Wp= 2*Fp/F;
Ws= 2*Fs/F;
[N,Wn]= buttord(Wp,Ws,Ap,As)
%% High-pass filter
[b,a]=butter(N,Wn,'high')
[h,W]= freqz(b,a);
subplot(2,1,1);
plot(W/pi,20*log10(abs(h)));
xlabel('Normalized Frequency')
ylabel('gain in db')
grid on;
title('Magnitude Response')
subplot(2,1,2);
plot(W/pi,20*angle(h));
xlabel('Normalized Frequency')
ylabel('Phase in Radians')
grid on;
title('Phase Response')

Department of EEE, SJBIT Page 54


DSP Laboratory - 18EEL67 2020-21
Output:
enter the passband attenuation in dB=1
enter the stopband attenuation in dB=50
enter the passband frequency in Hz=1000
enter the stopband frequency in Hz=600
enter the sampling frequency in Hz=3500
N=
9
Wn =
0.5395
b=
0.0029 -0.0260 0.1042 -0.2431 0.3646 -0.3646 0.2431 -0.1042 0.0260 -0.0029
a=
1.0000 0.7083 1.4087 0.6570 0.5412 0.1570 0.0622 0.0096 0.0015 0.0001

Department of EEE, SJBIT Page 55


DSP Laboratory - 18EEL67 2020-21
% Using BUTTERWORTH BANDSTOP FILTER
clc;
clear all;
close all;
Ap= 0.4;
As= 50;
Wp=[0.3 0.7];
Ws=[0.4 0.6];
[N,Wn]= buttord(Wp,Ws,Ap,As)
[b,a]=butter(N,Wn,'stop');
[h,W]= freqz(b,a);
subplot(2,1,1);
plot(W/pi,20*log10(abs(h)));
xlabel('Normalized Frequency')
ylabel('gain in db')
grid on;
title('Magnitude Response')
subplot(2,1,2);
plot(W/pi,20*angle(h));
xlabel('Normalized Frequency')
ylabel('Phase in Radians')
grid on;
title('Phase Response')

Department of EEE, SJBIT Page 56


DSP Laboratory - 18EEL67 2020-21
Output:
N= 9
Wn = 0.3243 0.6757
b=
Columns 1 through 12
0.0330 0.0000 0.2972 0.0000 1.1889 0.0000 2.7741 0.0000 4.1611 0.0001 4.1611
0.0000
Columns 13 through 19
2.7741 0.0000 1.1889 0.0000 0.2972 0.0000 0.0330
a=
Columns 1 through 12
1.0000 0.0000 2.6621 0.0000 4.1451 0.0001 4.1273 0.0001 2.8977 0.0000 1.4381
0.0000
Columns 13 through 19
0.5027 0.0000 0.1178 0.0000 0.0167 0.0000 0.0011

Department of EEE, SJBIT Page 57


DSP Laboratory - 18EEL67 2020-21
% Using CHEBYSHEV BANDPASS FILTER
clc;
clear all;
close all;
Ap= 3;
As= 40;
Wp=[60 200]/500;
Ws=[50 250]/500;
[N,Wp]= cheb1ord(Wp,Ws,Ap,As)
[b,a]=cheby1(N,Ap,Wp);
[h,W]= freqz(b,a);
subplot(2,1,1);
plot(W/pi,20*log10(abs(h)));
xlabel('Normalized Frequency')
ylabel('gain in db')
grid on;
title('Magnitude Response')
subplot(2,1,2);
plot(W/pi,20*angle(h));
xlabel('Normalized Frequency')
ylabel('Phase in Radians')
grid on;
title('Phase Response')

Department of EEE, SJBIT Page 58


DSP Laboratory - 18EEL67 2020-21
Output:
N=
7
Wp =
0.1200 0.4000

RESULT: The IIR low pass butter worth filter & high pass butter worth filter, low pass Chebyshev
bandpass filter and low pass Butterworth band stop filter is verified for the given specifications is
obtained.

Staff Signature:

Department of EEE, SJBIT Page 59


DSP Laboratory - 18EEL67 2020-21
EXPERIMENT 10

DESIGN AND IMPLEMENTATION OF FIR FILTERS USING WINDOW


FUNCTIONS

AIM: To implementation of FIR filters to meet the given specifications for low pass, high pass filter
Band pass and Band stop filter

THEORY:
A filter is a frequency selective system. Digital filters are classified as finite duration unit impulse
response (FIR) filters or infinite duration unit impulse response (IIR) filters, depending on the form of
the unit impulse response of the system. In the FIR system, the impulse response sequence is of finite
duration, i.e., it has a finite number of non-zero terms. The IIR system has an infinite number of non-
zero terms, i.e., its impulse response sequence is of infinite duration. IIR filters are usually implemented
using recursive structures (feedback-poles and zeros) and FIR filters are usually implemented using non-
recursive structures (no feedback-only zeros). The response of the FIR filter depends only on the present
and past input samples, whereas for the IIR filter, the present response is a function of the present and
past values of the excitation as well as past values of the response.
The following are the main advantages of FIR filters over IIR filters:
1. FIR filters are always stable.
2. FIR filters with exactly linear phase can easily be designed.
3. FIR filters can be realized in both recursive and non-recursive structures.
4. FIR filters are free of limit cycle oscillations, when implemented on a finite word
length digital system.
5. Excellent design methods are available for various kinds of FIR filters.
The disadvantages of FIR filters are as follows:
1. The implementation of narrow transition band FIR filters is very costly, as it requires considerably
more arithmetic operations and hardware components such as multipliers, adders and delay elements.
2. Memory requirement and execution time are very high.
FIR filters are employed in filtering problems where linear phase characteristics within the pass band of
the filter are required. If this is not required, either an FIR or an IIR filter may be employed. An IIR
filter has lesser number of side lobes in the stop band than an FIR filter with the same number of
parameters. For this reason if some phase distortion is tolerable, an IIR filter is preferable. Also, the

Department of EEE, SJBIT Page 60


DSP Laboratory - 18EEL67 2020-21
implementation of an IIR filter involves fewer parameters, less memory requirements and lower
computational complexity.
In window method, we begin with the desired frequency response specification Hd(ω) and
determine the corresponding unit sample response hd(n). The hd(n) is given by the inverse Fourier
transform of Hd(ω). The unit sample response hd(n) will be an infinite sequence and must be truncated at
some point, say, at n = N – 1 to yield an FIR filter of length N. The truncation is achieved by multiplying
hd(n) by a window sequence w(n). The resultant sequence will be of length N and can be denoted by
h(n). The Z-transform of h(n) will give the filter transfer function H(z). There have been many windows
proposed like Rectangular window, Triangular window, Hanning window, Hamming window,
Blackman window and Kaiser window that approximate the desired characteristics.

Department of EEE, SJBIT Page 61


DSP Laboratory - 18EEL67 2020-21
FIR Filter design
%% Using HAMMING WINDOW TECHNIQUE
clc;
clear all;
close all;
N= input('enter the length of the window=');
W1= input('enter the lower cut-off frequency=');
W2= input('enter the upper cut-off frequency=');
Wh= hamming(N);
%% low-pass filter
b=fir1(N-1,W1,'low',Wh);
[h,omega]=freqz(b,1,256);
mag= 20*log10(abs(h));
subplot(2,2,1)
plot(omega/pi,mag);
grid on;
xlabel('normalised frequency');
ylabel('gain');
title('Low-pass filter');
%%High-pass filter
b=fir1(N-1,W1,'high',Wh);
[h,omega]=freqz(b,1,256);
mag= 20*log10(abs(h));
subplot(2,2,2)
plot(omega/pi,mag);
grid on;
xlabel('normalised frequency');
ylabel('gain');
title('High-pass filter');
%%Band-reject/stop filter
b=fir1(N-1,[W1,W2],'stop',Wh);
[h,omega]=freqz(b,1,256);
mag= 20*log10(abs(h));
subplot(2,2,3)
plot(omega/pi,mag);
grid on;
xlabel('normalised frequency');
ylabel('gain');
title('Band-stop filter');
%%Band-pass filter
b=fir1(N-1,[W1,W2],Wh);
[h,omega]=freqz(b,1,256);
mag= 20*log10(abs(h));
subplot(2,2,4)
plot(omega/pi,mag);
grid on;
xlabel('normalised frequency');
ylabel('gain');
title('Band-pass filter');

Department of EEE, SJBIT Page 62


DSP Laboratory - 18EEL67 2020-21
Output:

enter the length of the window=25

enter the lower cut-off frequency=0.2

enter the upper cut-off frequency=0.4

Department of EEE, SJBIT Page 63


DSP Laboratory - 18EEL67 2020-21
%% Using RECTANGULAR WINDOW TECHNIQUE
clc;
clear all;
close all;
N= input('enter the length of the window=');
W1= input('enter the lower cut-off frequency=');
W2= input('enter the upper cut-off frequency=');
Wh= rectwin(N);
%% low-pass filter
b=fir1(N-1,W1,'low',Wh);
[h,omega]=freqz(b,1,256);
mag= 20*log10(abs(h));
subplot(2,2,1)
plot(omega/pi,mag);
grid on;
xlabel('normalised frequency');
ylabel('gain in dB');
title('Low-pass filter');
%%High-pass filter
b=fir1(N-1,W1,'high',Wh);
[h,omega]=freqz(b,1,256);
mag= 20*log10(abs(h));
subplot(2,2,2)
plot(omega/pi,mag);
grid on;
xlabel('normalised frequency');
ylabel('gain in dB');
title('High-pass filter');
%%Band-reject/stop filter
b=fir1(N-1,[W1,W2],'stop',Wh);
[h,omega]=freqz(b,1,256);
mag= 20*log10(abs(h));
subplot(2,2,3)
plot(omega/pi,mag);
grid on;
xlabel('normalised frequency');
ylabel('gain in dB');
title('Band-stop filter');
%%Band-pass filter
b=fir1(N-1,[W1,W2],Wh);
[h,omega]=freqz(b,1,256);
mag= 20*log10(abs(h));
subplot(2,2,4)
plot(omega/pi,mag);
grid on;
xlabel('normalised frequency');
ylabel('gain in dB');
title('Band-pass filter');

Department of EEE, SJBIT Page 64


DSP Laboratory - 18EEL67 2020-21
Output:

enter the length of the window=25

enter the lower cut-off frequency=0.2

enter the upper cut-off frequency=0.4

Department of EEE, SJBIT Page 65


DSP Laboratory - 18EEL67 2020-21
%% Using HANNING WINDOW TECHNIQUE
clc;
clear all;
close all;
N= input('enter the length of the window=');
W1= input('enter the lower cut-off frequency=');
W2= input('enter the upper cut-off frequency=');
Wh= hann(N);
%% low-pass filter
b=fir1(N-1,W1,'low',Wh);
[h,omega]=freqz(b,1,256);
mag= 20*log10(abs(h));
subplot(2,2,1)
plot(omega/pi,mag);
grid on;
xlabel('normalised frequency');
ylabel('gain in dB');
title('Low-pass filter');
%%High-pass filter
b=fir1(N-1,W1,'high',Wh);
[h,omega]=freqz(b,1,256);
mag= 20*log10(abs(h));
subplot(2,2,2)
plot(omega/pi,mag);
grid on;
xlabel('normalised frequency');
ylabel('gain in dB');
title('High-pass filter');
%%Band-reject/stop filter
b=fir1(N-1,[W1,W2],'stop',Wh);
[h,omega]=freqz(b,1,256);
mag= 20*log10(abs(h));
subplot(2,2,3)
plot(omega/pi,mag);
grid on;
xlabel('normalised frequency');
ylabel('gain in dB');
title('Band-stop filter');
%%Band-pass filter
b=fir1(N-1,[W1,W2],Wh);
[h,omega]=freqz(b,1,256);
mag= 20*log10(abs(h));
subplot(2,2,4)
plot(omega/pi,mag);
grid on;
xlabel('normalised frequency');
ylabel('gain in dB');
title('Band-pass filter');

Department of EEE, SJBIT Page 66


DSP Laboratory - 18EEL67 2020-21
Output:

enter the length of the window=25

enter the lower cut-off frequency=0.2

enter the upper cut-off frequency=0.4

Results: Design and Implementation of FIR Filters Using different Window Functions can be obtained
successfully.

Staff Signature:

Department of EEE, SJBIT Page 67


DSP Laboratory - 18EEL67 2020-21
EXPERIMENT 11

DESIGN AND IMPLEMENTATION OF FIR FILTERS USING FREQUENCY


SAMPLING METHOD

Aim: To Design FIR filters using methods of frequency sampling

Theory: The frequency-sampling method for FIR filter design is perhaps the simplest and most direct
technique imaginable when a desired frequency response has been specified. It consists simply of
uniformly sampling the desired frequency response, and performing an inverse DFTto obtain the
corresponding (finite) impulse response. The results are not optimal, however, because the response
generally deviates from what is desired between the samples. When the desired frequency-response
is under sampled, which is typical, the resulting impulse response will be time aliased to some extent. It
is important to evaluate the final impulse response via a simulated DTFT (FFT with lots of zero
padding), comparing to the originally desired frequency response.

DESIGN TECHNIQUES FOR FIR FILTER


The well known methods of designing FIR filters are as follows:
1. Fourier series method
2. Window method
3. Frequency sampling method
4. Optimum filter design

In frequency sampling method of filter design, we begin with the desired frequency response
specification Hd(ω), and it is sampled at N-points to generate a sequence H(k) which corresponds to the
DFT coefficients. The N-point IDFT of the sequence H(k) gives the impulse response of the filter h(n).
The Z-transform of h(n) gives the transfer function H(z) of the filter.

Department of EEE, SJBIT Page 68


DSP Laboratory - 18EEL67 2020-21
Program Code:
clear all
M = 20;
alpha = (M-1)/2;
k = 0:M-1;
wk = (2*pi/M)*k;
Hr = [1,1,1,zeros(1,15),1,1]; %Ideal Amp Res sampled
Hd = [1,1,0,0];
wdl = [0,0.25,0.25,1]; %Ideal Amp Res for plotting
k1 = 0:floor((M-1)/2);
k2 = floor((M-1)/2)+1:M-1;
angH = [-alpha*(2*pi)/M*k1, alpha*(2*pi)/M*(M-k2)];
H = Hr.*exp(j*angH);
h = real(ifft(H,M));
[h1,w] = freqz(h,k);
M= length(h);
L = M/2;
b= 2*[h(L:-1:1)];
n =[1:1:L];
n = n-0.5;
w=[0:1:500]'*pi/500;
Hr1 =cos(w*n)*b';
[h1,w]=freqz(h,1);
subplot(2,2,1);stem(Hr);
title('Frequency Samples: M=20')
xlabel('frequency in pi units');
ylabel('Hr(k)')
subplot(2,2,2);
stem(k,h);
axis([-1,M,-0.1,0.3])
title('Impulse Response');
xlabel('n');
ylabel('h(n)');
subplot(2,2,3);
plot(Hr1);
axis([0,600, -0.5,1.5]);
title('Amplitude Response')
xlabel('frequency in pi units');
ylabel('Hr(w)')
subplot(2,2,4);
plot(20*log10(abs(h1)));
axis([0,600,-100,50]);
title('Magnitude Response');
xlabel('frequency in pi units');
ylabel('Decibels');

Department of EEE, SJBIT Page 69


DSP Laboratory - 18EEL67 2020-21
Output:

Result: Realization of FIR filters can be done by using frequency sampling technique successfully.

Staff Signature:

Department of EEE, SJBIT Page 70


DSP Laboratory - 18EEL67 2020-21
EXPERIMENT 12

REALIZATION OF IIR AND FIR FILTERS

Aim: To Design and realize IIR and FIR filters using Direct form, Cascade and Parallel form.

Theory: To process signals, we have to design and implement systems called filters (or spectrum
analyzers in some contexts). The filter design issue is influenced by such factors as the type of the filter
(i.e., IIR or FIR) or the form of its implementation (structures). IIR filters as designed, can be modeled
by rational system functions or, equivalently, by difference equations.
IIR FILTER STRUCTURES
The system function of an IIR filter is given by

where bn and an are the coefficients of the filter. We have assumed without loss of generality that a0 =1.
The order of such an IIR filter is called N if aN ≠ 0. The difference equation representation of an IIR
filter is expressed as

Three different structures can be used to implement an IIR filter:


1. Direct form: In this form the difference equation is implemented directly as given. There are two
parts to this filter, namely the moving average part and the recursive part (or equivalently, the numerator
and denominator parts). Therefore this implementation leads to two versions: direct form I and direct
form II structures.
2. Cascade form: In this form the system function H(z) is factored into smaller 2nd-order sections,
called biquads. The system function is then represented as a product of these biquads. Each biquad is
implemented in a direct form, and the entire system function is implemented as a cascade of biquad
sections.
3. Parallel form: This is similar to the cascade form, but after factorization, a partial fraction expansion
is used to represent H(z) as a sum of smaller 2nd-order sections. Each section is again implemented in a
direct form, and the entire system function is implemented as a parallel network of sections. IIR filters

Department of EEE, SJBIT Page 71


DSP Laboratory - 18EEL67 2020-21
are generally described using the rational form version (or the direct form structure) of the system
function.
FIR FILTER STRUCTURES
A finite-duration impulse response filter has a system function of the form

Hence the impulse response h(n) is

and the difference equation representation is

y(n) = b0x(n) + b1x(n − 1) +… + bM−1x(n −M + 1)


which is a linear convolution of finite support.

The order of the filter is M − 1, and the length of the filter (which is equal to the number of coefficients)
is M. The FIR filter structures are always stable, and they are relatively simple compared to IIR
structures.
Furthermore, FIR filters can be designed to have a linear-phase response, which is desirable in some
applications.
We will consider the following four structures:
1. Direct form: In this form the difference equation is implemented directly as given.

2. Cascade form: In this form the system function H(z) is factored into 2nd-order factors, which are
then implemented in a cascade connection.

Department of EEE, SJBIT Page 72


DSP Laboratory - 18EEL67 2020-21
Matlab Code:

% IIR Filter realisation for the LTI system


% y(n)= -0.1 y(n-1) + 0.72 y(n-2)+ 0.7 x(n) - 0.252 x(n-2)
clc;
clear all;
close all;
% Grouping y(n) terms on LHS and x(n) terms on RHS
% y(n) + 0.1 y(n-1) - 0.72 y(n-2)= 0.7 x(n) + 0 x(n-1) - 0.252 x(n-2)
disp('Co-efficients for DFI & DFII form realisation');
[b]= [0.7 1 -0.252]
[a]= [1 0.1 -0.72]
% using the above co-efficients DF-I & DF-II can be obtained
disp('Co-efficients for cascade form realisation');
b0 = b(1)/a(1) % Gain co-efficient
cb= roots(b) % Roots of numerator
ca=roots(a) % Roots of denominator
% Values needed for cascade form realisation has bee obtained
disp('Co-efficients for parallel form realisation');
[r, p, k] = residue(b,a) % Parallel realisation

Department of EEE, SJBIT Page 73


DSP Laboratory - 18EEL67 2020-21
Output:

Co-efficients for DFI & DFII form realisation

b = 0.7000 1.0000 -0.2520

a = 1.0000 0.1000 -0.7200

Co-efficients for cascade form realisation

b0 =

0.7000

cb =

-1.6471

0.2186

ca =

-0.9000

0.8000

Co-efficients for parallel form realisation

r=

0.3441

0.5859

p=

-0.9000

0.8000

k=

0.7000

Department of EEE, SJBIT Page 74


DSP Laboratory - 18EEL67 2020-21
Sample Viva Questions and Answers

1. Classify the various signals?


Continuous time, continuous amplitude (Analog Signals)
Discrete time, continuous amplitude
Continuous time, discrete amplitude
Discrete time, discrete amplitude
2. What is the use of Random Signals?
Random signals are used to test dynamic response statistically for very small amplitudes and
time duration.
3. Classify the different Systems.
Linear, stable and time invariant.
4. What do you mean by aliasing in digital signal processing? How it can be avoided?
Aliasing refers to an effect due to which different signals become indistinguishable. It also refers
to distortion in the reconstructed signal when it is reconstructed from the original continuous
signal. To avoid aliasing we can simply filter out the high frequency components of the signal by
using antialiasing filter like optical antialiasing filter.
5. What are the differences between a microprocessor and a DSP processor?
DSP processors are featured to support high performance and repetitive and intensive tasks
whereas microprocessors are not application specific and they are designed to process control
oriented tasks.
6. What is the convolution?
Convolution is the technique of adding two signals in time domain. We can also do this unite
easily by changing the domain of signals from time domain to frequency domain using Fast
Fourier Transform (FFT).
7. What is FFT?
FFT is a fast way to calculate Discrete Fourier Transform (DFT). It is much more efficient then
DFT and require less number of coding lines. Due to FFT several kind of techniques are feasible.

8. What is a Digital Signal Processor (DSP)?

Department of EEE, SJBIT Page 75


DSP Laboratory - 18EEL67 2020-21
Microprocessor specifically designed to perform fast DSP operations (e.g., Fast Fourier
Transforms, inner products, Multiply & Accumulate). Good at arithmetic operations
(multiplication/division). Mostly programmed with Assembly and C through Integrated
Development Environment (IDE).

9. What is interpolation and decimation?

Interpolation is the process of increasing the sample rate in dsp whereas decimation is the
opposite of this that is, it is the process of decreasing the sample rate in dsp.
10. Define Signal.
A signal is defined as any physical quantity that varies with time, space or any other independent
variable or variables.
11. Define system.
A system is a physical device (i.e., hardware) or algorithm (i.e., software) that performs an
operation on the system.
12. What is static and dynamic system? Give examples.
A discrete time system is called static (or memory less), if its output at any instant depends on
the input sample at the same time (but does not depend on past or future samples). If the
response depends on past depends on past or future samples, then the system is called dynamic
system.
13. What is analog signal?
The analog signal is a continuous function of an independent variable such as time, space etc. the
analog signal is defines for every instant of the independent variable and so the magnitude of
analog signal is continuous in the specified range. Here both the magnitude of the signal and the
independent variable are continuous.
14. Define unit step sequence.
The discrete-time unit step sequence u(n) is defined as:

Department of EEE, SJBIT Page 76


DSP Laboratory - 18EEL67 2020-21
Annexure – Additional Experiments
Experiment 1: Study of sampling theorem, effect of under sampling.
Introduction
This experiment enables a student to learn
1. How to view the real life analog signal with an oscilloscope.
2. How to set the amplitude, frequency and phase of the signal source.
3. How to set the sampling frequency of the source such that the signal is exactly reconstructed
from its samples.
The principal objective of this experiment is to understand the principle of sampling of
continuous time analog signal.

Procedure
1.Click on the Experiment tab SIMULATOR It will open the workspace.
2.See the movie in experiment page by pressing help button ? to understand how the following steps are
to be executed.
3.In the workspace click on Browse Blocks BROWSE BLOCKS.to understand how the following steps
are to be executed.
4.Drag Sinewave Generator in the left side of the workspace. Click it to parameterize the sinusoidal
signal output. Make amplitude = 3V, frequency = 19 Hz, phase=0 angle.

5.Drag Sampling Block in the workplace. Place it after Sinewave Generator to its right. Click it to
parameterize. Make sampling frequency =40 Hz. It will show no. of sample as 80 for display. The
display is conformed for 2 sec.

Department of EEE, SJBIT Page 77


DSP Laboratory - 18EEL67 2020-21

6.Drag the scope in the workspace after sampler to its right.


7.For making connection, take the cursor to the node provided in each block where form connection is
to be made. Click on it a circle will appear in the background. If pointed properly a yellowish tinge will
appear click at that time to enable connection.

8.Connect sinewave generator O/P to I/O of sampler by clicking at both blocks and a link will appear.
9.Similarly connect sampler O/P to one of the I/O of scope.
10.Click somewhere in the middle of the link connecting signal generator & sampler. Keep clicking at
bends till you connect it to the other input of the scope.

Department of EEE, SJBIT Page 78


DSP Laboratory - 18EEL67 2020-21
11.Click on the scope, a new window will appear. It shows that the sinusoidal signal as well as sample
in red dots.

12.Move the cursor from one sample to another by draging the slider provide along the x-axis and you
will find sample values in the boxes at bottom left part of the window. Note the first 15 values in a note
book. This 15 values will go as table 1 in your report.
13.In this window, you can change parameters like frequency, amplitude and phase angle of sinusoidal
signal generator & sampling frequency.
14.Change the sinusoidal signal generator O/P amplitude to 1.5V and note 1st 15 values. This will form
Table 2 in your report. In your observation & discussion part of the reporting, you have to compare
Table 1 and Table 2.
15.Change sampling frequency to 43 Hz and amplitude of sinusoidal signal generator at 1V. Note first
15 readings to from Table 3. Compare Table 1 and Table 3
16.Make sampling frequency 40Hz, amplitude of sinusoidal signal generator 10V, phase=30. Table 1st
reading to from Table 4. Compare Table 1 and Table 4.

Department of EEE, SJBIT Page 79


DSP Laboratory - 18EEL67 2020-21
17.Under sampling:Change the Sinusoidal signal generator frequency 50Hz, amplitude 1.4V, phase=0.
Sampling frequency= 40 Hz. Take 1st 15 readings. This will from Table 5. Compare Table 1 and Table
5.
18.Change the Sinusoidal signal generator frequency=30Hz, Amplitude=1V, Phase=0, sampling
frequency = 40Hz. Take 15 readings. This will produce Table 6. Compare Table 1 and Table 6
19.Optional: Select sinusoidal signal generator frequency and sampling frequency simultaneously to
form more Table, Table 7 and Table 8 which leads to nyquist rate from observation phenomenon.
20.Click on Report Generation button. The reporting window will appear. Here on by clicking Add
Table , you can add tables. In each tables, by clicking add row will be able to add row.
21.For each table make a screenshot of your plots by taking print screen and edit in any image editor to
upload a image for each table.
22.The observation and discussion box, appears at the end. Then click Yes I have finished my
Experiment button to submit your report.
23.Note: if at any time during making circuit if you face any problem please use reset button to erase the
circuit and draw the circuit freshly.

Department of EEE, SJBIT Page 80


DSP Laboratory - 18EEL67 2020-21
Experiment 2: Study of FIR filter design using window method: Lowpass and highpass filter.
Introduction
This experiment enables a student to learn
• Basics of filter designs and different types of filter designing techniques.
• Different types of window functions.
• Designing of Lowpass and highpass FIR filters using these window functions

Procedure
1.Click on the simulator tab SIMULATOR It will open the workspace.
2.See the movie in experiment page by pressing help button to understand how the different steps, as
mentioned next, are to be executed..
3.User controls like filter selection, no. of samples in the ideal filter in time domain, cutoff frequency,
window length (filter order) and window type selection are given to compare the effect of different
windows for designing FIR filters.
4.In this experiment we have provided two types of filters Lowpass and Highpass filters. The sampling
frequency is set to 5000 Hz, you can vary cut-off frequency (fa) from 1000 Hz to 3000 Hz and window
length (Filter order M) from 0 to N. You can choose following window functions: Rectangular, Barlett,
Hamming, Hanning, Blackman and Kaiser windows. If you choose Kaiser window function there is a
parameter called a through which you can change shape of the Kaiser window.

Fig-1
5.Graph 1 plots the impulse response of the filter (Green) in time domain with a time shift of M/2 and
Window function (Red) . A slider is given along the x-axis by this you can read sample by sample value
for impulse response of the filter

Department of EEE, SJBIT Page 81


DSP Laboratory - 18EEL67 2020-21

Fig-2
6.Graph 2 plots the magnitude spectrum of FIR filter scale the x-axis range from 0 to 5000 Hz. Here
also a slider is provided along the x-axis by which you can read the values at different frequencies. A
check box is provided at right top corner on Graph 2 to switch between magnitude spectrum in Decibel
and absolute value.

Fig-3
7.Graph no. 3 showing the input signal before and after passing through the filter. In the same graph you
can observe the frequency response of the output signal just by checking the checkbox provides

Department of EEE, SJBIT Page 82


DSP Laboratory - 18EEL67 2020-21

Fig-4

Fig-5
8.While changing the window function you can see the window function expressions on the top right
half of the experiment space. Similarly a text box is provide on the top right hand side from this you can
copy the final filter difference equation to design your filter on hardware or software.

Department of EEE, SJBIT Page 83


DSP Laboratory - 18EEL67 2020-21

Fig-6
9.You can choose between sine or square signal as input signal and select input signal frequency.
10.When experiment loads first time the default values are set in all user controls. Initially for the
Lowpass filter fs = 5000 Hz, fa = 1000 Hz and M = 50. Rectangular window is being selected by
default. Square wave is the input by default and its frequency if 100 Hz.
11.Keeping filter cutoff frequency constant change the input signal frequency and note down input
signal and output signal peak to peak values. Which will give you table no. 1 for your report generation.
12.Similarly, choose Highpass filter and repeat the step 10 -12 to note down another set of data which
will give you Table no. 2.
13.Now change the window function and do the earlier steps which will give you number of tables.
14.From each table of data you have input and output signal peak to peak values, compute the gain in
decible and plot the gain Vs frequency plot which will give you the same plot what you have with filter
frequency response (Graph 2).
15.Use take snapshot button to take the screenshot of the experiment space.
16.Complete the Observation and discuss the results in report generation.
17.Then click Yes I have finished my Experiment button to submit your report.

Department of EEE, SJBIT Page 84


DSP Laboratory - 18EEL67 2020-21
Experiment 3: Study of Infinite Impulse Response (IIR) filter.
Introduction
This experiment enables a student to learn
• Basics of IIR filter designing and its implimentation.
• Filter designing techniques like Butterworth, Chebyshev 1, Chebyshev 2, Elliptic etc.
Procedure
1.Click on the Simulator tab SIMULATOR. It will open the workspace.
2.See the movie in experiment page by pressing help button ? to understand how the following steps
are to be executed.
3.In this experiment we have provided two types of filters Lowpass and Highpass filter. The sampling
frequency is set to 700 Hz.
4.We have provided user controls on top left side of the simulation screen. Here you have an option to
change filter design technique like Butterworth, Chebyshiv 1 etc. You can change filter passband and
stopband frequency from the dropdown menu also the passband ripple and stopband attenuation. You
can choose between filter frequency response or pole zero plot.

fig-1
5.Graph 1 plots the filter frequency response and pole zero plot. Two radio buttons has been provided to
change the plot from frequency response to pole zero plot vice versa. For frequency response plot you
can choose between magnitude or phase response or both by selecting the corresponding checkbox
given below the plot. In pole zero plot mode you will get the filter coefficients you can use these filter
coefficients for filter designing in any other software or programming.

Department of EEE, SJBIT Page 85


DSP Laboratory - 18EEL67 2020-21

fig-2
6.Here we have taken sum of two sinusoidals of different frequencies (60Hz & 190Hz) as input so that
user can easily understand the filter operation by choosing appropriate cutoff frequencies.
7.Graph 2 and 3 plots input and output signal respectively and also the corresponding frequency domain
plots. Here we have given two radio buttons so that user can change from time domain to frequency
domain plot.

Department of EEE, SJBIT Page 86


DSP Laboratory - 18EEL67 2020-21

fig-3 Input and output signal time domain plots.

Department of EEE, SJBIT Page 87


DSP Laboratory - 18EEL67 2020-21

fig-4 Input and output signal frequency domain plots


8.Select lowpass filter and Butterworth filter design observe the filter frequency response, pole zero
positions by changing cutoff frequency. Simultaneously see the output and compare with input signal.
Write the discussion in the report portion of this experiment. Similarly change the filter design to
remaining three types of designing techniques and change the cutoff frequency and observe the output.
9.Now change the passband ripple and stopband attenuation to another values and observe the results.
10.Repeat the step 8 and 9 for high pass also and observe the output signal and compare with input
signal.
11.Use take snapshot button to take the screenshot of the experiment space
12.Complete the Observation and discuss the results in report generation.
13.Then click Yes I have finished my Experiment button to submit your report

Department of EEE, SJBIT Page 88

You might also like