18ECL57

Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

Angadi Institute of Technology and

Management
SAVGAON ROAD, BELGAUM-590009
(Accredited by NAAC)

DEPARTMENT OF
ELECTRONICS & COMMUNICATION ENGINEERING

Subject with Code:


Digital Signal Processing Lab (18ECL57)
Experiment Experiment Name Page
Number No.

1 Verification of sampling theorem 1

Linear and circular convolution of two given


2 sequences, Commutative, distributive and associative 3
property of convolution.
Auto and cross correlation of two sequences and
3 10
verification of their properties

4 Solving a given difference equation. 12

Computation of N point DFT of a given sequence and


5 14
to plot magnitude and phase spectrum
(i) Verification of DFT properties (like Linearity and
6 Parseval’s theorem, etc.)
20
(ii) DFT computation of square pulse and Sinc
function etc.
Design and implementation of Low pass and High
7 22
pass FIR filter to meet the desired specifications

Design and implementation of a digital IIR filter (Low


8 24
pass and High pass) to meet given specifications

Following Experiments to be done using DSP kit

9 Obtain the Linear convolution of two sequences. 26

10 Compute Circular convolution of two sequences. 28

Compute the N-point DFT of a given sequence.


11 30

Determine the Impulse response of first order and


12 32
second order system.
Experiment number: 1

// Verification of sampling theorem


clc; //clears console
clear all;

t=0:0.001:1;
fm=15;//hz
x=cos(2*%pi*fm*t);

//PLOTTING original signals


subplot(331);
plot2d(x);
xtitle('Original analog signal','Time','Amplitude');
subplot(332);
plot2d3(x);
xtitle('Original discrete signal','Time','Amplitude');

//Under Sampling
fs=20;
n=0:1/fs:1;//n=Ts
Xn=cos(2*%pi*fm*n);
subplot(333);
plot2d3(n,Xn);
xtitle('undersampled signal fs<2fm','time','amplitude');
subplot(334);
plot2d(n,Xn);
xtitle('undersampled signal fs<2fm reconstructed','time','amplitude');

//Nyquist Rate Sampling


fs=30;
n=0:1/fs:1;
Xn=cos(2*%pi*fm*n);
subplot(335);
plot2d3(n,Xn);
xtitle('sampled at NR fs=2fm','time','amplitude');
subplot(336);
plot2d(n,Xn);
xtitle('sampled at NR fs=2fm reconstructed','time','amplitude');

Digital Signal Processing Lab (18ECL57) Dept of ECE, AITM, Belagavi.


Experiment number: 1

//Over Sampling
fs=1000;
n=0:1/fs:1;
Xn=cos(2*%pi*fm*n);
subplot(337)
plot2d3(n,Xn);
xtitle('sampled signal fs>2fm','time','amplitude');
subplot(338)
plot2d(n,Xn);
xtitle('reconstructed sampled signal fs>2fm','time','amplitude');

output:-

Digital Signal Processing Lab (18ECL57) Dept of ECE, AITM, Belagavi.


Experiment number: 2a

// Linear convolution of two given sequences

Clc;
clear all;
x=input('enter the first sequence: ');
y=input('enter the second sequence: ');

lc=conv(x,y);
disp(lc)
subplot(311)
plot2d3(x)
xtitle('first seq','time','magnitude')
subplot(312)
plot2d3(y)
xtitle('second seq','time','magnitude')
subplot(313)
plot2d3(lc)
xtitle('linear convolution','time','magnitude')

Input:
enter the first sequence: [ 1 2 3 4]

enter the second sequence: [4 5 6 9]

Output:

4. 13. 28. 52. 56. 51. 36.

Digital Signal Processing Lab (18ECL57) Dept of ECE, AITM, Belagavi.


Experiment number: 2a

Digital Signal Processing Lab (18ECL57) Dept of ECE, AITM, Belagavi.


Experiment number: 2b

// circular convolution of two sequences


clc;
clear all;

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


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

m=length(x);
n=length(h);
N=max(m,n);

for i=m+1:N
x(i)=0;
end
for i=n+1:N
h(i)=0;
end
y=zeros(1,N)
for n=1:N
for k=1:N
y(n)=y(n)+x(k)*h(pmodulo((n-k),N)+1)
end
end

disp(y);

//ploting
subplot(311);
plot2d3(x);
plot(x,"ok")
subplot(312);
plot2d3(h);
plot(h,"ok")
plot2d3(y);
plot(y,"ok")

Digital Signal Processing Lab (18ECL57) Dept of ECE, AITM, Belagavi.


