使用fftw进行傅立叶逆变换的问题

时间:2019-07-09 22:53:35

标签: c++ fft fftw

我正在使用库fftw3执行离散傅里叶变换。我有一组实际值,我的流程如下 input_vals-> Fourier变换-> out_vals-> inverse_fourier-> input_vals-> Fourier变换-> out_vals_2

根据这种逻辑,out_vals_2应该具有与out_vals相同的值(可以缩放或类似,但这不是事实。

void fill_vals(std::vector<double>& vec);
int main() {
    std::vector<double> in_vals;
    std::vector<double> in_vals_2;
    in_vals.resize(100 * 100);
    in_vals_2.resize(100 * 100);
    std::vector<std::complex<double>> out_vals;
    std::vector<std::complex<double>> out_vals_2;

    out_vals.resize(100 * 51);
    out_vals_2.resize(100 * 51); // fftw only needs half of the 2nd dim since hermitian symmetry
    fftw_plan p = fftw_plan_dft_r2c_2d(100, 100, in_vals.data(), reinterpret_cast<fftw_complex*>(out_vals.data()), FFTW_ESTIMATE);
    fill_vals(in_vals);
    fftw_execute(p);
    fftw_plan p2 = fftw_plan_dft_c2r_2d(100, 100, reinterpret_cast<fftw_complex*>(out_vals.data()),in_vals_2.data(), FFTW_ESTIMATE);
    fftw_execute(p2);
    std::for_each(in_vals_2.begin(), in_vals_2.end(), [](double& n) { n *= 1e-4; });

    fftw_plan p3 = fftw_plan_dft_r2c_2d(100, 100, in_vals_2.data(), reinterpret_cast<fftw_complex*>(out_vals_2.data()), FFTW_ESTIMATE);
    fftw_execute(p3);

    for (size_t i = 0; i < 100; i++) {
        for (size_t j = 0; j < 51; j++) {
            auto v1 = out_vals[j + i * 51];
            auto v2 = out_vals_2[j + i * 51];
            std::cout << "first: " << v1 << " second: " << v2 << std::endl;
        }  
    }
    fftw_destroy_plan(p);
    fftw_destroy_plan(p2);
    fftw_destroy_plan(p3);

}

void fill_vals(std::vector<double>& vec) {
    double dx = 0.1;
    double dy = 0.1;
    auto func = [](double x, double y) { return x * x + y * y; };
    for (size_t i = 0; i < 100; i++) {
        for (size_t j = 0; j < 100; j++) {
            vec[j + i * 100] = func(i*dx, j*dy);
        }
    }
}

如您所见,打印输出值的第一和第二个数组的结果。我已经验证了input_vals和input_vals_2是相同的,只是input_vals_2按预期比例缩放了10 ^ 4。 我期望out_vals和out_vals_2会有类似的情况,但这是我得到的结果的一个示例

first: (2.90192e-09,5.92922e-09) second: (-7.04593e-13,3.38256e-13)
first: (-2.36417e-08,1.25076e-08) second: (-1.50865e-12,3.40183e-13)
first: (1.39576e-08,-1.31249e-08) second: (-5.03563e-13,-2.52082e-12)
first: (2.30798e-08,-1.90146e-08) second: (1.09575e-12,-1.5964e-12)
first: (-6.1091e-09,-2.94573e-09) second: (-1.44963e-12,-2.07039e-13)
first: (-1.28917e-08,1.48862e-09) second: (-4.04995e-13,-7.62142e-14)
first: (2.25352e-09,-4.84718e-09) second: (2.69047e-13,-1.20759e-12)
first: (-9.71393e-09,4.56817e-09) second: (4.55367e-13,-1.94835e-12)
first: (7.21847e-09,-5.33878e-09) second: (7.24309e-13,4.32916e-14)
first: (4.15668e-09,4.29824e-09) second: (2.31217e-13,4.0434e-13)
first: (1.37686e-08,-6.91411e-10) second: (7.21783e-13,-5.61445e-13)
first: (6.36243e-09,-1.1004e-08) second: (9.90841e-13,-3.4591e-13)
first: (-2.90555e-08,2.09318e-08) second: (-1.14937e-12,-2.00677e-12)
first: (-1.94677e-10,7.85383e-09) second: (-9.95736e-14,7.70894e-13)
first: (4.99992e-09,-1.01281e-08) second: (-3.27121e-13,-2.32422e-14)
first: (-8.02526e-09,-7.56433e-09) second: (-3.79182e-13,-2.91813e-13)
first: (1.09941e-08,-6.26367e-10) second: (1.4779e-12,-5.72337e-13)
first: (1.5862e-08,-9.53884e-09) second: (-3.96342e-13,2.11923e-14)
first: (-6.90393e-10,-1.38592e-08) second: (2.41286e-13,-1.54535e-13)
first: (4.06874e-09,1.25382e-08) second: (2.47501e-13,1.03788e-12)
first: (8.54262e-09,5.28323e-09) second: (9.61306e-13,9.28605e-13)
first: (4.74809e-09,1.22679e-08) second: (7.74782e-13,1.21958e-12)
first: (-4.14073e-08,-3.15596e-08) second: (-6.37816e-12,-8.50306e-13)

编辑:我在索引编制中犯了一个错误,这是新的输出

0 个答案:

没有答案