#include <stdio.h>
#include <math.h>
#include "fft.h"
#define log2(N) (log10(N)/log10(2))
/*************************************
*函数名:BitReverse
*功能 :把自然顺序存储的数据变为倒位序存储
*变量 :real input/output
* image input/output
* n序列点数 input
*返回值:无
**************************************/
void BitReverse(float *real,
#if 0
float *image,
#endif
int n)
{
int i,j;
int p;
int a,b;
float tempR;
float tempI;
#if 0
for(i=1, p=0; i<n; i*=2)
{
p++;
}
#else
p=log2(n);
#endif
/*自然顺序的数据转为倒位序排列的数据*/
for(i=0; i<n; i++)
{
a = i;
b = 0;
for(j=0; j<p; j++)
{
b += (a & 0x01);
b<<=1;
a>>=1;
}
if(b > i)
{
tempR = real[i];
real[i] = real[b];
real[b] = tempR;
#if 0
tempI = image[i];
image[i] = image[b];
image[b] = tempI;
#endif
}
}
}
/*************************************
*函数名:FFTArithmetic
*功能 :对输入序列进行快速傅立叶变换,从时域转为频域
*变量 :dataR input/output
* dataI input/output
* n序列点数 input
*返回值:无
**************************************/
void FFTArithmetic(float *dataR, float *dataI, int n)
{
int i,j,k,r;
int index;
float arg; //相角
float WReal,WImage;
float tempR1,tempI1,tempR2,tempI2;
BitReverse(dataR,n);
arg = - 2 * PI / n;
for(i=2; i<=n; i*=2)
{
for(j=0; j<n; j+=i)
{
for(k=0; k<i/2; k++)
{
r = n * k / i;
index = k+j;
WReal = cos(arg * r);
WImage = sin(arg * r);
tempR1 = WReal * dataR[index + i/2] - WImage * dataI[index + i/2];
tempI1 = WReal * dataI[index + i/2] + WImage * dataR[index + i/2];
tempR2 = dataR[index];
tempI2 = dataI[index];
dataR[index] = tempR2 + tempR1;
dataI[index] = tempI2 + tempI1;
dataR[index+i/2] = tempR2 - tempR1;
dataI[index+i/2] = tempI2 - tempI1;
//k++;
}
}
}
}