DSP Lab Manual
DSP Lab Manual
DSP Lab Manual
#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
#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;
}
for(i=0;i<m+n-1;i++)
{
y[i]=0;
for(j=0;j<=i;j++)
y[i]+=x[j]*h[i-j];
Dept. of IT 14 KNSIT
DSP Lab Manual
}
printf("%d\t",y[k]);
}
}
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
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);
}
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]];
}
}
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
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);
}
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]];
}
}
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
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);
}
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