c语言编fft算法程序,FFT算法的C语言编程

《FFT算法的C语言编程》由会员分享,可在线阅读,更多相关《FFT算法的C语言编程(5页珍藏版)》请在人人文库网上搜索。

1、include #include #include #define PI 3./*This program is used to conduct FFT or IFFT transform based on 2 discompositiondata can be loaded by typing on the keyboard or from file.*/typedef structdouble real;double imag;complex;long NN;long t2t(long n, int M)long nn=0;for(int i=0;iM;i+)nn=nn+n%2*pow(2。

2、,M-1-i);n=n/2;return nn;int defM(long n)int M=0;while(pow(2,M)n)M+;return M;void datain(complex x,long N)int flag;long i;char s20;FILE *fpi;double a,b;printf(Load data from keybord(input 0) or file(input 1)n);scanf(%d,&flag);if(flag=0)printf(Please enter the data in the form of a,b-a+j*b ending with。

3、*n);for(i=0;getchar()!=*;i+)scanf(%lf%lf,&xi.real,&xi.imag);while(iN)xi.real=0;xi.imag=0;i+;elseif(flag=1)printf(Please input the name of the file with its File type suffix-(eg:data.txt)n);gets(s);gets(s);/To avoid the quick response to enterfpi=fopen(s,r);if(!fpi)printf(Open file error);i=0;while(!。

4、feof(fpi)fscanf(fpi,%lf,&a);fscanf(fpi,%lf,&b);xi.real=a;xi.imag=b;i+;fclose(fpi);while(iN)xi.real=0;xi.imag=0;i+;void dataout(complex x,long N)long i;for(i=0;iN;i+)printf(%.3lf+j*%.3lfn,xi.real,xi.imag);void reverse(complex x,complex xi,long n,int m)long i;long temp;for(i=0;in;i+)temp=t2t(i,m);xii.。

5、real=xtemp.real;xii.imag=xtemp.imag;complex com_add(complex x1,complex x2)complex y;y.real=x1.real+x2.real;y.imag=x1.imag+x2.imag;return y;complex com_sub(complex x1,complex x2)complex y;y.real=x1.real-x2.real;y.imag=x1.imag-x2.imag;return y;complex com_mul(complex x1,complex x2)complex y;y.real=x1.。

6、real*x2.real-x1.imag*x2.imag;y.imag=x1.real*x2.imag+x2.real*x1.imag;return y;complex com_div(complex x1,complex x2)complex y;double down=x2.real*x2.real+x2.imag*x2.imag;y.real=(x1.real*x2.real+x1.imag*x2.imag)/down;y.imag=(x2.real*x1.imag-x1.real*x2.imag)/down;return y;void fft(complex x,long NN, in。

7、t M)int m,i,j;long Nw,Ni;complex Wm,temp;for(m=0;mM;m+)Nw=pow(2,m+1);Ni=pow(2,m);for(i=0;iNi;i+)for(j=i;jNN;j=j+Nw)Wm.real=cos(2*i*PI/Nw);Wm.imag=-sin(2*i*PI/Nw);temp=com_mul(xj+Ni,Wm);xj+Ni=com_sub(xj,temp);xj=com_add(xj,temp);void ifft(complex x,long NN, int M)int m,i,j;long Nw,Ni;complex Wm,temp;。

8、for(m=0;mM;m+)Nw=pow(2,m+1);Ni=pow(2,m);for(i=0;iNi;i+)for(j=i;jNN;j=j+Nw)Wm.real=cos(2*i*PI/Nw);Wm.imag=-sin(2*i*PI/Nw);temp=com_mul(xj+Ni,Wm);xj+Ni=com_sub(xj,temp);xj=com_add(xj,temp);for(i=0;iNN;i+)xi.real=xi.real/NN;xi.imag=-xi.imag/NN;int main()long N,i;int M,flag;double a,b;printf(Please ente。

9、r the length of the sequencen);scanf(%ld,&N);M=defM(N);NN=pow(2,M);complex xyNN;complex xyiNN;datain(xy,N);for(i=N;iNN;i+)xyi.real=0;xyi.imag=0;reverse(xy,xyi,NN,M);printf(Please choose FFT(1) or IFFT(-1)n);scanf(%d,&flag);if(flag=1)fft(xyi,NN,M);elseif(flag=-1)ifft(xyi,NN,M);printf(The initial sequence is:n);dataout(xy,N);printf(The FFT results are as follows:n);dataout(xyi,N);return 0。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值