http://topic.csdn.net/t/20051214/15/4459205.html
傅立叶变换的程序贴出如下: #include <stdio.h> #include <math.h> #define N 64 #define m 6 //2^m=N /* float twiddle[N/2]={1.0,0.707,0.0,-0.707,}; float x_r[N]={1,1,1,1,0,0,0,0,}; float x_i[N]; //N=8 */ float twiddle[N/2]={1,0.9951,0.9808,0.9570,0.9239,0.8820,0.8317,0.7733, 0.7075,0.6349,0.5561,0.4721,0.3835,0.2912,0.1961,0.0991, 0.0000,-0.0991,-0.1961,-0.2912,-0.3835,-0.4721,-0.5561,-0.6349, -0.7075,-0.7733,0.8317,-0.8820,-0.9239,-0.9570,-0.9808,-0.9951,}; //N=64 float x_r[N]={1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,}; float x_i[N]; void fft_init() { int i; for(i=0;i<N;i++) x_i=0.0; } void bitrev() { int p=1,q,i; int bit_rev[N]; float xx_r[N]; bit_rev[0]=0; while(p<N) { for(q=0;q<p;q++) { bit_rev[q]=bit_rev[q]*2; bit_rev[q+p]=bit_rev[q]+1; } p=p*2; } for(i=0;i<N;i++) { xx_r=x_r; } for(i=0;i<N;i++) { x_r=xx_r[bit_rev]; } } void display() { int i; for(i=0;i<N;i++) printf("%f %f
",x_r,x_i); } void fft1() { int L,i,b,j,p,k,tx1,tx2; float TI,TR,temp; float tw1,tw2; for(L=1;L<=m;L++) { /* for(1) */ b=1; i=L-1; while(i>0) {b=b*2;i--;} /* b= 2^(L-1) */ for(j=0;j<=b-1;j++) /* for (2) */ { p=1; i=m-L; while(i>0) /* p=pow(2,7-L)*j; */ {p=p*2; i--;} p=p*j; tx1=p%(N); tx2=tx1+(3*N)/4; tx2=tx2%(N); if (tx1>=(N/2)) tw1=-twiddle[tx1-(N/2)]; else tw1=twiddle[tx1]; if (tx2>=(N/2)) tw2=-twiddle[tx2-(N/2)]; else tw2=twiddle[tx2]; for(k=j;k<N;k=k+2*b) /* for (3) */ {TR=x_r[k]; TI=x_i[k]; temp=x_r[k+b]; x_r[k]=x_r[k]+x_r[k+b]*tw1+x_i[k+b]*tw2; x_i[k]=x_i[k]-x_r[k+b]*tw2+x_i[k+b]*tw1; x_r[k+b]=TR-x_r[k+b]*tw1-x_i[k+b]*tw2; x_i[k+b]=TI+temp*tw2-x_i[k+b]*tw1; } } } } void dft() { int i,n,k,tx1,tx2; float tw1,tw2; float xx_r[N],xx_i[N]; //clear any data in Real and Imaginary result arrays prior to DFT for(k=0;k<=N-1;k++) xx_r[k]=xx_i[k]=x_i[k]=0.0; //caculate the DFT for(k=0;k<=(N-1);k++) { for(n=0;n<=(N-1);n++) { tx1=(n*k); tx2=tx1+(3*N)/4; tx1=tx1%(N); tx2=tx2%(N); if (tx1>=(N/2)) tw1=-twiddle[tx1-(N/2)]; else tw1=twiddle[tx1]; if (tx2>=(N/2)) tw2=-twiddle[tx2-(N/2)]; else tw2=twiddle[tx2]; xx_r[k]=xx_r[k]+x_r[n]*tw1; xx_i[k]=xx_i[k]+x_r[n]*tw2; } xx_i[k]=-xx_i[k]; } //display for(i=0;i<N;i++) printf("%f %f
",xx_r,xx_i); } void main() { /* fft_init(); bitrev(); fft1(); display(); //FFT */ dft(); //DFT } |