You are on page 1of 110

BMS INSTITUTE OF TECHNOLOGY& MANAGEMENT

DEPARTMENT OF TELECOMMUNICATION ENGINEERING

DSP LAB - 15ECL57


MANUAL

Compiled by

Mrs Thejaswini S. Assistant Professor


&
Mrs. Surekha.R.Gondkar. Associate Professor

Name: ...........................................................................

Class: .............................................................................

Semester: ........................................................................

Batch:.............................................................................
Mission and Vision of the Institution
Institute Vision

To emerge as one of the finest technical institutions of higher learning, to


develop engineering professionals who are technically competent, ethical and
environment friendly for betterment of the society.

Institute Mission

Accomplish stimulating learning environment through high quality academic


instruction, innovation and industry-institute interface.

Mission and Vision of the Department

Department Vision

Emerge as premier department developing high quality


Telecommunication Engineering professionals with ethics and eco
friendliness for the betterment of the society.

Department Mission

Impart quality education in Telecommunication Engineering by


facilitating:
 Conducive learning environment and research activities
 Good communication skills, leadership qualities and ethics
 Strong Industry-Institute interaction

Program Educational Objectives (PEOs):


After three to four years of graduation our graduates will:
PEO 1: Excel as Professionals in Telecommunication, Electronics and IT
related fields.
PEO 2: Engage in life-long learning.
PEO 3: Maintain ethical norms, exhibit good communication skills and
leadership qualities. .
Program Specific Outcomes(PSOs):
At the end of graduation our graduates will be able to:
PSO 1: Analyze and Design Communication Systems
PSO 2: Analyze and implement signal processing applications.
PSO 3: Design and implement embedded systems

Course Objectives of DSP LAB:

At the end of the course, the students will be able to attain the following
skills.

Write code to carry out various operations on signals and verify them
CO1
using OCTAVE software
CO2 Evaluate DFT’S of different signals and verify properties of DFT
CO3 Understand the implementation of Digital filter (IIR & FIR)
Simulate the programs and execute them on the DSP Starter Kit using
CO4
Code Composer Studio Software tool.
CO5 Write a Report on the Conducted experiment and answer for viva-voce

References

1. Sanjeet Mitra, Digital signal processing using MATLAB, TMH, 2001


2. J.G.Proakis & Ingale, Digital signal processing using, MATLAB,.MGH, 2000
3. B.Venkataramani and Bhaskar, Digital signal processors, TMH,2002
4. Udaykumar.S, Signals and systems
SYALLABUS
B.E., V Semester, EC/TC
[As per Choice Based Credit System (CBCS) scheme]

Subject Code 15ECL57 IA Marks 20


Number of Lecture 01Hr Tutorial (Instructions) Exam Marks 80
Hours/Week + 02 Hours Laboratory=03
Exam Hours 3 hrs
CREDITS – 02
Laboratory Experiments
PART –A
Following Experiments to be done using MATLAB / SCILAB / OCTAVE or
equivalent:
1. Verification of sampling theorem.
2. Linear and circular convolution of two given sequences, Commutative, distributive and
associative property of convolution.
3. Auto and cross correlation of two sequences and verification of their properties
4. Solving a given difference equation.
5. Computation of N point DFT of a given sequence and to plot magnitude and phase
spectrum (using DFT equation and verify it by built-in routine).
6. (i) Verification of DFT properties (like Linearity and Parsevals theorem, etc.)
(ii) DFT computation of square pulse and Sinc function etc.
7. Design and implementation of FIR filter to meet given specifications (using different
window techniques).
8. Design and implementation of IIR filter to meet given specifications.
PART-B
Following Experiments to be done using DSP kit
9. Linear convolution of two sequences
10. Circular convolution of two sequences
11. N-point DFT of a given sequence
12. Impulse response of first order and second order system
13. Implementation of FIR filter
Conduct of Practical Examination:
1. All laboratory experiments are to be included for practical examination.
2. Strictly follow the instructions as printed on the cover page of answer script for breakup of
marks.
3. Change of experiment is allowed only once and Marks allotted to the procedure part to be
made zero.
1. Introduction to OCTAVE 1
2. Signal Processing Basics Using OCTAVE 3
3. Introduction to CCS 95
CONTENTS
SLNO. EXPERIMENT PAGE NO.
PART A : EXPERIMENTS USING OCTAVE
1 Verification of sampling theorem. 8
2 2.1 Linear convolution of two given sequences 15
2.2 Circular Convolution of two sequences 19
2.3 Commutative, distributive and associative property of 25
Convolution
2.4 Commutative, distributive and associative property of 28
Circular convolution.
3 3.1 Autocorrelation of a given sequence and verification of its 31
properties.
3.2 Cross correlation of given sequences and verification of 37
its properties.
4 Solving a given difference equation. 42
5 Computation of N point DFT of a given sequence and to plot 51
magnitude and phase spectrum.
6 6.1 Verification of DFT properties (like Linearity and 58
Parsevals theorem, etc.)
6.2 DFT computation of square pulse and Sinc function etc 65
7 Design and implementation of FIR filter to meet given 68
specifications
8 Design and implementation of IIR filter to meet given 79
specifications.
PART B : EXPERIMENTS USING DSP PROCESSOR
9 Linear convolution of two given sequences. 96
10 Circular convolution of two given sequences. 97
11 Computation of N- Point DFT of a given sequence 98
12 Impulse response of first order and second order system 100
13 Realization of an FIR filter 102
CYCLE OF EXPERIMENTS

CYCLE 1 : EXPERIMENTS USING OCTAVE


1. Verification of sampling theorem.
2. Linear and circular convolution of two given sequences, Commutative, distributive
and
associative property of convolution.
3. Auto and cross correlation of two sequences and verification of their properties
4. Solving a given difference equation.
5. Computation of N point DFT of a given sequence and to plot magnitude and phase
spectrum (using DFT equation and verify it by built-in routine).
6. (i) Verification of DFT properties (like Linearity and Parsevals theorem, etc.)
(ii) DFT computation of square pulse and Sinc function etc.
7. Design and implementation of FIR filter to meet given specifications (using different
window techniques).
8. Design and implementation of IIR filter to meet given specifications

CYCLE 2 :EXPERIMENTS USING DSP PROCESSOR


1. Linear convolution of two given sequences.
2. Circular convolution of two given sequences.
3. Computation of N- Point DFT of a given sequence
4. Impulse response of first order and second order system
I. INTRODUCTION TO OCTAVE
GNU Octave is a high-level interpreted language, primarily intended
or numerical computations. It provides capabilities for the numerical
solution of linear and nonlinear problems, and for performing other
numerical experiments. It also provides extensive graphics capabilities for
data visualization and manipulation. Octave is normally used through its
interactive command line interface, but it can also be used to write non-
interactive programs. The Octave language is quite similar to OCTAVE so
that most programs are easily portable.
Octave has extensive tools for solving common numerical linear
algebra problems, finding the roots of nonlinear equations, integrating
ordinary functions, manipulating polynomials, and integrating ordinary
differential and differential-algebraic equations. It is easily extensible and
customizable via user-defined functions written in Octave's own language,
or using dynamically loaded modules written in C++, C, Fortran, or other
languages.
GNU Octave is also freely redistributable software. You may
redistribute it and/or modify it under the terms of the GNU General Public
License (GPL) as published by the Free Software Foundation.
Octave was written by John W. Eaton and many others. Because
Octave is free software you are encouraged to help make Octave more
useful by writing and contributing additional functions for it, and by
reporting any problems you may have.

QTOCTAVE
QTOCTAVE is a front-end user interface for the Octave.
It simplifies the use of Octave for user who are not familiar with the
original, command line based version of Octave.

Department of Telecommunication Page 1


Fig. A – Screen Shot of QtOctave (A GUI for GNU Octave)
BASIC STEPS TO RUN A PROGRAM USING QTOCTAVE
1. Start QtOctave
Make sure necessary packages are installed. You can see the list of packages installed,
by typing the command “pkg list” in the octave terminal.
2. Type the program
You can use either the command line interface of octave terminal or editor window.
1. Using editor window.
This is always preferred when you have a set of commands to be executed. Open the
editor window and type your program in the editor. Save the file as <filename.m>
format (this will be the default format). (You may not observe syntax highlighting
in the beginning – this seems to be a bug. Next time you restart the QtOctave syntax
highlighting will work).
3. Run the Program.
In the editor window, open Run>run or use F5 to run the program.
4. View the result in the octave terminal of figure windows depending on your
program.

Department of Telecommunication Page 2


II. SIGNAL PROCESSING BASICS USING OCTAVE/ OCTAVE
A computer or digital signal processor can process only discrete signal. When
the signal is discrete in time and amplitude (high or low) we call it, digital signal.
To process the analog signal as it is, without digitizing, the computers require
infinite memory and processing capacity.
I. Discrete Time Signals.
A discrete time signal will be denoted by x(n) where n is integer value and it
represents discrete instances in time.
x(n) = {x(n)} = { . . ., x(-2), x(-1), x(0), x(1), x(2), . . . }
here the range of n is. . . -2,-1,0,1,2, ….
In Octave/OCTAVE, we can represent a finite duration sequence by a row vector
of appropriate values. However such vector does not have information on sample
position n. For a proper representation along with sample position we need to
define n also along with x.
eg : x(n) = {1,5,6,2,3,5,4,2}
Where denotes the position n = 0
in octave we can write the program as follows
x=[1 5 6 2 3 5 4 2]
n=[-3 -2 -1 0 1 2 3 4]
to view the waveform :
stem (n,x) : (the x coordinate variable first and y coordinate variable second),
the output will be as shown in the figure.

Figure 2. discrete time signal with proper time index

Department of Telecommunication Page 3


In the above example if we do not explicitly use n
eg : stem (x) , the result will be as sown below . Please note that the time index
starts from n = 1,2,3, …....

Figure 3. discrete time signal without explicit time index

note: an arbitrary infinite duration sequence cannot be represented in octave due


to the finite memory limitations.
II. Unit sample sequence
general form:
n=[n1:n2];
x=[(n-n0) == 0]
Eg :
>>> n=[-5:5]
n = -5 -4 -3 -2 -1 0 1 2 3 4 5
>>> x=[(n-0) == 0]
x=00000100000 //impulse at n = 0
>>> x=[(n-2) == 0]
x=00000001000 //impulse at n = 2
>>> x=[(n+3) == 0]
x=00100000000 //impulse at n = -3
>>>

Department of Telecommunication Page 4


III. Unit step sequence
general form:
n=[n1:n2];
x=[(n-n0) >= 0]
Eg :
>>> n=[-6:5]
n = -6 -5 -4 -3 -2 -1 0 1 2 3 4 5
>>> x=[(n-0) >= 0]
x=000000111111 //step sequence from n = 0
>>> x=[(n-3) >= 0]
x=000000000111 //step sequence from n = 3
>>> x=[(n+5) >= 0]
x=011111111111 //step sequence from n = -5

III. Sinusoidal sequence


example 1 : x(n)=2sin(0.05 π n) , 0<= n <= 50
program :
>>> n=[0:50];
>>> x=2*sin(0.05*pi*n);
>>> stem (x);
//when you terminate a line with semicolon the result will not be
displayed in the terminal window.

example 2: x(n)=3cos(0.1 π n + π /3) + 2sin(0.5 π n),


0<= n <= 10
program :
>>> n=[0:10]
n = 0 1 2 3 4 5 6 7 8 9 10
>>> x=3*cos(0.1*pi*n+pi/3) + 2*sin(0.5*pi*n)
x = 1.50000 2.62374 -0.31359 -3.22021 -2.00739 -0.59808 -2.93444 -
4.98357 -2.74064 -0.22943 -1.50000

Department of Telecommunication Page 5


Figure 5. sinusoidal signal for example 2

Department of Telecommunication Page 6


PART I

EXPERIMENTS USING

OCTAVE

Department of Telecommunication Page 7


EXPERIMENT No 1
VERIFICATION OF SAMPLING THEOREM

Aim: To verify sampling theorem for a signal of given frequency.


Theory:
 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 (resulting in x (t ) ) is the process of
reconstruction. (D/A converter)
 Sopme techniques for reconstruction-
o ZOH (zero order hold) interpolation results in a staircase
waveform, is implemented by OCTAVE plotting function
stairs(n,x),
o FOH (first order hold) where the adjacent samples are joined by
straight lines is implemented by OCTAVE plotting function
plot(n,x),

 For x (t ) to be exactly the same as x(t), sampling theorem in the generation of
x(n) from x(t) is used. The sampling frequency fs determines the spacing
between samples.
 Aliasing-A high frequency signal is converted to a lower frequency, results
due to under sampling. Though it is undesirable in ADCs, it finds practical
applications in stroboscope and sampling oscilloscopes.
Algorithm:
1. Input the desired frequency fd (for which sampling theorem is to be verified).
2. Generate an analog signal xt of frequency fd for comparison.
3. Generate over sampled, nyquist & under sampled discrete time signals.
4. Plot the waveforms and hence prove sampling theorem.

Department of Telecommunication Page 8


OCTAVE Implementation:
Step 1. OCTAVE can generate only discrete time signals. For an approximate analog
signal x(t), choose the spacing between the samples very small (≈0), say 50µs =
0.00005.
Next choose the time duration, say xt exists for 0.05seconds.(tfinal in program) (for
low frequency say <1000 Hz choose 0.05 secs, for higher choose 0.01 secs or lesser as
appropriate).
Now begin with the vector that represents the time base-
t = 0:0.00005:0.05;
The colon (:) operator in OCTAVE creates a vector, in the above case a time
vector running from 0 to 0.05 in steps of 0.00005. The semicolon (;) tells OCTAVE
not to display the result.
Given t, the analog signal xt of frequency fd is generated as
(sin(ωt)=sin(2πft))-
xt=sin(2*pi*fd*t);
pi is recognized as 3.14 by OCTAVE.
Step 2 - To illustrate over-sampling condition, choose sampling frequency fs0 = 2.2 *
fd . For this sampling rate T0=1/fs0, generate the time vector as n1 = 0:T0:0.05;

& over sampled discrete time signal x1 = sin(2*pi*fd*n1);


[Let n1 be a fixed number of samples, say n = 0:10; & x1 = sin(2*pi*n*fd/fs0);]
The discrete signal is converted back to analog signal (reconstructed) using the
OCTAVE plot function (FOH). In one plot both the analog signal (approximate one in
blue color) and the reconstructed signal (in red) are plotted for comparison
Step 3:
Repeat step 2 for different sampling frequencies, i.e.,
Fs = 1.3*fd for under-sampling

Fs = 2*fd for Nyquist sampling conditions.

