gpt4 book ai didi

c - fftw 无法正确处理真实数据

转载 作者:行者123 更新时间:2023-11-30 18:46:28 25 4
gpt4 key购买 nike

我正在尝试在 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 可以帮助您检测将来未初始化值的使用情况。

关于c - fftw 无法正确处理真实数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51399388/

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