2015-04-22 106 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; 
} 
相關問題