You are on page 1of 47

JAWHARLAL NEHRU NATIONAL COLLEGE OF

ENGINEERING, SHIVAMOGGA

DEPARTMENT OF ELECTRONICS AND COMMUNICATION


ENGINEERING
LAB MANUAL
FOR

DSP LAB

17ECL57

Prepared by

Mr. Ajay Betur. P


Assistant Professor, Dept of ECE

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

Sl Experiment Name Page


No Number
Syllabus 2
Course Outcomes and Mapping 3
EXPERIMENTS USING SCILAB
1 Verification of sampling theorem 4-6
2 Linear Convolution and verification of its properties 7-9
3 DFT and IDFT computation of a sequence and property verification 10-13
4 Additional DFT property verification and DFT of square pulse and 14-17
sinc pulse
5 Circular Convolution and verification of its properties 18-21
6 Cross correlation and auto correlation and verification of its 22-25
properties
7 Solving a difference equation 26-27
8 IIR Filter implementation for the given specification 28-32
9 FIR Filter implementation for the given specification 33-36
EXPERIMENTS USING DSP KIT
1 Linear convolution of two sequences 39-40
2 Circular convolution of two sequences 41-42
3 N-point DFT of a given sequence 43-44
4 Impulse response of first order and second order system 45-46

Department of Electronics & Communication Page 1


DSP Lab Manual

Syllabus
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.

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

Department of Electronics & Communication Page 2


DSP Lab Manual

Course Outcomes

At the end of the course, students must be able to

Simulate discrete/digital signals using MATLAB.


C307.1

Develop a MATLAB program to simulate signal processing operations.


C307.2

Design IIR and FIR - band pass, band stop, low pass and high pass filters.
C307.3

Implement DSP algorithms on DSP processor TMS320C6713 floating point


C307.4 Processor using C language.

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

Department of Electronics & Communication Page 3


DSP Lab Manual

Experiment 1

Verification of sampling theorem


Aim: To verify sampling theorem for a sinusoidal sequence using SCILAB

clc;
clear;
close();

sig_freq=input("enter the frequency of the signal");


samp_freq=input("enter the sampling frequency");
L=input("enter the interpolation factor");

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

Enter the frequency of the signal 2

Enter the sampling frequency 20

Enter the interpolation factor 4

Enter the frequency of the signal 2

Enter the sampling frequency 4

Enter the interpolation factor 4

Department of Electronics & Communication Page 5


DSP Lab Manual

Department of Electronics & Communication Page 6


DSP Lab Manual

Experiment 2

Linear Convolution and verification of its properties


Aim: To compute the linear convolution of two sequences and verification if commutative,
associative and distributive property.

// 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();

x=input('Enter the first sequence');


h=input('Enter the second sequence');

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

disp('Convolution using the formula is given by');


disp(y);

disp('Convolution using the builtin function is given by');


disp(conv(x,h));

Department of Electronics & Communication Page 7


DSP Lab Manual

Result:

Enter the first sequence[1 2 3 4]

Enter the second sequence[4 5 6 7]

Convolution using the formula is given by

4. 13. 28. 50. 52. 45. 28.

Convolution using the built-in function is given by

4. 13. 28. 50. 52. 45. 28.

Program to verify the following convolution properties

// 1 Commutative Property :a[n]*b[n]=b[n]*a[n]


// 2 Associative Property :a[n]*(b[n]*c[n])=(a[n]*b[n])*c[n]
// 3 Distributive Propery :a[n]*b[n]+a[n]*c[n]=a[n]*(b[n]+c[n])
// Note: The sequence lengths should be same.

clc;
clear;
close();

a=input('Enter the first sequence');


b=input('Enter the second sequence');
c=input('Enter the third sequence');

// To Prove commutative property


lhs=conv(a,b);
rhs=conv(b,a);

if(lhs==rhs)
disp('Commutative property is proved');
else
disp('Commutative property fails');
end

// To prove Associative property


lhs=conv(a,conv(b,c));
rhs=conv(conv(a,b),c);
if(lhs==rhs)
disp('Associative property is proved');
Department of Electronics & Communication Page 8
DSP Lab Manual

else
disp('Associative property fails');
end

// To prove Distributive property


lhs=conv(a,b)+conv(a,c);
rhs=conv(a,b+c);
if(lhs==rhs)
disp('Distributive property is proved');
else
disp('Distributive property fails');
end

