Digital Signal Processing
Laboratory Manual

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

As an exercise, it is recommended to go through the basics of Signals & Sytems, Digital Signal Processing and some details about DSP hardware & processors.

Prerequisites: 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.

Matlab Programs .

title('Undersampling plot').'b'. plot(t. fs1=1.n2. t=0:0.n3. fs2=2*f. subplot(311). xlabel('time'). .'r*-'). xlabel('time').'r*-'). Sampling Theorem clear all. f=input('Enter the analog frequency. title('Nyquist plot').1.n1. n2=0:1/fs2:tf. subplot(313). close all. subplot(312). xt=cos(2*pi*f*t).3*f. xn=cos(2*pi*f*n3). fs3=6*f.xn.xt.05.xn.xn. tf=0. xlabel('time').'r*-'). xn=cos(2*pi*f*n1).f = '). ylabel('Amplitude'). plot(t. clc.xt. xn=cos(2*pi*f*n2).'b'.'b'. n3=0:1/fs3:tf. title('Oversampling plot'). ylabel('Amplitude'). plot(t.xt. n1=0:1/fs1:tf.00005:tf. ylabel('Amplitude').

f = 200 .Output Enter the analog frequency.

a.h). . clc. b=input('Coefficients of x[n] terms = '). xlabel('Real part'). h=impz(b. disp('Difference Equation of a digital system'). ylabel('Imaginary part'). n=0:1:N-1.N). title('Poles and Zeros of H[z] in Z-plane'). disp(h). a=input('Coefficients of y[n] terms = ').2. figure(1). disp('Impulse response of the system is h = '). ylabel('h[n]'). figure(2). close all. zplane(b. stem(n. N=input('Desired Impulse response length = '). xlabel('time index'). Impulse Response clear all. title('Impulse response').a).

9] .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.Output [Given y(n)-y(n-1)+0.

ylabel('y[n]'). axis([1 num low high]). clc. x_pad=[x.h])+5. new_mat(:.j)=new_mat(num. for i=2:num new_mat(i. new_matrix=zeros(num. stem(x).h])-5.3.x.num-m)].1). m=length(h). xlabel('n'). subplot(1. x=input('x[n]= ').2). Linear Convolution clear all. end end result=new_mat*h_pad'. stem(result').zeros(1.3).1)=x_pad'. for j=2:num new_mat(1. xlabel('n'). axis([1 num low high]). disp(result'). ylabel('h[n]'). l=length(x).j-1). subplot(1. high=max([result'.3.num-l)]. subplot(1. ylabel('x[n]').zeros(1. axis([1 num low high]). h_pad=[h.j-1).3. num=(l+m)-1. xlabel('n').3.num). close all.j)=new_mat(i-1. h=input('h[n]='). . disp('y[n]= '). stem(h).x. low=min([result'.

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

axis([1 num low high]). ylabel('x[n]').h])+5. stem(result'). subplot(1. xlabel('n'). stem(h).3. high=max([result'.1)=x'. new_mat(:. subplot(1. Circular Convolution clear all.j)=new_mat(num. new_matrix=zeros(num. subplot(1. .x. low=min([result'. end end result=new_mat*h'. disp('y[n]= ').num). h=input('h[n]= ').x. for i=2:num new_mat(i.j-1). close all. xlabel('n'). num=input('Enter the length of the sequences = ').h])-5.3. axis([1 num low high]).2). for j=2:num new_mat(1. stem(x). ylabel('y[n]').1).3.j-1). disp(result'). x=input('x[n]= ').3). ylabel('h[n]').j)=new_mat(i-1. xlabel('n'). axis([1 num low high]).4. clc.

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 .

t=-(L-1):(L-1). [max_val index]=max(Rxx). stem(EDS). stem(t. disp(max_Rxx). max_Rxx=max(Rxx). ylabel('Rxx[n]'). X_k=fft(x_n. if(Rxx==fliplr(Rxx)) disp('Rxx[n] and Rxx[-n] are identical'). title('Enegry density of x[n]'). ylabel('EDS').x_minus_n). ylabel('x[n]'). if(index==L) disp('maximum is at the origin'). xlabel('n'). EDS=abs(X_k). subplot(2. close all. disp('Hence Auto correlation has even symmetry'). xlabel('n'). else disp('Rxx[n] and Rxx[-n] are not identical'). disp('Energy of x[n] = ').L-1).2). Rxx=conv(x_n. L=length(x_n).2. subplot(2.[zeros(1.^2). end disp('Verification of property 2'). else disp('maximum is not at the origin'). . disp('Verification of property 1').2. x_minus_n=fliplr(x_n).1). x_n=input('x[n]= '). disp('Maximun of Rxx[n] = ').2. disp('Hence maximum of Rxx is equal to Energy of x[n]'). clc.N). energy=sum(x_n. stem(t. end disp('Verification of property 3').N). disp(energy).5. subplot(2. xlabel('n'). title('Autocorrelation').^2.3). Auto-correlation & its properties clear all.x_n]). Rxx_k=fft(Rxx.Rxx). N=length(Rxx).

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

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

N=length(x_n).y_minus_n). if(Rxy==fliplr(Ryx)) disp('are equal'). else disp('are not equal'). end disp('Verification of property 3'). end disp('DFT Rxy[n]=X[k]. stem(t.length(Rxy)). x_n=input('x[n]= '). subplot(3. Ryx=conv(x_minus_n. xlabel('n'). Rxy=conv(x_n. x_minus_n=fliplr(x_n).2. t=-(N-1):(M-1). subplot(3. y_minus_n=fliplr(y_n). disp('Verification of property 1'). temp3=abs(temp2).2). . xlabel('n'). X_k=fft(x_n. Rxy_k=abs(temp). end disp('Verification of property 2').[y_minus_n. ylabel('y[-n]').y_n). if(Rxy(M+1)==0) disp('are orthogonal'). disp('Rxy[n]and Ryx[n] '). temp2=(X_k). Cross-correlation & its properties clear all.*conj(Y_k). disp('The sequences '). stem(t. y_n=input('y[n]= ').6.2. Y_k=fft(y_n.M-1). if(Rxy==Ryx) disp('are not commutative').length(Rxy)). ylabel('x[n]').[zeros(1.zeros(1. M=length(y_n). else disp('are commutative'). else disp('are not orthogonal'). clc. temp=fft(Rxy).x_n]). t2=-(M-1):(N-1). disp('Rxy[n]and Ryx[-n] ').1). close all.Y[k]').N-1)]).

subplot(3. xlabel('n'). ylabel('X[k].4).Rxy). subplot(3. stem(t2. title('Crosscorrelation').subplot(3.6). subplot(3. . title('DFT of Rxy[n]'). xlabel('k'). stem(temp3). ylabel('Rxy[k]').3) stem(t. xlabel('n').2.2. ylabel('Rxy[n]'). ylabel('Ryx[n]'). title('DFT x[n].DFT y[n]').Ryx). xlabel('k').5). title('Crosscorrelation').2.Y[k]').2. stem(Rxy_k).

Y[k] .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].

1. a=input('Coefficients of y[n] terms = '). 7. y_natural=y_complete-y_forced. stem(y_natural). %yi of length 1 to (a-1) %xi of length 1 to (b-1) initialc=filtic(b.clear all. title('Natural response of the system').1. stem(y_complete).a. y_forced=filter(b. title('Complete response of the system'). %of length 1 to 100 yi=input('Enter the initial conditions of y = ').1).yi). b=input('Coefficients of x[n] terms = ').a. subplot(3. subplot(3. title('Forced response of the system'). %if necessary %xi=('Enter the initial conditions of x = ').2).xn. stem(y_forced).yi. Difference Equation .a.initialc).xi). close all. subplot(3.1.3). %initialc=filtic(b. y_complete=filter(b. clc. xn=input('Enter the input exitation x[n] = ').a. disp('Enter the parameters of Difference Equation of a digital system').xn).

9] Enter the input exitation x[n] = [1 zeros(1.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.99)] Enter the initial conditions of y = [-1 0 0] .