OCTAVE Program:

Department of Telecommunication Page 9


tfinal=0.05;
t=0:0.00005:tfinal;
fd=input('Enter analog frequency');
%define analog signal for comparison
xt=cos(2*pi*fd*t);
%simulate condition for under sampling i.e., fs1<2*fd
fs1=1.3*fd;
%define the time vector
n1=0:1/fs1:tfinal;
%Generate the under sampled signal
xn=cos(2*pi*n1*fd);
%plot the analog & sampled signals
subplot(3,1,1);
plot(t,xt,'b',n1,xn,'r*-');
title('undersampling plot');
%condition for Nyquist plot
fs2=2*fd;
n2=0:1/fs2:tfinal;
xn=cos(2*pi*fd*n2);
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;
xn=cos(2*pi*fd*n3);
subplot(3,1,3);
plot(t,xt,'b',n3,xn,'r*-');
title('Oversampling plot');
xlabel('time');
ylabel('amplitude');
legend('analog','discrete');

Department of Telecommunication Page 10


Result:
Enter analog frequency 200
The plots are shown in Fig.1.1
Inference:
1. From the under sampling plot observe the aliasing effect. The analog signal is
of 200Hz (T=0.005s). The reconstructed (from under sampled plot) is of a
lower frequency. The alias frequency is computed as
fd - fs1 = 200-1.3*200 = 200-260= -60Hz
This is verified from the plot. The minus sign results in a 180˚ phase shift.
(For example: 3kHz & 6kHz sampled at 5kHz result in aliases of -2kHz (3k-
5k) & 1kHz (6k-5k) respectively)
2. Sampling at the Nyquist rate results in samples sin(πn) which are identically
zero, i.e., we are sampling at the zero crossing points and hence the signal
component is completely missed. This can be avoided by adding a small phase
shift to the sinusoid. The above problem is not seen in cosine waveforms
(except cos(90n)). A simple remedy is to sample the analog signal at a rate
higher than the Nyquist rate. The Fig1.2 shows the result due to a cosine
signal (x1=cos(2*pi*fd*n1);
3. The over sampled plot shows a reconstructed signal almost similar to that of
the analog signal. Using low pass filtering the wave form can be further
smoothened.

Fig1. Plots of a sampled cosine wave of 200Hz in time domain

Department of Telecommunication Page 11


Further Self Study: Generate sine / cosine waveforms of multiple frequencies, say
x1=sin(2*pi*fd1*n1)+ sin(2*pi*fd2*n2); where fd1, fd2 are 2 different frequencies.

Exercise : Analyse the following programs in OCTAVE.

Program 1. Nyquist rate


fnyq=6800;fs=fnyq;
t=0.0:1/fs:0.2 % t from 0 to 0.2 in steps of 1/fs
x=cos(2*pi*300*t)+cos(2*pi*1300*t)+cos(2*pi*3400*t)
xk=fft(x)
f=[0:length(xk)-1]*fs/length(xk);
figure(1);
plot(f,abs(xk))
grid;
zoom on;
xlabel('FREQUENCY')
ylabel('MAGNITUDE')
title('Sampling at Nyquist Rate')

Program 2 over sampling


fnyq=6800; fs=fnyq*2;
t=0.0:1/fs:0.2 % t from 0 to 0.2 in steps of 1/fs
x=cos(2*pi*300*t)+cos(2*pi*1300*t)+cos(2*pi*3400*t)
xk=fft(x)
f=[0:length(xk)-1]*fs/length(xk);
figure(2);
plot(f,abs(xk))
grid;
zoom on;
xlabel('FREQUENCY')
ylabel('MAGNITUDE')
title('Sampling at twice the Nyquist Rate')

Department of Telecommunication Page 12


Program 3 Under sampling
fnyq=6800;fs=fnyq/2;
t=0.0:1/fs:0.2 % t from 0 to 0.2 in steps of 1/fs
x=cos(2*pi*300*t)+cos(2*pi*1300*t)+cos(2*pi*3400*t)
xk=fft(x)
f=[0:lenght(xk)-1]*fs/lenght(xk);
figure(3);
plot(f,abs(xk))
grid;
zoom on;
xlabel('FREQUENCY')
ylabel('MAGNITUDE')
title('Sampling at half the Nyquist Rate')

Program 4
fs=8000;
t=0.0:1/fs:0.2 % t from 0 to 0.2 in steps of 1/fs
x=sin(2*pi*4000*t)
xk=fft(x);
m=abs(xk);
f=[0:length(xk)-1]*fs/length(xk);
subplot(3,1,1)
plot(f,m)
grid;
zoom on;
xlabel('FREQUENCY')
ylabel('MAGNITUDE')
title('Sampling at Nyquist Rate')

fs=16000;
t=0.0:1/fs:0.2 % t from 0 to 0.2 in steps of 1/fs
x=sin(2*pi*4000*t)
xk=fft(x);

Department of Telecommunication Page 13


m=abs(xk);
f=[0:length(xk)-1]*fs/length(xk);
subplot(3,1,2)
plot(f,m)
grid;
zoom on;
xlabel('FREQUENCY')
ylabel('MAGNITUDE')
title('Sampling at twice the Nyquist Rate')

fs=2000;
t=0.0:1/fs:0.2 % t from 0 to 0.2 in steps of 1/fs
x=sin(2*pi*4000*t)
xk=fft(x);
m=abs(xk);
f=[0:length(xk)-1]*fs/length(xk);
subplot(3,1,3)
plot(f,m)
grid;
zoom on;
xlabel('FREQUENCY')
ylabel('MAGNITUDE')
title('Sampling at half the Nyquist Rate')

Viva Questions
1. Define plot, stem, and subplot in OCTAVE.
2. List all the functions used in the above program.
3. Briefly explain sampling theorem.
4. What is nyquist rate?
5. What is aliasing?

Department of Telecommunication Page 14


EXPERIMENT NO. 2
2.1 LINEAR CONVOLUTION OF TWO GIVEN SEQUENCES
Aim: To obtain convolution of two finite duration sequences.
Theory:
 The output y[n] of a LTI (linear time invariant) system can be obtained by
convolving the input x[n] with the system’s impulse response h[n].
 
 The convolution sum is y[ n]  x[n]  h[n]   x[k ]h[n  k ]   x[n  k ]h[k ]
k   k  

 x[n] and h[n] can be both finite or infinite duration sequences.


 Even if one (or both) of the sequences is infinite (say, h[n]  0.9n u[n] ), we can
analytically evaluate the convolution formula to get a functional form (closed
form solution) of y[n].
 If both the sequences are of finite duration, then we can use the OCTAVE
function ‘conv’ to evaluate the convolution sum to obtain the output y[n].
Convolution is implemented as polynomial multiplication (refer OCTAVE help).
 The length of y[n] = xlength + hlength -1.
 The conv function assumes that the two sequences begin at n=0 and is invoked by
y=conv(x,h).
 Even if one of the sequences begin at other values of n, say n=-3,or n=2; then we
need to provide a beginning and end point to y[n] which are
ybegin=xbegin+hbegin and yend=xend+hend respectively.

Algorithm:
1. Input the two sequences as x1, x2
2. Convolve both to get output y.
3. Plot the sequences.

OCTAVE Implementation:
OCTAVE recognizes index 1 to positive maximum. Index 0 is also not recognized. The
timing information for a sequence is provided by another vector, say n=-3:5; creates a
vector with values from -3 to 5 with an increment of 1.

Department of Telecommunication Page 15


During plotting, the time vector size and the sequence size should be the same, i.e., the
number of elements in the sequence x1 and in its time vector n1 should be the same.
Similarly for x2 and y.
Further Self Study: Without using the inbuilt conv function a OCTAVE program can be
written.
OCTAVE Program:

Level 1 Program (both sequences are right sided)

%main part of computation


x1=input('Enter first sequence')
x2=input('Enter second sequence')
y=conv(x1,x2);
disp('linear con of x1 & x2 is y=');
disp(y);
%graphical display part
subplot (2,1,1);
stem(y);
xlabel('time index n');
ylabel('amplitude ');
title('convolution output');
subplot(2,2,3);
stem(x1);
xlabel('time index n');
ylabel('amplitude ');
title('plot of x1');
subplot(2,2,4);
stem(x2);
xlabel('time index n');
ylabel('amplitude ');
title('plot of x2');

Department of Telecommunication Page 16


Result
Enter first sequence [1 2 3 4]
x1 = 1 2 3 4
Enter second sequence[ 2 3 2 4]
x2 = 2 3 2 4
linear con of x1 & x2 is
y= 2 7 14 25 26 20 16

Fig. 3.1 shows the plots

Level 2 Program (Two sided sequences)

%main part of computation


%first sequence
x1=[1 2 3 2 1 3 4]
%time vector of first seq
n1=-3:3
%second seq
x2=[ 2 -3 4 -1 0 1]
%time vector of second seq
n2=-1:4
%add first elements of time vector
ybegin=n1(1)+n2(1);
%add last elements of time vector
yend=n1(length(x1))+n2(length(x2));
ny=[ybegin:yend];
y=conv(x1,x2);
disp('linear con of x1 & x2 is y=');
disp(y);
%graphical display with time info
subplot (2,1,1);
stem(ny,y);
xlabel('time index n');

Department of Telecommunication Page 17


ylabel('amplitude ');
title('convolution output');
subplot(2,2,3);
stem(n1,x1);
xlabel('time index n');
ylabel('amplitude ');
title('plot of x1');
subplot(2,2,4);
stem(n2,x2);
xlabel('time index n');
ylabel('amplitude ');
title('plot of x2');

Result
x1 =1 2 3 2 1 3 4
n1 =-3 -2 -1 0 1 2 3
x2 =2 -3 4 -1 0 1
n2 =-1 0 1 2 3 4
linear con of x1 & x2 is
y = 2 1 4 2 6 9 3 2 15 -3 3 4
Fig.3.2 shows the plots

Fig.3.1 For right sided sequences (Without time information)

Department of Telecommunication Page 18


Fig.3.2 For two sided sequences (With time information)

2.2 CIRCULAR CONVOLUTION OF TWO GIVEN SEQUENCES

Aim: To obtain circular convolution of two finite duration sequences.


Theory:
 As seen in the last experiment, the output y[n] of a LTI (linear time invariant)
system can be obtained by convolving the input x[n] with the system’s impulse
response h[n].
 The above linear convolution is generally applied to aperiodic sequences.
Whereas the Circular Convolution is used to study the interaction of two signals
that are periodic.
 The linear convolution sum is
 
y[ n]  x[n]  h[n]   x[k ]h[n  k ]   x[n  k ]h[k ] .
k   k  

To compute this equation, the linear convolution involves the following


operations:
o Folding- fold h[k] to get h[-k] ( for y[0])
o Multiplication – vk[n] = x[k] × h[n-k] (both sequences are multiplied
sample by sample)
o Addition- Sum all the samples of the sequence vk[n] to obtain y[n]
o Shifting – the sequence h[-k] to get h[n-k] for the next n.

Department of Telecommunication Page 19



 The circular convolution sum is y[n]  x[n] N h[n]   x[k ]h[ n  k N
] where
k  

the index n  k N
implies circular shifting operation and  k N
implies folding

the sequence circularly.


 Steps for circular convolution are the same as the usual convolution, except all
index calculations are done "mod N" = "on the wheel".
o Plot f [m] and h [−m] as shown in Fig. 4.1. (use f(m) instead of x(k))
o Multiply the two sequences
o Add to get y[m]
o "Spin" h[−m] n times Anti Clock Wise (counter-clockwise) to get h[n-m].
 x[n] and h[n] can be both finite or infinite duration sequences. If infinite
sequences, they should be periodic, and the N is chosen to be at least equal to the
period. If they are finite sequences N is chosen as >= to max(xlength, hlength).
Whereas in linear convolution N>= xlength+hlength-1.
 Say x[n]  {2,3, 1,1} and N = 5, then the x[-1] and x[-2] samples are plotted at

x[N-1] = x[-4] and x[N-2] =x[-3] places. Now x[n] is entered as


x[n]  {1,1,0,2,3} .

Fig. 4.1 Plotting of f(m) and h(-m) for circular convolution


Algorithm:
1. Input the two sequences as x and h.
2. Circularly convolve both to get output y.
3. Plot the sequences.

OCTAVE Implementation:
OCTAVE recognizes index 1 to be positive maximum. Index 0 is not recognized.
Hence in the below program wherever y, x and h sequences are accessed, the index is
added with +1. the modulo index calculation for circular convolution is carried out using
the function - MOD Modulus (signed remainder after division). MOD(x,y) is x -
y.*floor(x./y) if y ~= 0. By convention, MOD(x,0) is x.The input x and y must be real

Department of Telecommunication Page 20


arrays of the same size, or real scalars. MOD(x,y) has the same sign as y while REM(x,y)
has the same sign as x. MOD(x,y) and REM(x,y) are equal if x and y have the same sign,
but differ by y if x and y have different signs.

OCTAVE PROGRAM:

Program 1: To find the circular convolution of two sequences of same length

x=[1 2 3 4];
h=[1 2 3 4];
N=length(x);
%Compute the output
for n=0:N-1

y(n+1)=0;

for k=0:N-1
i=mod((n-k),N);

if i<0
i=i+N;

end
y(n+1)=y(n+1)+h(k+1)*x(i+1);

end
end
disp('circular convolution of x & h is y=');
disp(y);
%plot
n1=0:N-1;
stem(n1,y);
title('Circular convolution output y(n)');

RESULT

circular convolution of x & h is y=


26 28 26 20

Department of Telecommunication Page 21


Circular convolution output y(n)
30

25

20

15

10

0
0 0.5 1 1.5 2 2.5 3

Fig 4.1 Circular Convoluted signal

Program 2: Circular convolution using Matrix Method


clc;
clear all;
close all;
x=input('enter the first sequence');
h=input('enter the second sequence');
y=x*h;
disp('circular convolution');
disp(y);
stem(y);
xlabel('n');
ylabel('y(n)');
grid on;

RESULT:
Note: The second sequence is entered in matrix(circulated) form

enter the first sequence[1 2 3 4]


enter the second sequence[1 2 3 4;4 1 2 3;3 4 1 2;2 3 4 1]
circular convolution
26 28 26 20
30

25

20
y(n)

15

10

0
1 1.5 2 2.5 3 3.5 4
n

Fig 4.2 Circular convolution using matrix method

Department of Telecommunication Page 22


Program 2 : Alternate Method (Generalized Case of matrix method)
clc
x=input('enter the first input sequence');
h=input('enter the second input sequence');
n=length(x)
c=zeros(n,'double')

for i=1:n
c(i,1)=x(i)
end
p=1; k=1;
for j=2:n
t=1;
for i=1:n
if i<j
c(i,j)=c((n-p)+t,1)
t=t+1
else
c(i,j)=c(i-k,1)
end
i
end
p=p+1
k=k+1
j
end
cnv=c*h'

Program 3: Circular Convolution Program using Zero Padding Method


clc;
clear all;
x=input('Enter first sequence ')
h=input('Enter second sequence ')
N1=length(x);
N2=length(h);
N=max(N1,N2);
N3=N1-N2;
if (N3>=0)

Department of Telecommunication Page 23


h=[h,zeros(1,N3)];
else
x=[x,zeros(1,N3)];
end
%compute the output
for r=1:N
y(r)=0;
for (s=1:N)
k=r-s+1; %calculation of x index
if (k<=0)
k=k+N;
end %end of ‘if’
y(r)=y(r)+x(s)*h(k);
end %end of inner ‘for loop’
end %end of outer ‘for loop’
disp('circular convolution of x & h is y=');
disp(y);
%plot
n1=0:N-1;
stem(n1,y);
title('Circular convolution output y(n)');

Result:

Enter first sequence [2 3 4 5]

x=

2 3 4 5

Enter second sequence [1 1 1]

h=

1 1 1

circular convolution of x & h is y=


11 10 9 12

Department of Telecommunication Page 24


Circular convolution output y(n)
12

10

0
0 0.5 1 1.5 2 2.5 3

Fig 4.3 Circular convoluted output using zero padding method

2.3 Commutative, distributive and associative property of linear convolution.

The linear convolution is the convolution process for the discrete time
systems. The convolution of discrete time (DT) signals possesses the following
properties:

Commutative Property: x(n)*h(n) = h(n)*x(n).

Associative Property: x(n)*[h1(n) *h2(n)] = [x(n)*h1(n)] *h2(n)

Distributive Property: x(n)*[h1(n) + h2(n)] = x(n)*h1(n) + x(n)*h2(n)

The definitions and interpretations of these properties are summarized below.

(1) Commutative Property


The commutative property states that the order in which two sequences
are convolved is not important. Mathematically, the commutative property
is x(n)*h(n) = h(n)*x(n).
From a systems point of view, this property states that a system with a unit
sample response h(n) and input x(n) behaves in exactly the same way as a system with
unit sample response x(n) and an input h(n). This is illustrated in Fig. 2 (a) .

Department of Telecommunication Page 25


(2) Associative Property :
The convolution operator satisfies the associative property, which is
x(n)*[h1 (n) *h2(n)] = [x(n)*h1 (n)] *h2(n)
From a systems point of view, the associative property states that if two
systems with unit sample responses h1(n) and h2(n) are connected in cascade as
shown in Fig. 2 (b), an equivalent system is one that has a unit sample response equal
to the convolution of hI (n) and h2(n): heq[n] =h1[n]*h2[n].

(3) Distributive Property :

The distributive property of the convolution operator states that


x(n)*[h1(n) + h2(n)] = x(n)*h1(n) + x(n)*h2(n). From a systems point of
view, this property asserts that if two systems with unit sample responses
h 1(n) and h2(n) are connected in parallel, as illustrated in Fig.2(c)), an
equivalent system is one that has a unit sample response equal to the sum of
h1(n) and h2(n): heq[n] =h1[n]+h2[n].