Experiment number: 2b

Input:
enter the first sequence [1 2 3 4]
enter the second sequence [1 4 5 6]

Output:
44. 44. 40. 32.

Digital Signal Processing Lab (18ECL57) Dept of ECE, AITM, Belagavi.


Experiment number: 2c

//verification of convolution properties


clc;
clear all;
x=input("enter the 1st sequence=");
y=input("enter the 2nd sequence=");
z=input("enter the 3rd sequence=");

function [y]=circ_conv(x, h)
m=length(x);
n=length(h);
N=max(m,n);

for i=m+1:N
x(i)=0;
end
for i=n+1:N
h(i)=0;
end
y=zeros(1,N);
for n=1:N
for k=1:N
y(n)=y(n)+x(k)*h(pmodulo((n-k),N)+1);
end
end
endfunction

//commutative property x*y=y*x)


f=circ_conv(x,y);//lhs
g=circ_conv(y,x);//rhs
if(f==g) then
disp('commutative property satisfies x1*x2=x2*x1');
disp('result f=',f);
else
disp('circular convolution is not commutative');
end

///associativity x*(y*z)=(x*y)*z

Digital Signal Processing Lab (18ECL57) Dept of ECE, AITM, Belagavi.


Experiment number: 2c

f1=circ_conv(x,(circ_conv(y,z)));//lhs
g1=circ_conv(circ_conv(x,y),z);//rhs
if (f1==g1) then
disp('associativity property satisfies x*(y*z)=(x*y)*z');
disp('result f1=',f1);
else
disp('circular convolution is not associative');
end
//distributive property x1*(x2+x3)=(x1*x2)+(x1*x3)
f2= circ_conv(x,(y+z));//lhs
g2= circ_conv(x,y)+circ_conv(x,z);
if(f2==g2) then
disp('distributive property satisfies x1*(x2+x3)=(x1*x2)+(x1*x3)');
disp('result f2=',f2);
else
disp('distributive property is not satisfied');
end

Input:
enter the 1st sequence=[ 1 2 3 4]
enter the 2nd sequence=[5 6 7 8]
enter the 3rd sequence=[1 3 5 2 ]

"commutative property satisfies x1*x2=x2*x1"


"result f="

66. 68. 66. 60.

"associativity property satisfies x*(y*z)=(x*y)*z"


"result f1="

712. 698. 720. 730.

"distributive property satisfies x1*(x2+x3)=(x1*x2)+(x1*x3)"


"result f2="

98. 99. 88. 85.

Digital Signal Processing Lab (18ECL57) Dept of ECE, AITM, Belagavi.


Experiment number: 2c

Digital Signal Processing Lab (18ECL57) Dept of ECE, AITM, Belagavi.


Experiment number: 3a

// Autocorrelation of two sequences and verification of their properties


clc;
clear all;

x=input('enter the input sequence:');


Rxx=xcorr(x);//AUTOCORRELATION
disp(Rxx);
N=length(Rxx)
mid=ceil(N/2)
///plot2d3(Rxx);

//SYMMETRY PROPERTY
Rxx_flip=0;
Rxx_flip==Rxx($:-1:1)
if Rxx_flip==Rxx then
disp('Property 1: autocorelation of a sequence has even symmetry');
disp(Rxx_flip);
end

//PROPERTY 2
mx=max(Rxx);
if mx==Rxx(mid) then
disp('Property 2: maximum value of autocorrelation is obtained at n=0');
disp(mx,'max=Rxx(mid)');
end

//PROPERTY 3
E=sum(x.^2);
if mx==E then
disp('Property 3: Energy of the sequence is present at Rxx(0),max=E');
disp(E);
else
disp('Mean square value not verified');
end

Digital Signal Processing Lab (18ECL57) Dept of ECE, AITM, Belagavi.


Experiment number: 3a

//plotting
subplot(211);
t1=0:1:(length(x)-1)
xtitle('input x(n)','x(n)');
plot2d3(t1,x);
t=-(N-mid):1:(N-mid);//-(mid-1):1:(mid-1);-((N/2)-1):1:((N/2)-1);
subplot(212);
xtitle('autocorrelation Rxx','samples n','Rxx');
plot2d3(t,Rxx);

Input:
enter the input sequence:[ 1 3 2 4]

Output:
4. 14. 17. 30. 17. 14. 4.
"Property 2: maximum value of autocorrelation is obtained at n=0"
30.
"max=Rxx(mid)"
"Property 3: Energy of the sequence is present at Rxx(0),max=E"
30

Digital Signal Processing Lab (18ECL57) Dept of ECE, AITM, Belagavi.


Experiment number: 3a

Digital Signal Processing Lab (18ECL57) Dept of ECE, AITM, Belagavi.


Experiment number: 3b

//Cross correlation of two sequences and verification of their properties


clc;
clear all;

x=input('Enter the 1st sequence:');


y=input('Enter the 2nd sequence:');

Rxy=xcorr(x,y);//CROSS CORRELATION
disp(Rxy,'cross correlation of sequence Rxy=');

N=length(Rxy);
Ryx=xcorr(y,x);
disp(Ryx,'cross correlation of sequence Ryx=');

z=Rxy($:-1:1);//SIGNAL FOLDING
disp(z);
if z==Ryx then
disp('Property Rxy[-n]=Ryx[n] satisfied');
disp(Ryx);
else
disp('property Rxy[-n]=Ryx[n] not satisfied');
end

Input:

Enter the 1st sequence:[4 6 7 5]

Enter the 2nd sequence:[1 5 3 4]

16. 36. 66. 75. 56. 32. 5.


"cross correlation of sequence Rxy="

5. 32. 56. 75. 66. 36. 16.


"cross correlation of sequence Ryx="

5. 32. 56. 75. 66. 36. 16.


"Property Rxy[-n]=Ryx[n] satisfied"

5. 32. 56. 75. 66. 36. 16.

Digital Signal Processing Lab (18ECL57) Dept of ECE, AITM, Belagavi.


Experiment number: 7
//Implementation of low pass and high pass FIR filter
clc;
clear all;
N= input('Enter the length of response = ')
xn = input ('enter the coefficient of x (n)') //[1 -3/4];
yn = input ('enter the coefficient of y (n)') //[1 -3/4 1/8];
x = [1,zeros(1,N-1)]
n = 0:N-1
h = filter(xn,yn,x)
disp('Response of filter =');
disp(h);

Input:

Enter the length of response = 4

enter the coefficient of x (n) [1 -3/4]

enter the coefficient of y (n) [1 -3/4 1/8]

Output:

"Response of filter ="

1. 0. -0.125 -0.09375

Digital Signal Processing Lab (18ECL57) Dept of ECE, AITM, Belagavi.


Experiment number: 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).

clc;
clear all;

y=input('enter the sequence= ');


subplot(221);
plot2d3(y)

N=length(y);
disp(y)
n=0:1:(N-1);
k=0:1:(N-1);
n=n'
w=exp(-%i*2*%pi*n*k/N)
disp(w)
Xk=y*w;
disp(Xk,'N point DFT');
mag=abs(Xk)
disp(mag,'magnitude response of X(k)');
subplot(222);
xtitle('magnitude response of X(k)','samples k','X(k)');
plot2d3(k,mag);
phase=atan(imag(Xk),real(Xk))
disp(phase,'phase of X(k)');
subplot(223);
xtitle('phase response of X(k)','samples k','X(k)');
plot2d3(k,phase);

//IDFT of X(k)
w1=exp(%i*2*%pi*n*k/N);
x2=(1/N)*Xk*w1;
x21=round(x2)
disp(x2,'IDFT of X(k)');
subplot(224);
xtitle('IDFT of X(k)','samples n','x2(n)');

Digital Signal Processing Lab (18ECL57) Dept of ECE, AITM, Belagavi.


Experiment number: 5

plot2d3(k,x2);
//verification
Xk1=fft(y)
disp(Xk1,'verification by direct function');
disp([y])

X2=asciimat(x21);
disp(X2)

Input:
enter the sequence= [ 1 3 2 4]

Output:
1. 3. 2. 4.
1. + 0.i 1. + 0.i 1. + 0.i 1. + 0.i
1. + 0.i 6.123D-17 - i -1. - 1.225D-16i -1.837D-16 + i
1. + 0.i -1. - 1.225D-16i 1. + 2.449D-16i -1. - 3.674D-16i
1. + 0.i -1.837D-16 + i -1. - 3.674D-16i 5.511D-16 - i
10. + 0.i -1. + 1.i -4. - 1.347D-15i -1. - 1.i
"N point DFT"
10. 1.4142136 4. 1.4142136
"magnitude response of X(k)"
0. 2.3561945 -3.1415927 -2.3561945
"phase of X(k)"
1. - 6.661D-16i 3. - 2.959D-16i 2. + 1.225D-16i 4. + 3.751D-16i
"IDFT of X(k)"
10. + 0.i -1. + i -4. + 0.i -1. - i
"verification by direct function"
1. 3. 2. 4.

Digital Signal Processing Lab (18ECL57) Dept of ECE, AITM, Belagavi.


Experiment number: 5

Digital Signal Processing Lab (18ECL57) Dept of ECE, AITM, Belagavi.


Experiment number: 6 ia
// (i) Verification of DFT properties Linearity
clc;
clear all;
x1=input('enter the first sequence : ');
x2=input('enter the second sequence: ');
a=1;
b=1;
y=(a*x1)+(b*x2);
LHS=fft(y);
disp(LHS,'DFT of LHS');
X1=fft(x1);
X2=fft(x2);
RHS=(a*X1)+(b*X2);
disp(RHS,'DFT of RHS');

Input:
enter the first sequence [1 2 3 4]

enter the second sequence: [5 6 7 8]

DFT of LHS

36. -4. + 4.i -4. -4. - 4.i

DFT of RHS

36. -4. + 4.i -4. -4. - 4.i

Digital Signal Processing Lab (18ECL57) Dept of ECE, AITM, Belagavi.


Experiment number: 6-iia

//DFT computation of square pulse

clc;
clear all;
t=-%pi:0.05:2*%pi;
X=squarewave(t);
disp(X);
figure(1)
plot2d3(X);
f1=fft(X);
f2=abs(f1);
disp(f2);
figure(2)
plot2d(f2)

OUTPUT:-

Digital Signal Processing Lab (18ECL57) Dept of ECE, AITM, Belagavi.


Experiment number: 6-iia

Digital Signal Processing Lab (18ECL57) Dept of ECE, AITM, Belagavi.


Experiment number: 6-ib

//(i) Verification of DFT properties Parseval’s theorem


clc;
clear all;
x=input('enter the samples: ');
mag=abs(x);
LHS=sum(mag.^2);
disp(LHS,'Energy of the sequence in time domain');

N=length(x);
X=fft(x);
mag1=abs(X);
RHS=(1/N)*sum(mag1.^2);
disp(RHS,'Energy of the sequence in frequency domain');

Input:

enter the samples: [ 1 2 3 4]

Energy of the sequence in time domain

30.

Energy of the sequence in frequency domain

30.

Digital Signal Processing Lab (18ECL57) Dept of ECE, AITM, Belagavi.


Experiment number: 6 iiB

//DFT computation of Sinc function.


clc;
clear all;
t=-20:0.03:20;
X=sinc(t);
disp(X);
figure(1)
plot2d(X);
f1=fft(X);
f2=abs(f1);
disp(f2);
figure(2)
plot2d(f2)

OUTPUT:-

Digital Signal Processing Lab (18ECL57) Dept of ECE, AITM, Belagavi.


Experiment number: 6 iiB

Digital Signal Processing Lab (18ECL57) Dept of ECE, AITM, Belagavi.


Experiment number: 7
//Implementation of low pass and high pass FIR filter
clc;
clear all;
wp=input('enter the pass band edge freq (rad)=');
ws=input('enter the stop band edge freq (rad)=');
ks=input('enter the stop band attenuation(dB)=');

N1=(2*%pi*4)/(ws-wp);//for k=4, hamming window


N=N1+1;
disp(N);
wc=(wp+(ws-wp)/2)
disp(wc);
[wft,wfm,fr]=wfir('lp',N,[wc/5,0],'hm',[0,0]);
figure(1)
xlabel('Normalized digital freq w');
ylabel('freq response in dB H(iw)=');
title('freq response of FIR LPF');
xgrid(1);
plot2d(2*fr,20*log(wfm));
disp(wft,'filter coefficients of FIR LPF=');
disp(wfm,'impulse response of FIR LPF=');
disp(fr,'impulse response of FIR LPF=');

OUTPUT:-

enter the pass band edge freq (rad)=0.375*%pi

enter the stop band edge freq (rad)=0.5*%pi

enter the stop band attenuation(dB)=50

65.

1.3744468

Digital Signal Processing Lab (18ECL57) Dept of ECE, AITM, Belagavi.


Experiment number: 7

Digital Signal Processing Lab (18ECL57) Dept of ECE, AITM, Belagavi.


Experiment number: 8

// Design and implementation of a digital IIR filter to meet given specifications.


clc;
clear all;

fp = input('Enter the Pass band frequency in Hz = ' ); //Define pass stop and ripple frequency
fs = input('Enter the Stop band frequency in Hz = ');
Fs = input('Enter the Sampling frequency in Hz = ');
Ap = input(' Enter the Pass band ripple in db:');
As = input('Enter theStop band ripple in db:');
wp=2*%pi*fp/ws;// Analog frequency
ws=2*%pi*Fs/ws;
Up = 2*tan(wp/2);// Prewrapped frequency
Us = 2*tan(ws/2);
[n,wn]= buttmag (Up,Us,Ap,As,'s');// Calculate order and cutoff freq
disp('order of the filter N =');
disp(N);
disp('Normalized cut off frequency = ');
disp(wn);
[num, den] = butter(n,wc,'s');// analog filter transfer
[b,a] = bilinear(num, den,1);
Freqz(b,a,512,Fs);
Printsys(b,a,’z’);

Digital Signal Processing Lab (18ECL57) Dept of ECE, AITM, Belagavi.


Experiment number: 9

/* Obtain the Linear convolution of two sequences */

#include<stdio.h>
main()
{ int m=5; /*Lenght of i/p samples sequence*/
int n=4; /*Lenght of impulse response Co-efficients */
int i=0,j;
int x[10]={2,2,3,4,6}; /*Input Signal Samples*/
int h[10]={1,2,3,4}; /*Impulse Response Co-efficients*/
/*At the end of input sequences pad 'M' and 'N' no. of zero's*/
int *y;
y=(int *)0x0000100;
for(i=0;i<m+n-1;i++)
{
y[i]=0;
for(j=0;j<=i;j++)
y[i]+=x[j]*h[i-j];
}
for(i=0;i<m+n-1;i++)
printf("%d\n",y[i]);
}

Digital Signal Processing Lab (18ECL57) Dept of ECE, AITM, Belagavi.


Experiment number: 10

/* Compute Circular convolution of two sequences */

#include<stdio.h>
int m,n,x[30],h[30],y[30],i,j,temp[30],k,x2[30],a[30];
void main()
{
//int *y;
//y=(int *)0x00010400;
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++)

Digital Signal Processing Lab (18ECL57) Dept of ECE, AITM, Belagavi.


Experiment number: 10
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 ",y[i]);
}

Digital Signal Processing Lab (18ECL57) Dept of ECE, AITM, Belagavi.


Experiment number: 11

/* Compute the N-point DFT of a given sequence*/

#include <stdio.h>
#include <math.h>

void dft(int *x, short k, float *real, float *imaginary);


float pi = 3.1415926535897932384626433832795;
float real[10],imaginary[10]; //Output results
int index=0;
int N,x[10]={0};

void main()
{
short i,j;
printf("Enter the length of the input sequence:\n");
scanf("%d",&N);
printf("Enter the input sequence:\n");
for(i=0;i<N;i++)
scanf("%d",&x[i]);

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


{
dft(x,j,real,imaginary); //call DFT function
}
}

void dft(int *x, short k, float *real, float *imaginary) //DFT function
{
float sumRe = 0; //initialize real component
float sumIm = 0; //initialize imaginary component
int i = 0;
float cs = 0; //initialize cosine component
float sn = 0; //initialize sine component

for (i = 0; i < N; i++) //for N-point DFT


{
cs = cos(2*pi*(k)*i/N); //real component
sn = sin(2*pi*(k)*i/N); //imaginary component
sumRe = sumRe + x[i]*cs; //sum of real components
sumIm = sumIm - x[i]*sn; //sum of imaginary components
}
real[index]=sumRe;

Digital Signal Processing Lab (18ECL57) Dept of ECE, AITM, Belagavi.


Experiment number: 11
imaginary[index]=sumIm;
index++;
printf("%f %f\n",sumRe,sumIm);
}

Digital Signal Processing Lab (18ECL57) Dept of ECE, AITM, Belagavi.


Experiment number: 12

/* Determine the Impulse response of first order and second order system */

#include<stdio.h>

#define Order 2
#define Len 5

float h[Len] = {0.0,0.0,0.0,0.0,0.0},sum;

void main()
{
int j,k;

float a[Order+1] = {0.5311, 0.8622, 1.1311};


float b[Order+1] = {.2009, -0.7478, 0.5722};
printf("Impulse response of the given system:\n");
for(j=0;j<Len;j++)
{
sum = 0.0;
for(k=1;k<=Order;k++)
{
if((j-k)>=0)
sum = sum+(b[k]*h[j-k]);
}
if(j<=Order)
{
h[j] = a[j]-sum;
}
else
{
h[j] = -sum;
}
printf("%f",h[j]);

}
}

Digital Signal Processing Lab (18ECL57) Dept of ECE, AITM, Belagavi.

You might also like