CUDA fft 1d与MATLAB fft不同的结果

时间:2018-03-27 12:27:18

标签: matlab cuda fft

我想用GPU加速我的matlab程序,但我发现了一个问题。 fft结果不同于CUDA到matlab。 我已经尝试了很多次,但无法解决它。 所以我来这里寻求帮助。

原始数据: name:cj1;size:1*8

在matlab中使用代码:

A1 = FFT(CJ1)';

获得结果: the fft result of matlab

和cuda代码:

cuFloatComplex *idata_m;
idata_m = (cuFloatComplex*)malloc(M * sizeof(cuFloatComplex));
for (int i = 0; i < 8; i++)
{
    idata_m[i].x = initA[i];
    idata_m[i].y = initB[i];
}
cuComplex *dev_test;
cudaMalloc((void**)&dev_test, M  * sizeof(cuFloatComplex));
cudaMemcpy(dev_test, idata_m, M * sizeof(cuFloatComplex), cudaMemcpyHostToDevice);
cufftHandle plantest;
cufftPlan1d(&plantest, 8, CUFFT_C2C, 1);
cufftExecC2C(plantest, dev_test, dev_test, CUFFT_FORWARD);//forward
cuComplex *test_out;
test_out = (cuFloatComplex*)malloc( M * sizeof(cuFloatComplex));
cudaMemcpy(test_out, dev_test, 8 * sizeof(cuFloatComplex), cudaMemcpyDeviceToHost);

the input data is the same to the original data in matlab

the fft result of cuda

不安全的事情是这两个结果非常相似,但顺序错误。 那么我该怎样做才能使结果与matlab的结果相同呢?

1 个答案:

答案 0 :(得分:2)

与CUDA代码一起使用的输入数据的虚部是与Matlab一起使用的虚部。所以你真的在计算复共轭输入的FFT,inverts the order of the result。要使用CUDA获得相同的结果,您应该使用相同的输入。

另外值得注意的是,在Matlab中,'运算符计算复共轭转置,因此您可能希望将您的CUDA结果与a1=transpose(fft(cj1));进行比较。