fftw 无法正确处理真实数据

fftw not working correctly with real data

我正在尝试在 C 中将 fftw3.3.5 用于 DFT。但是我目前在将 DFT 应用于真实数据时遇到了问题,它会导致一些奇怪的事情,比如非常大的数字,以及一些不是对称。

这里是测试代码:

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

//#define DEBUG

int main()
{
    fftw_complex *out;
    fftw_plan p;
    double *in;
    int N = 8;
    int i;

    in = (double*) fftw_malloc(sizeof(double) * N);
    out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);

    for (i = 0; i < N; i++)
    {
        in[i] = 1.0;
    }

    p = fftw_plan_dft_r2c_1d(N, in, out, FFTW_ESTIMATE);
    fftw_execute(p);

    for (i = 0; i < N; i++)
    {
        printf("%6.2f+j%6.2f\n", out[i][0],out[i][1]);
    }
    printf("\n");
    fftw_destroy_plan(p);
    fftw_free(in);
    fftw_free(out);
    return 0;
}

使用编译参数:

gcc -o test.exe test.c -L../lib/fftw-3.3.5-dll32 -lfftw3-3 -lm

它应该给出输出:

 8+j0
 0+j0
 0+j0
 0+j0
 0+j0
 0+j0
 0+j0
 0+j0

但我得到的是:

  8.00+j  0.00
  0.00+j  0.00
  0.00+j  0.00
  0.00+j  0.00
  0.00+j  0.00
403786757349850460000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00+j944948400857723240000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00
191040283242158910000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00+j89520024970316631000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00
11715519029813030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00+j49940585465016918000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

如果我尝试使用 128 个样本的数据,例如:

0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   -3,05175781250000e-05   0   -3,05175781250000e-05   0   -3,05175781250000e-05   0   -3,05175781250000e-05   0   -3,05175781250000e-05   0   -3,05175781250000e-05   0   -3,05175781250000e-05   0   0   -3,05175781250000e-05

我得到(使用 sqrt(real^2+imag^2)/len*2) 作为振幅:

3.81481e-006 3.71065e-006 3.40881e-006 2.93997e-006 2.35125e-006 1.70090e-006 1.05247e-006 4.74860e-007 1.86058e-007 4.41965e-007 6.28417e-007 6.74806e-007 5.98803e-007 4.41757e-007 2.77456e-007 2.51045e-007 3.64966e-007 4.63350e-007 4.86514e-007 4.21271e-007 2.76131e-007 7.43513e-008 1.51306e-007 3.64232e-007 5.29849e-007 6.21102e-007 6.23034e-007 5.36511e-007 3.84268e-007 2.43994e-007 3.02517e-007 4.96143e-007 6.74370e-007 7.82784e-007 7.98526e-007 7.19781e-007 5.70181e-007 4.21114e-007 4.20667e-007 5.92154e-007 7.92975e-007 9.36630e-007 9.83523e-007 9.20893e-007 7.62605e-007 5.64734e-007 4.74754e-007 6.25706e-007 8.81107e-007 1.10263e-006 1.22349e-006 1.21059e-006 1.05585e-006 7.81027e-007 4.81996e-007 5.15201e-007 9.35378e-007 1.42703e-006 1.88275e-006 2.25805e-006 2.53386e-006 2.71137e-006 2.80810e-006 2.85021e-006 2.86111e-006 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000 0.00000e+000

因为我的输入是真实的,这样的非对称输出应该永远不会发生...

任何人都可以指出我做错了什么以及如何解决它吗?

来自manual

fftw_plan fftw_plan_dft_r2c_1d(int n, double *in, fftw_complex *out, unsigned flags);

Here, n is the “logical” size of the DFT, not necessarily the physical size of the array. In particular, the real (double) array has n elements, while the complex (fftw_complex) array has n/2+1 elements (where the division is rounded down)

所以对称部分就不写了(反正是多余的)

在您的情况下,这与您正确阅读的 5 个元素完全匹配,其他 3 个未初始化。

valgrind 可以帮助您检测未来未初始化值的使用情况。