请用C++写一个FFT模板
时间: 2024-05-06 17:21:26 浏览: 7
好的,以下是C语言中的FFT模板:
```c
#include <stdio.h>
#include <math.h>
typedef struct _complex {
double real, imag;
} complex;
const double PI = atan(1.0) * 4;
void fft(complex *x, int N) {
if (N <= 1) return;
complex *even = new complex[N / 2];
complex *odd = new complex[N / 2];
for (int i = 0; i < N / 2; i++) {
even[i] = x[i * 2];
odd[i] = x[i * 2 + 1];
}
fft(even, N / 2);
fft(odd, N / 2);
for (int k = 0; k < N / 2; k++) {
double angle = -2 * PI * k / N;
complex t(cos(angle), sin(angle));
t = t * odd[k];
x[k] = even[k] + t;
x[k + N / 2] = even[k] - t;
}
delete[] even;
delete[] odd;
}
int main() {
int N;
printf("请输入N值:");
scanf("%d", &N);
complex *a = new complex[N];
printf("请输入%d个复数A[]:\n", N);
for (int i = 0; i < N; i++) {
scanf("%lf%lf", &a[i].real, &a[i].imag);
}
fft(a, N);
printf("FFT结果是:\n");
for (int i = 0; i < N; i++) {
printf("(%f, %f)\n", a[i].real, a[i].imag);
}
delete[] a;
return 0;
}
```
希望以上代码可以帮助到您!