Anda di halaman 1dari 30

# DEPARTMENT OF TELECOMMUNICATION PES INSTITUTE OF TECHNOLOGY

## Digital Signal Processing Lab

Practical Record
Anvesh Gopalam 1PI09TE015

## Experiment 1: Program to Verify Sampling Theorem

Code: clc; clear all; t=0:.001:0.1; f1=input ('Enter the input frequency1 f2=input ('Enter the input frequency2 y=cos(2*pi*f1*t)+cos(2*pi*f2*t); f3=max(f1,f2); % under sampling fs=f3; %fs = sampling freequency ts=1/fs; tx=0:ts:0.1; m=max(size(tx)); ys=cos(2*f1*pi*tx)+cos(2*pi*f2*tx); figure(1); subplot(3,1,1); plot(t,y); grid on; title('The sinusoidal signal cos(2*pi*f1*t)+cos(2*pi*f2*t)'); xlabel('Time in seconds'); ylabel('Amplitude in volts'); subplot(3,1,2); stem(tx,ys); grid on; title('The sinusoidal signal sampled at fs Hz'); xlabel('Time in seconds'); ylabel('Amplitude in volts'); subplot(3,1,3); plot(tx,ys); grid on; title('The recovered sinusidal sampled at fs Hz'); xlabel('Time in seconds'); ylabel('Amplitude in volts'); % Right sampling fs=2*f3; %fs = sampling frequency is twice the max f ts=1/fs; tx=0:ts:0.1; m=max(size(tx)); ys=cos(2*pi*f1*tx)+cos(2*pi*f2*tx); figure(2); subplot(3,1,1); plot(t,y); grid on; title('The sinusoidal signal cos(2*pi*f1*t)+cos(2*pi*f2*t)'); xlabel('Time in seconds'); ylabel('Amplitude in volts'); subplot(3,1,2); stem(tx,ys); grid on; title('The sinusoidal signal sampled at fs Hz'); xlabel('Time in seconds'); ylabel('Amplitude in volts'); subplot(3,1,3); plot(tx,ys); grid on; = '); = ');

title('The recovered sinusidal sampled at fs Hz'); xlabel('Time in seconds'); ylabel('Amplitude in volts'); % over sampling fs=3*f3; %fs = sampling frequency is greater than twice the max f ts=1/fs; tx=0:ts:0.1; m=max(size(tx)); ys=cos(2*pi*f1*tx)+cos(2*pi*f2*tx); figure(3); subplot(3,1,1); plot(t,y); grid on; title('The sinusoidal signal cos(2*pi*f1*t)+cos(2*pi*f2*t)'); xlabel('Time in seconds'); ylabel('Amplitude in volts'); subplot(3,1,2); stem(tx,ys); grid on; title('The sinusoidal signal sampled at fs Hz'); xlabel('Time in seconds'); ylabel('Amplitude in volts'); subplot(3,1,3); plot(tx,ys); grid on; title('The recovered sinusidal sampled at fs Hz'); xlabel('Time in seconds'); ylabel('Amplitude in volts');

Output:
1. Under Sampling:

2. Over Sampling

3. Right Sampling

## Experiment 2: Program to find the Impulse response of the system

Code: %a0*y(n)-a1*y(n-1)+a2*y(n-2)=b0*x(n)+b1*x(n-1)+b2*x(n-2) clc; clear all; close all; b=input('Enter the coefficients of x(n) in the order x(n),x(n1)...in the Matrix form = '); a=input('Enter the coefficients of y(n) in the order y(n),y(n1)...in the Matrix form = '); n=-5:20; x=(n==0); h=filter(b,a,x) figure(1); subplot(2,1,1); stem(n,x); xlabel('n'); ylabel('amplitude'); title('impulse sequence'); subplot(2,1,2); stem(n,h); xlabel('n'); ylabel('amplitude'); title('impulse response');

Output:
X Coefficients: 0.2, 0.4, 0.6 Y Coefficients: 1.5, 1.99, 0.45 H coefficients: 0, 0, 0, 0, 0.133, 0.898, 0.2409, -3.465, 0.3874, -0.4101, 0.4278, -0.4445, -0.4614, 0.4787 Graph Output:

## Experiment 3: Program to perform linear Convolution of two given sequences

