# MEX Generation with Coder: Link FFTW?

9 views (last 30 days)
Raj on 11 Feb 2014
Edited: Ryan Livingston on 11 Apr 2018
I have an algorithm that heavily uses FFTs, and I am using Coder to generate a MEX function. However, I see that for C/C++ targets, FFT lengths must be a power of 2: http://www.mathworks.com/help/simulink/ug/functions-supported-for-code-generation--alphabetical-list.html
My algorithm needs to perform FFTs that have length CLOSE to a power of two; my algorithm naturally generates a vector with length 2^(N-1), but then because my calculations require a type-I inverse DCT, I end up passing a vector of length (2^N-2) into the FFT algorithm. Because (2^N-2) is not a power of two, I get an error about FFT lengths when using Coder in this way. The Signal Processing toolbox function "idct" doesn't do the trick either, because that's a type-II transform.
I can get around this by declaring coder.extrinsic("fft"), but then I'm not really getting the full speed benefit of C/C++ because data is being sent back into MATLAB, which presumably calls FFTW anyway to do the calculation, then passes the result back.
My question is: can I get Coder to generate C/C++ that calls FFTW directly, and link the generated MEX against the FFTW library? It seems that Coder supports this kind of thing with BLAS already, where it generates calls to BLAS and links in the library (I think). Any one out there have thoughts or experience they can share about this?

Ryan Livingston on 13 Feb 2014
Edited: Ryan Livingston on 11 Apr 2018
Edit April 11, 2018 As of MATLAB R2017b MATLAB Coder supports generating FFTW calls from the base MATLAB functions fft,fft2,fftn and their inverses: https://www.mathworks.com/help/releases/R2017b/coder/ug/speed-up-fast-fourier-transforms-in-generated-standalone-code-by-using-fftw-library-calls.html. Generated MEX files use the FFTW library shipped with MATLAB. Generated standalone code uses the FFTW library that a user downloads and specifies.
Prior to R2017b, the base MATLAB Coder version of FFT does not support generating calls to FFTW. One option is to use the dsp.FFT system object if you have the DSP System Toolbox:
Otherwise, you could use an extrinsic as you mentioned or call external C code using coder.ceval. These external calls could be used to call FFTW directly or call a C wrapper which implements calling FFTW.