【问题标题】:CUDA fft different results from MATLAB fftCUDA fft 与 MATLAB fft 的结果不同
【发布时间】:2017-11-09 21:40:09
【问题描述】:

我尝试做一个简单的 fft 并比较 MATLAB 和 CUDA 之间的结果。

MATLAB: 9 个数字 1-9 的向量

I = [1 2 3 4 5 6 7 8 9];

并使用此代码:

fft(I)

给出结果:

  45.0000 + 0.0000i
  -4.5000 +12.3636i
  -4.5000 + 5.3629i
  -4.5000 + 2.5981i
  -4.5000 + 0.7935i
  -4.5000 - 0.7935i
  -4.5000 - 2.5981i
  -4.5000 - 5.3629i
  -4.5000 -12.3636i

还有CUDA代码:

int FFT_Test_Function() {

    int n = 9;

    double* in = new double[n];
    Complex* out = new Complex[n];

    for (int i = 0; i<n; i++)
    {
        in[i] = i + 1;
    }

    // Allocate the buffer
    cufftDoubleReal *d_in;
    cufftDoubleComplex *d_out;
    unsigned int out_mem_size = sizeof(cufftDoubleComplex)*n;
    unsigned int in_mem_size = sizeof(cufftDoubleReal)*n;
    cudaMalloc((void **)&d_in, in_mem_size);
    cudaMalloc((void **)&d_out, out_mem_size);

    // Save time stamp
    milliseconds timeStart = getCurrentTimeStamp();

    cufftHandle plan;
    cufftResult res = cufftPlan1d(&plan, n, CUFFT_D2Z, 1);
    if (res != CUFFT_SUCCESS) { cout << "cufft plan error: " << res << endl; return 1; }
    cudaCheckErrors("cuda malloc fail");

    cudaMemcpy(d_in, in, in_mem_size, cudaMemcpyHostToDevice);
    cudaCheckErrors("cuda memcpy H2D fail");

    res = cufftExecD2Z(plan, d_in, d_out);
    if (res != CUFFT_SUCCESS) { cout << "cufft exec error: " << res << endl; return 1; }
    cudaMemcpy(out, d_out, out_mem_size, cudaMemcpyDeviceToHost);
    cudaCheckErrors("cuda memcpy D2H fail");

    milliseconds timeEnd = getCurrentTimeStamp();
    milliseconds totalTime = timeEnd - timeStart;
    std::cout << "Total time: " << totalTime.count() << std::endl;

    return 0;
}

在这个 CUDA 代码中我得到了结果:

您可以看到 CUDA 给出了 4 个零(单元格 5-9)。

我错过了什么?

非常感谢您的关注!

【问题讨论】:

    标签: matlab math cuda signal-processing fft


    【解决方案1】:

    CUFFT_D2Z 是实数到复数 FFT,因此输出数据中的顶部 N/2 - 1 点是冗余的 - 它们只是变换下半部分的复共轭(您可以在 MATLAB 中看到这一点如果您比较在中点附近镜像的术语对,则输出)。

    如果需要,您可以填写这些“缺失”的术语,只需取每个相应术语的复共轭,但通常这样做没有多大意义。

    【讨论】:

    猜你喜欢
    • 2017-11-10
    • 2018-09-05
    • 2017-09-28
    • 2022-01-07
    • 2015-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-08
    相关资源
    最近更新 更多