Code: clc; clear all; close all; x1= input('Enter first sequence '); x2= input('Enter second sequence '); n1=length(x1); n2=length(x2); N= n1 + n2 -1; %Gets the length of the sequence x1=[x1 zeros(1,N-n1)]; x2=[x2 zeros(1,N-n2)]; m=0:1:N-1; for n=0:N-1 y(n+1)=sum(x2(mod(n-m,N)+1).*x1); end; %Display and plotting disp('Linear convoluted sequence'); ydisp('First sequence'); x1 disp('Second sequence'); x2 disp('Convolved sequence'); ysubplot(3,1,1); stem(x1); title('First sequence'); xlabel('Signal'); ylabel('Time'); subplot(3,1,2); stem(x2); title('Second sequence'); xlabel('Signal'); ylabel('Time'); subplot(3,1,3); stem(y); title('Convolved sequence'); xlabel('Signal'); ylabel('Time');

Output:
Console output:

Graph output:

## Experiment 4: Program to perform circular convolution of two given sequences

Code: clc; clear all; x=input('Input first sequence '); h=input('Input second sequence '); N1=length(x); N2=length(h); N=max(N1,N2); x=[x zeros(1,N-N1)]; h=[h zeros(1,N-N2)]; %Calculation for m=1:N y(m)=0; for n=1:N i=m-n+1; if(i<=0) i=N+i; end y(m)=y(m)+x(n)*h(i); end end %display disp('First sequence'); x disp('Second sequence'); h disp('Convolved sequence'); y n=0:N-1; subplot(3,1,1); stem(x); title('First sequence'); ylabel('Signal'); xlabel('Time'); subplot(3,1,2); stem(h); title('Second sequence'); ylabel('Signal'); xlabel('Time'); subplot(3,1,3); stem(y); title('Convolved sequence'); ylabel('Signal'); xlabel('Time');

Output:
Console output:

Graph output:

## Experiment 5: Autocorrelation of a given sequence

Code: clc; clear all; close all; x=input('Enter the Sequence'); y=xcorr(x,x); %When the same sequence is passed to the xcorr fn, it returns the auto correlation %Display figure; subplot(2,1,1); stem(x); ylabel('Amplitude'); xlabel('n'); subplot(2,1,2); stem(fliplr(y)); %fliplr flips the sequence (Reverses) y; ylabel('Amplitude'); xlabel('n'); disp('The resultant signal is'); fliplr(y);

Output:
Console output:

Graph output:

## Experiment 6: Cross-Correlation of a given sequence

Code: clc; clear all; close all; x=input('Enter the Sequence'); h=input('Enter the Second Sequence'); xcorr(x,h); %Passes sequence x and h to the xcorr fn figure; subplot(3,1,1); stem(x); %Sequence X ylabel('Anplitude'); xlabel('n'); subplot(3,1,2); stem(h); %Sequence H ylabel('Anplitude'); xlabel('n'); subplot(3,1,3); stem(y); y %Correlated sequence ylabel('Anplitude'); xlabel('n'); disp('The resultant signal is '); fliplr(y);

Output:
Console:

Graph:

## Experiment 7: Program to solve differential equations

Code 1:
% y(n) -0.5 y(n-1) = x(n) with x(n)=u(n)-u(n-4) b=input('enter the coefficients of x'); a=input('enter the coefficients of y'); n=[-5:50]; x=[(n>=0)]-[(n>4)]; figure(1); subplot(2,1,1); stem(n,x); title('input sequence x(n)'); xlabel('n'); ylabel('x'); subplot(2,1,2); y=filter(b,a,x); stem(n,y); title('output sequence y(n)'); xlabel('n'); ylabel('y');

Code 2:
% y(n) -0.25 y(n-1) -0.125 y(n-2)= x(n) with x(n)=u(n)-u(n-10) and initial condition y(-1)=1, y(-2)=-2 b=input('enter the coefficients of x'); a=input('enter the coefficients of y'); c=input('enter the initial conditions'); n=[-5:50]; ic=filtic(b,a,c); x=[(n>=0)]-[(n>10)]; y=filter(b,a,x,ic); subplot(2,1,1); stem(n,x); title('input sequence x(n)'); xlabel('n'); ylabel('x'); subplot(2,1,2); stem(n,y); title('output sequence y(n)'); xlabel('n'); ylabel('y');

Output:
Input data: Y Coefficients: 1, -0.5 AND -0.25, -0.125, -1 X Coefficients: 1, 1 AND 1, -1 Initial condition: y(-1)=1, y(-2)=-2 Graph output:

## Experiment 8: Program to implement linear convolution using DFT

