dsp lab manual digital edition

42

Upload: nelson-jacob

Post on 07-Mar-2015

839 views

Category:

Documents


17 download

TRANSCRIPT

Page 1: DSP Lab Manual Digital Edition
Page 2: DSP Lab Manual Digital Edition

Digital Signal Processing

Laboratory Manual

Page 3: DSP Lab Manual Digital Edition

Contents

1. Sampling Theorem 2. Impulse Response 3. Linear Convolution 4. Circular Convolution 5. Auto correlation & its properties 6. Cross correlation & its properties 7. Difference Equation 8. N-Point DFT 9. Linear Convolution using DFT & IDFT 10. Circular Convolution using DFT & IDFT 11. FIR Filters 12. a. IIR Filters - Butterworth

b. IIR Filters - Chebyshev Type I

1. Linear Convolution 2. Circular Convolution 3. N-Point DFT

Page 4: DSP Lab Manual Digital Edition

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.

Page 5: DSP Lab Manual Digital Edition

Matlab Programs

Page 6: DSP Lab Manual Digital Edition

1. Sampling Theorem

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

Page 7: DSP Lab Manual Digital Edition

Output

Enter the analog frequency,f = 200

Page 8: DSP Lab Manual Digital Edition

2. Impulse Response clear 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');

Page 9: DSP Lab Manual Digital Edition

Output [Given y(n)-y(n-1)+0.9y(n-2)= x(n)] Difference Equation of a digital system Desired Impulse response length = 100 Coefficients of x[n] terms = 1 Coefficients of y[n] terms = [1 -1 0.9]

Page 10: DSP Lab Manual Digital Edition

3. Linear Convolution clear 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]);

Page 11: DSP Lab Manual Digital Edition

Output x[n]= [1 2 3 4] h[n]=[2 1] y[n]=

2 5 8 11 4

Page 12: DSP Lab Manual Digital Edition

4. Circular Convolution clear 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); end

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

Page 13: DSP Lab Manual Digital Edition

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

Page 14: DSP Lab Manual Digital Edition

5. Auto-correlation & its properties clear 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');

else disp('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]');

Page 15: DSP Lab Manual Digital Edition

subplot(2,2,4); stem(abs(Rxx_k)); xlabel('k'); ylabel('Rxx[k]'); title('DFT of Rxx[n]');

Page 16: DSP Lab Manual Digital Edition

Output x[n]= [1 2 3 4] Verification of property 1 Rxx[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]

Page 17: DSP Lab Manual Digital Edition

6. Cross-correlation & its properties clear 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'); else disp('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]');

Page 18: DSP Lab Manual Digital Edition

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

Page 19: DSP Lab Manual Digital Edition

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]

Page 20: DSP Lab Manual Digital Edition

7. Difference Equation clear 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');

Page 21: DSP Lab Manual Digital Edition

Output Enter the parameters of Difference Equation of a digital system Coefficients of x[n] terms = 1 Coefficients 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]

Page 22: DSP Lab Manual Digital Edition

8. N-point DFT clear 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)); end end 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]);

Page 23: DSP Lab Manual Digital Edition

Output Enter the sequence x[n]= [1 2 3 4] Enter the value N point= 4 N point DFT is X[k] =

10.0000 -2.0000 + 2.0000i -2.0000 - 0.0000i -2.0000 - 2.0000i

Page 24: DSP Lab Manual Digital Edition

9. Linear Convolution using DFT & IDFT clear 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]);

Page 25: DSP Lab Manual Digital Edition

Output x[n]= [1 2 3 4] h[n]= [2 1] y[n]= 2 5 8 11 4

Page 26: DSP Lab Manual Digital Edition

10. Circular Convolution using DFT & IDFT clear 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]);

Page 27: DSP Lab Manual Digital Edition

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

Page 28: DSP Lab Manual Digital Edition

11. FIR Filters clear 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');

Page 29: DSP Lab Manual Digital Edition

Output Enter the passband frequency between 0 & 1 (normalised) = 0.3 Enter the order of the filter = 20

Page 30: DSP Lab Manual Digital Edition

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;

Page 31: DSP Lab Manual Digital Edition

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

Page 32: DSP Lab Manual Digital Edition

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;

Page 33: DSP Lab Manual Digital Edition

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

Page 34: DSP Lab Manual Digital Edition

C Programs

Page 35: DSP Lab Manual Digital Edition

1. Linear Convolution #include <stdio.h> #include <math.h> 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<m;i++)

scanf("%d",&x[i]); printf("Enter h\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;

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("Linear convolution, y is "); for(i=0;i<m+n-1;i++)

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

Page 36: DSP Lab Manual Digital Edition

Output Enter the length of x 4 Enter the length of h 2 Enter x 1 2 3 4 Enter h 2 1 Linear convolution, y is 2 5 8 11 4

Page 37: DSP Lab Manual Digital Edition

2. Circular Convolution #include <stdio.h> #include <math.h> 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<m;i++)

scanf("%d",&x[i]); printf("Enter h\n"); for(i=0;i<n;i++)

scanf("%d",&h[i]); a[0]=h[0]; for(i=1;i<m;i++)

a[i]=h[m-i]; y[0]=0; for(i=0;i<m;i++)

y[0]+=x[i]*a[i]; for(k=1;k<m;k++) {

y[k]=0; b[0]=a[n-1]; for(i=1;i<m;i++)

b[i]=a[i-1]; for(i=0;i<m;i++)

a[i]=b[i]; for(j=0;j<m;j++)

y[k]+=x[j]*a[j]; } printf("Circular convolution, y is "); for(i=0;i<m;i++)

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

Page 38: DSP Lab Manual Digital Edition

Output Enter the length of x 4 Enter x 1 2 3 4 Enter h 2 1 2 1 Circular convolution, y is 14 16 14 16

Page 39: DSP Lab Manual Digital Edition

3. N-Point DFT #include <stdio.h> #include <math.h> 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;i<N;i++) scanf("%d",&x[i]); if(N>n)

for(i=n;i<N;i++) x[i]=0;

printf("N point DFT is\n"); for(i=0;i<N;i++) {

sumRe=0; sumIm=0; for(j=0;j<N;j++) {

cosine=cos(2*pi*i*j/N); sine=sin(2*pi*i*j/N); sumRe+=x[j]*cosine; sumIm+=x[j]*sine;

} out_real[i]=sumRe; out_imag[i]=sumIm; printf("%.1f +j(%.1f)\n",out_real[i],out_imag[i]);

} }

Page 40: DSP Lab Manual Digital Edition

Output Enter the length of the sequence 4 Enter 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)

Page 41: DSP Lab Manual Digital Edition

Text Key Steps Text Commands for plotting a figure Text Comments

Page 42: DSP Lab Manual Digital Edition