Result:

Enter the first sequence [1 2 3 5]

Enter the second sequence [4 5 6 7 ]

Enter the third sequence [5 6 7 9]

Commutative property is proved

Associative property is proved

Distributive property is proved

Department of Electronics & Communication Page 9


DSP Lab Manual

Experiment 3

DFT and IDFT computation of a sequence and property verification


Aim: To compute the DFT of a given sequence. And to verify linearity and Parsevel’s
Theorem.

program to find the DFT and IDFT of the given sequence.

// The formula for DFT and IDFT is used for computation


// The obtained result should be verified using built-in function fft()

clc;
clear;
close();

x=input('Enter the input sequence');


N=input('Enter the vaue of N');

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_ang = atan(imag (X),real (X));


subplot(2,1,2);
plot2d(k,X_ang*180/%pi);
title('Phase Plot');

disp('The DFT of the sequence by using the formula is');


disp(X);

X_fft=fft(x);
disp('The DFT of the sequence by using the builtin function is');
disp(X_fft);

Department of Electronics & Communication Page 10


DSP Lab Manual

wn=exp(%i*2*%pi/N);
kn=k'*n;
wnkn =wn.^kn;
x_r=(X*wnkn)/N;

disp('The IDFT of the sequence by using the formula is');


disp(abs(x_r));

x_r1=fft(X_fft,1);
disp('The IDFT of the sequence by using the builtin function is');
disp(x_r1);

Result:

Enter the input sequence [1 2 3 1]

Enter the vaue of N 4

The DFT of the sequence by using the formula is

7. - 2. - i 1. + 1.225D-16i - 2. + i

The DFT of the sequence by using the built-in function is

7. - 2. - i 1. - 2. + i

The IDFT of the sequence by using the formula is

1. 2. 3. 1.

The IDFT of the sequence by using the built-in function is

1. 2. 3. 1.

Department of Electronics & Communication Page 11


DSP Lab Manual

To prove the following properties

// Linearity property
// Parseval's Thoerem

clc;
clear;
close();

// To prove linearity property


// a1 x1(t) + a2 x2(t) <-------> a1 X1(t) + a2 X2(t)

a1=0.4;
a2=0.5;

x1=[1 2 3];

Department of Electronics & Communication Page 12


DSP Lab Manual

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

// To prove Parsevel Theorem

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:

Linearity property is proved

Parsevel Theorem is proved

Department of Electronics & Communication Page 13


DSP Lab Manual

Experiment 4

Additional DFT property verification and DFT of square pulse and sinc
pulse
Aim:

I. To verify circular time shift and circular frequency shift.


II. To Plot the DFT of sinc pulse
III. To plot the DFT of square pulse.

To verify circular time shift and circular frequency shift

// To prove the following properties


// Circular time shift
// Circular frequency shift

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);

m=input('Enter the shift');

//To prove circular time shift


// x(n-m)<---->WN^mk X(k)
// To prove IDFT[WN^mk X(k)] =x(n-m)

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');

Department of Electronics & Communication Page 14


DSP Lab Manual

else
disp('Ciruclar time shift property fails');
end

// To prove circular frequency shift


// WN^-mn x(n)<----> X(k-m)
// To prove DFT[WN^-mn . x(n)] = X(k-m)

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:

Enter the shift2


Circular time shift property is proved
Circular frequency shift property is proved

To Plot the DFT of sinc pulse

//program to compute DFT of sinc pulse.

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;

Department of Electronics & Communication Page 15


DSP Lab Manual

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;

Department of Electronics & Communication Page 16


DSP Lab Manual

To Plot the DFT of sinc pulse

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;

Department of Electronics & Communication Page 17


DSP Lab Manual

Experiment 5

Circular Convolution and verification of its properties


Aim: To compute the circular convolution of two sequences and verification of
commutative, associative and distributive property

To compute the circular convolution of two sequences

clc;
clear;
close();

x=input('Enter the first sequence');


h=input('Enter the second sequence');

length_x=length(x);
length_h=length(h);

// Making both sequences of equal length


N=max(length_x,length_h);
a=[x zeros(1,N-length_x)];
b=[h zeros(1,N-length_h)];

// Using the circular convolution formula


for i=1:N
y(i)=0;
for j=1:N
y(i)=y(i)+a(j)*b(pmodulo(i-j,N)+1);
end
end

