Main Content

Speed Up Fast Fourier Transforms in Code Generated from a MATLAB Function Block

When you simulate a model that includes a MATLAB Function block that calls MATLAB® fast Fourier transform (FFT) functions, the simulation software uses the library that MATLAB uses for FFT algorithms. If you generate C/C++ code for this model, by default, the code generator produces code for the FFT algorithms instead of producing FFTW library calls. To increase the speed of fast Fourier transforms in generated code, specify that the code generator produce calls to the FFTW library. For more information about FFTW, see www.fftw.org.

To produce calls to the FFTW library instead of generating FFT algorithms, you can choose from these configuration parameters in Configuration Parameters > Code Generation under the Advanced Parameters:

  • Select Built-in FFTW library callback to use the FFTW library included with MATLAB. This instructs the code generator to use the built-in callback for FFTW library calls in the generated code.

  • Select Custom FFT library callback to use the specific installed FFTW library.

The code generator produces FFTW library calls when:

  • A MATLAB Function block calls one of these MATLAB functions: fft, fft2, fftn, ifft, ifft2, or ifftn.

  • You generate C/C++ code from a model that includes the MATLAB Function block.

Use Built-in FFTW Library Callback for FFTW Library Usage

If you select Built-in FFTW library callback, the code generator produces FFTW library calls using the FFTW library version 3.3.8 included with the MATLAB.

You don't need to perform the additional steps required in Custom FFT library callback if you select this parameter during code generation.

Use Custom FFT Library Callback for FFTW Library Usage

If you select Custom FFT library callback, the code generator produces FFTW library calls when all of these conditions are true:

  • You have access to an FFTW library installation, version 3.2 or later.

  • You specify the FFTW library installation in an FFT library callback class that derives from coder.fftw.StandaloneFFTW3Interface.

  • You set the Custom FFT library callback configuration parameter to the name of the callback class.

Install an FFTW Library

If you do not have access to an installed FFTW library, version 3.2 or later, then you must install one.

For a Linux® platform or a Mac platform, consider using a package manager to install the FFTW library.

For a Windows® platform, in addition to .dll files, you must have .lib import libraries, as described in the Windows installation notes on the FFTW website.

See the installation instructions for your platform on the FFTW website.

Write FFT Callback Class

To specify your installation of the FFTW library, write an FFT callback class. Share the callback class with others who want to use this FFTW library for FFTW calls.

The callback class must derive from the abstract class coder.fftw.StandaloneFFTW3Interface. Use this example callback class as a template.

% copyright 2017 The MathWorks, Inc.

classdef useMyFFTW < coder.fftw.StandaloneFFTW3Interface
     
    methods (Static)
        function th = getNumThreads
            coder.inline('always');
            th = int32(coder.const(1));
        end
                
        function updateBuildInfo(buildInfo, ctx)
            fftwLocation = '/usr/lib/fftw';
            includePath = fullfile(fftwLocation, 'include');
            buildInfo.addIncludePaths(includePath);
            libPath = fullfile(fftwLocation, 'lib');
            
            %Double
            libName1 = 'libfftw3-3';
            [~, libExt] = ctx.getStdLibInfo();
            libName1 = [libName1 libExt];
            addLinkObjects(buildInfo, libName1, libPath, 1000, true, true);
            
            %Single
             libName2 = 'libfftw3f-3';
            [~, libExt] = ctx.getStdLibInfo();
            libName2 = [libName2 libExt];
            addLinkObjects(buildInfo, libName2, libPath, 1000, true, true);
        end
    end           
end

Implement the updateBuildInfo and getNumThreads methods. In the updateBuildInfo method, set fftwLocation to the full path for your installation of the library. Set includePath to the full path of the folder that contains fftw3.h. Set libPath to the full path of the folder that contains the library files. If your FFTW installation uses multiple threads, modify the getNumThreads method to return the number of threads that you want to use.

Optionally, you can implement these methods:

Generate FFTW Calls by Specifying an FFT Callback Class

  1. Create a Simulink® model.

  2. Add a MATLAB Function block to the model.

  3. In the MATLAB Function block, add code that calls a MATLAB FFT function. For example, add the function myfft that calls the MATLAB function fft.

    function y = myfft()
    t = 0:1/50:10-1/50;
    x = sin(2*pi*15*t) + sin(2*pi*20*t);
    y = fft(x);
    end

  4. Connect the blocks.

  5. Indicate that the code generator produce calls to the FFTW library specified in the FFT library callback class useMyFFTW. In the Configuration Parameters dialog box, set Custom FFT library callback to useMyFFTW.

    The callback class must be on the MATLAB path.

  6. Build the model.

Locate FFTW Library in Execution Environment.  The FFTW library must be available in your execution environment. If the FFTW library is shared, use environment variables or linker options to specify the location of the library.

  • On a Windows platform, modify the PATH environment variable.

  • On a Linux platform, modify the LD_LIBRARY_PATH environment variable or use the rpath linker option.

  • On a macOS platform, modify the DYLD_LIBRARY_PATH environment variable or use the rpath linker option.

To specify the rpath linker option, you can use the build information addLinkFlags method in the updateBuildInfo method of your coder.fftw.StandaloneFFTW3Interface class. For example, for a GCC compiler:

buildInfo.addLinkFlags(sprintf('-Wl,-rpath,"%s"',libPath));

See Also

Related Topics

External Websites