Figure 2. The interpretation of convolution properties from a systems point of view.

Department of Telecommunication Page 26


Main Program
clc;
clear all;
close all;
% inputs
x=[1 2 3 4];
h1=[2 5 6 7];
h2=[1 2 3 4];

% Commutative Property : x(n) * h1(n) =h1(n) * x(n)


y1=conv(x,h1); % y1 (n) = x(n) * h1(n)
y2=conv(h1,x); % y2 (n) = h1(n) * x(n)
if y1==y2
disp('Commutative Property is proved');
else
disp('Commutative Property is not proved');
end

%Associative Property: x(n) * [h1(n) * h2(n)] = [x(n) * h1(n)] * h2(n)


y3= conv(x,conv(h1,h2)); % y3[n] = x(n) * [h1(n) * h2(n)]
y4=conv(conv(x,h1),h2); % y4[n] = [x(n) * h1(n)] * h2(n)
if y3==y4
disp('Associative property is proved');
else
disp('associative property is not proved');
end

%Distributive Property : x(n) *[ h1(n) + h2(n)] = [x(n) * h1(n)] + [x(n) *


h2(n) ]
y5=conv(x,(h1+h2)); % y5(n) = x(n) *[ h1(n) + h2(n)]
%z1 = conv(x,h1);
%z2= conv(x,h2);
y6=(conv(x,h1)+conv(x,h2)); % y6(n) = [x(n) * h1(n)] + [x(n) * h2(n) ]

Department of Telecommunication Page 27


if y5==y6
disp('distributive property proved');
else
disp('distributive property not proved');
end

Result :

Commutative Property is proved


Associative property is proved
distributive property proved

2.4 Commutative, distributive and associative property of Circular convolution.


Main Program
clc;
clear all;
close all;
% inputs
x=[1 2 3 4];
h1=[2 5 6 7];
h2=[1 2 3 4];

% Commutative Property
y1=circonv(x,h1);
y2=circonv(h1,x);
if y1==y2
disp('Commutative Property is proved');
else
disp('Commutative Property is not proved');
end

%Associative Property
y3= circonv(x,circonv(h1,h2));
y4=circonv(circonv(x,h1),h2);
if y3==y4

Department of Telecommunication Page 28


disp ('Associative property is proved');
else
disp ('associative property is not proved');
end
%Distributive Property
y5=circonv (x,(h1+h2));
y6= ((circonv (x,h1))+(circonv (x,h2)));
if y5==y6
disp ('distributive property proved');
else
disp ('distributive property not proved');
end

Function file to compute Circular Convolution


function[y]=circonv(x,h)
N=length(x);
for n=0:N-1;
y(n+1)=0;
for k=0:N-1;
i=mod((n-k),N);
if i<0
i=i+1;
end
y(n+1)=y(n+1)+h(k+1)*x(i+1);
end
end

Note : Write the function file separately and save in the same
folder with file name as: function name with dot m (.m)
extension.

Department of Telecommunication Page 29


Result :
Commutative Property is proved
Associative property is proved
distributive property proved

Viva Questions :
1. Explain how linear convolution can be obtained.
2. Explain different properties of convolution
3. What is the length of the output sequence of linear convolution
4. Give the different methods for to find convolution
5. Write A Program to find the circular convolution of two sequences using
Matrix Method
6. Explain how circular convolution can be obtained.
7. Explain different properties of circular convolution
8. List out the difference between linear and circular convolution.
9. Why is zero padding used in circular convolution .
10. Give the different methods for performing circular convolution

Department of Telecommunication Page 30


EXPERIMENT NO. 3
AUTO AND CROSS CORRELATION OF TWO SEQUENCES AND
VERIFICATION OF THEIR PROPERTIES

3.1 Autocorrelation of the given sequence and verify its properties

Aim: To obtain autocorrelation of the given sequence and verify its properties.
Theory:
 Correlation is mathematical technique which indicates whether 2 signals are
related and in a precise quantitative way how much they are related. A measure of
similarity between a pair of energy signals x[n] and y[n] is given by the cross

correlation sequence rxy[l] defined by rxy [l ]   x[n] y[n  l ]; l  0,1,2,... .
n  

 The parameter ‘l’ called ‘lag’ indicates the time shift between the pair.

 Autocorrelation sequence of x[n] is given by rxx [l ]   x[n]x[n  l ]; l  0,1,2,...
n  

 Some of the properties of autocorrelation are enumerated below


o The autocorrelation sequence is an even function i.e., rxx [l ]  rxx [l ]

o At zero lag, i.e., at l=0, the sample value of the autocorrelation sequence has its
maximum value (equal to the total energy of the signal x) i.e.,

rxx [l ]  rxx [0]   x  x
n  
2
[ n] .

This is verified in Fig. 5.1, where the autocorrelation of the rectangular pulse
(square) has a maximum value at l=0. All other samples are of lower value. Also
the maximum value = 11 = energy of the pulse [12+12+12..].
o A time shift of a signal does not change its autocorrelation sequence. For example,
let y[n]=x[n-k]; then ryy[l] = rxx[l] i.e., the autocorrelation of x[n] and y[n] are the
same regardless of the value of the time shift k. This can be verified with a sine
and cosine sequences of same amplitude and frequency will have identical
autocorrelation functions.
o For power signals the autocorrelation sequence is given by
k
1
rxx [l ]  lim
k  2k  1
 x[n]x[n  l ]; l  0,1,2,... and for periodic signals with
n k

Department of Telecommunication Page 31


N 1
1
period N it is rxx [l ] 
N
 x[n]x[n  l ]; l  0,1,2,...
n 0
and this rxx[l] is also

periodic with N. This is verified in Fig. 5.3 where we use the periodicity property
of the autocorrelation sequence to determine the period of the periodic signal y[n]
which is x[n] (=cos(0.25*pi*n)) corrupted by an additive uniformly distributed
random noise of amplitude in the range [-0.5 0.5]
Algorithm:
1. Input the sequence as x.
2. Use the ‘xcorr’ function to get auto correlated output r.
3. Plot the sequences.
OCTAVE Implementation:
OCTAVE has the inbuilt function XCORR(A), when A is a vector, is the auto-
correlation sequence. If A is of length M vector (M>1), then the xcorr function returns
the length 2*M-1 auto-correlation sequence. The zeroth lag of the output correlation
is in the middle of the sequence at element M.
XCORR(...,MAXLAG) computes the (auto/cross) correlation over the range of lags:
-MAXLAG to MAXLAG, i.e., 2*MAXLAG+1 lags. If missing, default is MAXLAG
= M-1.
[C,LAGS] = XCORR(...) returns a vector of lag indices (LAGS).

OCTAVE Programs
Computation of Autocorrelation of rectangular Sequence [with out in-built function]
n = -5:5;
N=10;
%Generate the square sequence
x = ones(1,11);
%Compute the correlation sequence
r = conv(x,fliplr(x));
disp('autocorrelation sequence r=');
disp(r);
%plot the sequences
subplot(2,1,1)
stem(n,x);

Department of Telecommunication Page 32


title('square sequence');
subplot(2,1,2)
k = -N:N;
stem(k, r);
title('autocorrelation output');
xlabel('Lag index');
ylabel('Amplitude');
disp('auto-correlation r=');
disp(r);
% to calculate the Energy of the input signal
e=sum(x.^2);
disp('Maximum value of energy =');
disp(e);
disp('Maximum value of energy is at the zero lag index');
disp('The auto-correlated sequence is a even sequence');

Computation of Autocorrelation of rectangular Sequence [with in-built function]


n = -5:5;
N=10;
%Generate the square sequence
x = ones(1,11);
%Compute the correlation sequence
r=xcorr(x);
disp('autocorrelation sequence r=');
disp(r);
%plot the sequences
subplot(2,1,1)
stem(n,x);
title('square sequence');
subplot(2,1,2)
k = -N:N;
stem(k, r);
title('autocorrelation output');
xlabel('Lag index');
ylabel('Amplitude');
disp('auto-correlation r=');

Department of Telecommunication Page 33


disp(r);
% to calculate the Energy of the input signal
e=sum(x.^2);
disp('Maximum value of energy =');
disp(e);
disp('Maximum value of energy is at the zero lag index');
disp('The auto-correlated sequence is a even sequence');

Result:
The output plot is in Fig.5.1
Autocorrelation sequence r = 1.0000 2.0000 3.0000 4.0000
5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000
10.0000 9.0000 8.0000 7.0000 6.0000 5.0000 4.0000
3.0000 2.0000 1.0000
Maximum value of energy = 11

Maximum value of energy is at the zero lag index


The auto-correlated sequence is a even sequence

Fig.5.1 Auto-correlated sequence for rectangular pulses

Inference: Following autocorrelation properties are verified


1. Max peak of 11 (=energy of the pulse) at zero lag (l=0)
2. The autocorrelation sequence is an even function

Department of Telecommunication Page 34


Exercise problem: Modify the above program for a given input x(n) =[1 2 3 4]
Computation of Autocorrelation of noise corrupted sinusoidal sequence
N = 30; %number of points of input
n = 1:N;
% Generate the sinusoidal sequence
x = cos(pi*0.25*n);
% Generate the noise sequence
d = rand(1,N) - 0.5;
%Generate the noise-corrupted sinusoidal sequence
y = x + d;
% Compute the autocorrelation sequence of corrupted signal
r = conv(y, fliplr(y));
[ry,lag]=xcorr(y);
% Compute the autocorrelation sequence of noise
[rn,lag1]=xcorr(d);
%plot the sequences
figure(1)
subplot(3,1,1)
stem(x);
title('reference sinusoidal sequence');
subplot(3,1,2)
stem(y)
title('noise corrupted signal');
subplot(3,1,3)
stem(lag, r);
title('autocorrelation seq of corrupted signal');
xlabel('Lag index'); ylabel('Amplitude');
figure(2);
stem(lag1,rn);
title('autocorrelation seq of noise');
xlabel('Lag index'); ylabel('Amplitude');

Result: Plots in Fig.5.2 & 5.3

Department of Telecommunication Page 35


Fig. 5.2 Auto-correlation of noise sequence

Fig. 5.3 Auto-correlation of sequences


Inference 2: The plot of noise corrupted signal y[n] (Fig. 5.3) does not show any
noticeable periodicity, but its autocorrelation sequence has distinct peaks at lags that
are multiples of 8,indicating thaty[n] is a periodic with N=8 (cos(0.25*pi*n)=cos(wn)
implies w=2*pi*f; N= 1/f = 2/0.25 =8). Also notice the strong peak at zero lag. From
Fig.5.2, the autocorrelation of the noise component has a very strong peak only at
zero lag. The amplitudes are considerably smaller at other values of the lag as the
sample values (of noise generated by rand function) are uncorrelated with each other.

Department of Telecommunication Page 36


3.2 Cross Correlation Of A Given Sequence And verification of Its Properties
Aim: To obtain cross correlation of the given sequence and verify its properties.
Theory:
 Cross Correlation has been introduced in the last experiment. Comparing the
equations for the linear convolution and cross correlation we find that
 
rxy [l ]   x[n] y[n  l ] 
n  
 x[n] y[(l  n)]  x[l ]  y[l ] . i.e., convolving the
n  

reference signal with a folded version of sequence to be shifted (y[n]) results