disp('The circular convolution using formula');


disp(y);

// Checking the result using convolution theorem


X=fft(a);
H=fft(b);

y1=fft(X.*H,1);
disp('The circular convolution using DFT/IDFT is-->');
disp(y1);

Department of Electronics & Communication Page 18


DSP Lab Manual

Result:

Enter the first sequence[1 2 3 4]

Enter the second sequence[3 4 5 6]

The circular convolution using formula

46.

48.

46.

40.

The circular convolution using DFT/IDFT is-->

46. 48. 46. 40.

To verify circular convolution properties

// Program to verify the following convolution properties


// 1 Commutative Property :a[n]*b[n]=b[n]*a[n]
// 2 Associative Property :a[n]*(b[n]*c[n])=(a[n]*b[n])*c[n]
// 3 Distributive Propery :a[n]*b[n]+a[n]*c[n]=a[n]*(b[n]+c[n])
// Note: The sequence lengths should be same

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

x1=input('Enter the first sequence');


x2=input('Enter the second sequence');

Department of Electronics & Communication Page 19


DSP Lab Manual

x3=input('Enter the third sequence');

length_x1=length(x1);
length_x2=length(x2);
length_x3=length(x3);

// Making both sequences of equal length


N=max(length_x1,length_x2,length_x3);
a=[x1 zeros(1,N-length_x1)];
b=[x2 zeros(1,N-length_x2)];
c=[x3 zeros(1,N-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

// To prove Associative property


lhs_A=cconv(a,cconv(b,c));
rhs_A=cconv(cconv(a,b),c);

disp(lhs_A);
disp(rhs_A);

if(lhs_A==rhs_A)
disp('Associative property is proved');
else
disp('Associative property fails');
end

// To prove Distributive property


lhs_D=cconv(a,b)+cconv(a,c);
rhs_D=cconv(a,b+c);

disp(lhs_D);
disp(rhs_D);

if(lhs_D==rhs_D)

Department of Electronics & Communication Page 20


DSP Lab Manual

disp('Distributive property is proved');


else
disp('Distributive property fails');
end

Enter the first sequence[1 2 3]

Enter the second sequence[4 5 6]

Enter the third sequence[5 6 7]

Result:

Commutative property is proved

Associative property is proved

Distributive property is proved

Department of Electronics & Communication Page 21


DSP Lab Manual

Experiment 6

Cross correlation and auto correlation and verification of its


properties
Aim:

I. To perform auto correlation and cross correlation using SCILAB


II. To verify properties of cross correlation
III. To verify properties of auto correlation

To perform cross correlation and auto correlation

clc;
clear;
close();

x1=input('Enter the first sequence');


x2=input('Enter the second sequence');

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

disp('Correlation using the formula is given by');


disp(y);

disp('Correlation using the builtin function is given by');


disp(xcorr(x1,x2));

Department of Electronics & Communication Page 22


DSP Lab Manual

Output for cross correlation

Enter the first sequence[1 2 3]


Enter the second sequence[4 5 6]

Correlation using the formula is given by

6. 17. 32. 23. 12.

Correlation using the builtin function is given by

6. 17. 32. 23. 12.

Output for auto correlation

Enter the first sequence[1 2 3]


Enter the second sequence[1 2 3]

Correlation using the formula is given by

3. 8. 14. 8. 3.

Correlation using the built-in function is given by

3. 8. 14. 8. 3.

To verify properties of auto correlation

clc;
clear;
close();

x=[1 4 2 3];
rxx=xcorr(x,x);

//Verification of Symmetry property


rxxcenter=ceil(length(rxx)/2);
rxxleft=rxx(rxxcenter:-1:1)
rxxright=rxx(rxxcenter:1:(length(rxx)))
if(rxxleft==rxxright)
disp('symmetry property proved');

Department of Electronics & Communication Page 23


DSP Lab Manual

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:

symmetry property proved

Upper bound property Rxx(0)>=Rxx(k) is verified

Mean square value=correlation value at zero shift

To verify properties of cross correlation

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;

Department of Electronics & Communication Page 24


DSP Lab Manual

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:

Property rxy(-m)=ryx is verified

Property Rxy^2<=Rxx(0).Ryy(0) is verified

Property Rxy(m)<=1/2[Rxx(0)+Ryy(0)] is verified

Department of Electronics & Communication Page 25


DSP Lab Manual

Experiment 7

Solving a difference equation


Aim:

Consider the equation y(n)=0.2x(n)+0.4x(n-1)+0.5y(n-2)


1. Calculate and plot the impulse response h(n) at n=-20,...100
2. Calculate and plot the unit response s(n) at n=-20,...100

clc;
clear;
close();

B=[0.2 0.4];
A=[1 -0.5];

x = [1 zeros(1,9)];// Impulse input


n=0:9
h=filter(B,A,x);// Impulse response

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();

Department of Electronics & Communication Page 26


DSP Lab Manual

Result:

Department of Electronics & Communication Page 27


DSP Lab Manual

Experiment 8

IIR Filter implementation for the given specification

Aim: Design a digital butterworth filter for the given specifications

Design a digital low pass butterworth filter with the following specifications:

1. 3-db attenuation at the pass band frequency of 1.5KHz


2. 10-db stopband attenuation at the frequency of 3KHz
3. Sampling frequency at 8000Hz

clc;
clear;
close();

kp=input('enter the pass band ripple: ');


ks=input('enter the stop band attenuation: ');
fp=input('enter the pass band edge frequency: ');
fs=input('enter the stop band edge frequency: ');
fsamp=input('enter the sampling frequency: ');

// Conversion to angular frequency


wp=2*%pi*fp*1/fsamp;
ws=2*%pi*fs*1/fsamp;

// Performing pre warpping


ohmp_prewarp = 2*fsamp*tan(wp/2);
ohms_prewarp = 2*fsamp*tan(ws/2);

// Using the backward design equation


ohms=ohms_prewarp/ohmp_prewarp;

// 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);

Department of Electronics & Communication Page 28


DSP Lab Manual

// Perform low pass to low pass tansformation


s=poly(0,'s');
Hds=horner(hs,s/ohmp_prewarp);
disp('The transfer function after lowpass to lowpass transformation is')
disp(Hds);

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);

[hzm ,fr ]= frmag (Hz ,512) ;


magz =20* log10(hzm);

plot2d (fr*fsamp ,magz);


xtitle ( 'Magnitude response' , 'w(Hz)' , 'Magnitude (dB)');

Inputs:

enter the pass band ripple: -3

enter the stop band attenuation: -10

enter the pass band edge frequency: 1500

enter the stop band edge frequency: 3000

enter the sampling frequency: 8000

Department of Electronics & Communication Page 29


DSP Lab Manual

Design a digital high pass butterworth filter with the following specifications:

0.5-db attenuation at the pass band frequency of 3KHz


25-db stopband attenuation at the frequency of 1KHz
Sampling frequency at 8000Hz

clc;
clear;
close();

kp=input('enter the pass band ripple: ');


ks=input('enter the stop band attenuation: ');
fp=input('enter the pass band edge frequency: ');
fs=input('enter the stop band edge frequency: ');
fsamp=input('enter the sampling frequency: ');

// Conversion to angular frequency


wp=2*%pi*fp*1/fsamp;
ws=2*%pi*fs*1/fsamp;

Department of Electronics & Communication Page 30


DSP Lab Manual

// Performing pre warpping


ohmp_prewarp = 2*fsamp*tan(wp/2);
ohms_prewarp = 2*fsamp*tan(ws/2);

// Using the backward design equation


ohms=ohmp_prewarp/ohms_prewarp;

// 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);

// Perform lowpass to high pass tansformation


s=poly(0,'s');
Hds=horner(hs,ohmp_prewarp/s);
disp('The transfer function after lowpass to highpass transformation is')
disp(Hds);

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);

