DSP Lab
DSP Lab
Department of ECE
INCHARGES
A.JHANSI RANI
List of Contents
1. JNTUH syllabus.
2. Dos and Don’ts.
3. Course objectives and outcomes.
4. Basic Introduction
5. List of Experiments
JAWAHARLAL NEHRU TECHNOLOGICAL UNIVERSITY HYDERABAD
The programs shall be implemented in software (Using MATLAB / Lab view / C programming/
Equivalent) and hardware (Using TI / Analog devices / Motorola / Equivalent DSP processors).
Cycle I
Cycle II:
7. Implementations of HP IIR filter for a given sequence/signal.
8. Generation of narrow band signal through filtering.
9. Implementation of Decimation Process.
10. Implementation of Interpolation Process.
11. Implementation of I/D sampling rate converters.
12. Impulse response of first order and second order systems.
Additional experiments:
13. Generation of DTMF signals.
INSTRUCTIONS / Do’s and Don’t
Instruction to Students:-
1. Do not handle any equipment without reading the instructions /Instruction manuals.
2. Observe type of sockets of equipment power to avoid mechanical damage.
3. Strictly observe the instructions given by the Teacher/ Lab Instructor.
4. It is mandatory to come to lab in a formal dress (Shirts, Trousers, ID card, and Shoes for boys).
Strictly no Jeans for both Girls and Boys.
5. It is mandatory to come with observation book and lab record in which previous experiment should
be written in Record and the present lab‘s experiment in Observation book.
6. Observation book of the present lab experiment should be get corrected on the same day
and Record should be corrected on the next scheduled lab session.
7. Mobile Phones should be Switched OFF in the lab session.
8. Students have to come to lab in-time. Late comers are not allowed to enter the lab.
9. Prepare for the viva questions. At the end of the experiment, the lab faculty will ask the viva
Questions and marks are allotted accordingly.
10. Bring all the required stationery like graph sheets, pencil & eraser, different color pens etc. for
the lab class.
1. Observation book and lab records submitted for the lab work are to be checked and
signed before the next lab session.
2. Students should be instructed to switch ON the power supply after the connections are
checked by the lab assistant / teacher.
3. The promptness of submission should be strictly insisted by awarding the marks accordingly.
Objectives:
2. To Calculate and Plot DFT / IDFT of given DT signal and prove it theoretical.
6. To Plot Magnitude and Phase of LP FIR filter for any given sequence.
12. To plot the Impulse response of first order and second order systems
Learning Outcomes:
1.Able to generate elementary signals/ waveforms and perform arithmetic operations on signals.
2.Able to Calculate and Plot DFT / IDFT of given DT signal.
3.Able to plot frequency response of a given system and verify the properties of LTI system.
4.Able to Implement FFT of given sequence and identify the reduction of computations using FFT.
5.Able to Implement LP FIR filter for a given sequence and calculate the filter coefficients.
6.Able to Implement HP IIR filter for a given sequence.
7.Able to generate Narrowband signal through filtering.
8.Able to Implement Decimation Process and vary (decrease) the sampling rate.
9.Able to Implement Interpolation Process and vary (increase) the sampling rate.
10. Able to Implement I/D sampling rate converters and identify the importance of multi rate sampling.
11. Able to determine the Power Spectrum of a given signal(s) and demonstrate the importance of
frequency domain.
12. Able to generate impulse response of first order and second order systems .
Introduction
Starting MATLAB:
After logging into your account, you can enter MATLAB by double-clicking
on the MATLAB shortcut icon (MATLAB 7.0.4) on your Windows desktop.
When you start MATLAB, a special window called the MATLAB desktop
appears. The desktop is a window that contains other windows. The major
tools within or accessible from the desktop are:
The Command Window
The Command History
The Workspace
The Current Directory
The Help Browser
in the Figure 1.1. This illustration also shows the default configuration of
the MATLAB desktop. You can customize the arrangement of tools and
documents to suit your needs.
educational purpose.
>> 1+2*3
ans = 7
You will have noticed that if you do not specify an output variable, MATLAB
uses a
default variable ans, short for answer, to store the results of the current
calculation. Note that the variable ans is created (or overwritten, if it is
already existed). To avoid this, you may assign a value to a variable or
output argument name. For example
>> x = 1+2*3
x =7
Will result in x being given the value 1 + 2*3=7. This variable name can
always
>> 4*x
ans =28.0000
Basic arithmetic
operators Symbol
Operation Example
+ Addition 2+3
- Subtraction 2-3
* Multiplication 2*3
/ Division 2/3
Quitting MATLAB
To end your MATLAB session, type quit in the Command
Getting started
After learning the minimum MATLAB session, we will now learn to use some
additional operations.
MATLAB variables are created with an assignment statement. The syntax of variable
For example,
>> x = expression
Overwriting variable
Once a variable has been created, it can be reassigned. In addition, if you do not
wish to see the intermediate results, you can suppress the numerical output by
putting a semicolon (;) at the end of the line. Then the sequence of commands
looks like this:
>> t = 5;
>> t = t+1
t =6
PROGRAM:
clc;
clear all;
close all;
b=[1];
a=[+1,-1,0.9];
n=0:3:100;
%generating impulse signal
x1=(n==0);
y1=filter(b,a,x1);
subplot(3,1,1);
stem(n,y1);
xlabel('n');
ylabel('y1(n)');
title ('impulse response');
%generating step signal
x2=(n>0);
%step response
y2=filter(b,a,x2);
subplot(3,1,2);
stem(n,y2);
xlabel('n');
ylabel('y2(n)');
title('step response');
%generating sinusoidal signal
t=0:0.1:2*pi;
x3=sin(t);
%sinusoidal response
y3=filter(b,a,x3);
subplot(3,1,3);
stem(t,y3);
xlabel('n');
ylabel('y3(n)');
title('sin response');
6. Define C.T.S
7. Define D.T.S.
9. Define Stem, Plot, Plot3,fplot, ezplot, linspace, flyplr, grid, mesh and legend
PROGRAM:
%program to perform N-point DFT of the given discrete time sequence
%
clc;
clear all;
N=input('Enter the length of sequence');
j=sqrt(-1);
xn=input('Enter input sequence');
xk=zeros(1,N);
for k=0:1:N-1
for n=0:1:N-1
xk(k+1)=xk(k+1)+xn(n+1)*exp(-j*2*pi*k*n/N);
end
end
disp('The DFT sequence is,');xk
disp('Magnitude sequence is,');
magxk=abs(xk)
disp('Phase sequence is,');
phaxk=angle(xk)
wk=0:1:N-1;
subplot(2,1,1);
stem(wk,magxk);
title('Magnitude Spectrum');
xlabel('k');ylabel('magnitude');
subplot(2,1,2);
stem(wk,phaxk);
title('Phase Spectrum');
xlabel('k');ylabel('phase');
INPUT VALUES:
xk =
magxk =
phaxk =
6
magnitude
0
0 0.5 1 1.5 2 2.5 3
k
Phase Spectrum
4
2
phase
-2
-4
0 0.5 1 1.5 2 2.5 3
k
PROGRAM:
%program to find the IDFT%
clear all;
clc;
N=input('Enter the length of inverse DFT');
i=sqrt(-1);
xk=input('Enter input sequence');
xn=zeros(1,N);
for k=0:1:N-1
for n=0:1:N-1
xn(n+1)=xn(n+1)+(xk(k+1)*exp(i*2*pi*k*n/N))/N;
end
end
disp('The iDFT sequence is,');xn
INPUT VALUES:
OUTPUT VALUES:
PROGRAM:
%Frequency Response of I order%
clc;
clear all;
b=input('enter the num coff');
a=input('enter the den coff');
w=0:0.01:pi;
h=freqz(b,a,w);
subplot(2,1,1);
plot(w/pi,abs(h));
xlabel('normalised freq');
ylabel('magnitude');
grid;
subplot(2,1,2);
plot(w/pi,angle(h));
xlabel('normalised freq');
ylabel('phase in radus');
grid;
INPUT VALUES:
1.2
1.1
magnitude
0.9
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
normalised freq
0
phase in radus
-0.05
-0.1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
normalised freq
PROGRAM:
%Frequency Response of II order%
clc;
clear all;
b=input('enter the num coff');
a=input('enter the den coff');
w=0:0.01:pi;
h=freqz(b,a,w);
subplot(2,1,1);
plot(w/pi,abs(h));
xlabel('normalised freq');
ylabel('magnitude');
grid;
subplot(2,1,2);
plot(w/pi,angle(h));
xlabel('normalised freq');
ylabel('phase in radus');
grid;
INPUT VALUES:
1.5
1
magnitude
0.5
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
normalised freq
0.5
phase in radus
-0.5
-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
normalised freq
RESULT: Hence we generated the frequency response of a given System of I order and II
order.
VIVA QUESTIONS:
THEORY: The Fast Fourier Transform is useful to map the time-domain sequence into a
continuous function of a frequency variable. The FFT of a sequence {x(n)} of length N is
given by a complex-valued sequence X(k).
M nk
j2
X(k) x(n) e n ;0 k N 1
k0
The above equation is the mathematical representation of the DFT. As the number of
computations involved in transforming a N point time domain signal into corresponding
frequency domain signal was found to be N 2 complex multiplications, an alternative
algorithm involving lesser number of computations is opted. Dividing the DFT into smaller
DFTs is the basis of the FFT. A radix-2 FFT divides the DFT into two smaller DFTs, each of
which is divided into smaller DFTs and so on, resulting in a combination of two-point DFTs.
The Decimation -In-Time (DIT) FFT divides the input (time) sequence into two groups, one
of even samples and the other of odd samples. N/2 point DFT are performed on the these
sub-sequences and their outputs are combined to form the N point DFT.
PROGRAM:
% FFT of given sequence %
clc;
clear all;
x=input('enter input sequence a(n)');
n=length(x);
y=fft(x,n);
m=abs(y);
subplot(1,2,1);
stem(m);
xlabel('time');
ylabel('magnitude');
p=angle(y);
subplot(1,2,2);
stem(p);
xlabel('time');
ylabel('phase in radians');
title('phase plot');
INPUT:
phase plot
7 4
6 3
5 2
phase in radians
4 1
magnitude
3 0
2 -1
1 -2
0 -3
1 2 3 4 1 2 3 4
time time
VIVA QUESTIONS:
AIM: To write a program to determine the power spectral density of given signal and
sequence.
THEORY: The power spectral density (P.S.D) is a measurement of the energy at various
frequencies. In the previous section we saw how to unwrap the FFT and get back the sine
and cosine coefficients. Usually we only care how much information is contained at a
particular frequency and we don’t really care whether it is part of the sine or cosine series.
Therefore, we are interested in the absolute value of the FFT coefficients. The absolute
value will provide you with the total amount of information contained at a given
frequency, the square of the absolute value is considered the power of the signal.
Remember that the absolute value of the Fourier coefficients is the distance of the
complex number from the origin. To get the power in the signal at each frequency
(commonly called the power spectrum) you can try the following commands.
>> N = 8; %% number of points
>> t = [0:N-1]’/N; %% define time
>> f = sin(2*pi*t); %%define function
>> p = abs(fft(f))/(N/2); %% absolute value of the fft
>> p = p(1:N/2).^2 %% take the positve frequency half, only
This set of commands will return something much easier to understand, you should get 1
at a frequency of 1 and zeros everywhere else. Try substituting cos for sin in the above
commands, you should get the same result. Now try making >>f = sin(2*pi*t) +
cos(2*pi*t). This change should result in twice the power contained at a frequency of 1.
PROGRAM:
clc;
clear all;
f1=500;
f2=1000;
fs=2000;
t=0:1/fs:1;
x=2*sin(2*pi*f1*t)+3*sin(2*pi*f2*t)+rand(size(t));
psd1=abs(fft(x));
subplot(2,2,1);
plot(t*fs,10*log(psd1));
xlabel ('frequency');
ylabel ('magnitude');
title('power spectral density using square magnitude method');
psd2=abs(fft(xcorr(x),length(t)));
subplot(2,2,2);
plot(t*fs,10*log(psd2));
xlabel('frequency');
ylabel('magnitude');
title('power spectral density using auto corelation method');
S=[1,1,1,1,0,0,0,0];
N1=length(S);
N=32;
N2=64;
subplot(2,2,3);
psd_=abs(fft(S,N))/N1;
stem((0:(N-1))/N,psd_);
xlabel('discrete frequency f,f=K/N,K=0,1,2...N-1');
ylabel('power');
title('power spectrum ,FFT length N=32');
grid on;
subplot(2,2,4);
psd3=abs(fft(S,N2))/N1;
stem((0:(N2-1))/N2,psd3);
xlabel('discrete frequency f,f=K/N,K=0,1,2,....N-1');
ylabel('magnitude');
title('power spectrum,FFT length N=64');
RESULT: Hence we generated the power spectral density of given signal and sequence.
VIVA QUESTIONS:
AIM: To write a program for Implementation of LP FIR filter for a given sequences
PROGRAM:
-10
-20
gain
-30
-40
-50
-60
-70
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
normalized frequency
1. What is filter?
2. What is FIR and IIR filter define, and distinguish between these two?
3. What is window method? How you will design an FIR filter using window
method?
4. What are low-pass and band-pass filter and what is the difference between these
two?
5. What is the matlab command for Hamming window? Explain.
6. What do you mean by built in function ‘abs’ and where it is used?
7. Explain how the FIR filter are stable?
8. Why is the impulse response "finite"?
9. What does "FIR" mean?
10.What are the advantages of FIR Filters (compared to IIR filters)?
11.What are the disadvantages of FIR Filters (compared to IIR filters)?
12.What terms are used in describing FIR filters?
13.What is the delay of a linear-phase FIR?
EXPT NO 7: IMPLEMENTATION OF HP IIR FILTER FOR A GIVEN
SEQUENCE/SIGNAL
AIM: To write a program for Implementation of HP IIR filter for a given sequences
a) Using Butterworth Filter
b) Using Chebyshev Filter
Ellipord ( for calculating the order of filter) Ellip (creates an IIR filter)
Cheb1ord (for calculating the order of filter) Cheyb1 (creates an IIR filter)
[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 pass band and has at least Rs dB
of attenuation in the stop band. Wp and Ws are the pass band and stop band edge
frequencies, normalized from 0 to 1 (where 1 corresponds to pi radians/sample).
PROGRAM:
%Implementation of HP IIR filter for a given sequences Using Butterworth Filter%
clc;
clear all;
close all;
rp=input('enter the value of rp');
rs=input('enter the value of rs');
fp=input('enter the value of fp');
fs=input('enter the value of fs');
%%instalation of sapling freq%
fsp=10000;
%%normalised frequencie%%
wp=((2*fp)/fsp);
ws=((2*fs)/fsp);
%%order,cutoff freq.of the filter%%
[n,wn]=buttord(wp,ws,rp,rs,'s');
[b,a]=butter(n,wn,'high','s');
w=0:0.01:pi;
[h,om]=freqs(b,a,w);
m=(20*(log10(abs(h))));
%%for phase response%%
an=angle(h);
%%plotting the graphs%%
subplot(2,1,1);
plot(om/pi,m,'.r');
xlabel('normalized frequency');
ylabel('gain in db');
title('magnitude response in db');
%%plotting the graphs%%
subplot(2,1,2);
plot(om/pi,an,'--');
xlabel('frequency');
ylabel('phase in radians');
title('phase plot');
Input Values:
-100
gain in db
-200
-300
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
normalized frequency
phase plot
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
frequency
Result: Hence we implemented HP IIR filter for a given sequences using Butterworth
Filter
PROGRAM:
%Implementation of HP IIR filter for a given sequences Using Chebyshev Filter%
clc;
clear all;
close all;
rp=input('enter the value of rp');
rs=input('enter the value of rs');
fp=input('enter the valuek if fp');
fs=input('enter the value of fs');
%%instalation of sapling freq%
fsp=10000;
%%normalised frequencie%%
wp=((2*fp)/fsp);
ws=((2*fs)/fsp);
%%order,cutoff freq.of the filter%%
[n,wn]=cheb1ord(wp,ws,rp,rs,'s');
[b,a]=cheby1(n,rp,wp,'high','s');
w=0:0.01:pi;
[h,om]=freqs(b,a,w);
m=(20*(log10(abs(h))));
%%for phase response%%
an=angle(h);
%%plotting the graphs%%
subplot(2,1,1);
plot(om/pi,m,'.r');
xlabel('normalized frequency');
ylabel('gain in db');
title('magnitude response in db');
%%plotting the graphs%%
subplot(2,1,2);
plot(om/pi,an,'--');
xlabel('frequency');
ylabel('phase in radians');
title('phase plot');
Input Values:
Output Waveform:
magnitude plot
0
-100
magnitude
-200
-300
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
frequency
phase plot
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
frequency
Result: Hence we implemented HP IIR filter for a given sequences using Chebyshev
Filter.
VIVA QUESTION:
1. What do you mean by cut-off frequency?
2. Give the difference between analog and digital filter?
3. What is the difference between type 1 and type 2 filter structures?
4. What is the role of delay element in filter design?
5. Explain how the frequency is filter in filters?
6. Differences between Butterworth chebyshev filters?
7. Can IIR filters be linear phase? How to make it linear Phase?
EXPT NO 8.GENERATION OF SINUSOIDAL SIGNAL THROUGH
FILTERING
PROGRAM:
close all;
clear all;
clc;
b=[1];
a=[1,-1,0.9];
n=[-20:120];
t=0:0.1:2*pi;
x=sin(t);
s=filter(b,a,x);
stem(t,s);
title('sinusoidal response');
xlabel('n');
ylabel('s(n)');
OUTPUT WAVEFORM:
clc;
clear all;
close all;
t=0:0.03:2;
x=cos(2*pi*t);
y=decimate(x,2);
t1=decimate(t,2);
%%Plotting Decimation Signals
% Plot for Input signal
subplot(2,1,1)
stem(t,x)
xlabel('Number of samples')
ylabel('Amplitude')
title('Input signal')
% Plot for Decimation
subplot(2,1,2)
stem(t1,y)
xlabel('Number of samples')
ylabel('Amplitude')
title('Decimated signal')
%disp('GRAPHS:')
Output Waveform:
Input signal
1
0.5
Amplitude
-0.5
-1
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
Number of samples
Decimated signal
1
0.5
Amplitude
-0.5
-1
-0.5 0 0.5 1 1.5 2
Number of samples
clc;
clear all;
close all;
t=0:0.03:2;
x=cos(2*pi*t);
y1=interp(x,2);
t2=interp(t,2);
%Plotting Interpolation Signals
% Plot for Input signal
subplot(2,1,1)
stem(t,x)
xlabel('Number of samples')
ylabel('Amplitude')
title('Input signal')
% Plot for Interpolation
subplot(2,1,2)
stem(t2,y1)
xlabel('Number of samples')
ylabel('Amplitude')
title('Interpolated signal')
disp('GRAPHS:')
Output Waveform:
Input signal
1
0.5
Amplitude
-0.5
-1
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
Number of samples
Interpolated signal
2
1
Amplitude
-1
-2
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
Number of samples
PROGRAM:
clc;% clear comand window
clear all; % clear work space
close all; %close all figure windows
%input signal
N=50 ;% no of samples
n=0:1:N-1;
x=sin(2*pi*n/15);% input signal
subplot(3,1,1)
stem(n,x);
xlabel('n');
ylabel('Amplitude');
title('Original Sequence');
%up sampling
L=2;% upsampling factor
x1=[zeros(1,L*N)];
n1=1:1:L*N;
j =1:L:L*N;
x1(j)=x;
subplot(3,1,2)
stem(n1-1,x1);
xlabel('n');
ylabel('Amplitude');
title('Upsampled Sequence');
% down sampling
M=2;
x2=x(1:M:N);
n2=1:1:N/M;
subplot(3,1,3)
stem(n2-1,x2);
xlabel('n');
ylabel('Amplitude');
title('Downsampled Sequence');
PROGRAM:
1
input sequence x(n)
0.5
0
0 2 4 6 8 10 12 14 16 18 20
n
1
impulse response
0.5
0
0 2 4 6 8 10 12 14 16 18 20
n
PROGRAM :
INPUT VALUES:
1
input sequence x(n)
0.5
0
0 2 4 6 8 10 12 14 16 18 20
n
1
impulse response
0.5
-0.5
0 2 4 6 8 10 12 14 16 18 20
n
PROGRAM:
clc;
clear all;
%DTMF Generation%
d = input('enter the telephone digit= ','s');
symbol = abs(d);
tm = [49 50 51 65; 52 53 54 66; 55 56 57 67; 42 48 35 68];
for i = 1:4
for j = 1:4
if tm(i,j) == symbol,
break
end
end
if tm(i,j) == symbol,
break
end
end
f1 = [697 770 852 941];
f2 = [1209 1336 1477 1633];
fs = 8000;
dt = 1/fs;
t = 0:dt:1023/fs;
N = length(t);
x = sin(2*pi*f1(i)*t)+ sin(2*pi*f2(j)*t);
xf = 1/N*abs(fft(x,N));
f = fs*(0:round(N/2))/N;
f = f(1:round(N/2));
figure(1);
subplot(211)
plot(t,x)
subplot(212)
stem(f,xf(1:round(N/2)))
%DTMF Detection%
xfs = fliplr(sort(xf(1:round(N/2))));
fd = [];
for k = 1:N/2
if((xf(k) == xfs(1)) | (xf(k) == xfs(2)))
fd = [fd k];
end
end
fd =sort(fd);
dtmf1 = round(f(fd(1)));
dtmf2 = round(f(fd(2)));
for p = 1:4
if ((dtmf1 > f1(p)-5) & (dtmf1 < f1(p)+5))
dtmf1 = f1(p);
break
end
end
for q = 1:4
if ((dtmf2 > f2(q)-5) & (dtmf2 < f2(q)+5))
dtmf2 = f2(q);
break
end
end
disp('The tone frequencies are');
dtmf1
dtmf2
Output Waveform:
2
-1
-2
0 0.02 0.04 0.06 0.08 0.1 0.12 0.14
0.5
0.4
0.3
0.2
0.1
0
0 500 1000 1500 2000 2500 3000 3500 4000