in cross correlation output. (Use ‘fliplr’ function for folding the sequence for
correlation).
 The properties of cross correlation are 1) the cross correlation sequence
sample values are upper bounded by the inequality
rxx [l ]  rxx [0]ryy [0]   x  y

2) The cross correlation of two sequences x[n] and y[n]=x[n-k] shows a peak
at the value of k. Hence cross correlation is employed to compute the exact
value of the delay k between the 2 signals. Used in radar and sonar
applications, where the received signal reflected from the target is the delayed
version of the transmitted signal (measure delay to determine the distance of
the target).
3) The ordering of the subscripts xy specifies that x[n] is the reference
sequence that remains fixed in time, whereas the sequence y[n] is shifted w.r.t
x[n]. If y[n] is the reference sequence then ryx [l ]  rxy [ l ] . Hence ryx[l] is

obtained by time reversing the sequence rxy[l].


%Verification of the properties
%Small OCTAVE code
xi=[1,2,3,-1,0,0];
x2=[0,0,1,2,3,-1];
%=xr[n-2],i.e., k=2
>> xcorr(xi,x2)
ans = {1,1,5,15,5, 1,1,0,0,0,0}

Inference:
Strong peak of 15 at lag = -2 implies the delay between xi and x2 is 2.

Department of Telecommunication Page 37


Also peak =15=energy of xi (1+22+32+(-1)2) implies that both xi and x2 are strongly
correlated.

%consider the below sequences


xr=[1,2,3,-1];
x2=[3,-1,1,2];
xcorr(xr,x2) ans = {2,5,7, 2,2,10,3}

xcorr(x2,xr) ans = {3,10,2, 2,7,5,2}


Inference:
Strong peak of 10 at lag = 2 implies the delay between xr and x2 is 2, but since
10<15, it implies that xr and x2 are uncorrelated slightly (may be due to noise,etc).
ryx [l ]  rxy [ l ] is verified.

Algorithm:
1. Input the sequence as x and y.
2. Use the ‘xcorr’ function to get cross correlated output r.
3. Plot the sequences.
OCTAVE Implementation:
OCTAVE has the inbuilt function XCORR: Say C = XCORR(A,B), where A and B
are length M vectors (M>1), returns the length 2*M-1 cross-correlation sequence C. If
A and B are of different length, the shortest one is zero-padded. Using convolution to
implement correlation, the instruction is FLIPLR Flip matrix in left/right direction.
FLIPLR(X) returns X with row preserved and columns flipped in the left/right
direction. X = 1 2 3 becomes 3 2 1.

Department of Telecommunication Page 38


OCTAVE Programs

Computation of Cross-correlation Sequence using folded sequence and


convolution [ without in-built function]
x = input('Type in the reference sequence = ');
y = input('Type in the second sequence = ');
% Compute the correlation sequence
n1 = length(y)-1;
n2 = length(x)-1;
r = conv(x,fliplr(y));
disp(‘Cross correlation output is =’);
disp(r);
k = (-n1):n2; %time vector for plotting
stem(k,r);
xlabel('Lag index');
ylabel('Amplitude');

Result:
Type the reference sequence = [1 -2 6 1]

Type the second sequence = [1 2 3 4]

Cross correlation output is =


4 -5 20 19 13 8 1

20

15

10
Amplitude

-5

-10
-3 -2 -1 0 1 2 3
Lag index

Department of Telecommunication Page 39


Fig.6.1 Cross correlation output using Convolution
Computation of Cross-correlation Sequence using xcorr function
x = input('Type in the reference sequence = ');
y = input('Type in the second sequence = ');
% Compute the correlation sequence
n1 = length(y)-1;
n2 = length(x)-1;
r = xcorr(x,y);
%[r,lag]=xcorr(x,y);
disp('Cross correlation output is =');
disp(r);
N=max(n1,n2) ;
k = (-N):N;
stem(k,r); %stem(lag,r);
xlabel('Lag index');
ylabel('Amplitude');

Verification: with xcorr(x,y), the output is


Type the reference sequence = [1 3 -2 1 2 -1 4 4 2]
Type the second sequence = [2 -1 4 1 -2 3]
Cross correlation output is =
Columns 1 through 9

-0.0000 -0.0000 0.0000 3.0000 7.0000 -11.0000 14.0000 13.0000 -


15.0000

Columns 10 through 17

28.0000 6.0000 -2.0000 21.0000 12.0000 12.0000 6.0000 4.0000

Department of Telecommunication Page 40


Fig.6.2 Cross correlation output using xcorr

Note: For sequences with different lengths, the length of the output using xcorr is
2*max(n1,n2)-1 = 2* 9-1=17 whereas using conv program, the length of output is
n1+n2-1.

Viva Questions
1. Explain the differences between convolution and correlation
2. List the methods for performing correlation
3. Which are the OCTAVE functions used to perform correlation
4. List out the properties of Auto-correlation.
5. Explain the following functions
rand(1,N) - 0.5
fliplr(y)
xcorr(y)
6. Explain the differences between cross correlation and autocorrelation
7. List the methods for performing cross correlation
8. List out the applications of cross correlation.
9. Which are the OCTAVE functions used to perform cross correlation

Department of Telecommunication Page 41


EXPERIMENT NO. 4
SOLVING A GIVEN DIFFERENCE EQUATION
Aim: To obtain the impulse response/step response/steady state response/response to
an arbitrary input of a system described by the given difference equation
Theory:
 A difference equation with constant coefficients describes a LTI system. For
example the difference equation y[n] + 0.8y[n-2] + 0.6y[n-3] = x[n] + 0.7x[n-
1] + 0.5x[n-2] describes a LTI system of order 3. The coefficients 0.8, 0.7, etc
are all constant i.e., they are not functions of time (n). The difference equation
y[n]+0.3ny[n-1]=x[n] describes a time varying system as the coefficient 0.3n
is not constant.
 The difference equation can be solved to obtain y[n], the output for a given
input x[n] by rearranging as y[n] = x[n] + 0.7x[n-1]+0.5x[n-2]- 0.8y[n-2]-
0.6y[n-3] and solving.
 The output depends on the input x[n]
o With x[n]= δ[n], an impulse, the computed output y[n] is the impulse
response.
o If x[n]=u[n], a step response is obtained.
o If x[n] = cos(wn) is a sinusoidal sequence, a steady state response is
obtained (wherein y[n] is of the same frequency as x[n], with only an
amplitude gain and phase shift-refer Fig.7.3).
o Similarly for any arbitrary sequence of x[n], the corresponding output
response y[n] is computed.
 The difference equation containing past samples of output, i.e., y[n-1], y[n-2],
etc leads to a recursive system, whose 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=∞). The OCTAVE function filter is used to
compute the impulse response/ step response/ response to any given x[n].
Note: The filter function evaluates the convolution of an infinite sequence
(IIR) and x[n], which is not possible with conv function (remember conv
requires both the sequences to be finite).
 The difference equation having only y[n] and present and past samples of
input (x[n], x[n-k]), represents a system whose impulse response is of finite

Department of Telecommunication Page 42


duration (FIR). The response of FIR systems can be obtained by both the
‘conv’ and ‘filter’ functions. The filter function results in a response whose
length is equal to that of the input x[n], whereas the output sequence from
conv function is of a longer length (xlength + hlength-1).

Algorithm:
1. Input the two sequences as a and b representing the coefficients of y and x.
2. If IIR response, then input the length of the response required (say 100, which
can be made constant).
3. Compute the output response using the ‘filter’ command.
4. Plot the input sequence & impulse response (and also step response, etc if
required).

OCTAVE Implementation:
OCTAVE has an inbuilt function ‘filter’ to solve difference equations numerically,
given the input and difference equation coefficients (b,a).
y=filter(b,a,x)
where x is the input sequence, y is the output sequence which is of same length as x.
Given a difference equation a0 y[n]+a1 y[n-1]+a2 y[n-2]=b0x[n]+b2 x[n-2], the
coefficients are written in a vector as b=[b0 0 b2] and a=[a0 a1 a2]. Note the zero in b
(x[n-1] term is missing).
Also remember a0, the coefficient of y[n] should always be 1.
For impulse response x[n]  {1,0,0,0,....} the number of zeros = the length of the IIR

response required (say 100 implemented by function zeros(1,99)).


For step response x[n]  {1,1,1,1,1,....} the number of ones = the length of the IIR

response required-1 (say 100 implemented by function ones(1,100).


Similarly for any given x[n] sequence, the response y[n] can be calculated.

Department of Telecommunication Page 43


Problem 1.
Find the impulse response h(n), step response, response of the system to the input
given by x(n) = 2n u(n) of an LTI system described by a linear constant coefficient
Difference equation : y(n) +0.5 y(n-1) = 2x(n)
Plot the steady state response y[n] for the input x[n]=cos(0.05πn)u(n).

OCTAVE Program
To find Impulse Response
N=input('Length of response required=');
b=[2]; %x[n] coefficient
a=[1,0.5]; %y 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)');

Result:
Length of response required=8

h= 2.0000 -1.0000 0.5000 -0.2500 0.1250 -0.0625 0.0313 -0.0156

Department of Telecommunication Page 44


impulse input
1

?(n)
0.5

0
0 1 2 3 4 5 6 7
n
impulse response
2

1
h(n)

-1
0 1 2 3 4 5 6 7
n

Fig.7.1 Impulse Response

To find step response


N=input('Length of response required=');
b=[2]; %x[n] coefficient
a=[1,0.5]; %y 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)');

Department of Telecommunication Page 45


Result:
Length of response required=10

y= 2.0000 1.0000 1.5000 1.2500 1.3750 1.3125 1.3438


1.3281 1.3359 1.3320

Plots in Fig. 7.2

step input
1
u(n)

0.5

0
0 1 2 3 4 5 6 7 8 9
n
step response
2

1.5
y(n)

0.5

0
0 1 2 3 4 5 6 7 8 9
n

Fig.7.2 Step Response


To find response of the system for the given input
N=input('Length of response required=');
b=[2]; %x[n] coefficient
a=[1,0.5]; %y coefficients
n=0:1:N-1; %time vector for plotting
x= 2.^n ; %Arbitrary input
y=filter(b,a,x) %step response
%plot the waveforms
subplot(2,1,1);
stem(n,x);
title('input sequence');
xlabel('n');

Department of Telecommunication Page 46


ylabel('x(n)');
subplot(2,1,2);
stem(n,y);
title('system response');
xlabel('n');
ylabel('y(n)');

Result:
Length of response required=5

y=

2.0000 3.0000 6.5000 12.7500 25.6250

Plots in Fig. 7.3

input sequence
20

15
x(n)

10

0
0 0.5 1 1.5 2 2.5 3 3.5 4
n
system response
30

20
y(n)

10

0
0 0.5 1 1.5 2 2.5 3 3.5 4
n

Fig. 7.3 System response for a given input

To find steady state response


N=input('Length of response required=');
b=[2]; % x(n) coefficient
a=[1,0.5]; % 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

Department of Telecommunication Page 47


%plot the waveforms
subplot(2,1,1);
stem(n,x);
title('input sequence');
xlabel('n');
ylabel('x(n)');
subplot(2,1,2);
stem(n,y);
title('steady state response');
xlabel('n');
ylabel('y(n)');

Result:
Length of response required=25
y=
Columns 1 through 10
2.0000 0.9754 1.4144 1.0748 1.0806 0.8739 0.7386 0.5387 0.3487
0.1385
Columns 11 through 20
-0.0693 -0.2782 -0.4789 -0.6685 -0.8413 -0.9936 -1.1213 -1.2214 -1.2914
-1.3297
Columns 21 through 25
-1.3352 -1.3078 -1.2482 -1.1579 -1.0391

Plots in Fig. 7.4


input sequence
1

0.5
x(n)

-0.5

-1
0 5 10 15 20 25
n
steady state response
2

1
y(n)

-1

-2
0 5 10 15 20 25
n

Fig. 7.4 Steady state response to a Cosine input

Department of Telecommunication Page 48


Further Self Study:
Use [h,t]=impz(b,a,N) to find the impulse response and find the difference between
y~=h’ (=0).
Using impz, we need not specify the impulse input x[n], just the coefficients and
length N that we desire (refer experiment 2).
Solve the difference equation with initial conditions.
Find the first 8 samples of the complete response y[n] for the difference equation

y[n] 
1
x[n]  x[n  1]  x[n  2]  0.95 y[n  1]  0.9025y[n  2]; n≥0;
3
where
 n 
x[ n]  cos u[n]
 3 
and the initial conditions are
y[-1]=-2; y[-2]=-3; x[-1]=1; x[-2]=1.

Solution: Use OCTAVE function filter(b,a,x,xic), where xic are the initial conditions
computed as xic=filtic(b,a,Y,X), where Y=[y(-1) y(-2)…y(N-1)] & X=[x(-1) x(-
2)…x(-M)] are the initial condition arrays. If x[n]=0 for n<0, then X need not be
specified.

Department of Telecommunication Page 49


The sample program for the above problem is:
b=[1/3 1/3 1/3];
a=[1 -0.95 0.9025]; %difference equation coefficient arrays
Y=[-2 -3]; X=[1 1]; %initial condition arrays
xic=filtic(b,a,Y,X);
n=0:7; %For 8 output samples using filter
function, the input should also be of size 8
x=cos(pi*n/3); %input sequence
y=filter (b,a,x,xic); %output response
Answer: y = 1.8075 4.3555 2.8398 -1.5664 -4.7176 -3.4014
1.3596 5.0281

Questions
1. Explain the OCTAVE function filter
2. How to find various responses using filter function.
3. Given y(n) – 0.25y(n-1) – 0.375y(n-2) = -x(n) + 2x(n-1) find
a. Impulse response
b. Step response
c. Response of the system to the input given by x(n) = 2n u(n).
d. Steady state response.

Department of Telecommunication Page 50


EXPERIMENT NO. 5
COMPUTATION OF N POINT DFT OF A GIVEN SEQUENCE AND TO
PLOT MAGNITUDE AND PHASE SPECTRUM.

Aim: To compute DFT of the given sequence.


Algorithm:
1. Input the sequence for which DFT is to be computed.
2. Input the length of the DFT required (say 4, 8, >length of the sequence).
3. Compute the DFT using the function dft.
4. Plot the magnitude & phase spectra.

