gpt4 book ai didi

c - 2D FFTW 中的零困难

转载 作者:行者123 更新时间:2023-11-30 15:48:04 26 4
gpt4 key购买 nike

我有一个由结构 double input[N][M] 表示的实数矩阵,其中我想沿 N 方向对每列 j = 0..M 进行 1D FT。我的尝试如下:

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

#define M_PI 3.14159265358979323846264338327

int main(void)
{
int N = 16; int M = 2;

double input[N][M]; // input data
double *in = (double *) input;

fftw_complex output[N][M]; // output data
fftw_complex *out = (fftw_complex *) output;

for (int i = 0; i < N; ++i)
{
for (int j = 0; j < M; ++j)
{
double x = (double) i / (double) N;
input[i][j] = sin(2*(j+1) * M_PI * x);
fprintf (stderr, "input[%d][%d] = %.9f, ", i, j, input[i][j]);
}
fprintf (stderr, "\n");
}
fprintf (stderr, "\n");

// setup plans
int rank = 1; int howmany = M;
int istride = M; int ostride = M; int idist = 1; int odist = 1;

fftw_plan fp = fftw_plan_many_dft_r2c(rank, &N, howmany,
in, NULL, istride, idist,
out, NULL, ostride, odist,
FFTW_MEASURE);


// take the forward transform
fftw_execute(fp); fprintf (stderr, "FFT complete\n");

for (int j = 0; j < M; ++j)
for (int i = 0; i < N/2; ++i)
fprintf (stderr, "OUT[%3d] = (%.4f + %.4fi)\n",
i, output[i][j][0], output[i][j][1]);

fprintf (stderr, "\n");

fftw_destroy_plan(fp);

return 0;
}

我正在使用 gcc fft.c -std=c99 -g -lfftw3 -lm 进行编译。然而,代码似乎不起作用:FT 输出全为零(请参阅 here )

该函数的文档是 here .

编辑:更新,因此它似乎仅适用于 FFTW_ESTIMATE,而不适用于任何 other flags 。知道为什么会这样吗?

最佳答案

啊破解了!来自 Planner Flags page :

Important: the planner overwrites the input array during planning unless a saved plan (see Wisdom) is available for that problem, so you should initialize your input data after creating the plan. The only exceptions to this are the FFTW_ESTIMATE and FFTW_WISDOM_ONLY flags, as mentioned below.

因此,它正在设置计划并覆盖输入:简单的解决方案是在数据数组初始化之前移动计划创建。

工作示例代码(FT 向前,然后向后)here .

关于c - 2D FFTW 中的零困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17050036/

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