c++ - 混淆测试fftw3——泊松方程2d检验

标签 c++ fftw

我无法解释/理解以下现象: 为了测试 fftw3,我正在使用 2d 泊松测试用例:

laplacian(f(x,y)) = - g(x,y) 具有周期性边界条件。

将傅里叶变换应用于方程后,我们得到:F(kx,ky) = G(kx,ky)/(kx² + ky²) (1)

如果我取 g(x,y) = sin (x) + sin(y) , (x,y)\in [0,2\pi] 我立即有 f(x,y) = g(x ,y)

这就是我想用 fft 获得的:

我用正向傅立叶变换从 g 计算 G

据此我可以用 (1) 计算 f 的傅立叶变换。

最后,我用反向傅立叶变换计算 f(不要忘记按 1/(nx*ny) 归一化)。

实际上,结果很糟糕?

(例如,N = 256 时的振幅是 N = 512 时振幅的两倍)

更糟糕的是,如果我尝试 g(x,y) = sin(x)*sin(y) ,曲线甚至没有相同形式的解。

(请注意,我必须更改等式;在这种情况下,我将拉普拉斯算子除以二:(1) 变为 F(kx,ky) = 2*G(kx,ky)/(kx²+ky²)

代码如下:

/*
* fftw test -- double precision
*/
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <fftw3.h>
using namespace std;

int main()
{
 int N = 128;
 int i, j ;
 double pi = 3.14159265359;
 double *X, *Y  ; 
 X = (double*) malloc(N*sizeof(double));
   Y = (double*) malloc(N*sizeof(double));
   fftw_complex  *out1, *in2, *out2, *in1;
   fftw_plan     p1, p2;
   double L  = 2.*pi;
   double dx = L/(N - 1);

   in1 = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*(N*N) );
   out2 = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*(N*N) );
   out1 = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*(N*N) );
   in2 = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*(N*N) );

   p1 = fftw_plan_dft_2d(N, N, in1, out1, FFTW_FORWARD,FFTW_MEASURE ); 
   p2 = fftw_plan_dft_2d(N, N, in2, out2, FFTW_BACKWARD,FFTW_MEASURE);

   for(i = 0; i < N; i++){
       X[i] = -pi + i*dx ;
       for(j = 0; j < N; j++){
            Y[j] = -pi + j*dx ;
        in1[i*N + j][0] = sin(X[i]) + sin(Y[j]) ; // row major ordering
        //in1[i*N + j][0] = sin(X[i]) * sin(Y[j]) ; // 2nd test case
        in1[i*N + j][1] = 0 ; 
       }
   }

     fftw_execute(p1); // FFT forward 

     for ( i = 0; i < N; i++){   // f = g / ( kx² + ky² )  
       for( j = 0; j < N; j++){
         in2[i*N + j][0] = out1[i*N + j][0]/ (i*i+j*j+1e-16); 
         in2[i*N + j][1] = out1[i*N + j][1]/ (i*i+j*j+1e-16); 
         //in2[i*N + j][0] = 2*out1[i*N + j][0]/ (i*i+j*j+1e-16); // 2nd test case
         //in2[i*N + j][1] = 2*out1[i*N + j][1]/ (i*i+j*j+1e-16); 
       }
     }

     fftw_execute(p2); //FFT backward

     // checking the results computed

     double erl1 = 0.;
     for ( i = 0; i < N; i++) {
       for( j = 0; j < N; j++){
         erl1 += fabs( in1[i*N + j][0] -  out2[i*N + j][0]/N/N )*dx*dx; 
         cout<< i <<" "<< j<<" "<< sin(X[i])+sin(Y[j])<<" "<<  out2[i*N+j][0]/N/N <<" "<< endl; // > output
        }
      }
      cout<< erl1 << endl ;  // L1 error

      fftw_destroy_plan(p1);
      fftw_destroy_plan(p2);
      fftw_free(out1);
      fftw_free(out2);
      fftw_free(in1);
      fftw_free(in2);

      return 0;
    }

我在我的代码中找不到任何(更多)错误(我上周安装了 fftw3 库),我也没有发现数学问题,但我认为这不是 fft 的错。因此我的困境。我完全没有想法,也完全没有谷歌。

如果您能帮助解决这个难题,我们将不胜感激。

注意:

编译:g++ test.cpp -lfftw3 -lm

执行:./a.out > 输出

我使用 gnuplot 来绘制曲线: (在 gnuplot 中)splot "output"u 1:2:4(对于计算的解决方案)

最佳答案

这里有一些需要修改的小点:

  • 您需要考虑所有小频率,包括负频率!索引 i 对应于频率 2PI i/N 也对应于频率 2PI (i-N)/N。在傅立叶空间中,数组的结尾与开头一样重要!在我们的例子中,我们保持最小频率:数组的前半部分是 2PI i/N,后半部分是 2PI(i-N)/N。

  • 当然,正如Paul所说,N-1应该是Nin double dx = L/(N - 1); => double dx = L/(N ); N-1 不对应连续周期信号。很难将其用作测试用例...

  • 缩放...我凭经验做了