OCTAVE Implementation:
Create a OCTAVE function dft. The procedure is as follows.
The commands and functions that comprise the new function must be put in a file
whose name defines the name of the new function, with a filename extension of '.m'.
At the top of the file must be a line that contains the syntax definition for the new
function. For example, the existence of a file on disk called dft.m with body as
follows
function[Xk] =dft(xn,N)
n=[0:1:N-1];
k=[0:1:N-1];
WN=exp(-j*2*pi/N);
nk=n'*k;
WNnk=WN.^nk;
Xk=xn*WNnk;
defines a new function called dft that calculates the DFT of a sequence xn. The
variables within the body of the function are all local variables
In the Main body of the program call this function to calculate the DFT.
The magnitude spectrum is computed using the function abs (Absolute value).
abs(x) is the absolute value of the elements of x. When x is complex, abs(x) is the
complex modulus (magnitude) of the elements of x.
The phase spectrum is computed using the function angle (Phase angle). angle (h)
returns the phase angles, in radians, of a matrix with complex elements.

Department of Telecommunication Page 51


OCTAVE PROGRAMS --
Program:
Program 1: N point DFT and IDFT of a given sequence
clc;
clear all;
xn=input('enter the sequence x(n)');
N=input('enter the number of points of computation ');
y=fft(xn,N);
disp(y);
mag=abs(y);
phase=angle(y);
subplot(2,2,1);
stem(xn);
title('Input Sequence x(n)');
subplot(2,2,2)
stem(mag);
xlabel('K');
ylabel('MAGNITUDE');
title('magnitude plot');
subplot(2,2,3)
stem(phase);
xlabel('K');
ylabel('phase');
title('phase plot');
z=ifft(y,N);
mag1=real(z);
subplot(2,2,4);
stem(mag1);
title('signal sequence constituted from spectrum');

Department of Telecommunication Page 52


RESULT:

enter the sequence x(n)[1 1 1 1 0 0 0 0]


enter the number of points of computation 8

Columns 1 through 6

4.0000 1.0000 - 2.4142i 0 1.0000 - 0.4142i 0 1.0000 +


0.4142i

Columns 7 through 8

0 1.0000 + 2.4142i

Plots in Fig. 8.1

Input Sequence x(n) magnitude plot


1 4

3
MAGNITUDE

0.5 2

0 0
0 2 4 6 8 0 42 6 8
K
phase plot signal sequence constituted from spectrum
2 1

1
phase

0 0.5

-1

-2 0
0 2 4 6 8 0 2 4 6 8
K

Fig.8.1 DFT and IDFT

Department of Telecommunication Page 53


Program : N point DFT of a given sequence using a function file dft

clc;
clear all;
xn=input('enter the sequence x(n) = ');
N=input('enter the number of points of computation = ');
y=dft(xn,N);
disp(y);
mag=abs(y);
phase=angle(y);
subplot(2,2,1);
stem(xn);
title('Input Sequence x(n)');
subplot(2,2,2);
stem(mag);
xlabel('K');
ylabel('MAGNITUDE');
title('magnitude plot');
subplot(2,2,3)
stem(phase);
xlabel('K');
ylabel('phase');
title('phase plot');
Note : Write the function file separately and save in the same folder with file
name as: function name with dot m (.m) extension.
function[xk]=dft(xn,N)
n=[0:1:N-1];
k=[0:1:N-1];
WN=exp(-j*2*pi/N);
nk=n'*k;
WNnk=WN.^nk;
xk=xn*WNnk;

Here name of the function is dft and save it as dft.m

Department of Telecommunication Page 54


RESULT:

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


enter the number of points of computation = 8

Columns 1 through 3
4.0000 1.0000 - 2.4142i -0.0000 - 0.0000i

Columns 4 through 6
1.0000 - 0.4142i 0 - 0.0000i 1.0000 + 0.4142i

Columns 7 through 8
0.0000 - 0.0000i 1.0000 + 2.4142i

Plots in Fig. 8.2

Input Sequence x(n) magnitude plot


1 4

3
MAGNITUDE

0.5 2

0 0
0 2 4 6 8 0 2 4 6 8
K
phase plot
2

0
phase

-2

-4
0 2 4 6 8
K

Fig. 8.2 DFT

Program 3 : N point IDFT of a given sequence using a function file idft


clc;
clear all;
XK=input('enter the sequence X(K) = ');
N=input('enter the number of points of computation = ');
y=idft(XK,N);
disp(y);
mag=abs(y);
phase=angle(y);
subplot(2,1,1);
stem(mag);

Department of Telecommunication Page 55


xlabel('K');
ylabel('MAGNITUDE');
title('magnitude plot');

subplot(2,1,2)
stem(phase);
xlabel('K');
ylabel('phase');
title('phase plot');
Write the function file separately and save in the same folder with file name as:
function name with dot m (.m) extension
function[xn]=idft(XK,N)
n=[0:1:N-1];
k=[0:1:N-1];
WN=exp(-j*2*pi/N);
nk=n'*k;
WNnk=WN.^(-nk);
xn=(XK*WNnk)/N;

Here name of the function is idft and save it as idft.m

RESULT:
enter the sequence X(K) = [4 0-2i 0 0+2i]
enter the number of points of computation = 4
Columns 1 through 3

1.0000 2.0000 - 0.0000i 1.0000

Column 4
0 + 0.0000i

Plots in Fig. 8.3


magnitude plot
2

1.5
MAGNITUDE

0.5

0
1 1.5 2 2.5 3 3.5 4
K
phase plot
2

1.5
phase

0.5

0
1 1.5 2 2.5 3 3.5 4
K

Fig.8.3 IDFT

Department of Telecommunication Page 56


Inputs -
Run1: Give 4 point sequence and find 4 point DFT
Run2: Give 8 point sequence and find 8 point DFT
Run 3: Give a Sinusoidal Signal with a frequency 250 Hz with fs = 8 KHz and sample
it for 64 points and perform 64 point DFT on it.
Run 4: Give a Sinusoidal Signal with a frequency 250 Hz and 500 Hz with fs = 8 KHz
and sample it for 64 points and perform 64 point DFT on it.

Viva Questions
1. Explain DFT
2. What are the methods of finding DFT
3. Explain differences between DFT and FFT
4. Explain how functions can be declared in MATLB.
5. Explain OCTAVE function fft(x)
6. How can user create a OCTAVE function.

Department of Telecommunication Page 57


EXPERIMENT NO. 6
(1 ) VERIFICATION OF DFT PROPERTIES (LIKE LINEARITY &
PARSEVALS THEOREM, etc .)
(2) DFT COMPUTATION OF SQUARE PULSE AND SINC FUNCTION etc.

6. 1 VERIFICATION OF DFT PROPERTIES (LIKE LINEARITY &


PARSEVALS THEOREM, etc .)
Aim: To verify following properties of DFT
(i) Linearity
(ii) Parseval’s Theorem
(iii) Convolution Property
(i) Linearity Property :
Linearity Property
clc;
clear all;
close all;
% LINEARITY PROPERTY
% a1*x1(n) + a2*x2(n)<-->a1*X1(K) + a2*X2(K).

x1=[1,2,3,4];
x2=[1,1,1,1];
a1=1;
a2=1;

term1= a1.*x1 + a2.*x2;


LHS=fft(term1);
term2= fft(a1.*x1);
term3 = fft(a2.*x2);
RHS = term2 + term3;
if (LHS==RHS)
disp('Linearity property proved');
disp('LHS=');
disp(LHS);

Department of Telecommunication Page 58


disp('RHS=');
disp(RHS);
else
disp ('DFTs are not linear');
end

Result :
Linearity property proved
LHS=
14.0000 + 0.0000i -2.0000 + 2.0000i -2.0000 + 0.0000i -2.0000 - 2.0000i

RHS=
14.0000 + 0.0000i -2.0000 + 2.0000i -2.0000 + 0.0000i -2.0000 - 2.0000i

(ii) Parsevals Theorem :

Parsevals Property
clc;
clear all;
close all;
% Parseval's theoerm
x=[1,2,3,4];
XK =fft (x);
en1 = sum(x.^2);
en2= (1/4)* sum(abs(XK).^2);

if (en1==en2)
disp('Parevals theorem proved');
disp('Energy=');
disp(en2);

end

Department of Telecommunication Page 59


Result :
Parevals theorem proved
Energy = 30

(iii) Circular Convolution Property:

Theory:
 By multiplying two N-point DFTs in the frequency domain, we get the
circular convolution in the time domain.
N  DFT
y[n]  x[n]  h[n]   Y (k )  X (k )  H (k )
 Circular Convolution is made identical to linear convolution by choosing the
number of points in DFT as N ≥ xlength + hlength -1.
 For Circular Convolution, N = max (xlength, length).
Algorithm:
1. Input the two sequences as x1, x2
2. Compute N=max(xlength + hlength))
3. Obtain N-point DFTs (X1, X2) of both the sequences.
4. Multiply both the DFTs (Y=X1×X2).
5. Perform IDFT on Y to get y[n] (the linearly convolved sequence) in time
domain.
6. Verify using the ‘conv’ command.
7. Plot the sequences.

Circular Convolution Program using DFT


x1=[1 2 3 4];
x2= [1 2 3 4];
N=max(length(x1),length(x2));

%obtain DFTs
X1= fft(x1,N);
X2= fft(x2,N);

%Perform vector multiplication


y= X1.*X2;

Department of Telecommunication Page 60


%ifft to get y[n]
yn= ifft(y,N);
disp('circular convolution of x1 &x2 is yn=');
disp(yn);
%plot
stem(yn);
title('Circular convolution output y(n)');

Result:
circular convolution of x1 &x2 is yn= 26 28 26 20
Circular convolution output y(n)
30

25

20

15

10

0
1 1.5 2 2.5 3 3.5 4

Fig.10.1 Circular convolution using DFT

(iv) circular shift Property :

Circular shift Property


% Circular shift Property

x = [3 1 5 2 4]';

N = length(x); % N: data length


W = exp(2*pi/N*sqrt(-1));
X = fft(x);
k = [0:N-1]'; % frequency index
m = 2; % shift
G = W.^(-m*k) .* X;

Department of Telecommunication Page 61


g = ifft(G);
disp ('the circular shifted version of x = ');
disp(g);

Result :

the circular shifted version of x =


2.0000 - 0.0000i
4.0000 + 0.0000i
3.0000 - 0.0000i
1.0000 - 0.0000i
5.0000 + 0.0000i
(v) Time Reversal Property:
Time Reversal Property
%Time reversal Property

clc;clear all;
close all;
N=5;
n = 0:N-1;
x = [3 6 2 4 7]';
X = fft(x);
G = X(mod(-n,N)+1);
g = ifft(G);
disp ('the Time reverse of x = ');
disp(g);

Result :
the Time reverse of x =
3
7
4
2
6

Department of Telecommunication Page 62


(vi) Complex Conjugation Property of The DFT

COMPLEX CONJUGATION PROPERTY OF THE DFT


clc; clear all; close all
N = 5;
n = 0:N-1;
i = sqrt(-1);
x = [3+2*i 5-3*i 6+i 4-7*i 8+9*i];
Xk = fft(x);
Gk = conj(Xk(mod(-n,N)+1));
g = ifft(Gk);
disp('X(n) =');
disp(x);
disp ('X*(n)=');
disp(g);

Result :
X(n) =
3.0000 + 2.0000i 5.0000 - 3.0000i 6.0000 + 1.0000i 4.0000 - 7.0000i 8.0000 +
9.0000i

X*(n)=
3.0000 - 2.0000i 5.0000 + 3.0000i 6.0000 - 1.0000i 4.0000 + 7.0000i 8.0000 -
9.0000i

(Vii) Real X[N] Property of The DFT :


COMPLEX CONJUGATION PROPERTY OF THE DFT
clc; clear all; close all
N = 5;
n = 0:N-1;
i = sqrt(-1);
x = [3+2*i 5-3*i 6+i 4-7*i 8+9*i];
Xk = fft(x);
Gk = conj(Xk(mod(-n,N)+1));
g = ifft(Gk);

Department of Telecommunication Page 63


disp('X(n) =');
disp(x);
disp ('X*(n)=');
disp(g);

Result :
X(n) =
3.0000 + 2.0000i 5.0000 - 3.0000i 6.0000 + 1.0000i 4.0000 - 7.0000i 8.0000 +
9.0000i

X*(n)=
3.0000 - 2.0000i 5.0000 + 3.0000i 6.0000 - 1.0000i 4.0000 + 7.0000i 8.0000 -
9.0000i

(vii) circular symmetric property of real signals


circular symmetries present in the DFT of a real signal.
% In this case, the signal is of odd length.
clc; clear all; close all
x1 = [3 7 5 2 4 1 5]';
Xk1 = fft(x1);
y1= [real(Xk1) imag(Xk1)];
disp(' circular symmetries present in the DFT of a real odd signal= ');
disp(y1);

% the signal is of even length.


x2 = [3 7 5 2 4 1 3 2]';
Xk2 = fft(x2);
y2= [real(Xk2) imag(Xk2)];
disp(' circular symmetries present in the DFT of a real even signal= ');
disp(y2);

Department of Telecommunication Page 64


Result :
circular symmetries present in the DFT of circular symmetries present in the DFT of
a real odd signal= a real even signal=
27.0000 0 27.0000 0
3.7409 -4.5956 3.2426 -6.2426
-1.3351 -1.7780 -1.0000 -4.0000
-5.4058 4.2094 -5.2426 -2.2426
-5.4058 -4.2094 3.0000 0
-1.3351 1.7780 -5.2426 2.2426
3.7409 4.5956 -1.0000 4.0000
3.2426 6.2426

6.2 DFT COMPUTATION OF SQUARE PULSE AND SINC FUNCTION

Aim: To obtain DFT computation of square pulse and sinc function

OCTAVE Program:
To find DFT of Square pulse
clc;
clear all;
close all;
% generating square pulse
x1=[zeros(1,32),ones(1,64),zeros(1,32)];
X1K= fft(x1);
x2= fftshift(X1K);
subplot(2,1,1);
stem(x1);
title('time domain');
subplot(2,1,2);
plot(abs(x2));
title('frequency domain');

Result :

Department of Telecommunication Page 65


To find DFT of SINC function
% generating a sinc function
x= -pi : 0.05 : pi;
x1=sinc(x);
X1K=fft(x1);
x2=fftshift(X1K);
subplot(2,1,1);
stem(x1);
title('time domain');
subplot(2,1,2);
plot(abs(x2));
title('frequency domain');

Department of Telecommunication Page 66


Viva Questions
1. State all properties of DFT.
2. Define sinc function
3. Define pulse signals

Department of Telecommunication Page 67


