简体   繁体   English

用C计算3D FFT和逆FFT

[英]Computing 3D FFT and Inverse FFT in C

I want to compute FFT and back transform to check if it works the same. 我想计算FFT并进行逆变换,以检查其工作原理是否相同。 I have an application of large 3D matrix in my code I tried to test it with 4*4*4 matrix and here is my code 我的代码中有一个大型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;

}`

Apparently my transformed results are not the same. 显然我的转换结果不一样。 I don't understand what is going wrong? 我不明白怎么了?

As described in the manual of FFTW3 : FFTW3手册中所述

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. 这些转换未归一化,因此r2c和c2r转换(反之亦然)将导致原始数据按真实数据元素的数量(即,真实数据的(逻辑)维的乘积)进行缩放。

The number of real dimensions is 4^3 in your case and dividing the first result 115474520512 by that number gives back the first input 115474520512/(4^3) = 1804289383 . 实际维数为4^3 ,将第一个结果115474520512除以该数字即得到第一个输入115474520512/(4^3) = 1804289383

Your FFT is not normalized. 您的FFT未标准化。 There is a constant factor between your input and output. 您的输入和输出之间有一个常数。

Take a look 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. 这些转换未归一化,因此r2c和c2r转换(反之亦然)将导致原始数据按真实数据元素的数量(即,真实数据的(逻辑)维的乘积)进行缩放。

So the factor should be N * N * N . 因此,该因子应为N * N * N Just divide your data by this factor and you should get back the same data as your input. 只要将您的数据除以这个系数,您就应该获得与输入相同的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM