DSP Lab Manual
DSP Lab Manual
DSP Lab Manual
ENGINEERING, SHIVAMOGGA
DSP LAB
17ECL57
Prepared by
Mr. Darshan. K. V
Assistant Professor, Dept of ECE
Ms. Prema. K. N
Assistant Professor, Dept of ECE
Ms. Smitha. S. M
Assistant Professor, Dept of ECE
DSP Lab Manual
CONTENTS
Syllabus
Following Experiments to be done using MATLAB / SCILAB / OCTAVE or
equivalent:
6. (i) Verification of DFT properties (like Linearity and Parsevals theorem, etc.)
Course Outcomes
Design IIR and FIR - band pass, band stop, low pass and high pass filters.
C307.3
CO-PO Mapping
CO PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12
CO.1 3 2
CO.2 3 2
CO.3 3 2 2
CO.4 3 2
Experiment 1
clc;
clear;
close();
t=0:0.001:1;
y=sin(2*%pi*sig_freq*t);
subplot(411);
plot2d(t,y);
xlabel("time");
ylabel("Amplitude");
title("Input Signal");
samp_instant=1/samp_freq;
n=0:samp_instant:1;
yn=sin(2*%pi*sig_freq*n);
subplot(412);
plot2d3(n,yn);
xlabel("time");
ylabel("Amplitude");
title("Sampled Signal");
ni=0:samp_instant/L:1;
yni=interp1(n,yn,ni,'linear');
subplot(413);
plot2d3(ni,yni);
xlabel("time");
ylabel("Amplitude");
title("Interpolated Signal");
subplot(414);
plot2d(ni,yni);
xlabel("time");
ylabel("Amplitude");
title("Reconstructed Signal");
Department of Electronics & Communication Page 4
DSP Lab Manual
Experiment 2
// Program to compute the convolution of two sequences without using the built-in
function conv()
// User should enter the input sequence, impulse response
// Convolution is computed using the corresponding formula
// The computed convolved sequence should be displayed and cross checked
clc;
clear;
close();
length_x=length(x);
length_h=length(h);
length_y=length_x+length_h-1;
a=[x,zeros(1,length_h)];
b=[h,zeros(1,length_x)];
y=zeros(1,length_y);
for i=1:length_y
y(i)=0;
for j=1:i
y(i)=y(i)+a(j)*b(i-j+1)
end
end
Result:
clc;
clear;
close();
if(lhs==rhs)
disp('Commutative property is proved');
else
disp('Commutative property fails');
end
else
disp('Associative property fails');
end
Result:
Experiment 3
clc;
clear;
close();
x=[x,zeros(1,N-length(x))];
n=0:N-1;
k=0:N-1;
wn=exp(-%i*2*%pi/N);
kn=k'*n;
wnkn =wn.^kn;
X=x*wnkn;
subplot(2,1,1);
plot2d(k,abs(X));
title('Magnitude Plot');
X_fft=fft(x);
disp('The DFT of the sequence by using the builtin function is');
disp(X_fft);
wn=exp(%i*2*%pi/N);
kn=k'*n;
wnkn =wn.^kn;
x_r=(X*wnkn)/N;
x_r1=fft(X_fft,1);
disp('The IDFT of the sequence by using the builtin function is');
disp(x_r1);
Result:
7. - 2. - i 1. + 1.225D-16i - 2. + i
7. - 2. - i 1. - 2. + i
1. 2. 3. 1.
1. 2. 3. 1.
// Linearity property
// Parseval's Thoerem
clc;
clear;
close();
a1=0.4;
a2=0.5;
x1=[1 2 3];
x2=[4 5 6 7];
N=8;
k=0:N-1;
n=0:N-1;
x1=[x1,zeros(1,N-length(x1))];
x2=[x2,zeros(1,N-length(x2))];
X1=fft(x1);
X2=fft(x2);
a1X1=a1*X1;
a2X2=a2*X2;
LHS_L=abs(fft(a1*x1+a2*x2));
RHS_L=abs(a1X1+a2X2);
if(round(LHS_L)==round(RHS_L))
disp('Linearity property is proved');
else
disp('Linearity property fails');
end
E1=sum(x1.^2);
E2=sum(abs(X1).^2)/N;
if(round(E1)==round(E2))
disp('Parsevel Theorem is proved');
else
disp('Parsevel Theorem fails');
end
Result:
Experiment 4
Additional DFT property verification and DFT of square pulse and sinc
pulse
Aim:
clc;
clear;
close();
x1=[1 2 3 4];
N=4;
k=0:N-1;
n=0:N-1;
x1=[x1,zeros(1,N-length(x1))];
X1=fft(x1);
wn=exp(-%i*2*%pi/N);
wnkm=wn.^(k*m);
wnkmX1=wnkm.*X1;
RHS_TS=abs(fft(wnkmX1,1));
LHS_TS=x1(pmodulo(n-m,N)+1)
if(round(LHS_TS)==round(RHS_TS))
disp('Ciruclar time shift property is proved');
else
disp('Ciruclar time shift property fails');
end
RHS_FS=abs(X1(pmodulo(k-m,N)+1));
wn=exp(%i*2*%pi/N);
wnmn=wn.^(n*m);
LHS_FS=abs(fft(wnmn.*x1));
if(round(LHS_FS)==round(RHS_FS))
disp('Ciruclar frequency shift property is proved');
else
disp('Ciruclar frequency shift property fails');
end
Result:
clc;
clear;
close();
t=linspace(-5,5,50);
y=sinc(%pi*t);
subplot(2,1,1);
plot2d(t,y,2);
a=gca();
title('Sinc Pulse');
a.x_location="origin";
a.y_location="origin";
a.children.children(1).thickness=2;
y=[y,zeros(1,512-length(y))];
N=length(y);
Y=abs(fft(y));
f=-N/2:N/2-1;
subplot(2,1,2);
plot2d(f,fftshift(Y),2);
title('DFT of the Sinc Pulse');
a=gca();
a.x_location="origin";
a.y_location="origin";
a.children.children(1).thickness=2;
clc; clear;close();
t=-2:0.1:2;
y=[zeros(1,length(t)/4),ones(1,length(t)/4),ones(1,length(t)/4),zeros(1,length(t)/4+1)]
subplot(2,1,1);
plot2d(t,y,2);
title('Square Pulse');
a=gca(); a.x_location="origin"; a.y_location="origin"; a.children.children(1).thickness=2;
y=[y,zeros(1,512-length(y))];
N=length(y);
Y=abs(fft(y));
f=-N/2:N/2-1;
subplot(2,1,2);
plot2d(f,fftshift(Y),2);
title('DFt of Square Pulse');
a=gca(); a.x_location="origin"; a.y_location="origin"; a.children.children(1).thickness=2;
Experiment 5
clc;
clear;
close();
length_x=length(x);
length_h=length(h);
y1=fft(X.*H,1);
disp('The circular convolution using DFT/IDFT is-->');
disp(y1);
Result:
46.
48.
46.
40.
clc;
clear;
close();
function y=cconv(x, h, N)
for i=1:N
y(i)=0;
for j=1:N
y(i)=y(i)+x(j)*h(pmodulo(i-j,N)+1);
end
end
endfunction
length_x1=length(x1);
length_x2=length(x2);
length_x3=length(x3);
lhs_C=cconv(a,b,N);
rhs_C=cconv(b,a,N);
disp(lhs_C);
disp(rhs_C);
if(lhs_C==rhs_C)
disp('Commutative property is proved');
else
disp('Commutative property fails');
end
disp(lhs_A);
disp(rhs_A);
if(lhs_A==rhs_A)
disp('Associative property is proved');
else
disp('Associative property fails');
end
disp(lhs_D);
disp(rhs_D);
if(lhs_D==rhs_D)
Result:
Experiment 6
clc;
clear;
close();
length_x1=length(x1);
length_x2=length(x2);
length_y=length_x1+length_x2-1;
x2_inv=x2(length_x2:-1:1);
a=[x1,zeros(1,length_x2)];
b=[x2_inv,zeros(1,length_x1)];
y=zeros(1,length_y);
for i=1:length_y
y(i)=0;
for j=1:i
y(i)=y(i)+a(j)*b(i-j+1)
end
end
3. 8. 14. 8. 3.
3. 8. 14. 8. 3.
clc;
clear;
close();
x=[1 4 2 3];
rxx=xcorr(x,x);
end
//Verification of rxx(0)>=rxx(l)
if(rxx(rxxcenter)>=rxx())
disp('Upper bound property Rxx(0)>=Rxx(k) is verified');
end
// Verification of rxx(0)=Energy(x)
meanvalue=sum(x.^2);
if (meanvalue==rxx(rxxcenter))
disp('Mean square value=correlation value at zero shift');
end
Result:
clc;
clear;
close();
x=[1 4 2 3];
y=[1 2 3 4];
rxy=xcorr(x,y);
ryx=xcorr(y,x);
rxx=xcorr(x,x);
ryy=xcorr(y,y);
rxxcenter=ceil(length(rxx)/2);
ryycenter=ceil(length(ryy)/2);
rxx0=rxx(rxxcenter);
ryy0=ryy(ryycenter);
rxxryy0=rxx0*ryy0;
rxy2=rxy.^2;
rxyinv=rxy(length(rxy):-1:1);
if (rxyinv==ryx)
disp('Property rxy(-m)=ryx is verified')
end
if(rxy2<=rxxryy0)
disp('Property Rxy^2<=Rxx(0).Ryy(0) is verified')
end
if(rxy<=1/2*(rxx0+ryy0))
disp('Property Rxy(m)<=1/2[Rxx(0)+Ryy(0)] is verified')
end
Result:
Experiment 7
clc;
clear;
close();
B=[0.2 0.4];
A=[1 -0.5];
subplot(2,1,1);
plot2d3(n,h,2);
a=gca();
a.x_location="origin";
a.y_location="origin";
a.children.children(1).thickness=2;
a.children.children(1).mark_style=3;
xlabel('n');
ylabel('h(n)');
title('Impulse Response');
x = ones(1,10);//Step input
y=filter(B,A,x);//Step response
subplot(2,1,2);
plot2d3(n,y,2);
a=gca();
Result:
Experiment 8
Design a digital low pass butterworth filter with the following specifications:
clc;
clear;
close();
// Order calculation
num1= 10.^(-kp/10)-1;
den1= 10.^(-ks/10)-1;
num=log10(num1/den1);
den=2*log10(1/ohms);
n=round(num/den);
disp('The order of the filter is--->');
disp(n);
hs=analpf(n,"butt",[0 0],1);
disp('The transfer function of the normalized filter is')
disp(hs);
z=poly(0,'z');
Hz=horner(Hds,2*fsamp*(z-1)/(z+1));
disp('The transfer function of the filter after bilinear transformation is')
disp(Hz);
Inputs:
Design a digital high pass butterworth filter with the following specifications:
clc;
clear;
close();
// Order calculation
num1= 10.^(-kp/10)-1;
den1= 10.^(-ks/10)-1;
num=log10(num1/den1);
den=2*log10(1/ohms);
n=round(num/den);
disp('The order of the filter is--->');
disp(n);
hs=analpf(n,"butt",[0 0],1);
disp('The transfer function of the normalized filter is')
disp(hs);
z=poly(0,'z');
Hz=horner(Hds,2*fsamp*(z-1)/(z+1));
disp('The transfer function of the filter after bilinear transformation is')
disp(Hz);
Inputs:
Experiment 9
clc;
clear;
close();
fp=1850;
fs=2150;
fsamp=8000;
hn=wfir('lp',N,fp/fsamp,'re',[0,0]);
M=1024;
hn=[hn,zeros(1,M-length(hn))]; //Zero padding for computing 1024 point DFT
H= fft(hn);
H_mag =20*log10(abs(H));
H_ang = atan(imag(H),real(H));
H_phase = (unwrap(H_ang))*180/%pi;
f =(0:M/2-1).*( fsamp/M);
subplot(2,1,1);
plot2d(f,H_mag (1:M/2),5);
xtitle('Magnitude response' ,'w(Hz)','Magnitude (dB)');
a=gca();
a.children.children(1).thickness=2;
subplot(2,1,2);
plot2d(f,H_phase(1:M/2),2);
xtitle ('Phase response' , 'w(Hz)' , 'Phase(Deg)');
a=gca();
a.children.children(1).thickness=2;
clc;
clear;
close();
fp=2500;
fs=1500;
fsamp=8000;
hn=wfir('hp',N,fp/fsamp,'hn',[0,0]);
M=1024;
hn=[hn,zeros(1,M-length(hn))]; //Zero padding for computing 1024 point DFT
H= fft(hn);
H_mag =20*log10(abs(H));
H_ang = atan(imag(H),real(H));
H_phase = (unwrap(H_ang))*180/%pi;
f =(0:M/2-1).*( fsamp/M);
subplot(2,1,1);
plot2d(f,H_mag (1:M/2));
xtitle('Magnitude response' ,'w(Hz)','Magnitude (dB)');
a=gca();
a.children.children(1).thickness=2;
subplot(2,1,2);
plot2d(f,H_phase(1:M/2));
1. Open Code Composer Studio [6713 DSK CCStudio V3.1], and make sure that the DSP kit
is turned on.
2. Use the Debug » Connect menu option to open a debug connection to the DSK board
3. Start a new project using Project » New pull down menu, and save it in a separate
directory with some name Dummy.pjt
[Default path will be C:\CCStudio_v3.1\MyProjects\.......]
4. Open an editor window, going to, File » New » Source File and type the program. Then
save it with an extension .c [Let us save with some name XXXX.c and default path for
the saved file will be C:\CCStudio_v3.1\MyProjects\Dummy\XXXX.c]
5. Add the source files XXXX.c to the project using Project » Add Files to Project pull down
menu.
6. Add the linker command file hello.cmd to the project using Project » Add Files to
Project pull down menu. [Default path will
be C:\CCStudio_v3.1\tutorial\dsk6713\hello1\hello.cmd and by chance if you are using
TMS320C6416DSK then add C:\CCStudio_v3.1\tutorial\dsk6416\hello1\hello.cmd]
7. Add the run time support library file rts6700.lib to the project using Project » Add Files
to Project pull down menu. [Default path will be
C:\CCStudio_v3.1\C6000\cgtools\lib\rts6700.lib and by chance if you are using
TMS320C6416DSK then add C:\CCStudio_v3.1\C6000\cgtools\lib \rts6400.lib]
8. Compile the program using the Project » Compile pull down menu or by clicking the
shortcut icon on the left side of the program window. If there exists any error in
program, then correct it and compile it.
9. Build the program using the Project » Build pull down menu or by clicking the shortcut
icon on the left side of the program window. If there exists any error in program, then
correct it and compile it.
10. Load the program in program memory of DSP Chip using the File » Load Program pull
down menu. [After compiling and building, CCStudio will generate executable file format
.out along with many other supporting files. And .out file default path will be usually
C:\CCStudio_v3.1\MyProjects\Dummy\Debug\Dummy.out]
11. Then run the program using Debug » Run pull down menu. And result will be displayed
on the screen.
Write a C program to implement Linear Convolution of two sequences using DSP 6713
processor
#include<stdio.h>
#include<math.h>
int y[20];
main()
int l,m,n,i,j;
int x[15];
int h[15];
scanf("%d",&m);
scanf("%d",&n);
scanf("%d",&x[i]);
scanf("%d",&h[i]);
x[i]=0;
h[i]=0;
y[i]=0;
y[i]+=x[j]*h[i-j];
printf("%d\n",y[i]);
Write a C program to implement Circular Convolution of two sequences using DSP 6713 processor
#include<stdio.h>
#include<math.h>
int y[20];
main()
int N,m,n,k=0,i,j;
int x[10];
int h[10];
scanf("%d",&m);
scanf("%d",&n);
scanf("%d",&x[i]);
scanf("%d",&h[i]);
x[i]=0;
h[i]=0;
if(m>n)
N=m;
else
N=n;
y[i]=0;
k=i-j;
if(k<0)
k=k+N;
y[i]+=x[j]*h[k];
printf("%d\t",y[i]);
Write a C program to implement DFT of a given sequence using DSP 6713 processor
#include<stdio.h>
#include<math.h>
void main ()
int x[10],n=0,M,N,k,i;
float sumre,diffim,cs=0,sn=0,pi=3.1416;
scanf("%d",&M);
scanf("%d",&N);
for(i=0;i<N;i++)
scanf("%d",&x[i]);
if(M-N!=0)
if(M>N)
for(i=N;i<M;i++)
x[i]=0;
N=M;
for(k=0;k<N;k++)
sumre=0;
diffim=0;
for(n=0;n<N;n++)
cs=cos(2*pi*(k)*n/N);
sn=sin(2*pi*(k)*n/N);
sumre+=x[n]*cs;
diffim-=x[n]*sn;
printf("x[%d]=%7.3f %7.3fj\n",k,sumre,diffim);
Write a C program to implement impulse response of a given second order system with the following
difference equation using DSP 6713 processor.
y(n) - 0.7478 y(n-1) + 0.2720 y(n-2) = 0.1311 x(n) + 0.2622 x(n-1)+ 0.1311 x(n-2)
y(n) = 0.1311 x(n) + 0.2622 x(n-1) + 0.1311 x(n-2) - (-0.7478 y(n-1)) - (0.2720 y(n-2))
#include <stdio.h>
float y[3]={0,0,0};
float x[3]={0,0,0};
float z[5];
float impulse[5]={1,0,0,0,0};
main()
int i=0,j;
for(j=0;j<3;j++)
x[0]=impulse[j];
y[0] = (cx [0] *x[0]) +( cx [1]* x[1] ) +( cx [2]*x[2]) - (y[1]* cy [1])-(y[2]* cy [2]);
printf("%f\n",y[0]);
z[j]=y[0];
y[2]=y[1];
y[1]=y[0];
x[2]=x[1];
x[1] = x[0];