C 语言 fft 1024代码
时间: 2023-09-25 22:08:44 浏览: 38
以下是一个简单的 C 语言 FFT 1024 的代码示例:
```c
#include <stdio.h>
#include <math.h>
#define PI 3.14159265359
void fft(double *real, double *imag, int n) {
int i, j, k, m;
double arg, cosarg, sinarg;
double *real_tmp, *imag_tmp;
// Allocate memory for temporary arrays
real_tmp = (double*)malloc(n * sizeof(double));
imag_tmp = (double*)malloc(n * sizeof(double));
// Copy input data to temporary arrays
for (i = 0; i < n; i++) {
real_tmp[i] = real[i];
imag_tmp[i] = imag[i];
}
// Calculate FFT
m = log2(n);
for (i = 0; i < m; i++) {
k = 1 << i;
for (j = 0; j < k; j++) {
arg = -2 * j * PI / (2 * k);
cosarg = cos(arg);
sinarg = sin(arg);
for (int l = j; l < n; l += 2 * k) {
double t1 = cosarg * real_tmp[l + k] - sinarg * imag_tmp[l + k];
double t2 = sinarg * real_tmp[l + k] + cosarg * imag_tmp[l + k];
real_tmp[l + k] = real_tmp[l] - t1;
imag_tmp[l + k] = imag_tmp[l] - t2;
real_tmp[l] += t1;
imag_tmp[l] += t2;
}
}
}
// Copy data back to real and imag arrays
for (i = 0; i < n; i++) {
real[i] = real_tmp[i];
imag[i] = imag_tmp[i];
}
// Free temporary arrays
free(real_tmp);
free(imag_tmp);
}
int main() {
int n = 1024;
double *real = (double*)malloc(n * sizeof(double));
double *imag = (double*)malloc(n * sizeof(double));
// Initialize input data
for (int i = 0; i < n; i++) {
real[i] = sin(2 * PI * i / n);
imag[i] = 0;
}
// Perform FFT
fft(real, imag, n);
// Print output data
for (int i = 0; i < n; i++) {
printf("%lf + %lfi\n", real[i], imag[i]);
}
// Free memory
free(real);
free(imag);
return 0;
}
```
注意:此代码仅供参考,实际使用中需要根据具体情况进行修改和优化。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)