这几天在网上查阅资料,研究了几天的快速傅里叶变换,彻底搞清楚了这怎么一回事。下载过几个版本的fft算法,但计算结果都不尽如人意,所以今天花了一上午时间,照自己的思路实现了fft算法,与dft对比运算结果,发现误差较小。算法依据原理可参照以下文章:FFT快速傅里叶变换(蝶形算法)详解
以下就分享一下代码咯,思路都体现在源程序的本身结构和注释中。
part 1,myfft.h(自定义函数声明)
#ifndef __myfft_h
#define __myfft_h
float mysin(float x);
float mycos(float x);
float myfabs(float x);
float mysqrt(float x);
int mypow(int i,int j);
int mylog(int n,int a);
void dft(const float x[],float real[],float imag[],const int n,int isign);
int rev(int i,int m);
void fft(const float real_in[],const float imag_in[],float real_out[],float imag_out[],const int n,int isign);
#endif
part 2,data.h(正弦表)
#ifndef __data_h
#define __data_h
//正弦表
const float sin_tab[360]={//0-359度正弦表
0.000,0.017,0.035,0.052,0.070,0.087,0.105,0.122,0.139,0.156,
0.174,0.191,0.208,0.225,0.242,0.259,0.276,0.292,0.309,0.326,
0.342,0.358,0.375,0.391,0.407,0.423,0.438,0.454,0.469,0.485,
0.500,0.515,0.530,0.545,0.559,0.574,0.588,0.602,0.616,0.629,
0.643,0.656,0.669,0.682,0.695,0.707,0.719,0.731,0.743,0.755,
0.766,0.777,0.788,0.799,0.809,0.819,0.829,0.839,0.848,0.857,
0.866,0.875,0.883,0.891,0.899,0.906,0.914,0.921,0.927,0.934,
0.940,0.946,0.951,0.956,0.961,0.966,0.970,0.974,0.978,0.982,
0.985,0.988,0.990,0.993,0.995,0.996,0.998,0.999,0.999,1.000,
1.000,1.000,0.999,0.999,0.998,0.996,0.995,0.993,0.990,0.988,
0.985,0.982,0.978,0.974,0.970,0.966,0.961,0.956,0.