代碼示例:
floatar[1024],ai[1024];/*原始數據實部,虛部*/
floata[2050];
voidfft(intnn)/*nn數據長度*/
{
intn1,n2,i,j,k,l,m,s,l1;
floatt1,t2,x,y;
floatw1,w2,u1,u2,z;
floatfsin[10]={0.000000,1.000000,0.707107,0.3826834,0.1950903,0.09801713,0.04906767,0.02454123,0.01227154,0.00613588,};
floatfcos[10]={-1.000000,0.000000,0.7071068,0.9238796,0.9807853,0.99518472,0.99879545,0.9996988,0.9999247,0.9999812,};
switch(nn)
{
case1024:s=10;break;
case512:s=9;break;
case256:s=8;break;
}
n1=nn/2;n2=nn-1;
j=1;
for(i=1;i<=nn;i++)
{
baia[2*i]=ar[i-1];
a[2*i+1]=ai[i-1];
}
for(l=1;l<n2;l++)
{
if(l<j)
{
t1=a[2*j];
t2=a[2*j+1];
a[2*j]=a[2*l];
a[2*j+1]=a[2*l+1];
a[2*l]=t1;
a[2*l+1]=t2;
}
k=n1;
while(k<j)
{
j=j-k;
k=k/2;
}
j=j+k;
}
for(i=1;i<=s;i++)
{
u1=1;
u2=0;
m=(1<<i);
k=m>>1;
w1=fcos[i-1];
w2=-fsin[i-1];
for(j=1;j<=k;j++)
{
for(l=j;l<nn;l=l+m)
{
l1=l+k;
t1=a[2*l1]*u1-a[2*l1+1]*u2;
t2=a[2*l1]*u2+a[2*l1+1]*u1;
a[2*l1]=a[2*l]-t1;
a[2*l1+1]=a[2*l+1]-t2;
a[2*l]=a[2*l]+t1;
a[2*l+1]=a[2*l+1]+t2;
}
z=u1*w1-u2*w2;
u2=u1*w2+u2*w1;
u1=z;
}
}
for(i=1;i<=nn/2;i++)
{
ar[i]=4*a[2*i+2]/nn;/*實部*/
ai[i]=-4*a[2*i+3]/nn;/*虛部*/
a[i]=4*sqrt(ar[i]*ar[i]+ai[i]*ai[i]);/*幅值*/
}
}