《最新FFT的C语言算法实现.doc》由会员分享,可在线阅读,更多相关《最新FFT的C语言算法实现.doc(18页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精品资料FFT的C语言算法实现.FFT的C语言算法实现程序如下: /*FFT*/ #include #include #include #define N 1000 typedef struct double real; double img; complex; void fft(); /*快速傅里叶变换*/ void ifft(); /*快速傅里叶逆变换*/ void initW(); void change(); void add(complex ,complex ,complex *); /*复数加法*/ void mul(complex ,complex ,complex *); /*
2、复数乘法*/ void sub(complex ,complex ,complex *); /*复数减法*/ void divi(complex ,complex ,complex *);/*复数除法*/ void output(); /*输出结果*/ complex xN, *W;/*输出序列的值*/ int size_x=0;/*输入序列的长度,只限2的N次方*/ double PI; int main() int i,method; system(cls); PI=atan(1)*4; printf(Please input the size of x:n); /*输入序列的长度*/ s
3、canf(%d,&size_x); printf(Please input the data in xN:(such as:5 6)n); /*输入序列对应的值*/ for(i=0;isize_x;i+) scanf(%lf %lf,&xi.real,&xi.img); initW(); /*选择FFT或逆FFT运算*/ printf(Use FFT(0) or IFFT(1)?n); scanf(%d,&method); if(method=0) fft(); else ifft(); output(); return 0; /*进行基-2 FFT运算*/ void fft() int i=
4、0,j=0,k=0,l=0; complex up,down,product; change(); for(i=0;i log(size_x)/log(2) ;i+) l=1i; for(j=0;jsize_x;j+= 2*l ) for(k=0;kl;k+) mul(xj+k+l,Wsize_x*k/2/l,&product); add(xj+k,product,&up); sub(xj+k,product,&down); xj+k=up; xj+k+l=down; void ifft() int i=0,j=0,k=0,l=size_x; complex up,down; for(i=0;
5、i (int)( log(size_x)/log(2) );i+) /*蝶形运算*/ l/=2; for(j=0;jsize_x;j+= 2*l ) for(k=0;kl;k+) add(xj+k,xj+k+l,&up); up.real/=2;up.img/=2; sub(xj+k,xj+k+l,&down); down.real/=2;down.img/=2; divi(down,Wsize_x*k/2/l,&down); xj+k=up; xj+k+l=down; change(); void initW() int i; W=(complex *)malloc(sizeof(compl
6、ex) * size_x); for(i=0;isize_x;i+) Wi.real=cos(2*PI/size_x*i); Wi.img=-1*sin(2*PI/size_x*i); void change() complex temp; unsigned short i=0,j=0,k=0; double t; for(i=0;i0 ) j=j1; if(ji) temp=xi; xi=xj; xj=temp; void output() /*输出结果*/ int i; printf(The result are as followsn); for(i=0;i=0.0001) printf
7、(+%.4fjn,xi.img); else if(fabs(xi.img)real=a.real+b.real; c-img=a.img+b.img; void mul(complex a,complex b,complex *c) c-real=a.real*b.real - a.img*b.img; c-img=a.real*b.img + a.img*b.real; void sub(complex a,complex b,complex *c) c-real=a.real-b.real; c-img=a.img-b.img; void divi(complex a,complex b,complex *c) c-real=( a.real*b.real+a.img*b.img )/( b.real*b.real+b.img*b.img); c-img=( a.img*b.real-a.real*b.img)/(b.real*b.real+b.img*b.img);
限制150内