Code: clc; clear all; x=input('Input First Sequence: '); h=input('Input Second Sequence: '); n1=length(x); n2=length(h); N=n1+n2-1; % length = l+m -1 % Set the vector arrays and perform the operations a=fft(x,N); % X(K) b=fft(h,N); % H(K) c=a.*b; % c(K)=a(K) x b(K) d=ifft(c,N); % d= x(n) * h(n) %Console Displays disp('First Sequence'); x disp('Second sequence'); h disp('Convolved Sequence'); d n=0:N-1; subplot(3,1,1); % Plots stem(x); title('First Sequence'); ylabel('Signal'); xlabel('Time'); subplot(3,1,2); stem(h); title('Second Sequence'); ylabel('Signal'); xlabel('Time'); subplot(3,1,3); stem(d); title('Convolved Sequence'); ylabel('Signal'); xlabel('Time');

Output:
Console:

Graph:

## Experiment 9: Program to implement circular convolution using DFT

Code: clc; clear all; x=input('Input First Sequence: '); h=input('Input Second Sequence: '); n1=length(x); n2=length(h); N=max(n1,n2); %length= max legth % Set the vector arrays and perform the operations a=fft(x,N); % X(K) b=fft(h,N); % H(K) c=a.*b; % c(K)=a(K) x b(K) d=ifft(c,N); % d= x(n) * h(n) %Console Displays disp('First Sequence'); x disp('Second sequence'); h disp('Convolved Sequence'); d % Plots n=0:N-1; subplot(3,1,1); stem(x); title('First Sequence'); ylabel('Signal'); xlabel('Time'); subplot(3,1,2); stem(h); title('Second Sequence'); ylabel('Signal'); xlabel('Time'); subplot(3,1,3); stem(d); title('Convolved Sequence'); ylabel('Signal'); xlabel('Time');

Output:
Console:

Graph:

## Experiment 10: Program to implement linear convolution using a DSP processor

Code: #include<stdio.h> #define LENGHT1 6 /*Lenght of i/p samples sequence*/ #define LENGHT2 4 /*Lenght of impulse response Co-efficients */ int x[2*LENGHT1-1]={1,2,3,4,5,6,0,0,0,0,0}; /*Input Signal Samples*/ int h[2*LENGHT1-1]={1,2,3,4,0,0,0,0,0,0,0}; /*Impulse Response Coefficients*/ int y[LENGHT1+LENGHT2-1]; main() { int i=0,j; for(i=0;i<(LENGHT1+LENGHT2-1);i++) { y[i]=0; for(j=0;j<=i;j++) y[i]+=x[j]*h[i-j]; } for(i=0;i<(LENGHT1+LENGHT2-1);i++) printf("%d\n",y[i]); }

Output:
Console:

1 4 10 20 30 40 43

## Experiment 11: Program to implement circular convolution using a DSP processor

Code: #include<stdio.h> int m,n,x[30],h[30],y[30],i,j,temp[30],k,x2[30],a[30]; void main() { printf("Enter the length of the first sequence\n"); scanf("%d",&m); printf("Enter the length of the second sequence\n"); scanf("%d",&n); printf("Enter the first sequence\n"); for(i=0;i<m;i++) scanf("%d",&x[i]); printf("Enter the second sequence\n"); for(j=0;j<n;j++) scanf("%d",&h[j]); if(m-n!=0) /*If length of both sequences are not equal*/ { if(m>n) /* Pad the smaller sequence with zero*/ { for(i=n;i<m;i++) h[i]=0; n=m; } for(i=m;i<n;i++) x[i]=0; m=n; } y[0]=0; a[0]=h[0]; for(j=1;j<n;j++) /*folding h(n) to h(-n)*/ a[j]=h[n-j]; /*Circular convolution*/ for(i=0;i<n;i++) y[0]+=x[i]*a[i]; for(k=1;k<n;k++) { y[k]=0; /*circular shift*/ for(j=1;j<n;j++) x2[j]=a[j-1]; x2[0]=a[n-1]; for(i=0;i<n;i++) { a[i]=x2[i]; y[k]+=x[i]*x2[i]; } } /*displaying the result*/ printf("The circular convolution is\n"); for(i=0;i<n;i++) printf("%d \t",y[i]); }

Output:
30 25 25 30 40

## Experiment 12: Program to implement DFT using a DSP processor

Code: #include <stdio.h> #include <math.h> float x[4]={1,3,2,5}; //input only real sequence main() { float y[16]; //for 8 point DFT to store real & imaginary float w; int n,k,k1,N=8,xlen=4; for(k=0;k<2*N;k=k+2) { y[k]=0; y[k+1]=0; //initialize real & imag parts k1=k/2; //actual k index for(n=0;n<xlen;n++) { w=-2*3.14*k1*n/N; //careful about minus sign y[k]=y[k]+x[n]*cos(w); y[k+1]=y[k+1]+x[n]*sin(w); } printf("%f+j%f \n",y[k],y[k+1]); } }

