C语言实现傅里叶变换算法详解
#include <stdio.h> #include <math.h>
#define PI 3.14159265358979323846
void fourier_transform(double x[], double y[], int n) { int i, j; double wr, wi, angle, tempr, tempi;
for (i = 0; i < n; i++) {
x[i] = x[i] / n;
y[i] = y[i] / n;
}
for (i = 0; i < n; i++) {
tempr = 0.0;
tempi = 0.0;
angle = 2 * PI * i / n;
for (j = 0; j < n; j++) {
wr = cos(angle * j);
wi = sin(angle * j);
tempr += x[j] * wr - y[j] * wi;
tempi += x[j] * wi + y[j] * wr;
}
x[i] = tempr;
y[i] = tempi;
}
}
int main() { int i, n; double x[100], y[100];
printf("Enter the number of data points: ");
scanf("%d", &n);
printf("Enter the data points:
"); for (i = 0; i < n; i++) { scanf("%lf %lf", &x[i], &y[i]); }
fourier_transform(x, y, n);
printf("
Fourier transform: "); for (i = 0; i < n; i++) { printf("%lf + %lfi\n", x[i], y[i]); }
return 0;
}
此示例中,输入数据点并通过fourier_transform函数进行傅里叶变换,然后输出变换结果。请注意,此示例仅适用于离散傅里叶变换(DFT),并且需要在n个数据点上运行。
原文地址: https://www.cveoy.top/t/topic/oOWf 著作权归作者所有。请勿转载和采集!