EXPERIMENT NO. 7
DESIGN AND IMPLEMENTATION OF FIR FILTER TO MEET GIVEN
SPECIFICATIONS
Aim: To design and implement a FIR filter for given specifications.
Theory: There are two types of systems – Digital filters (perform signal filtering in
time domain) and spectrum analyzers (provide signal representation in the frequency
domain). The design of a digital filter is carried out in 3 steps- specifications,
approximations and implementation.
DESIGNING AN FIR FILTER (using window method):
Method I: Given the order N, cutoff frequency fc, sampling frequency fs and the
window.
 Step 1: Compute the digital cut-off frequency Wc (in the range -π < Wc < π,
with π corresponding to fs/2) for fc and fs in Hz. For example let fc=400Hz,
fs=8000Hz
Wc = 2*π* fc / fs = 2* π * 400/8000 = 0.1* π radians
For OCTAVE the Normalized cut-off frequency is in the range 0 and 1, where
1 corresponds to fs/2 (i.e.,fmax)). Hence to use the OCTAVE commands
wc = fc / (fs/2) = 400/(8000/2) = 0.1
Note: if the cut off frequency is in radians then the normalized frequency is
computed as
wc = Wc / π
 Step 2: Compute the Impulse Response h(n) of the required FIR filter using
the given Window type and the response type (lowpass, bandpass, etc). For
example given a rectangular window, order N=20, and a high pass response,
the coefficients (i.e., h[n] samples) of the filter are computed using the
OCTAVE inbuilt command ‘fir1’ as
h =fir1(N, wc , 'high', boxcar(N+1));
Note: In theory we would have calculated h[n]=hd[n]×w[n], where hd[n] is the
desired impulse response (low pass/ high pass,etc given by the sinc function)
and w[n] is the window coefficients. We can also plot the window shape as
stem(boxcar(N)).
Plot the frequency response of the designed filter h(n) using the freqz function and
observe the type of response (lowpass / highpass /bandpass).

Department of Telecommunication, BMSIT Page 68


Method 2: Given the pass band (wp in radians) and Stop band edge (ws in radians)
frequencies, Pass band ripple Rp and stopband attenuation As.
 Step 1: Select the window depending on the stop-band attenuation required.
Generally if
As>40 dB, choose Hamming window. (Refer table )
 Step 2: Compute order N based on the edge frequencies as Transition
bandwidth = tb=ws-wp; N=ceil (6.6*pi/tb);
 Step 3: Compute the digital cut-off frequency Wc as Wc=(wp+ws)/2. Now
compute the normalized frequency in the range 0 to 1 for OCTAVE as
wc=Wc/pi;
Note: In step 2 if frequencies are in Hz, then obtain radian frequencies (for
computation of tb and N) as wp=2*pi*fp/fs, ws=2*pi*fstop/fs, where fp, fstop and fs
are the passband, stop band and sampling frequencies in Hz
 Step 4: Compute the Impulse Response h(n) of the required FIR filter using N,
selected window, type of response(low/high,etc) using ‘fir1’ as in step 2 of
method 1.

IMPLEMENTATION OF THE FIR FILTER


1. Once the coefficients of the FIR filter h[n] are obtained, the next step is to
simulate an input sequence x[n], say input of 100, 200 & 400 Hz (with
sampling frequency of fs), each of 20/30 points. Choose the frequencies such
that they are >, < and = to fc.
2. Convolve input sequence x[n] with Impulse Response, i.e., x (n)*h (n) to
obtain the output of the filter y[n]. We can also use the ‘filter’ command.
3. Infer the working of the filter (low pass/ high pass, etc).

OCTAVE IMPLEMENTATION
FIR1 Function
B = FIR1(N,Wn) designs an N'th order lowpass FIR digital filter and returns the filter
coefficients in length N+1 vector B. The cut-off frequency Wn must be between 0 <
Wn < 1.0, with 1.0 corresponding to half the sample rate. The filter B is real and has

Department of Telecommunication, BMSIT Page 69


linear phase, i.e., even symmetric coefficients obeying B(k) = B(N+2-k), k =
1,2,...,N+1.
If Wn is a two-element vector, Wn = [W1 W2], FIR1 returns an order N bandpass
filter with passband W1 < W < W2. B = FIR1(N,Wn,'high') designs a highpass filter.
B = FIR1(N,Wn,'stop') is a bandstop filter if Wn = [W1 W2]. If Wn is a multi-element
vector, Wn = [W1 W2 W3 W4 W5 ... WN], FIR1 returns an order N multiband
filter with bands
0 < W < W1, W1 < W < W2, ..., WN < W < 1.
FREQZ Digital filter frequency response. [H,W] = FREQZ(B,A,N) returns the N-
point complex frequency response vector H and the N-point frequency vector W in
radians/sample of the filter whose numerator and denominator coefficients are in
vectors B and A. The frequency response is evaluated at N points equally spaced
around the upper half of the unit circle. If N isn't specified, it defaults to 512.
For FIR filter enter A=1 and B = h[n] coefficients. Appropriately choose N as 128,
256, etc
Commonly used window function characteristics
Window Transition Width  Min. Stop band
OCTAVE
Name Approximate Exact values Attenuation Command

4 1 . 8
Rectangular 21db B
M M
=FIR1(N,Wn,boxcar)
8 6.1
Bartlett 25db
M M
B=FIR1(N,Wn,bartlett)
8 6 .2 
Hanning 44db
M M
B=FIR1(N,Wn,hanning)
8 6 .6 
Hamming 53db B=
M M
FIR1(N,Wn)

Department of Telecommunication, BMSIT Page 70


12 11
Blackman 74db
M M
B=FIR1(N,Wn,blackman)

RECTANGULAR WINDOW

clc; %high pass filter


