单个C文件中的FFT

我正在寻找C中的FFT实现。但是,我不是在寻找一个庞大的库(如FFTW),而是一个易于使用的单个C文件实现。 不幸的是我找不到这样的东西。

有人可以推荐一个简单的实现?

你最好的选择是KissFFT – 因为它的名字暗示它很简单 ,但它仍然相当快,并且比FFTW轻得多。 它也是免费的,如果你想将它包含在商业产品中,那么FFTW需要大量的许可费。

此文件可以正常工作:只需复制并粘贴到您的计算机中即可。 在网上冲浪我在维基百科页面上找到了这个简单的实现。 该页面是意大利语,所以我重新编写了一些翻译代码。 这里有几乎相同的信息,但英文。 请享用!

#include  #include  #define MAX 200 using namespace std; #define M_PI 3.1415926535897932384 int log2(int N) /*function to calculate the log2(.) of int numbers*/ { int k = N, i = 0; while(k) { k >>= 1; i++; } return i - 1; } int check(int n) //checking if the number of element is a power of 2 { return n > 0 && (n & (n - 1)) == 0; } int reverse(int N, int n) //calculating revers number { int j, p = 0; for(j = 1; j <= log2(N); j++) { if(n & (1 << (log2(N) - j))) p |= 1 << (j - 1); } return p; } void ordina(complex* f1, int N) //using the reverse order in the array { complex f2[MAX]; for(int i = 0; i < N; i++) f2[i] = f1[reverse(N, i)]; for(int j = 0; j < N; j++) f1[j] = f2[j]; } void transform(complex* f, int N) // { ordina(f, N); //first: reverse order complex *W; W = (complex *)malloc(N / 2 * sizeof(complex)); W[1] = polar(1., -2. * M_PI / N); W[0] = 1; for(int i = 2; i < N / 2; i++) W[i] = pow(W[1], i); int n = 1; int a = N / 2; for(int j = 0; j < log2(N); j++) { for(int i = 0; i < N; i++) { if(!(i & n)) { complex temp = f[i]; complex Temp = W[(i * a) % (n * a)] * f[i + n]; f[i] = temp + Temp; f[i + n] = temp - Temp; } } n *= 2; a = a / 2; } } void FFT(complex* f, int N, double d) { transform(f, N); for(int i = 0; i < N; i++) f[i] *= d; //multiplying by step } int main() { int n; do { cout << "specify array dimension (MUST be power of 2)" << endl; cin >> n; } while(!check(n)); double d; cout << "specify sampling step" << endl; //just write 1 in order to have the same results of matlab fft(.) cin >> d; complex vec[MAX]; cout << "specify the array" << endl; for(int i = 0; i < n; i++) { cout << "specify element number: " << i << endl; cin >> vec[i]; } FFT(vec, n, d); cout << "...printing the FFT of the array specified" << endl; for(int j = 0; j < n; j++) cout << vec[j] << endl; return 0; } 

您可以开始将此java片段转换为C作者声明他已根据您在线找到的图书数字收件人将其转换为C! 这里

这是一个允许许可的C库,它具有各种不同的FFT实现 ,每个实现都在它自己的独立C文件中。