[hzm ,fr ]= frmag (Hz ,512) ;


magz =20* log10(hzm);

plot2d (fr*fsamp ,magz);


xtitle ( 'Magnitude response' , 'w(Hz)' , 'Magnitude (dB)');

Inputs:

enter the pass band ripple: -0.5

enter the stop band attenuation: -25

enter the pass band edge frequency: 3000

Department of Electronics & Communication Page 31


DSP Lab Manual

enter the stop band edge frequency: 1000

enter the sampling frequency: 8000

Department of Electronics & Communication Page 32


DSP Lab Manual

Experiment 9

FIR Filter implementation for the given specification


Design a lowpass FIR filter for the following specifications:

Pass band edge frequency = 1850 Hz


Stop band edge frequency = 2150 Hz
Stopband attenuation = 20 db
Sampling rate= 8000Hz

clc;
clear;
close();

fp=1850;
fs=2150;
fsamp=8000;

// To find the order of the filter N .


k=1;
trans_width=(fs-fp)/fsamp;
N=ceil(k/trans_width);

// To make even order to odd order


remainder = N-fix(N./2) .*2;
if remainder ==0
N=N+1;
end

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;

Department of Electronics & Communication Page 33


DSP Lab Manual

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;

Department of Electronics & Communication Page 34


DSP Lab Manual

Design a high pass FIR filter for the following specifications:

Pass band edge frequency = 2500 Hz


Stop band edge frequency = 1500 Hz
Stopband attenuation = 40 db
Sampling rate= 8000Hz

clc;
clear;
close();

fp=2500;
fs=1500;
fsamp=8000;

// To find the order of the filter N .


k=3;
trans_width=(fp-fs)/fsamp;
N=ceil(k/trans_width);

// To make even order to odd order


remainder = N-fix(N./2) .*2;
if remainder ==0
N=N+1;
end

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));

Department of Electronics & Communication Page 35


DSP Lab Manual

xtitle ('Phase response' , 'w(Hz)' , 'Phase(Deg)');


a=gca();
a.children.children(1).thickness=2;

Department of Electronics & Communication Page 36


DSP Lab Manual

DSP Lab Hardware programs

Steps to use CC studio and Hardware kit

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.

Department of Electronics & Communication Page 37


DSP Lab Manual

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.

Department of Electronics & Communication Page 38


DSP Lab Manual

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];

printf("\n enter length of x(n)\n");

scanf("%d",&m);

printf("enter length h(n)\n");

scanf("%d",&n);

printf("enter the sequence x(n):\n");

for(i=0; i<m; i++)

scanf("%d",&x[i]);

printf("enter the sequence h(n):\n");

for(i=0; i<n; i++)

scanf("%d",&h[i]);

for(i=m; i<m+n-1; i++)

x[i]=0;

for(i=n; i<m+n-1; i++)

h[i]=0;

Department of Electronics & Communication Page 39


DSP Lab Manual

for(i=0; i<m+n-1; i++)

y[i]=0;

for(j=0; j<=i; j++)

y[i]+=x[j]*h[i-j];

printf("The convolved sequence is:\n");

for(i=0; i<m+n-1; i++)

printf("%d\n",y[i]);

Department of Electronics & Communication Page 40


DSP Lab Manual

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];

printf("enter length of x(n)\n");

scanf("%d",&m);

printf("enter length h(n)\n");

scanf("%d",&n);

printf("enter the sequence x(n):\n");

for(i=0; i<m; i++)

scanf("%d",&x[i]);

printf("enter the sequence h(n):\n");

for(i=0; i<n; i++)

scanf("%d",&h[i]);

for(i=m; i<m+n; i++)

x[i]=0;

for(i=n; i<m+n; i++)

h[i]=0;

if(m>n)

Department of Electronics & Communication Page 41


DSP Lab Manual

N=m;

else

N=n;

for(i=0; i<N; i++)

y[i]=0;

for(j=0; j<N; j++)

k=i-j;

if(k<0)

k=k+N;

y[i]+=x[j]*h[k];

printf("The convolved sequence is:\n");

for(i=0; i<N; i++)

printf("%d\t",y[i]);

Department of Electronics & Communication Page 42


DSP Lab Manual

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;

printf("enter the length of the dft req\n");

scanf("%d",&M);

printf("enter the length of the seq given");

scanf("%d",&N);

printf("enter the seq x(n)\n");

for(i=0;i<N;i++)

scanf("%d",&x[i]);

if(M-N!=0)

if(M>N)

for(i=N;i<M;i++)

Department of Electronics & Communication Page 43


DSP Lab Manual

x[i]=0;

N=M;

printf("the dft is\n");

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);

Department of Electronics & Communication Page 44


DSP Lab Manual

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)

Note: The coefficient of y(n) should always be 1

The above equation can also be written as:

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;

float cx[3]={0.1311, 0.2622, 0.1311};

float cy[3]={1, -0.7478, 0.2722};

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]);

Department of Electronics & Communication Page 45


DSP Lab Manual

z[j]=y[0];

y[2]=y[1];

y[1]=y[0];

x[2]=x[1];

x[1] = x[0];

Department of Electronics & Communication Page 46

You might also like