写一个二维离散傅里叶变换的C语言代码
时间: 2023-11-29 14:57:49 浏览: 214
以下是一个简单的二维离散傅里叶变换的C语言实现:
```c
#include <stdio.h>
#include <math.h>
#define N 8 //矩阵大小
void DFT2D(double input[N][N], double output[N][N]) {
int u, v, x, y;
double sum_real, sum_imag;
double W_real, W_imag;
for (u = 0; u < N; u++) {
for (v = 0; v < N; v++) {
sum_real = 0.0;
sum_imag = 0.0;
for (x = 0; x < N; x++) {
for (y = 0; y < N; y++) {
W_real = cos(2 * M_PI * ((double)(u * x) / N + (double)(v * y) / N));
W_imag = -sin(2 * M_PI * ((double)(u * x) / N + (double)(v * y) / N));
sum_real += input[x][y] * W_real - input[x][y] * W_imag;
sum_imag += input[x][y] * W_imag + input[x][y] * W_real;
}
}
output[u][v] = sqrt(sum_real * sum_real + sum_imag * sum_imag);
}
}
}
int main() {
double input[N][N] = {
{10, 20, 30, 40, 50, 60, 70, 80},
{90, 100, 110, 120, 130, 140, 150, 160},
{170, 180, 190, 200, 210, 220, 230, 240},
{250, 260, 270, 280, 290, 300, 310, 320},
{330, 340, 350, 360, 370, 380, 390, 400},
{410, 420, 430, 440, 450, 460, 470, 480},
{490, 500, 510, 520, 530, 540, 550, 560},
{570, 580, 590, 600, 610, 620, 630, 640}
};
double output[N][N];
DFT2D(input, output);
int i, j;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
printf("%.2f ", output[i][j]);
}
printf("\n");
}
return 0;
}
```
该代码中的DFT2D函数实现了二维离散傅里叶变换,输入矩阵为input,输出矩阵为output。在主函数中,我们定义了一个8x8的输入矩阵,通过DFT2D函数进行变换,并输出结果。
阅读全文