clear all;
close all; b=fir1(n,wp,'high',y);
lp=input('enter the passband ripple:'); [h,o]=freqz(b,1,256);
ls=input('enter the stopband ripple:'); m=20*log10(abs(h));
fp=input('enter the passband subplot(2,2,2);
frequency:'); plot(o/pi,m)
fs=input('enter the stopband ylabel('gain in db');
frequency:'); xlabel('(b)normalised freq');
f=input('enter the sampling grid on;
frequency:'); %bandpass filter
wp=2*fp/f;
ws=2*fs/f; wn=[wp,ws];
num=-20*log10(sqrt(lp*ls))-13; b=fir1(n,wn,y);
sum=14.6*(fs-fp)/f; [h,o]=freqz(b,1,256);
n=ceil(num/sum); m=20*log10(abs(h));
n1=n+1; subplot(2,2,3);
if(rem(n,2)) plot(o/pi,m)
N=0; ylabel('gain in db');
n1=n; xlabel('(c)normalised freq');
n=n-1; grid on;
end %band stop filter
y=boxcar(n1); b=fir1(n,wn,'stop',y);
%low pass filter [h,o]=freqz(b,1,256);
m=20*log10(abs(h));
b=fir1(n,wp,y); subplot(2,2,4);
[h,o]= freqz(b,1,256); plot(o/pi,m)
m=20*log10(abs(h)); ylabel('gain in db');
subplot(2,2,1); xlabel('(d)normalised freq');
plot(o/pi,m) grid on;
ylabel('gain in db');
xlabel('(a)normalised freq');
grid on;

RESULT:
enter the passband ripple:0.05
enter the stopband ripple:0.04
enter the passband frequency:1500
enter the stopband frequency:2000
enter the sampling frequency:9000

Department of Telecommunication, BMSIT Page 71


50 50

0 0
gain in db

gain in db
-50 -50

-100 -100
0 0.5 1 0 0.5 1
(a)normalised freq (b)normalised freq

20 5

0 0
gain in db

gain in db
-20 -5

-40 -10

-60 -15

-80 -20
0 0.5 1 0 0.5 1
(c)normalised freq (d)normalised freq

Fig 11.1 Frequency response of rectangular window

HAMMING WINDOW

clc; %high pass filter


clear all;
close all; b=fir1(n,wp,'high',y);
lp=input('enter the passband ripple:'); [h,o]=freqz(b,1,256);
ls=input('enter the stopband ripple:'); m=20*log10(abs(h));
fp=input('enter the passband subplot(2,2,2);
frequency:'); plot(o/pi,m)
fs=input('enter the stopband ylabel('gain in db');
frequency:'); xlabel('(b)normalised freq');
f=input('enter the sampling grid on;
frequency:'); %bandpass filter
wp=2*fp/f;
ws=2*fs/f; wn=[wp,ws];
num=-20*log10(sqrt(lp*ls))-13; b=fir1(n,wn,y);
sum=14.6*(fs-fp)/f; [h,o]=freqz(b,1,256);
n=ceil(num/sum); m=20*log10(abs(h));
n1=n+1; subplot(2,2,3);
if(rem(n,2)) plot(o/pi,m)
N=0; ylabel('gain in db');
n1=n; xlabel('(c)normalised freq');
n=n-1; grid on;
end
y=hamming(n1); %band stop filter

%low pass filter b=fir1(n,wn,'stop',y);


[h,o]=freqz(b,1,256);

Department of Telecommunication, BMSIT Page 72


b=fir1(n,wp,y); m=20*log10(abs(h));
[h,o]= freqz(b,1,256); subplot(2,2,4);
m=20*log10(abs(h)); plot(o/pi,m)
subplot(2,2,1); ylabel('gain in db');
plot(o/pi,m) xlabel('(d)normalised freq');
ylabel('gain in db'); grid on;
xlabel('(a)normalised freq');
grid on;

RESULT:
enter the passband ripple:0.05
enter the stopband ripple:0.001
enter the passband frequency:1200
enter the stopband frequency:1700
enter the sampling frequency:9000

50 50

0
0
gain in db

gain in db

-50
-50
-100

-150 -100
0 0.5 1 0 0.5 1
(a)normalised freq (b)normalised freq

0 5

0
-50
gain in db

gain in db

-5

-10
-100
-15

-150 -20
0 0.5 1 0 0.5 1
(c)normalised freq (d)normalised freq

Fig 11.2 Frequency response of hamming window

Department of Telecommunication, BMSIT Page 73


BARTLETT WINDOW

clc; %high pass filter


clear all;
close all; b=fir1(n,wp,'high',y);
lp=input('enter the passband ripple:'); [h,o]=freqz(b,1,256);
ls=input('enter the stopband ripple:'); m=20*log10(abs(h));
fp=input('enter the passband subplot(2,2,2);
frequency:'); plot(o/pi,m)
fs=input('enter the stopband ylabel('gain in db');
frequency:'); xlabel('(b)normalised freq');
f=input('enter the sampling grid on;
frequency:');
wp=2*fp/f; %bandpass filter
ws=2*fs/f;
num=-20*log10(sqrt(lp*ls))-13; wn=[wp,ws];
sum=14.6*(fs-fp)/f; b=fir1(n,wn,y);
n=ceil(num/sum); [h,o]=freqz(b,1,256);
n1=n+1; m=20*log10(abs(h));
if(rem(n,2)) subplot(2,2,3);
N=0; plot(o/pi,m)
n1=n; ylabel('gain in db');
n=n-1; xlabel('(c)normalised freq');
end grid on;
y=bartlett(n1);
%band stop filter
%low pass filter
b=fir1(n,wn,'stop',y);
b=fir1(n,wp,y); [h,o]=freqz(b,1,256);
[h,o]= freqz(b,1,256); m=20*log10(abs(h));
m=20*log10(abs(h)); subplot(2,2,4);
subplot(2,2,1); plot(o/pi,m)
plot(o/pi,m) ylabel('gain in db');
ylabel('gain in db'); xlabel('(d)normalised freq');
xlabel('(a)normalised freq'); grid on;
grid on;

RESULT:
enter the passband ripple:0.05
enter the stopband ripple:0.04
enter the passband frequency:1500
enter the stopband frequency:2000
enter the sampling frequency:9000

Department of Telecommunication, BMSIT Page 74


0 0

-10
-10
gain in db

gain in db
-20
-20
-30

-40 -30
0 0.5 1 0 0.5 1
(a)normalised freq (b)normalised freq

0 2

-10 0
gain in db

gain in db
-20 -2

-30 -4

-40 -6
0 0.5 1 0 0.5 1
(c)normalised freq (d)normalised freq

Fig 11.3 Frequency response of barlet window

HANNING WINDOW

clc; %high pass filter


clear all;
close all; b=fir1(n,wp,'high',y);
lp=input('enter the passband ripple:'); [h,o]=freqz(b,1,256);
ls=input('enter the stopband ripple:'); m=20*log10(abs(h));
fp=input('enter the passband subplot(2,2,2);
frequency:'); plot(o/pi,m)
fs=input('enter the stopband ylabel('gain in db');
frequency:'); xlabel('(b)normalised freq');
f=input('enter the sampling grid on;
frequency:');
wp=2*fp/f; %bandpass filter
ws=2*fs/f;
num=-20*log10(sqrt(lp*ls))-13; wn=[wp,ws];
sum=14.6*(fs-fp)/f; b=fir1(n,wn,y);
n=ceil(num/sum); [h,o]=freqz(b,1,256);
n1=n+1; m=20*log10(abs(h));
if(rem(n,2)) subplot(2,2,3);
N=0; plot(o/pi,m)
n1=n; ylabel('gain in db');
n=n-1; xlabel('(c)normalised freq');
end grid on;
y=hanning(n1);

Department of Telecommunication, BMSIT Page 75


%low pass filter %band stop filter

b=fir1(n,wp,y); b=fir1(n,wn,'stop',y);
[h,o]= freqz(b,1,256); [h,o]=freqz(b,1,256);
m=20*log10(abs(h)); m=20*log10(abs(h));
subplot(2,2,1); subplot(2,2,4);
plot(o/pi,m) plot(o/pi,m)
ylabel('gain in db'); ylabel('gain in db');
xlabel('(a)normalised freq'); xlabel('(d)normalised freq');
grid on; grid on;

RESULT:
enter the passband ripple:0.03
enter the stopband ripple:0.01
enter the passband frequency:1400
enter the stopband frequency:2000
enter the sampling frequency:8000

50 50

0
0
gain in db

gain in db

-50
-50
-100

-150 -100
0 0.5 1 0 0.5 1
(a)normalised freq (b)normalised freq

0 5

0
-50
gain in db

gain in db

-5
-100
-10

-150 -15
0 0.5 1 0 0.5 1
(c)normalised freq (d)normalised freq

Fig 11.4 Frequency response of hanning window

Department of Telecommunication, BMSIT Page 76


KAISER WINDOW (optional)

clc; %high pass filter


clear all;
close all; b=fir1(n,wp,'high',y);
lp=input('enter the passband [h,o]=freqz(b,1,256);
ripple:'); m=20*log10(abs(h));
ls=input('enter the stopband subplot(2,2,2);
ripple:'); plot(o/pi,m)
fp=input('enter the passband ylabel('gain in db');
frequency:'); xlabel('(b)normalised freq');
fs=input('enter the stopband grid on;
frequency:');
f=input('enter the sampling %bandpass filter
frequency:');
beta=input('enter the beta value'); wn=[wp,ws];
wp=2*fp/f; b=fir1(n,wn,y);
ws=2*fs/f; [h,o]=freqz(b,1,256);
num=-20*log10(sqrt(lp*ls))-13; m=20*log10(abs(h));
sum=14.6*(fs-fp)/f; subplot(2,2,3);
n=ceil(num/sum); plot(o/pi,m)
n1=n+1; ylabel('gain in db');
if(rem(n,2)) xlabel('(c)normalised freq');
N=0; grid on;
n1=n;
n=n-1; %band stop filter
end
y=kaiser(n1,beta); b=fir1(n,wn,'stop',y);
[h,o]=freqz(b,1,256);
%low pass filter m=20*log10(abs(h));
subplot(2,2,4);
b=fir1(n,wp,y); plot(o/pi,m)
[h,o]= freqz(b,1,256); ylabel('gain in db');
m=20*log10(abs(h)); xlabel('(d)normalised freq');
subplot(2,2,1); grid on;
plot(o/pi,m)
ylabel('gain in db');
xlabel('(a)normalised freq');
grid on;

RESULT:
enter the passband ripple:0.05
enter the stopband ripple:0.04
enter the passband frequency:1500
enter the stopband frequency:2000
enter the sampling frequency:9000
enter the beta value5.8

Department of Telecommunication, BMSIT Page 77


50 50

0
0
gain in db

gain in db
-50
-50
-100

-150 -100
0 0.5 1 0 0.5 1
(a)normalised freq (b)normalised freq

0 2

-20
0
gain in db

gain in db
-40
-2
-60
-4
-80

-100 -6
0 0.5 1 0 0.5 1
(c)normalised freq (d)normalised freq

Fig 11.5 Frequency response of kaiser window

Questions
1. What is a Filter? What are its specifications?
2. Mention the types of filters.
3. Compare analog and digital filters.
4. Define a FIR filter.
5. What are the important properties of FIR filter?
6. What are the window techniques used?
7. Why the Kaiser window is important?
8. What is Gibb’s Phenomenon?
9. How do you calculate the length of Kaiser Window?
10. What are the functions used in designing a FIR filter in OCTAVE?

Department of Telecommunication, BMSIT Page 78


EXPERIMENT NO. 8
DESIGN AND IMPLEMENTATION OF IIR FILTER TO MEET GIVEN
SPECIFICATIONS

Aim: To design and implement an IIR filter for given specifications.

Theory: There are two methods of stating the specifications as illustrated in previous
program. In the first program, the given specifications are directly converted to digital
form and the designed filter is also implemented. In the last two programs the
butterworth and chebyshev filters are designed using bilinear transformation (for
theory verification).

Method I: Given the order N, cutoff frequency fc, sampling frequency fs and the IIR
filter type (butterworth, cheby1, cheby2).
 Step 1: Compute the digital cut-off frequency Wc (in the range -π < Wc < π, with
π corresponding to fs/2) for fc and fs in Hz. For example let fc=400Hz,
fs=8000Hz Wc = 2*π* fc / fs = 2* π * 400/8000 = 0.1* π radians. For OCTAVE
the Normalized cut-off frequency is in the range 0 and 1, where 1 corresponds to
fs/2 (i.e.,fmax)). Hence to use the OCTAVE commands wc = fc / (fs/2) =
400/(8000/2) = 0.1
Note: if the cut off frequency is in radians then normalized frequency is computed
as wc = Wc / π
 Step 2: Compute the Impulse Response [b,a] coefficients of the required IIR filter
and the response type (lowpass, bandpass, etc) using the appropriate butter,
cheby1, cheby2 command. For example given a butterworth filter, order N=2, and
a high pass response, the coefficients [b,a] of the filter are computed using the
OCTAVE inbuilt command ‘butter’ as [b,a] =butter(N, wc , 'high');

Method 2: Given the pass band (Wp in radians) and Stop band edge (Ws in radians)
frequencies, Pass band ripple Rp and stopband attenuation As.
 Step 1: Since the frequencies are in radians divide by π to obtain normalized
frequencies to get wp=Wp/pi and ws=Ws/pi. If the frequencies are in Hz
(note: in this case the sampling frequency should be given), then obtain

Department of Telecommunication, BMSIT Page 79


normalized frequencies as wp=fp/(fs/2), ws=fstop/(fs/2), where fp, fstop and fs
are the passband, stop band and sampling frequencies in Hz
 Step 2: Compute the order and cut off frequency as [N, wc] = BUTTORD(wp,
ws, Rp, Rs)
 Step 3: Compute the Impulse Response [b,a] coefficients of the required IIR
filter and the response type as [b,a] =butter(N, wc , 'high');

IMPLEMENTATION OF THE IIR FILTER


1. Once the coefficients of the IIR filter [b,a] are obtained, the next step is to
simulate an input sequence x[n], say input of 100, 200 & 400 Hz (with
sampling frequency of fs), each of 20/30 points. Choose the frequencies such
that they are >, < and = to fc.
2. Filter the input sequence x[n] with Impulse Response, to obtain the output of
the filter y[n] using the ‘filter’ command.
3. Infer the working of the filter (low pass/ high pass, etc).

OCTAVE IMPLEMENTATION
BUTTORD Butterworth filter order selection. [N, Wn] = BUTTORD(Wp, Ws, Rp,
Rs) returns the order N of the lowest order digital Butterworth filter that loses no
more than Rp dB in the passband and has at least Rs dB of attenuation in the
stopband. Wp and Ws are the passband and stopband edge frequencies, normalized
from 0 to 1 (where 1 corresponds to pi radians/sample). For example,
Lowpass: Wp = .1, Ws = .2 Highpass: Wp = .2, Ws = .1
Bandpass: Wp = [.2 .7], Ws = [.1 .8] Bandstop: Wp = [.1 .8], Ws = [.2 .7]
BUTTORD also returns Wn, the Butterworth natural frequency (or, the "3 dB
frequency") to use with BUTTER to achieve the specifications. [N, Wn] =
BUTTORD(Wp, Ws, Rp, Rs, 's') does the computation for an analog filter, in which
case Wp and Ws are in radians/second. When Rp is chosen as 3 dB, the Wn in
BUTTER is equal to Wp in BUTTORD.

BUTTER Butterworth digital and analog filter design. [B,A] = BUTTER(N,Wn)


designs an Nth order lowpass digital Butterworth filter and returns the filter
coefficients in length N+1 vectors B (numerator) and A (denominator). The

Department of Telecommunication, BMSIT Page 80


coefficients are listed in descending powers of z. The cutoff frequency Wn must be
0.0 < Wn < 1.0, with 1.0 corresponding to half the sample rate. If Wn is a two-
element vector, Wn = [W1 W2], BUTTER returns an order 2N bandpass filter with
passband W1 < W < W2. [B,A] = BUTTER(N,Wn,'high') designs a highpass filter.
[B,A] = BUTTER(N,Wn,'stop') is a bandstop filter if Wn = [W1 W2].
BUTTER(N,Wn,'s'), BUTTER(N,Wn,'high','s') and BUTTER(N,Wn,'stop','s') design
analog Butterworth filters. In this case, Wn is in [rad/s] and it can be greater than 1.0.

OCTAVE PROGRAMS
BUTTERWORTH FILTERS

1.Butterworth LPF
clc;
clear all;
close all;
format long
lp=input('Enter the passband attenuation:');
ls=input('Enter the stopband attenuation:');
wp=input('Enter the passband frequency:');
ws=input('Enter the stopband frequency:');
fs=input('Enter the sampling frequency:');
w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=buttord(w1,w2,lp,ls);
[b,a]=butter(n,wn);
w=0:0.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
figure(1);
subplot(2,1,1);
title('Butterworth Low Pass Filter');
plot(om/pi,m);

Department of Telecommunication, BMSIT Page 81


grid on;
ylabel('Gain in db-->');
xlabel('(a)Normalised frequency-->');
subplot(2,1,2);
plot(om/pi,an);
xlabel('(b)Normalised frequency-->');
ylabel('Phase in radians-->');
grid on;

RESULT:
Enter the passband attenuation: 0.5
Enter the stopband attenuation: 50
Enter the passband frequency: 1200
Enter the stopband frequency: 2400
Enter the sampling frequency:10000

200
Gain in db-->

-200

-400
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
(a)Normalised frequency-->

4
Phase in radians-->

-2

-4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
(b)Normalised frequency-->

Fig 12.1 Magnitude and phase plot of Butterworth LFP


1. Butterworth HPF
clc;
clear all;
close all;
format long
lp=input('Enter the passband attenuation:');

Department of Telecommunication, BMSIT Page 82


ls=input('Enter the stopband attenuation:');
wp=input('Enter the passband frequency:');
ws=input('Enter the stopband frequency:');
fs=input('Enter the sampling frequency:');
w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=buttord(w1,w2,lp,ls);
[b,a]=butter(n,wn,'high');
w=0:0.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
figure(1);
subplot(2,1,1);
title('Butterworth Low Pass Filter');
plot(om/pi,m);
grid on;
ylabel('Gain in db-->');
xlabel('(a)Normalised frequency-->');
subplot(2,1,2);
plot(om/pi,an);
xlabel('(b)Normalised frequency-->');
ylabel('Phase in radians-->');
grid on

RESULT:
Enter the passband attenuation: 0.5
Enter the stopband attenuation: 50
Enter the passband frequency: 1200
Enter the stopband frequency: 2400
Enter the sampling frequency: 10000

Department of Telecommunication, BMSIT Page 83


200

Gain in db-->
0

-200

-400
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
(a)Normalised frequency-->

4
Phase in radians-->

-2

-4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
(b)Normalised frequency-->

Fig 12.2 Magnitude and phase plot of Butterworth HFP


3. Butter worth BPF
clc;
close all;
clear all;
format long
rp=input('enter the passband ripple');
rs=input('enter the stopband ripple');
wp=input('enter the passband frequency');
ws=input('enter the stopband frequency');
fs=input('enter the sampling frequency');
w1=2*wp/fs;
w2=2*ws/fs;
[n]=buttord(w1,w2,rp,rs);
wn=[w1,w2];
[b,a]=butter(n,wn,'bandpass');
w=0:0.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);plot(om/pi,m);
ylabel('gain in db-->');

Department of Telecommunication, BMSIT Page 84


xlabel('(a) normalised frequency-->');
subplot(2,1,2);plot(om/pi,an);
xlabel('(b) normalised frequency-->');
ylabel('phase in radians-->');

RESULT:
enter the passband ripple 0.3
enter the stopband ripple 40
enter the passband frequency 1500
enter the stopband frequency 2000
enter the sampling frequency 9000

-200
gain in db-->

-400

-600

-800
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
(a) normalised frequency-->

4
phase in radians-->

-2

-4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
(b) normalised frequency-->

Fig 12.3 Magnitude and phase plot of Butterworth BPF

Department of Telecommunication, BMSIT Page 85


4. Butter worth BSF
clc;
clear all;
format long
rp=input('enter the passband ripple');
rs=input('enter the stopband ripple');
wp=input('enter the passband frequency');
ws=input('enter the stopband frequency');
fs=input('enter the sampling frequency');
w1=2*wp/fs;
w2=2*ws/fs;
[n]=buttord(w1,w2,rp,rs);
wn=[w1,w2];
[b,a]=butter(n,wn,'stop');
w=0:0.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m);
ylabel('gain in db');
xlabel('(a) normalised freq');
subplot(2,1,2);
plot(om/pi,an);
xlabel('(b) normalised freq');
ylabel('phase in radians');

RESULT:
enter the passband ripple 0.4
enter the stopband ripple 46
enter the passband frequency 1100
enter the stopband frequency 2200

Department of Telecommunication, BMSIT Page 86


enter the sampling frequency 6000

100

0
gain in db-->

-100

-200

-300
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
(a) normalised frequency-->

4
phase in radians-->

-2

-4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
(b) normalised frequency-->

Fig 12.4 Magnitude and phase plot of Butterworth BSF

CHEBYSHEV FILTERS

1. Chebyshev LPF
clc;
close all;
clear all;
format long
rp=input('enter the passband ripple');
rs=input('enter the stopband ripple');
wp=input('enter the passband frequency');
ws=input('enter the stopband frequency');
fs=input('enter the sampling frequency');
w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=cheb1ord(w1,w2,rp,rs);
[b,a]=cheby1(n,rp,wn);
w=0:0.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);plot(om/pi,m);
ylabel('gain in db-->');

Department of Telecommunication, BMSIT Page 87


xlabel('(a) normalised frequency-->');
subplot(2,1,2);plot(om/pi,an);
xlabel('(b) normalised frequency-->');
ylabel('phase in radians-->');

RESULT:
enter the passband ripple 0.2
enter the stopband ripple 45
enter the passband frequency 1300
enter the stopband frequency 1500
enter the sampling frequency 10000

0
gain in db-->

-200

-400

-600
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
(a) normalised frequency-->

4
phase in radians-->

-2

-4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
(b) normalised frequency-->

Fig 12.5 Magnitude and phase plot of Chebyshev LPF

2. Chebyshev HPF
clc;
close all;
clear all;
format long
rp=input('enter the passband ripple');
rs=input('enter the stopband ripple');
wp=input('enter the passband frequency');
ws=input('enter the stopband frequency');

Department of Telecommunication, BMSIT Page 88


fs=input('enter the sampling frequency');
w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=cheb1ord(w1,w2,rp,rs);
[b,a]=cheby1(n,rp,wn,'high');
w=0:0.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);plot(om/pi,m);
ylabel('gain in db-->');
xlabel('(a) normalised frequency-->');
subplot(2,1,2);plot(om/pi,an);
xlabel('(b) normalised frequency-->');
ylabel('phase in radians-->');

RESULT:
enter the passband ripple0.3
enter the stopband ripple60
enter the passband frequency1500
enter the stopband frequency2000
enter the sampling frequency9000

-100
gain in db-->

-200

-300

-400
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
(a) normalised frequency-->

4
phase in radians-->

-2

-4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
(b) normalised frequency-->

Fig 12.6 Magnitude and phase plot of Chebyshev HPF

Department of Telecommunication, BMSIT Page 89


3. Chebyshev BPF
clc;
clear all;
close all;
format long
lp=input('Enter the passband attenuation:');
ls=input('Enter the stopband attenuation:');
wp=input('Enter the passband frequency:');
ws=input('Enter the stopband frequency:');
fs=input('Enter the sampling frequency:');
w1=2*wp/fs;
w2=2*ws/fs;
[n]=cheb1ord(w1,w2,lp,ls);
wn=[w1 w2]
[b,a]=cheby1(n,lp,wn,'bandpass');
w=0:0.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
figure(1);
subplot(2,1,1);
title('Butterworth Low Pass Filter');
plot(om/pi,m);
grid on;
ylabel('Gain in db-->');
xlabel('(a)Normalised frequency-->');
subplot(2,1,2);
plot(om/pi,an);
xlabel('(b)Normalised frequency-->');
ylabel('Phase in radians-->');
grid on

Department of Telecommunication, BMSIT Page 90


Result:
Enter the passband attenuation:0.4
Enter the stopband attenuation:35
Enter the passband frequency:2000
Enter the stopband frequency:2500
Enter the sampling frequency:10000
wn = 0.40000000000000 0.50000000000000

0
Gain in db-->

-200

-400

-600
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
(a)Normalised frequency-->

4
Phase in radians-->

-2

-4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
(b)Normalised frequency-->

Fig 12.7 Magnitude and phase plot of Chebyshev BPF

4. Chebyshev BSF
clc;
clear all;
close all;
format long
lp=input('Enter the passband attenuation:');
ls=input('Enter the stopband attenuation:');
wp=input('Enter the passband frequency:');
ws=input('Enter the stopband frequency:');
fs=input('Enter the sampling frequency:');
w1=2*wp/fs;
w2=2*ws/fs;
[n]=cheb1ord(w1,w2,lp,ls);

Department of Telecommunication, BMSIT Page 91


wn=[w1 w2]
[b,a]=cheby1(n,lp,wn,'stop');
w=0:0.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
figure(1);
subplot(2,1,1);
title('Chebyshev bandstop Filter');
plot(om/pi,m);
grid on;
ylabel('Gain in db-->');
xlabel('(a)Normalised frequency-->');
subplot(2,1,2);
plot(om/pi,an);
xlabel('(b)Normalised frequency-->');
ylabel('Phase in radians-->');
grid on;

RESULT:
Enter the passband attenuation:0.4
Enter the stopband attenuation:46
Enter the passband frequency:1100
Enter the stopband frequency:2200
Enter the sampling frequency:6000

wn = 0.36666666666667 0.73333333333333

Department of Telecommunication, BMSIT Page 92


0

Gain in db-->
-100

-200

-300
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
(a)Normalised frequency-->

4
Phase in radians-->

-2

-4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
(b)Normalised frequency-->

Fig 12.8 Magnitude and phase plot of Chebyshev BSF

Viva Questions:
1. Define an IIR filter.
2. Compare Impulse invariance and bilinear transformations.
3. How do you convert analog filter prototype to a digital filter?
4. What are the functions used in OCTAVE for designing a digital
Butterworth and Chebyshev low pass filter using BLT?
5. Draw typical responses of Chebyshev filter for order odd & even.
6. Compare FIR & IIR filters.

Department of Telecommunication, BMSIT Page 93


PART II
EXPERIMENTS USING DSP
PROCESSOR

Department of Telecommunication, BMSIT Page 94


INTRODUCTION to CCS
Procedure
1. Double click on CCS icon
2. Create a new project: project  New
3. Type the project name (x.pjt) & store in myprojects folder
4. To open an existing project: project  open
5. Files to be added to the project: project  Add Files to project
a. c: \ CCStudio \ c6000 \ cgtools \ lib \ rts6700.lib
<Select type of file as: library>
b. c: \ ti \ tutorial \ dsk6713 \ hello1 \ hello.cmd
< Select type of file as: Linker Command file>
Note: for simulator using 5402 add the below 2 files
1) C:\CCStudio_v3.1\C5400\cgtools\lib\rts.lib
2) C:\CCStudio_v3.1\tutorial\dsk5402\datedisplay\mainapplication.cmd
2. File  New  source file  same as xx.c
( Select type of file as: C/C++ file before saving) & add this C file to your
project
3. Project  Build. (obtain Build successful)
4. To execute ; File  load program (select pjtname.out file)
5. To Run : Debug  Run
6. Observe the output on the stdout window or waveforms using graph or CRO
7. To view waveforms View  Graph changes in graph property dialog box to be
made
a. Display type  Dual (to observe 2 waveforms)
b. Title  User defined
c. Start address – upper display  x (user defined variable array names used in
C program. Note: x, y should be made global to be used by graph)
d. Start address – lower display  y
e. Acquisition Buffer size  60 (depends on the array size)
f. Display Buffer size  60
g. DSP data type – 32-bit Floating pt
h. Auto scale  ON (if off  choose max yvalue=1)

Department of Telecommunication, BMSIT Page 95


EXPERIMENT NO. 9
LINEAR CONVOLUTION OF TWO SEQUENCES

Aim: To perform linear convolution for the given sequences


 
The linear convolution sum is y[ n]  x[n]  h[n]   x[k ]h[n  k ] 
k  
 x[n  k ]h[k ]
k  

C Program

# include<stdio.h>
# include<math.h>
main()
{
float h[4] = { 2,2,2,2}; float x[4] ={1,2,3,4};
float y[10];
int xlen=4; int hlen=4;
int N=xlen+hlen-1;
int k,n;
for(n=0;n<N;n++) //outer loop for y[n] array
{
y[n]=0;
for(k=0;k<hlen;k++)
//inner loop for computing each y[n] point
{
if (((n-k)>=0) & ((n-k)<xlen))
y[n]=y[n]+h[k]*x[n-k]; //compute output
} //end of inner for loop
printf("%f\t", y[n]);
} //end of outer for loop
} //end of main

Result
6.000000 12.000000 20.000000 18.000000 14.000000 8.000000

Department of Telecommunication, BMSIT Page 96


EXPERIMENT NO. 10
CIRCULAR CONVOLUTION OF TWO GIVEN SEQUENCES
Aim: To perform circular convolution of two given sequences

 h[k ]x n  k 

The circular convolution sum is y[n]  x[n] N h[n]  N
k  

C Program

# include<stdio.h>
# include<math.h>
main()
{
float x[5]={1,2,3,4,5};
float h[5]={2,1,3,4,5};
float y[10]; //output sequence
int N=5; // N=max of xlen and hlen//
int k,n,i;
for(n=0;n<N;n++) //outer loop for y[n] array
{
y[n]=0;
for(k=0;k<N;k++) //inner loop for computing each y[n] point
{
i=(n-k)%N; //compute the index modulo N
if(i<0) //if index is <0, say x[-1], then convert to x[N-1]
i=i+N;
y[n]=y[n]+h[k]*x[i]; //compute output
} //end of inner for loop
printf("%f\t",y[n]);
} //end of outer for loop
} //end of main

Result
41.000000 51.000000 51.000000 46.000000 36.000000

Department of Telecommunication, BMSIT Page 97


EXPERIMENT NO. 11
COMPUTATION OF N- POINT DFT OF A GIVEN SEQUENCE

Aim: To compute the N (=4/8/16) point DFT of the given sequence

Theory:
The N point DFT of discrete time signal x[n] is given by the equation
N -1  j 2kn

X (k )   x[n]e N
; k  0,1,2,....N - 1
n 0

Where N is chosen such that N  L , where L=length of x[n]. To implement using C


 j 2kn
 2kn   2kn 
program we use the expression e N
 cos   j sin  and allot memory
 N   N 
space for real and imaginary parts of the DFT X(k)

C Program

#include <stdio.h>
#include <math.h>
main()
{
float y[16]; //for 8 point DFT to store real & imaginary
float x[4]={1,3,2,5}; //input only real sequence
float w;
int n,k,k1,N=8,xlen=4;
for(k=0;k<2*N;k=k+2)
{
y[k]=0; y[k+1]=0; //initialize real & imag parts
k1=k/2; //actual k index
for(n=0;n<xlen;n++)
{
w= -2*3.14*k1*n/N; //careful about minus sign
y[k]=y[k]+x[n]*cos(w);
y[k+1]=y[k+1]+x[n]*sin(w);

Department of Telecommunication, BMSIT Page 98


}
printf("%f+j%f \n",y[k],y[k+1]);
}
}//end of main

Result: (on std out)


11.000000+j0.000000 -0.407551+j-7.660230 -1.009554+j1.996801
2.424618+j-3.646700 -4.999950+j-0.022297 2.396780+j3.673694
-0.971311+j-2.009436 -0.460726+j7.633037

Department of Telecommunication, BMSIT Page 99


EXPERIMENT NO. 12
IMPULSE RESPONSE OF THE GIVEN FIRST ORDER / SECOND ORDER
SYSTEM
Aim: To find the Impulse response of the given first order / second order system
Theory:
A linear constant coefficient difference equation representing a second order system is
given by y[n]  a1 y[n  1]  a 2 y[n  2]  b0 x[n]  b1 x[n  1]  b2 x[n  2];

For a first order system the coefficients a2 and b2 are zero.


Since the difference equation contains past samples of output, i.e., y[n-1], y[n-2], 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=∞).
Impulse response implies x[n]= δ[n], an impulse, and the initial conditions x[-1], x[-
2], y[-1] and y[-2] are zero.
(refer expt 2 &7of part A - h=impz(b,a,N); %impulse response verification in
OCTAVE)
To implement in C the following algorithm is used.
Algorithm
1. Input the coefficients b0 b1 b2, a1 a2 (Say the coefficients from a 2nd order
butterworth filter. Note coefficient of y[n] is a0=1 always)
2. Generate the impulse input of size n (1st element=1, rest all=0)
3. Compute the output as
y[n]  a1 y[n  1]  a 2 y[n  2]  b0 x[n]  b1 x[n  1]  b2 x[n  2];

4. Repeat step 3 for n=0 through N (say 50,100, etc)

Department of Telecommunication, BMSIT Page 100


C Program
#include <stdio.h>
float x[60],y[60];
main()
{
float a1,a2,b0,b1,b2;
int i,j,N=20;
a1= -1.1430; a2= 0.4128;
b0=0.0675;b1=0.1349;b2=0.0675;
//generate impulse input
x[0]=1;
for(i=1;i<N;i++)
x[i]=0;
//generate output
for(j=0;j<N;j++)
{
y[j]=b0*x[j];
if(j>0)
y[j]=y[j]+b1*x[j-1]-a1*y[j-1];
if ((j-1)>0)
y[j]=y[j]+b2*x[j-2]-a2*y[j-2];
printf("%f \t",y[j]);
}
}//end of main

Note: For a first order system just enter a2=0 and b2=0.
Result (on std out)
0.067500 0.212053 0.282012 0.234804 0.151967 0.076771
0.025017 -0.003096 -0.013866 -0.014571 -0.010931 -
0.006479 -0.002893 -0.000632 0.000471 0.000800 0.000720
0.000492 0.000266 0.000100

Department of Telecommunication, BMSIT Page 101


EXPERIMENT NO. 13
REALIZATION OF AN FIR FILTER ( ANY TYPE ) TO MEET GIVEN
SPECIFICATIONS
Aim: Realization of an FIR filter ( any type ) to meet given specifications. The input
can be a signal from function generator / speech signal .
Pre-requisite: Input is given from the signal generator of amplitude 1 v p-p and
frequency > 20 Hz.
TMS kit: Plug in the signal generator o/p to line in and line out of TMS to CRO
Before compiling, Copy header files “dsk6713.h” and “dsk6713_aic23.h” from
C:\CCStudio_v3.1\c6000\dsk6713\include and paste it in the current project folder.
C Program

#include "FIRcfg.h"
#include "dsk6713.h"
#include "dsk6713_aic23.h"
float filter_Coeff[] ={0.000000,-0.001591,-0.002423,0.000000,0.005728, 0.011139,
0.010502,-0.000000, -0.018003,-0.033416,-0.031505,0.000000,0.063010, 0.144802,
0.220534,0.262448,0.220534,0.144802,0.063010,0.000000,-0.031505,-0.033416,
-0.018003, 0 ,0.010502, 0.011139,0.005728,0.000000, -0.002423,-0.001591, 0.0};
static short in_buffer[100];
DSK6713_AIC23_Config config = { \
0x0017, /* 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume */ \
0x0017, /* 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume
*/\
0x00d8, /* 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume */
\
0x00d8, /* 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume
*/ \
0x0011, /* 4 DSK6713_AIC23_ANAPATH Analog audio path control */ \
0x0000, /* 5 DSK6713_AIC23_DIGPATH Digital audio path control */ \
0x0000, /* 6 DSK6713_AIC23_POWERDOWN Power down control */ \
0x0043, /* 7 DSK6713_AIC23_DIGIF Digital audio interface format */ \

Department of Telecommunication, BMSIT Page 102


0x0081, /* 8 DSK6713_AIC23_SAMPLERATE Sample rate control */ \
0x0001 /* 9 DSK6713_AIC23_DIGACT Digital interface activation */ \
};
/* main() - Main code routine, initializes BSL and generates tone */
void main()
{ DSK6713_AIC23_CodecHandle hCodec;
Uint32 l_input, r_input,l_output, r_output;
/* Initialize the board support library, must be called first */
DSK6713_init();
hCodec = DSK6713_AIC23_openCodec(0, &config); /* Start the codec */
DSK6713_AIC23_setFreq(hCodec, 1);

while(1)
{ /* Read a sample to the left channel */
while (!DSK6713_AIC23_read(hCodec, &l_input));
/* Read a sample to the right channel */
while (!DSK6713_AIC23_read(hCodec, &r_input));
l_output=(Int16)FIR_FILTER(&filter_Coeff
,l_input);
r_output=l_output;
while (!DSK6713_AIC23_write(hCodec, l_output)); /* Send o/p to the left channel */
while (!DSK6713_AIC23_write(hCodec, r_output)); /* Send o/p to the right channel
*/
}
DSK6713_AIC23_closeCodec(hCodec); /* Close the codec */
}

signed int FIR_FILTER(float * h, signed int x)


{ int i=0; signed long output=0;
in_buffer[0] = x; /* new input at buffer[0] */
for(i=30;i>0;i--)
in_buffer[i] = in_buffer[i-1]; /* shuffle the buffer */
for(i=0;i<32;i++)

Department of Telecommunication, BMSIT Page 103


output = output + h[i] * in_buffer[i];
return(output);
}

Result: The output plot is observed on the CRO. This behaves as a highpass filter.
See the effect of filtering by giving a sinusoidal input to DSP kit and slowly varying
its frequency in the range from 0 to 3.5 kHz. What is the cutoff frequency of the
filter?
Change the filter co-efficients for different types of window & Cut-off frequencies
(refer FIR filter design part from experiment 11 in part A-OCTAVE)

Department of Telecommunication, BMSIT Page 104

You might also like