How to conduct fft with fftw on eigen matrix?

54 Views Asked by At

I want to conduct 2d fft on eigen matrix, and when i write my the function of 2d-fft which can accept the input eigen matrix, i have to copy the eigen data into the pre-allocated memory with fftw_malloc, so the efficience is low.

fftw_complex* input1 = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * row * col);
fftw_complex* output1 = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * row * col);
fftw_plan forwardPlan = fftw_plan_dft_2d(row, col, input1, output1, FFTW_FORWARD,  FFTW_ESTIMATE);

MatrixXcd FFT_R(const MatrixXd& inputdata){
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            input1[i * col + j][0] = inputdata(i, j);
            input1[i * col + j][1] = 0.0; // Imaginary part is set to 0 for real input
        }
    }
    
    fftw_execute(forwardPlan);

    output1_(row, col);
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            output1_(i, j) = std::complex<double>(output1[i * col + j][0], output1[i * col + j][1]);
        }
    }

I expect one way to optimize my code, is there one solution to avoid the data copy?

0

There are 0 best solutions below