8/3/2019 DSP Lab Manual Print Edition
1/41
Digital Signal
Processing
Lab Manual
DSPDigital Signal Processing Laboratory Manual for Under-Graduatecourse in Electronics and Communication. Bharath P
8/3/2019 DSP Lab Manual Print Edition
2/41
Contents
1. Sampling Theorem2. Impulse Response3. Linear Convolution4. Circular Convolution5. Auto correlation & its properties6. Cross correlation & its properties7. Difference Equation8. N-Point DFT9. Linear Convolution using DFT & IDFT10. Circular Convolution using DFT & IDFT11. FIR Filters12.a. IIR Filters - Butterworth
b. IIR Filters - Chebyshev Type I
1. Linear Convolution2. Circular Convolution3. N-Point DFT
8/3/2019 DSP Lab Manual Print Edition
3/41
Prerequisites:
As an exercise, it is recommended to go through the basics of Signals & Sytems,Digital Signal Processing and some details about DSP hardware & processors.
References:
[1] Oppenheim & Schaffer: Digital Signal Processing, Prentice-Hall.[2] B P Lathi: Modern Digital and Analog communication systems, Oxford.[3] Rulph Chassaing: Digital Signal Processing and Applications with the C6713 and
C6416 DSK,John Wiley.[4] Avatar Singh & S Srinivasan: Digital Signal Processing, Thomson Learning.
[5] TMS320C6713 DSK- Technical Reference, Spectrum Digital, Inc.[6]TMS320C6000 Code Composer Studio Tutorial (Literature Number: SPRU301C),Texas Instruments.
[7] Getting Started with Matlab, MATLAB.
8/3/2019 DSP Lab Manual Print Edition
4/41
Matlab Programs
8/3/2019 DSP Lab Manual Print Edition
5/41
1.Sampling Theoremclear all;
close all;
clc;
tf=0.05;
t=0:0.00005:tf;
f=input('Enter the analog frequency,f = ');
xt=cos(2*pi*f*t);
fs1=1.3*f;
n1=0:1/fs1:tf;
xn=cos(2*pi*f*n1);
subplot(311);
plot(t,xt,'b',n1,xn,'r*-');title('Undersampling plot');
xlabel('time');
ylabel('Amplitude');
fs2=2*f;
n2=0:1/fs2:tf;
xn=cos(2*pi*f*n2);
subplot(312);
plot(t,xt,'b',n2,xn,'r*-');
title('Nyquist plot');
xlabel('time');
ylabel('Amplitude');
fs3=6*f;
n3=0:1/fs3:tf;
xn=cos(2*pi*f*n3);
subplot(313);
plot(t,xt,'b',n3,xn,'r*-');
title('Oversampling plot');
xlabel('time');
ylabel('Amplitude');
8/3/2019 DSP Lab Manual Print Edition
6/41
Output
Enter the analog frequency,f = 200
8/3/2019 DSP Lab Manual Print Edition
7/41
2.Impulse Responseclear all;
close all;
clc;
disp('Difference Equation of a digital system');
N=input('Desired Impulse response length = ');
b=input('Coefficients of x[n] terms = ');
a=input('Coefficients of y[n] terms = ');
h=impz(b,a,N);
disp('Impulse response of the system is h = ');
disp(h);
n=0:1:N-1;
figure(1);
stem(n,h);
xlabel('time index');
ylabel('h[n]');
title('Impulse response');
figure(2);
zplane(b,a);
xlabel('Real part');
ylabel('Imaginary part');
title('Poles and Zeros of H[z] in Z-plane');
8/3/2019 DSP Lab Manual Print Edition
8/41
Output[Given y(n)-y(n-1)+0.9y(n-2)= x(n)]
Difference Equation of a digital system
Desired Impulse response length = 100Coefficients of x[n] terms = 1
Coefficients of y[n] terms = [1 -1 0.9]
8/3/2019 DSP Lab Manual Print Edition
9/41
3.Linear Convolutionclear all;
close all;
clc;
x=input('x[n]= ');
l=length(x);
h=input('h[n]=');
m=length(h);
num=(l+m)-1;
x_pad=[x,zeros(1,num-l)];
h_pad=[h,zeros(1,num-m)];
new_matrix=zeros(num,num);
new_mat(:,1)=x_pad';
for j=2:num new_mat(1,j)=new_mat(num,j-1);for i=2:num
new_mat(i,j)=new_mat(i-1,j-1);
end
end
result=new_mat*h_pad';
disp('y[n]= ');
disp(result');
low=min([result',x,h])-5;
high=max([result',x,h])+5;
subplot(1,3,1);
stem(x);xlabel('n');
ylabel('x[n]');
axis([1 num low high]);
subplot(1,3,2);
stem(h);
xlabel('n');
ylabel('h[n]');
axis([1 num low high]);
subplot(1,3,3);
stem(result');
xlabel('n');
ylabel('y[n]');
axis([1 num low high]);
8/3/2019 DSP Lab Manual Print Edition
10/41
Output
x[n]= [1 2 3 4]
h[n]=[2 1]
y[n]=2 5 8 11 4
8/3/2019 DSP Lab Manual Print Edition
11/41
4.Circular Convolutionclear all;
close all;
clc;
x=input('x[n]= ');
h=input('h[n]= ');
num=input('Enter the length of the sequences = ');
new_matrix=zeros(num,num);
new_mat(:,1)=x';
for
j=2:num new_mat(1,j)=new_mat(num,j-1);
for i=2:num
new_mat(i,j)=new_mat(i-1,j-1);
endend
result=new_mat*h';
disp('y[n]= ');
disp(result');
low=min([result',x,h])-5;
high=max([result',x,h])+5;
subplot(1,3,1);
stem(x);
xlabel('n');
ylabel('x[n]');
axis([1 num low high]);subplot(1,3,2);
stem(h);
xlabel('n');
ylabel('h[n]');
axis([1 num low high]);
subplot(1,3,3);
stem(result');
xlabel('n');
ylabel('y[n]');
axis([1 num low high]);
8/3/2019 DSP Lab Manual Print Edition
12/41
Output
x[n]= [1 2 3 4]
h[n]= [2 1 2 1]Enter the length of the sequences = 4
y[n]=
14 16 14 16
8/3/2019 DSP Lab Manual Print Edition
13/41
5.Auto-correlation & its propertiesclear all;
close all;
clc;
x_n=input('x[n]= ');
L=length(x_n);
x_minus_n=fliplr(x_n);
Rxx=conv(x_n,x_minus_n);
disp('Verification of property 1');
if(Rxx==fliplr(Rxx))
disp('Rxx[n] and Rxx[-n] are identical');
disp('Hence Auto correlation has even symmetry');
elsedisp('Rxx[n] and Rxx[-n] are not identical');
end
disp('Verification of property 2');
[max_val index]=max(Rxx);
if(index==L)
disp('maximum is at the origin');
else
disp('maximum is not at the origin');
end
disp('Verification of property 3');
energy=sum(x_n.^2);max_Rxx=max(Rxx);
disp('Energy of x[n] = ');
disp(energy);
disp('Maximun of Rxx[n] = ');
disp(max_Rxx);
disp('Hence maximum of Rxx is equal to Energy of x[n]');
N=length(Rxx);
X_k=fft(x_n,N);
EDS=abs(X_k).^2;
Rxx_k=fft(Rxx,N);
t=-(L-1):(L-1);
subplot(2,2,1);stem(t,[zeros(1,L-1),x_n]);
xlabel('n');
ylabel('x[n]');
subplot(2,2,2);
stem(t,Rxx);
xlabel('n');
ylabel('Rxx[n]');
title('Autocorrelation');
subplot(2,2,3);
stem(EDS);
xlabel('n');
ylabel('EDS');
title('Enegry density of x[n]');
8/3/2019 DSP Lab Manual Print Edition
14/41
subplot(2,2,4);
stem(abs(Rxx_k));
xlabel('k');ylabel('Rxx[k]');
title('DFT of Rxx[n]');
8/3/2019 DSP Lab Manual Print Edition
15/41
Output
x[n]= [1 2 3 4]
Verification of property 1Rxx[n] and Rxx[-n] are identical
Hence Auto correlation has even symmetry
Verification of property 2
maximum is at the origin
Verification of property 3
Energy of x[n] =
30
Maximun of Rxx[n] =
30
Hence maximum of Rxx is equal to Energy of x[n]
8/3/2019 DSP Lab Manual Print Edition
16/41
6.Cross-correlation & its propertiesclear all;
close all;
clc;
x_n=input('x[n]= ');
N=length(x_n);
y_n=input('y[n]= ');
M=length(y_n);
y_minus_n=fliplr(y_n);
x_minus_n=fliplr(x_n);
Rxy=conv(x_n,y_minus_n);
t=-(N-1):(M-1);
t2=-(M-1):(N-1);
disp('Verification of property 1');disp('Rxy[n]and Ryx[n] ');
Ryx=conv(x_minus_n,y_n);
if(Rxy==Ryx)
disp('are not commutative');
else
disp('are commutative');
end
disp('Verification of property 2');
disp('Rxy[n]and Ryx[-n] ');
if(Rxy==fliplr(Ryx))
disp('are equal');
elsedisp('are not equal');
end
disp('Verification of property 3');
disp('The sequences ');
if(Rxy(M+1)==0)
disp('are orthogonal');
else
disp('are not orthogonal');
end
disp('DFT Rxy[n]=X[k].Y[k]');
temp=fft(Rxy);Rxy_k=abs(temp);
X_k=fft(x_n,length(Rxy));
Y_k=fft(y_n,length(Rxy));
temp2=(X_k).*conj(Y_k);
temp3=abs(temp2);
subplot(3,2,1);
stem(t,[zeros(1,M-1),x_n]);
xlabel('n'); ylabel('x[n]');
subplot(3,2,2);
stem(t,[y_minus_n,zeros(1,N-1)]);
xlabel('n');
ylabel('y[-n]');
8/3/2019 DSP Lab Manual Print Edition
17/41
subplot(3,2,3)stem(t,Rxy);
xlabel('n');
ylabel('Rxy[n]');
title('Crosscorrelation');subplot(3,2,4);
stem(t2,Ryx);
xlabel('n');
ylabel('Ryx[n]');
title('Crosscorrelation');
subplot(3,2,5);
stem(Rxy_k);
xlabel('k');
ylabel('Rxy[k]');
title('DFT of Rxy[n]');
subplot(3,2,6);
stem(temp3);
xlabel('k');
ylabel('X[k].Y[k]');
title('DFT x[n].DFT y[n]');
8/3/2019 DSP Lab Manual Print Edition
18/41
Output
x[n]= [1 2 3 4]
y[n]= [4 3 2 1]
Verification of property 1
Rxy[n]and Ryx[n]
are commutative
Verification of property 2
Rxy[n]and Ryx[-n]
are equal
Verification of property 3
The sequences are not orthogonal
DFT Rxy[n]=X[k].Y[k]
8/3/2019 DSP Lab Manual Print Edition
19/41
7.Difference Equationclear all;
close all;
clc;
disp('Enter the parameters of Difference Equation of a digital system');
b=input('Coefficients of x[n] terms = ');
a=input('Coefficients of y[n] terms = ');
xn=input('Enter the input exitation x[n] = '); %of length 1 to 100
yi=input('Enter the initial conditions of y = '); %if necessary
%xi=('Enter the initial conditions of x = ');
%yi of length 1 to (a-1)
%xi of length 1 to (b-1)
initialc=filtic(b,a,yi);
%initialc=filtic(b,a,yi,xi);y_complete=filter(b,a,xn,initialc);
y_forced=filter(b,a,xn);
y_natural=y_complete-y_forced;
subplot(3,1,1);
stem(y_natural);
title('Natural response of the system');
subplot(3,1,2);
stem(y_forced);
title('Forced response of the system');
subplot(3,1,3);
stem(y_complete);
title('Complete response of the system');
8/3/2019 DSP Lab Manual Print Edition
20/41
Output
Enter the parameters of Difference Equation of a digital system
Coefficients of x[n] terms = 1Coefficients of y[n] terms = [1 -1 0.9]
Enter the input exitation x[n] = [1 zeros(1,99)]
Enter the initial conditions of y = [-1 0 0]
8/3/2019 DSP Lab Manual Print Edition
21/41
8.N-point DFTclear all;
close all;
clc;
x=input('Enter the sequence x[n]= ');
N=input('Enter the value N point= ');
L=length(x);
x_n=[x,zeros(1,N-L)];
for i=1:N
for j=1:N
temp=-2*pi*(i-1)*(j-1)/N;
DFT_mat(i,j)=exp(complex(0,temp));
endend
X_k=DFT_mat*x_n';
disp('N point DFT is X[k] = ');
disp(X_k);
mag=abs(X_k);
phase=angle(X_k)*180/pi;
subplot(2,1,1);
stem(mag);
xlabel('frequency index k');
ylabel('Magnitude of X[k]');
axis([0 N+1 -2 max(mag)+2]);
subplot(2,1,2);
stem(phase);
xlabel('frequency index k');
ylabel('Phase of X[k]');
axis([0 N+1 -180 180]);
8/3/2019 DSP Lab Manual Print Edition
22/41
Output
Enter the sequence x[n]= [1 2 3 4]
Enter the value N point= 4N point DFT is X[k] =
10.0000
-2.0000 + 2.0000i
-2.0000 - 0.0000i
-2.0000 - 2.0000i
8/3/2019 DSP Lab Manual Print Edition
23/41
9. Linear Convolution using DFT & IDFTclear all;
close all;
clc;
x=input('x[n]= ');
l=length(x);
h=input('h[n]= ');
m=length(h);
num=(l+m)-1;
XN=fft(x,num);
HN=fft(h,num);
YN=XN.*HN;y=ifft(YN,num);
disp('y[n]= ');
disp(y);
low=min([y,x,h])-5;
high=max([y,x,h])+5;
subplot(1,3,1);
stem(x);
xlabel('n');
ylabel('x[n]');
axis([1 num low high]);
subplot(1,3,2);
stem(h);
xlabel('n');
ylabel('h[n]');
axis([1 num low high]);
subplot(1,3,3);
stem(y);
xlabel('n');
ylabel('y[n]');
axis([1 num low high]);
8/3/2019 DSP Lab Manual Print Edition
24/41
Output
x[n]= [1 2 3 4]
h[n]= [2 1]y[n]=
2 5 8 11 4
8/3/2019 DSP Lab Manual Print Edition
25/41
10. Circular Convolution using DFT & IDFTclear all;
close all;
clc;
disp('Enter the sequences of equal lengths');
x=input('x[n]= ');
N=length(x);
h=input('h[n]= ');
XN=fft(x,N);
HN=fft(h,N);
YN=XN.*HN;
y=ifft(YN,N);
disp('y[n]= ');
disp(y);
low=min([y,x,h])-5;
high=max([y,x,h])+5;
subplot(1,3,1);
stem(x);
xlabel('n');
ylabel('x[n]');
axis([1 N low high]);
subplot(1,3,2);
stem(h);
xlabel('n');
ylabel('h[n]');
axis([1 N low high]);
subplot(1,3,3);
stem(y);
xlabel('n');
ylabel('y[n]');
axis([1 N low high]);
8/3/2019 DSP Lab Manual Print Edition
26/41
Output
Enter the sequences of equal lengths
x[n]= [1 2 3 4]h[n]= [2 1 2 1]
y[n]=
14 16 14 16
8/3/2019 DSP Lab Manual Print Edition
27/41
11. FIR Filtersclear all;
close all;
clc;
wn=input('Enter the passband frequency between 0 & 1 (normalised) = ');
%wn=[w1 w2];
%wn is a vector,for Bandpass & Bandstop
n=input('Enter the order of the filter = ');
%beta=input('Enter the value of beta');
%beta for Kaiser only
b=fir1(n,wn,hamming(n+1)); %replace by following for other windows
%blackman(n);
%hanning(n);%hann(n);
%barlett(n);
%boxcar(n);
%kaiser(n,beta);
%b=fir1(n,wn,'high',XXXXXXX(n)); Highpass window
%b=fir1(n,wn,XXXXXXX(n)); Bandpass window
%b=fir1(n,wn,'stop',XXXXXXX(n)); Bandstop window
[h,w]=freqz(b,1,512);
dB=20*log10(abs(h));
figure(1);
subplot(2,1,1);
plot(w/pi,dB);
title('Magnitude response');
xlabel('Normalised frequency');
ylabel('Magnitude in dB');
grid;
subplot(2,1,2);
plot(w/pi,angle(h));
title('Phase response');
xlabel('Normalised frequency');
ylabel('Phase in degrees');
grid;
figure(2);
stem(impz(b,1));
title('Impulse response');
xlabel('Time index n');
ylabel('Amplitude');
8/3/2019 DSP Lab Manual Print Edition
28/41
Output
Enter the passband frequency between 0 & 1 (normalised) = 0.3
Enter the order of the filter = 20
8/3/2019 DSP Lab Manual Print Edition
29/41
12.a IIR Filters - Butterworth
clear all;
close all;
clc;
wp=input('Enter the passband edge Normalised frequency = ');
%wp=wn, for 3dB cutoff frequency
ws=input('Enter the stopband edge Normalised frequency = ');
%wp=[w1 w2]; ws=[w3 w4];
%wp & ws are vectors, for Bandpass & Bandstop
Dp=input('Enter the passband attenuation level (dB) = ');
Ds=input('Enter the stopband attenuation level (dB) = ');
%N=input('Enter the order of the filter = ');
%order=N/2, for Bandpass & Bandstop
[N,wn]=buttord(wp,ws,Dp,Ds); %skip if N & 3dB cutoff frequency is known
[b,a]=butter(N,wn); %replace by following for other filters
%[b,a]=butter(N,wn,'high'); Highpass filter
%[b,a]=butter(N,wn); Bandpass filter
%[b,a]=butter(N,wn,'stop'); Bandstop filter
[h,w]=freqz(b,a);
mag=20*log10(abs(h));
phase=180*angle(h)/pi;
figure(1);
plot(w,abs(h));
title('Butterworth Lowpass Filter');
xlabel('Normalised frequency');
ylabel('Magnitude');
grid;
figure(2);
subplot(2,1,1);
plot(w,mag);
title('Magnitude response');
xlabel('Normalised frequency');
ylabel('Magnitude in dB');
grid; subplot(2,1,2);
plot(w,phase);
title('Phase response');
xlabel('Normalised frequency');
ylabel('Phase in degrees');
grid;
8/3/2019 DSP Lab Manual Print Edition
30/41
Output
Enter the passband edge Normalised frequency = 0.3
Enter the stopband edge Normalised frequency = 0.6Enter the passband attenuation level (dB) = 3
Enter the stopband attenuation level (dB) = 40
8/3/2019 DSP Lab Manual Print Edition
31/41
12.b IIR Filters - Chebyshev
clear all;
close all;
clc;
wp=input('Enter the passband edge Normalised frequency = ');
%wp=wn, for 3dB cutoff frequency
ws=input('Enter the stopband edge Normalised frequency = ');
%wp=[w1 w2]; ws=[w3 w4];
%wp & ws are vectors, for Bandpass & Bandstop
Rp=input('Enter the passband attenuation level (dB) = ');
Rs=input('Enter the stopband attenuation level (dB) = ');
%N=input('Enter the order of the filter = ');
%order=N/2, for Bandpass & Bandstop[N,wn]=cheb1ord(wp,ws,Rp,Rs);
%skip if order and 3dB cutoff frequency is known
[b,a]=cheby1(N,Rp,wn); %replace by following for other filters
%[b,a]=cheby1(N,Rp,wn,'high'); Highpass filter
%[b,a]=cheby1(N,Rp,wn); Bandpass filter
%[b,a]=cheby1(N,Rp,wn,'stop'); Bandstop filter
[h,w]=freqz(b,a);
mag=20*log10(abs(h));
phase=180*angle(h)/pi;
figure(1);
plot(w,abs(h));
title('Chebyshev Lowpass Filter');
xlabel('Normalised frequency');
ylabel('Magnitude');
grid;
figure(2);
subplot(2,1,1);
plot(w,mag);
title('Magnitude response');
xlabel('Normalised frequency');
ylabel('Magnitude in dB');
grid;
subplot(2,1,2);
plot(w,phase);
title('Phase response');
xlabel('Normalised frequency');
ylabel('Phase in degrees');
grid;
8/3/2019 DSP Lab Manual Print Edition
32/41
Output
Enter the passband edge Normalised frequency = 0.3
Enter the stopband edge Normalised frequency = 0.6
Enter the passband attenuation level (dB) = 3
Enter the stopband attenuation level (dB) = 40
8/3/2019 DSP Lab Manual Print Edition
33/41
C Programs
8/3/2019 DSP Lab Manual Print Edition
34/41
1. Linear Convolution#include
#include
int m,n,i,j,x[30],y[30],h[30];
void main()
{
printf("Enter the length of x\n");
scanf("%d",&m);
printf("Enter the length of h\n");
scanf("%d",&n);
printf("Enter x\n");
for(i=0;i
8/3/2019 DSP Lab Manual Print Edition
35/41
8/3/2019 DSP Lab Manual Print Edition
36/41
2. Circular Convolution#include
#include
int m,n,i,j,k,x[30],y[30],h[30],a[30],b[30];
void main()
{
printf("Enter the length of x\n");
scanf("%d",&m);
printf("Enter x\n");
for(i=0;i
8/3/2019 DSP Lab Manual Print Edition
37/41
Output
Enter the length of x
4Enter x
1
2
3
4
Enter h
2
1
2
1
Circular convolution, y is 14 16 14 16
8/3/2019 DSP Lab Manual Print Edition
38/41
3. N-Point DFT#include
#include
void main()
{
int i,j,n;
short N;
short x[10];
float pi=3.1416;
float sumRe,sumIm;
float cosine=0,sine=0;
float out_real[10]={0.0},out_image[10]=(0.0);
printf("Enter the length of the sequence\n");
scanf("%d",&n);
printf("Enter N\n");
scanf("%d",&N);
printf("Enter x\n");
for(i=n+1;in)for(i=n;i
8/3/2019 DSP Lab Manual Print Edition
39/41
Output
Enter the length of the sequence
4Enter N
4
Enter x
1
2
3
4
N point DFT is
10.0 +j(0.0)
-2.0 +j(-2.0)
-2.0 +j(-0.0)
-2.0 +j(2.0)
8/3/2019 DSP Lab Manual Print Edition
40/41
Text Key Steps
Text Commands for plotting a figure
Text Comments
8/3/2019 DSP Lab Manual Print Edition
41/41