gpt4 book ai didi

用 C 计算 3D FFT 和逆 FFT

转载 作者:行者123 更新时间:2023-11-30 16:57:48 25 4
gpt4 key购买 nike

我想计算 FFT 和反向变换以检查其工作是否相同。我在代码中应用了大型 3D 矩阵,我尝试使用 4*4*4 矩阵对其进行测试,这是我的代码

`

#include <stdio.h>
#include <stdlib.h>
#include <complex.h>
#include <time.h>
#include <math.h>
#include <fftw3.h>


int main()
{
int N = 4; //Dimension of matrix
unsigned int seed = 1;
double *in = (double*)malloc(sizeof(double)*N*N*N);
fftw_complex *out = fftw_malloc(sizeof(fftw_complex)*N*N*N);
double *out1 = (double*)malloc(sizeof(double)*N*N*N);

fftw_plan plan_backward;
fftw_plan plan_forward;

srand ( seed );

for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
for(int k = 0; k < N; k++)
{
in[i*(N*N) + j*N + k] = rand ( );
}
}
}

printf(" Given matrix in\n");


for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
for(int k = 0; k < N; k++)
{
printf("%f\n", in[i*(N*N) + j*N + k]);
}
}
}


printf("\n");

plan_backward = fftw_plan_dft_r2c_3d ( N, N, N, in, out, FFTW_ESTIMATE );

fftw_execute ( plan_backward );

fftw_destroy_plan ( plan_backward );

printf("out matrix\n");

for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
for(int k = 0; k < N; k++)
{
printf("%f\t%f\n", creal(out[i*(N*N) + j*N + k]), cimag(out[i*(N*N) + j*N + k]));
}
}
}

printf("\n");

plan_forward = fftw_plan_dft_c2r_3d ( N, N, N, out, out1, FFTW_ESTIMATE );

fftw_execute ( plan_forward );

fftw_destroy_plan ( plan_forward );

printf("out1 matrix\n");


for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
for(int k = 0; k < N; k++)
{
printf("%f\n", out1[i*(N*N) + j*N + k]);
}
}
}

fftw_free(in);
free(out);
fftw_free(out1);

return 0;

}`

显然我的转换结果不一样。我不明白出了什么问题?

最佳答案

您的 FFT 未标准化。您的输入和输出之间存在一个恒定因素。

看看here

These transforms are unnormalized, so an r2c followed by a c2r transform (or vice versa) will result in the original data scaled by the number of real data elements—that is, the product of the (logical) dimensions of the real data.

所以因子应该是N * N * N。只需将您的数据除以这个因子,您就应该得到与您的输入相同的数据。

关于用 C 计算 3D FFT 和逆 FFT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39335632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com