Output:
11.000000+j0.000000 -0.407551+j-7.660229 -1.009553+j1.996802 2.424618+j-3.646700 -4.999949+j-0.022297 2.396778+j3.673697 -0.971310+j-2.009436 -0.460720+j7.633044

Experiment 13: Program to implement impulse response of order 2 using a DSP processor
Code: #include <stdio.h> #define order 2 // Define the order of impulse response #define len 10 // Define the length float Y[len]={0,0,0},sum; main() { int j,k; float a[order+1]={1,1,1}; float b[order+1]={1,2,1}; for(j=0;j<len;j++) { sum=0.0; for(k=1;k<=order;k++) { if((j-k)>=0) sum=sum+(a[k]*Y{j-k]); } if(j<=order) Y[j]=b[j]-sum; else Y[j]=sum; printf("response[%d]= %f",j,Y[j];); //print the computed response } } }

Output:
Response Response Response Response Response Response Response Response Response Response [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] = = = = = = = = = = 1.000000 1.000000 -1.000000 0.000000 -1.000000 -1.000000 -2.000000 -3.000000 -5.000000 -8.000000

## Experiment 14: Program to implement FIR filter to meet given specification

Code:
clc; clear all; close all; %Input% rp=input('Enter the passband attenuation :'); rs=input('Enter the stopband attenuation :'); fp=input('Enter the passband frequency :'); fs=input('Enter the stopband frequency :'); f=input('Enter the sampling frequency :'); %Computation% wp=2*fp/f; ws=2*fs/f; num=-20* log10(sqrt(rp*rs))-13; den=14.6*(fs-fp)/f; n=ceil(num/den); n1=n+1; if(rem(n,2)~=0) n1=n; n=n-1; end %Boxcar% figure(1); y=boxcar(n1); b=fir1(n,wp,y); [H,f]=freqz(b,1,256); H_mag=20*log10(abs(H)); plot(f/pi,H_mag); %Bartlett% figure(2); y=bartlett(n1); b=fir1(n,wp,y); [H,f]=freqz(b,1,256); H_mag=20*log10(abs(H)); plot(f/pi,H_mag); %Hanning% figure(3); y=hanning(n1); b=fir1(n,wp,y); [H,f]=freqz(b,1,256); H_mag=20*log10(abs(H)); plot(f/pi,H_mag); %Hamming% figure(4); y=hamming(n1); b=fir1(n,wp,y); [H,f]=freqz(b,1,256); H_mag=20*log10(abs(H));

plot(f/pi,H_mag); %Blackman% figure(5); y=blackman(n1); b=fir1(n,wp,y); [H,f]=freqz(b,1,256); H_mag=20*log10(abs(H)); plot(f/pi,H_mag); %Kaiser% figure(6); y=kaiser(n1); b=fir1(n,wp,y); [H,f]=freqz(b,1,256); H_mag=20*log10(abs(H)); plot(f/pi,H_mag);

Output:
Console:

Graphs:

## Experiment 15: Program to implement IIR filter to meet given specification

Code:
clc; clear all; %Inputs% pb=input('Enter the pass band edge frequency '); sb=input('Enter the stop band edge frequency '); pbr=input('Enter the pass band attenuation '); sbr=input('Enter the stop band attenuation '); fs= input('Enter the sampling frequency '); pbrad=pb*2*pi; sbrad=sb*2*pi; %Butterworth% [n,wn]=buttord(pbrad,sbrad,pbr,sbr,'s'); [b,a]=butter(n,wn,'s'); [z,p,k]=tf2zp(b,a); [zd,pd,kd]=bilinear(z,p,k,fs); [num,den]=zp2tf(zd,pd,kd); figure(1); freqz(num,den,512,fs); title('Butter worth frequency response'); %ChebyShev% [n,wn]=cheb1ord(pbrad,sbrad,pbr,sbr,'s'); [b,a]=cheby1(n,pbr,wn,'s'); [z,p,k]=tf2zp(b,a); [zd,pd,kd]=bilinear(z,p,k,fs); [num,den]=zp2tf(zd,pd,kd); figure(2); freqz(num,den,512,fs); title('Chebychev1 Freequency Response');

Output:
Console:

Graphs: