0% found this document useful (0 votes)
68 views

DSP Lab Manual

The document contains instructions for a DSP lab covering several topics: 1. Illustrating the sampling theorem by generating two sinusoidal sequences with different sampling frequencies. 2. Conducting experiments to determine linear convolution, circular convolution, and correlation of sequences using theoretical computations. 3. Calculating linear convolution of sequences using the Fast Fourier Transform (FFT). 4. Determining correlation of sequences using the FFT. 5. Calculating the spectrum of a sequence using the FFT.

Uploaded by

ugmadhu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

DSP Lab Manual

The document contains instructions for a DSP lab covering several topics: 1. Illustrating the sampling theorem by generating two sinusoidal sequences with different sampling frequencies. 2. Conducting experiments to determine linear convolution, circular convolution, and correlation of sequences using theoretical computations. 3. Calculating linear convolution of sequences using the Fast Fourier Transform (FFT). 4. Determining correlation of sequences using the FFT. 5. Calculating the spectrum of a sequence using the FFT.

Uploaded by

ugmadhu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 14

DSP Lab Manual

1. Illustrate Sampling theorem for the given frequency.

#include<stdio.h>
#include<math.h>
#define pi 3.141592654

main()
{
int i,k,k1;
float term,term1,s[100],s1[100],fo,fs,fs1;
fo=1000.0;
fs=5000.0;
fs1=1000.0;
k=0;

term=(float)(fo+k*fs)/fs;
for(i=0;i<100;i++)
{
s[i]=sin(2*pi*term*i);
}

k1=10;
term1=(float)(fo+k1*fs1)/fs1;
for(i=0;i<100;i++)
{
s1[i]=sin(2*pi*term1*i);
}

return 0;

Dept. of IT 14 KNSIT
DSP Lab Manual

2. Conduct experiment to determine linear convolution, circular convolution & correlation


of two given sequences (sequences of 4 points each).verify the result using theoretical
computations.
x= { }, h = { }

#include<stdio.h>
#define NPT 4
int x[30],y[30],h[30],h1[30],m,n,i=0,j,k,l;

main()
{

for(i=0;i<30;i++)
{
x[i]=0;
h[i]=0;
y[i]=0;
h1[i]=0;
}

printf("enter the length of input seq\n");


scanf("%d",&n);
printf("enter the length of impulse seq\n");
scanf("%d",&m);

printf("Enter i/p seq\n");


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

printf("Enter impulse seq\n");


for(j=0,l=(NPT-1);j<NPT;j++,l--)
{
scanf("%d",&h[j]);
h1[l]=h[j];
}

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 result of Linear Convolution is:\n");


for(i=0;i<m+n-1;i++)
printf("%d\t",y[i]);

Dept. of IT 14 KNSIT
DSP Lab Manual

printf(" output1 \n");


for(l=-(NPT-1),k=0;l<NPT;l++,k++)
{
y[k]=0;
for(n=0;n<NPT;n++)
{
if((n-l)>=0)
if((n-l)<NPT)
y[k]=y[k]+x[n]*h[n-1];

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

printf("The result of Circular correlation is:\n");


if((m-n)!=0)
{
if(m>n)
{
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++)
a[j]=h[n-j];
for(i=0;i<n;i++)
y[0]+=x[i]*a[i];

for(k=1;k<n;k++)
{
y[k]+=x[i]*x2[i];

}
printf("The result of Cross correlation is:\n");
for(n=0;n<(2*NPT-1);n++)
{
y[n]=0;
for(k=0;k<NPT;k++)
{
if((n-k)>=0)
if((n-k)<NPT)
y[n]=y[n]+x[k]*h1[n-k];

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

Dept. of IT 14 KNSIT
DSP Lab Manual

Result :

3. Determine the linear convolution of two given 4 point sequences using FFT

Dept. of IT 14 KNSIT
DSP Lab Manual

x= { }, h = { }

#include<stdio.h>
#include<math.h>
#define PI 3.142857142857
#define NPT 8
#define NSTAGE 3
#define NPT_BY_2 4

float ffr[NPT], ffi[NPT];


void fft(float*,float*);

int main()
{
float a1r[NPT]={2,1,2,1,0,0,0,0},a1i[NPT]={0,0,0,0,0,0,0,0};
float a2r[NPT]={1,2,3,4,0,0,0,0},a2i[NPT]={0,0,0,0,0,0,0,0};
float y1r[NPT],y1i[NPT];
float y2r[NPT],y2i[NPT];
float y3r[NPT],y3i[NPT];

int i;

fft(a1r,a1i);

for(i=0;i<NPT;i++)
{
y1r[i]=ffr[i];
y1i[i]=ffi[i];
}

fft(a2r,a2i);

for(i=0;i<NPT;i++)
{
y2r[i]=ffr[i];
y2i[i]=ffi[i];
}

for(i=0;i<NPT;i++)
{
y3r[i]=y1r[i]*y2r[i]-y1i[i]*y2i[i];
y3i[i]=y1r[i]*y2i[i]+y1i[i]*y2r[i];
y3i[i]=(-1)*y3i[i];
}
fft(y3r,y3i);

for(i=0;i<NPT;i++)
{
y3r[i]=ffr[i]/NPT;
y3i[i]=(-1)*ffi[i]/NPT;
}

for(i=0;i<NPT-1;i++)

Dept. of IT 14 KNSIT
DSP Lab Manual

{
printf("%f\n",y3r[i]);
}

return (0);
}

void fft(float real[NPT], float imag[NPT])


{
int i,j,k,p,g,tob,bob;
float b,c,d;
float tr[NPT_BY_2], ti[NPT_BY_2];
int i1, work,j2,bb[NPT];

for(i=0;i<NPT_BY_2;i++)
{
tr[i]=cos(2*PI*i/NPT);
ti[i]=-sin(2*PI*i/NPT);
}

for(k=0;k<NSTAGE;k++)
{
if(k==0)
g=1;
else
g=pow(2,(k-1));
p=pow(2,k);

for(j=0;j<p;j++)
{
for(i=0;i<(NPT_BY_2/p);i++)
{
tob=i+j*NPT_BY_2/g;
bob=tob+(NPT_BY_2/p);
b=real[tob]+real[bob];
c=imag[tob]+imag[bob];
real[bob]=real[tob]-real[bob];
imag[bob]=imag[tob]-imag[bob];
real[tob]=b;
imag[tob]=c;
d=real[bob]*tr[p*i]-imag[bob]-ti[p*i];

imag[bob]=imag[bob]*tr[p*i]+real[bob]-ti[p*i];
real[bob]=d;
}
}
}

for(i=0;i<NPT;i++)
{
work=0;

Dept. of IT 14 KNSIT
DSP Lab Manual

i1=i;
for(j=0;j<NSTAGE;j++)
{
j2=i1%2;
work=2*work+j2;
i1=i1/2;
}
bb[i]=work;
}

for(i=0;i<NPT;i++)
{
ffr[i]=real[bb[i]];
ffi[i]=imag[bb[i]];
}
}

4. Determine the correlation of 4 point sequence using FFT.


x= { }, h = { }

Dept. of IT 14 KNSIT
DSP Lab Manual

#include<stdio.h>
#include<math.h>
#define PI 3.142857142857
#define ARRAYSIZE 16
#define NPT 8
#define NSTAGE 3
#define NPT_BY_2 4

float ffr[NPT], ffi[NPT];


void fft(float*,float*);

int main()
{
float a1r[NPT]={2,1,2,1,0,0,0,0}, a1i[NPT]={0,0,0,0,0,0,0,0};
float a2r1[NPT]={1,2,3,4,0,0,0,0}, a2i[NPT]={0,0,0,0,0,0,0,0};
float ansr[NPT],sar[NPT],sai[NPT],a2r[NPT];
float y1r[NPT],y1i[NPT],y2r[NPT],y2i[NPT],ar[NPT],ai[NPT];

int i,j;

for(i=0,j=NPT_BY_2;i<NPT_BY_2;i++,j++)
{
a2r[(NPT_BY_2-1)-i]=a2r1[i];
a2r[j]=0;

fft(a1r,a1i);
for(i=0;i<NPT;i++)
{
y1r[i]=ffr[i];
y1i[i]=ffi[i];
}

fft(a2r,a2i);
for(i=0;i<NPT;i++)
{
y2r[i]=ffr[i];
y2i[i]=ffi[i];
}

for(i=0;i<NPT;i++)
{
sar[i]=y1r[i]*y2r[i]-y1i[i]*y2i[i];
sai[i]=(-1)*(y1r[i]*y2i[i]+y2r[i]*y1i[i]);
}

fft(sar,sai);
for(i=0;i<NPT;i++)
{
ar[i]=ffr[i]/8;
ai[i]=(-1)*ffi[i]/8;

Dept. of IT 14 KNSIT
DSP Lab Manual

for(i=0;i<NPT-1;i++)
{
printf("%f\n",ar[i]) ;
}

return (0);
}

void fft(float real[NPT], float imag[NPT])


{
int i,j,k,p,g,tob,bob;
float b,c,d;
float tr[NPT_BY_2], ti[NPT_BY_2];
int i1,work,j2,bb[NPT];

for(i=0;i<NPT_BY_2;i++)
{
tr[i]=cos(2*PI*i/NPT);
ti[i]=-sin(2*PI*i/NPT);
}

for(k=0;k<NSTAGE;k++)
{
if(k==0)
g=1;
else
g=pow(2,(k-1));
p=pow(2,k);

for(j=0;j<p;j++)
{
for(i=0;i<(NPT_BY_2/p);i++)
{
tob=i+j*NPT_BY_2/g;
bob=tob+(NPT_BY_2/p);
b=real[tob]+real[bob];
c=imag[tob]+imag[bob];
real[bob]=real[tob]-real[bob];
imag[bob]=imag[tob]-imag[bob];
real[tob]=b;
imag[tob]=c;
d=real[bob]*tr[p*i]-imag[bob]-ti[p*i];
imag[bob]=imag[bob]*tr[p*i]+real[bob]-ti[p*i];
real[bob]=d;
}
}
}

for(i=0;i<NPT;i++)
{
work=0;
i1=i;
for(j=0;j<NSTAGE;j++)

Dept. of IT 14 KNSIT
DSP Lab Manual

{
j2=i1%2;
work=2*work+j2;
i1=i1/2;
}
bb[i]=work;
}

for(i=0;i<NPT;i++)
{
ffr[i]=real[bb[i]];
ffi[i]=imag[bb[i]];
}
}

5. Determine the spectrum of given sequence(minimum of 16 points) using FFT.


Generate the 16 point sequence for a sine wave of frequency 1000Hz sampled at 8000
Hz

Dept. of IT 14 KNSIT
DSP Lab Manual

#include<stdio.h>
#include<math.h>
#define PI 3.142857142857
#define NPT 16
#define NSTAGE 4
#define NPT_BY_2 8

float ffr[NPT], ffi[NPT];


void fft(float*,float*);

int main()
{
float ar[NPT]={1,2,3,4,5,6,7,8,1,2,2,2,2,2,7,8};
float ai[NPT]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

int i;

for(i=0;i<NPT;i++)
{
ffr[i]=0;
ffi[i]=0;
}

fft(ar,ai);

for(i=0;i<NPT;i++)
{
printf("%f+(%fi)\n",ffr[i],ffi[i]);
}

for(i=0;i<NPT;i++)
{
printf("mag=%f,phase=%f\n",sqrt((ffr[i]*ffr[i])+
(ffi[i]*ffi[i])),atan2(ffi[i],ffr[i]));
}

return (0);
}

void fft(float real[NPT], float imag[NPT])


{
int i,j,k,p,g,tob,bob;
float b,c,d;
float tr[NPT_BY_2], ti[NPT_BY_2];
int i1,work,j2,bb[NPT];

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

Dept. of IT 14 KNSIT
DSP Lab Manual

{
tr[i]=cos(2*PI*i/NPT);
ti[i]=-sin(2*PI*i/NPT);
}

for(k=0;k<NSTAGE;k++)
{
if(k==0)
g=1;
else
g=pow(2,(k-1));
p=pow(2,k);

for(j=0;j<p;j++)
{
for(i=0;i<(NPT_BY_2/p);i++)
{
tob=i+j*NPT_BY_2/g;
bob=tob+(NPT_BY_2/p);
b=real[tob]+real[bob];
c=imag[tob]+imag[bob];
real[bob]=real[tob]-real[bob];
imag[bob]=imag[tob]-imag[bob];
real[tob]=b;
imag[tob]=c;
d=real[bob]*tr[p*i]-imag[bob]-ti[p*i];
imag[bob]=imag[bob]*tr[p*i]+real[bob]-ti[p*i];
real[bob]=d;
}
}
}

for(i=0;i<NPT;i++)
{
work=0;
i1=i;
for(j=0;j<NSTAGE;j++)
{
j2=i1%2;
work=2*work+j2;
i1=i1/2;
}
bb[i]=work;
}

for(i=0;i<NPT;i++)
{
ffr[i]=real[bb[i]];
ffi[i]=imag[bb[i]];
}
}

Dept. of IT 14 KNSIT
DSP Lab Manual

6. Design & test Butterworth I & II order low pass filter for the given cutoff frequency

#include<stdio.h>
#include<math.h>
#define pi 3.141592654

Dept. of IT 14 KNSIT
DSP Lab Manual

main()
{
int i;
float fs,T,t,x[256],y[256];

fs=10000.0;
T=1/fs;

for(i=0,t=0.0;i<256;i++,t+=T)
{
x[i]=sin(2*pi*2031.25*t)+sin(2*pi*2773.44*t)
+sin(2*pi*3906.25*t);
}

y[0]=0.0;
for(i=1;i<256;i++)
{
y[i]=0.15835*y[i-1]-0.4208*(x[i]+x[i-1]);
}

return(0);

Dept. of IT 14 KNSIT

You might also like