对于这两种情况,我获得的结果都更接近预期。这是代码:

    /*
 * fftw test -- double precision
 */
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <fftw3.h>
using namespace std;

int main()
{
    int N = 128;
    int i, j ;
    double pi = 3.14159265359;
    double *X, *Y  ; 
    X = (double*) malloc(N*sizeof(double));
    Y = (double*) malloc(N*sizeof(double));
    fftw_complex  *out1, *in2, *out2, *in1;
    fftw_plan     p1, p2;
    double L  = 2.*pi;
    double dx = L/(N );


    in1 = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*(N*N) );
    out2 = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*(N*N) );
    out1 = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*(N*N) );
    in2 = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*(N*N) );

    p1 = fftw_plan_dft_2d(N, N, in1, out1, FFTW_FORWARD,FFTW_MEASURE ); 
    p2 = fftw_plan_dft_2d(N, N, in2, out2, FFTW_BACKWARD,FFTW_MEASURE);

    for(i = 0; i < N; i++){
        X[i] = -pi + i*dx ;
        for(j = 0; j < N; j++){
            Y[j] = -pi + j*dx ;
            in1[i*N + j][0] = sin(X[i]) + sin(Y[j]) ; // row major ordering
            //  in1[i*N + j][0] = sin(X[i]) * sin(Y[j]) ; // 2nd test case
            in1[i*N + j][1] = 0 ; 
        }
    }

    fftw_execute(p1); // FFT forward 

    for ( i = 0; i < N; i++){   // f = g / ( kx² + ky² )  
        for( j = 0; j < N; j++){
            double fact=0;
            in2[i*N + j][0]=0;
            in2[i*N + j][1]=0;
            if(2*i<N){
                fact=((double)i*i);
            }else{
                fact=((double)(N-i)*(N-i));
            }
            if(2*j<N){
                fact+=((double)j*j);
            }else{
                fact+=((double)(N-j)*(N-j));
            }
            if(fact!=0){
                in2[i*N + j][0] = out1[i*N + j][0]/fact;
                in2[i*N + j][1] = out1[i*N + j][1]/fact;
            }else{
                in2[i*N + j][0] = 0;
                in2[i*N + j][1] = 0;
            }
            //in2[i*N + j][0] = out1[i*N + j][0];
            //in2[i*N + j][1] = out1[i*N + j][1];
            //  in2[i*N + j][0] = out1[i*N + j][0]*(1.0/(i*i+1e-16)+1.0/(j*j+1e-16)+1.0/((N-i)*(N-i)+1e-16)+1.0/((N-j)*(N-j)+1e-16))*N*N; 
            //  in2[i*N + j][1] = out1[i*N + j][1]*(1.0/(i*i+1e-16)+1.0/(j*j+1e-16)+1.0/((N-i)*(N-i)+1e-16)+1.0/((N-j)*(N-j)+1e-16))*N*N; 
            //in2[i*N + j][0] = 2*out1[i*N + j][0]/ (i*i+j*j+1e-16); // 2nd test case
            //in2[i*N + j][1] = 2*out1[i*N + j][1]/ (i*i+j*j+1e-16); 
        }
    }

    fftw_execute(p2); //FFT backward

    // checking the results computed

    double erl1 = 0.;
    for ( i = 0; i < N; i++) {
        for( j = 0; j < N; j++){
            erl1 += fabs( in1[i*N + j][0] -  out2[i*N + j][0]/(N*N))*dx*dx; 
            cout<< i <<" "<< j<<" "<< sin(X[i])+sin(Y[j])<<" "<<  out2[i*N+j][0]/(N*N) <<" "<< endl; // > output
            //  cout<< i <<" "<< j<<" "<< sin(X[i])*sin(Y[j])<<" "<<  out2[i*N+j][0]/(N*N) <<" "<< endl; // > output
        }
    }
    cout<< erl1 << endl ;  // L1 error

    fftw_destroy_plan(p1);
    fftw_destroy_plan(p2);
    fftw_free(out1);
    fftw_free(out2);
    fftw_free(in1);
    fftw_free(in2);

    return 0;
}

这段代码远非完美,既不优化也不美观。但它几乎给出了预期的结果。

再见,

关于c++ - 混淆测试fftw3——泊松方程2d检验,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23998054/

相关文章:

c++ - 为什么多重继承具有不同签名的方法不明确?

c++ - 何时使用 : class destructor or delete operator

c++ - 如何正确地使模拟方法调用原始虚方法

c - 如果可能的话,如何进行 fftw3 MPI "transposed"2D 变换?

c++ - 如何获取 wav 文件中的音符列表?

c++ - 在自定义通用容器中查找元素(使用 lambda 进行对象转换)C++

c++ - 空字符串的 back() 方法会返回什么?

c++ - mp3 中的频率

c++ - 非常大的 float 会导致不确定性吗?

cuda - 为什么cuFFT这么慢?