2015-04-22 73 views
5

我需要执行FFT和Inverse-FFT转换。输入将是矢量和双矩阵。理想情况下,输出应该是一个std :: complex数组,但我可以使用double _Complex。有没有关于如何使用英特尔MKL FFT的简单C++示例?

我还没有找到任何简单的例子,所有的英特尔示例都一次做了很多事情,没有足够的评论。

我只想一个简单的例子,在C++中将双向量(或矩阵)作为输入并输出FFT转换结果(理想情况下使用std :: complex)。

+0

我假设您指的是[这些示例](https://software.intel.com/zh-cn/node/471390),也许更具体的是“C接口”。如果出现这种情况,请确保阅读顶部的[傅里叶变换函数](https://software.intel.com/zh-cn/node/470818#8EB0A29C-06D8-4C97-ACD0-C8A320501A6A)链接。 – SleuthEye

+0

@SleuthEye是的,我指的是他们。我个人并没有发现这些臃肿的例子有用,但对其他人来说可能就足够了。我想找到更简单的例子。看来我可能不得不对他们做。 –

+0

我继续检查官方的例子,但其中一些甚至没有编译...这不是一个很好的开始... –

回答

5

我结束了几件事情的测试,最后终于得到了这三个功能,这些功能完成了我想要的功能,并且我考虑了一些简单的例子。

我测试了一些输入,我得到了很好的结果。虽然我没有做过广泛的测试。

//Note after each operation status should be 0 on success 

std::vector<std::complex<float>> fft_complex(std::vector<std::complex<float>>& in){ 
    std::vector<std::complex<float>> out(in.size()); 

    DFTI_DESCRIPTOR_HANDLE descriptor; 
    MKL_LONG status; 

    status = DftiCreateDescriptor(&descriptor, DFTI_SINGLE, DFTI_COMPLEX, 1, in.size()); //Specify size and precision 
    status = DftiSetValue(descriptor, DFTI_PLACEMENT, DFTI_NOT_INPLACE); //Out of place FFT 
    status = DftiCommitDescriptor(descriptor); //Finalize the descriptor 
    status = DftiComputeForward(descriptor, in.data(), out.data()); //Compute the Forward FFT 
    status = DftiFreeDescriptor(&descriptor); //Free the descriptor 

    return out; 
} 

std::vector<std::complex<float>> fft_real(std::vector<float>& in_real){ 
    std::vector<std::complex<float>> in(in_real.size()); 

    std::copy(in_real.begin(), in_real.end(), in.begin()); 

    return fft_complex(in); 
} 

std::vector<float> ifft(std::vector<std::complex<float>>& in){ 
    std::vector<std::complex<float>> out(in.size()); 

    DFTI_DESCRIPTOR_HANDLE descriptor; 
    MKL_LONG status; 

    status = DftiCreateDescriptor(&descriptor, DFTI_SINGLE, DFTI_COMPLEX, 1, in.size()); //Specify size and precision 
    status = DftiSetValue(descriptor, DFTI_PLACEMENT, DFTI_NOT_INPLACE); //Out of place FFT 
    status = DftiSetValue(descriptor, DFTI_BACKWARD_SCALE, 1.0f/in.size()); //Scale down the output 
    status = DftiCommitDescriptor(descriptor); //Finalize the descriptor 
    status = DftiComputeBackward(descriptor, in.data(), out.data()); //Compute the Forward FFT 
    status = DftiFreeDescriptor(&descriptor); //Free the descriptor 

    std::vector<float> output(out.size()); 

    for(std::size_t i = 0; i < out.size(); ++i){ 
     output[i] = out[i].real(); 
    } 

    return output; 
} 
相关问题