写一段 1024个点的FFT 代码
时间: 2023-07-10 18:09:41 浏览: 51
以下是一个简单的 1024 点 FFT 的代码,使用了 Cooley-Tukey 算法:
```c++
#include <complex>
#include <cmath>
const double PI = acos(-1);
void fft(std::complex<double>* x, int n, int s) {
if (n == 1) {
return;
}
// Split the array into even and odd parts
std::complex<double>* xe = new std::complex<double>[n / 2];
std::complex<double>* xo = new std::complex<double>[n / 2];
for (int i = 0; i < n / 2; i++) {
xe[i] = x[2 * i];
xo[i] = x[2 * i + 1];
}
// Recursively compute the FFT of the even and odd parts
fft(xe, n / 2, 2 * s);
fft(xo, n / 2, 2 * s);
// Combine the results
for (int k = 0; k < n / 2; k++) {
std::complex<double> t = std::polar<double>(1.0, -2 * PI * k / n) * xo[k];
x[k] = xe[k] + t;
x[k + n / 2] = xe[k] - t;
}
delete[] xe;
delete[] xo;
}
void fft(std::complex<double>* x, int n) {
fft(x, n, 1);
}
```
这个代码中假设输入数组 x 的长度为 1024,而且它会被修改为输出结果。调用 `fft(x, 1024)` 就可以计算 1024 点 FFT。注意这个代码中没有做任何优化,所以它可能不够快。如果你需要更快的 FFT 实现,可以考虑使用其他库或者优化算法。