N-point DFT clear all. x=input('Enter the sequence x[n]= '). xlabel('frequency index k'). axis([0 N+1 -2 max(mag)+2]). disp(X_k). stem(mag).j)=exp(complex(0. mag=abs(X_k).1. end end X_k=DFT_mat*x_n'. N=input('Enter the value N point= ').2).zeros(1. ylabel('Magnitude of X[k]'). clc. phase=angle(X_k)*180/pi. stem(phase). close all. subplot(2. xlabel('frequency index k').1).8. . subplot(2. x_n=[x. for i=1:N for j=1:N temp=-2*pi*(i-1)*(j-1)/N. L=length(x). ylabel('Phase of X[k]').N-L)]. DFT_mat(i. axis([0 N+1 -180 180]).temp)). disp('N point DFT is X[k] = ').1.

0.0000 -2.2.0000 .0000 + 2.0000i -2.0000i .0000i -2.0000 .Output Enter the sequence x[n]= [1 2 3 4] Enter the value N point= 4 N point DFT is X[k] = 10.

Linear Convolution using DFT & IDFT clear all. m=length(h).x. stem(x). subplot(1. h=input('h[n]= '). subplot(1. HN=fft(h. xlabel('n'). xlabel('n'). ylabel('h[n]'). axis([1 num low high]). xlabel('n'). disp(y). high=max([y. ylabel('y[n]'). subplot(1. stem(h).3. num=(l+m)-1.1). y=ifft(YN.9. stem(y).3.2). . disp('y[n]= '). x=input('x[n]= '). axis([1 num low high]).x. clc. XN=fft(x. close all.num). axis([1 num low high]).h])-5.3.h])+5.num). l=length(x). low=min([y. YN=XN.*HN.3).num). ylabel('x[n]').

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

subplot(1.N). YN=XN. xlabel('n'). . stem(y).h])-5. axis([1 N low high]). clear all. subplot(1. ylabel('h[n]'). x=input('x[n]= '). clc.*HN. xlabel('n'). xlabel('n'). axis([1 N low high]). y=ifft(YN. disp(y).3.x. low=min([y.10. XN=fft(x. N=length(x).3).3. h=input('h[n]= '). subplot(1. axis([1 N low high]).N). close all. disp('y[n]= ').1). ylabel('x[n]'). stem(x). Circular Convolution using DFT & IDFT disp('Enter the sequences of equal lengths').2). high=max([y.N).h])+5. stem(h).3.x. HN=fft(h. ylabel('y[n]').

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 .

title('Phase response').11. grid. %wn=[w1 w2]. grid. FIR Filters wn=input('Enter the passband frequency between 0 & 1 (normalised) = ').XXXXXXX(n)). xlabel('Normalised frequency'). %replace by following for other windows %blackman(n). title('Impulse response'). subplot(2. xlabel('Normalised frequency'). Highpass window %b=fir1(n.wn. xlabel('Time index n'). figure(1). %kaiser(n.512).'high'. %barlett(n). plot(w/pi.dB). Bandpass window %b=fir1(n.hamming(n+1)).angle(h)). . ylabel('Phase in degrees'). %beta=input('Enter the value of beta').'stop'.for Bandpass & Bandstop n=input('Enter the order of the filter = '). %hanning(n). Bandstop window [h.1. clear all.beta).2).1). %hann(n). ylabel('Magnitude in dB').XXXXXXX(n)).1. clc. %wn is a vector. dB=20*log10(abs(h)). ylabel('Amplitude').wn. subplot(2.1)). figure(2). %beta for Kaiser only b=fir1(n. plot(w/pi. %boxcar(n).wn.wn.w]=freqz(b. title('Magnitude response'). %b=fir1(n. stem(impz(b.1.XXXXXXX(n)). close all.

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

12.1. mag=20*log10(abs(h)).a]=butter(N. plot(w. grid. figure(1). wp=input('Enter the passband edge Normalised frequency = ').mag). for Bandpass & Bandstop [N.Dp. xlabel('Normalised frequency').phase).a). Highpass filter %[b. %wp & ws are vectors.wn). subplot(2. clc.wn). for Bandpass & Bandstop Dp=input('Enter the passband attenuation level (dB) = ').wn]=buttord(wp. %wp=[w1 w2]. phase=180*angle(h)/pi. plot(w.Ds). ylabel('Magnitude'). Bandpass filter %[b.2). ylabel('Phase in degrees'). xlabel('Normalised frequency').w]=freqz(b.a]=butter( title('Magnitude response'). title('Butterworth Lowpass Filter').Butterworth clear all.a]=butter(N. %skip if N & 3dB cutoff frequency is known [b.1). . %wp=wn.a]=butter(N. plot(w. for 3dB cutoff frequency ws=input('Enter the stopband edge Normalised frequency = '). xlabel('Normalised frequency').1. Ds=input('Enter the stopband attenuation level (dB) = '). subplot(2. title('Phase response').'high'). close all.'stop'). %N=input('Enter the order of the filter = '). grid. ws=[w3 w4]. %replace by following for other filters %[b.wn. %order=N/2. grid. ylabel('Magnitude in dB').a IIR Filters .abs(h)). figure(2). Bandstop filter [h.

Output Enter Enter Enter Enter the the the the passband stopband passband stopband edge Normalised frequency = 0.3 edge Normalised frequency = 0.6 attenuation level (dB) = 3 attenuation level (dB) = 40 .

a]=cheby1(N. wp=input('Enter the passband edge Normalised frequency = ').Rp. . ylabel('Magnitude in dB'). plot(w. figure(2). ylabel('Magnitude'). subplot(2. %wp=[w1 w2]. for Bandpass & Bandstop Rp=input('Enter the passband attenuation level (dB) = '). %replace by following for other filters %[b.Chebyshev clear all.wn]=cheb1ord(wp. %N=input('Enter the order of the filter = '). close all.1).mag). %wp=wn. title('Phase response'). ylabel('Phase in degrees'). xlabel('Normalised frequency'). phase=180*angle(h)/pi. mag=20*log10(abs(h)).Rp. for 3dB cutoff frequency ws=input('Enter the stopband edge Normalised frequency = ').a). grid.a]=cheby1(N.Rp.abs(h)). Bandstop filter [h. %wp & ws are vectors. %order=N/2. for Bandpass & Bandstop [N.'stop').Rp.Rs). xlabel('Normalised frequency').1.wn.phase) Rs=input('Enter the stopband attenuation level (dB) = ').b IIR Filters . %skip if order and 3dB cutoff frequency is known [b.w]=freqz(b. title('Magnitude response'). Bandpass filter %[b.a]=cheby1(N. xlabel('Normalised frequency'). Highpass filter %[b. title('Chebyshev Lowpass Filter').1.wn).2).wn. clc.Rp. ws=[w3 w4].a]=cheby1(N. grid.wn). grid. subplot(2.'high'). plot(w. figure(1). plot(w.

6 attenuation level (dB) = 3 attenuation level (dB) = 40 .Output Enter Enter Enter Enter the the the the passband stopband passband stopband edge Normalised frequency = 0.3 edge Normalised frequency = 0.

C Programs .

x[30].&m).h> 1. for(i=0. scanf("%d".i++) printf("%d ". for(i=0. for(i=0. } .i.&n).h[30]. for(j=0. for(i=m.i++) x[i]=0. printf("Enter x\n"). printf("Enter h\n").&h[i]).i++) scanf("%d".j.i<n.y[i]).i<m.j<=i.#include <stdio. } printf("Linear convolution. scanf("%d". y is ").i++) h[i]=0. for(i=0.i++) { y[i]=0. Linear Convolution int m.i<m+n-1.i<m+n-1.n.h> #include <math.i<m+n-1.&x[i]).j++) y[i]+=x[j]*h[i-j]. printf("Enter the length of h\n").y[30].i++) scanf("%d".i<m+n-1. for(i=n. void main() { printf("Enter the length of x\n").

y is 2 5 8 11 4 .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.

} .k++) { y[k]=0.i<m.h> #include <math. for(i=1.&m).i++) a[i]=h[m-i].j.h> 2.k. for(k=1. for(i=0.b[30].i++) printf("%d ". } printf("Circular convolution.&x[i]).h[30]. printf("Enter h\n"). for(i=0.&h[i]). for(i=1.i++) b[i]=a[i-1].#include <stdio.a[30]. a[0]=h[0].j++) y[k]+=x[j]*a[j].i<m.i++) a[i]=b[i].x[30].i++) scanf("%d".i<m.i++) scanf("%d". for(j=0.y[i]). for(i=0.i<m.i. y is "). Circular Convolution int m.i++) y[0]+=x[i]*a[i]. scanf("%d". for(i=0.y[30]. printf("Enter x\n"). void main() { printf("Enter the length of x\n").k<m.i<m. b[0]=a[n-1].i<m.j<m.i<n. for(i=0. y[0]=0.n.

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

scanf("%d". sumIm+=x[j]*sine. printf("Enter the length of the sequence\n").out_real[i]. float cosine=0.3.i<N. float sumRe.j.out_imag[i]).j++) { cosine=cos(2*pi*i*j/N). printf("Enter x\n").sine=0. sine=sin(2*pi*i*j/N). short x[10].1f +j(%. printf("%.i++) { sumRe=0.&x[i]).1416.&n). sumRe+=x[j]*cosine.h> #include <math.0). for(j=0.sumIm.i<N.h> void main() { int i. out_imag[i]=sumIm. } out_real[i]=sumRe. } } . float pi=3. if(N>n) for(i=n. printf("N point DFT is\n").n. printf("Enter N\n").j<N. for(i=0. N-Point DFT #include <stdio.&N). for(i=n+1.1f)\n". sumIm=0. scanf("%d".i++) scanf("%d". float out_real[10]={0.i++) x[i]=0. short N.out_image[10]=(0.i<N.0}.

0 +j(-2.0) .Output Enter the length of the sequence 4 Enter N 4 Enter x 1 2 3 4 N point DFT is 10.0) -2.0) -2.0 +j(-0.0) -2.0 +j(2.0 +j(0.

Text Text Text Key Steps Commands for plotting a figure Comments .

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master Your Semester with a Special Offer from Scribd & The New York Times

Cancel anytime.