Click here to Skip to main content
13,738,608 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

29.4K views
1K downloads
18 bookmarked
Posted 14 May 2016
Licenced CPOL

Comparison of FFT Implementations for .NET

, 13 Jun 2016
Rate this:
Please Sign up or sign in to vote.
Comparison and benchmark of Fast Fourier Transform implementations for the .NET platform

Introduction

In this short article, we will compare a couple of Fast Fourier Transform (FFT) implementations for the .NET platform. The contestants are:

  Accord AForge DSPLib Exocortex Math.NET NAudio Lomont FFTW
Version: 3.8.0 - 1.03.1 1.2 4.5.1 1.8.4 1.1 3.3.5
License: LGPL LGPL MIT BSD MIT Ms-PL - GPL
Assemblies: 3 - - 1 1 1 - 1+1
Size: 3.6 MB - - - 1.5 MB 0.5 MB - 2.3 MB
Nuget: yes yes no no yes yes no no

Remarks

  • Accord.NET is an extension to AForge.NET and the original AForge code is part of the Accord assembly, so the AForge assemblies weren't tested separately.
  • The Exocortex project was created in the early .NET 1.0 days. The copy provided with this article was updated to target .NET 4.6.2 and to use the Complex type of the System.Numerics namespace.
  • NAudio uses a custom Complex type implementation with single precision real and imaginary part.
  • FFTW is a popular, native FFT implementation. It is probably the fastest open source implementation one can find on the internet, so comparison with the managed code is a bit unfair. Still, I thought it might be interesting to see how the code competes.

The FFTW binaries are not distributed with this article. If you want FFTW to be included in the benchmark, go to http://www.fftw.org/install/windows.html and copy libfftw3-3.dll and libfftw3f-3.dll to the application directory.

Benchmark

I was particularly interested in 1D FFTs for real valued input (audio processing). The following interface is used for all tests. If you have your own FFT implementation, it should be easy to incorporate it into the benchmark by implementing this interface and instantiate the test in the Util.LoadTests() method.

interface ITest
{
    bool Enabled {get; set; }

    /// <summary>
    /// Prepare the real valued data for FFT processing.
    /// </summary>
    /// <param name="data">The samples array.</param>
    void Initialize(double[] data);

    /// <summary>
    /// Apply FFT to data.
    /// </summary>
    /// <param name="forward">If false, apply inverse FFT.</param>
    void FFT(bool forward);

    // Ignore for benchmark (used only for 'FFT Explorer', see next section)
    double[] Spectrum(double[] input, bool scale);
}

Take a look at the different tests in the FFTBench.Benchmark namespace to see how to implement the interface properly.

Exocortex, Lomont and FFTW have specialised implementations for real valued data and the code can be expected to be about twice as fast as the standard complex implementation.

Accord.NET, Math.NET and FFTW support input arrays of any size (i.e., the size doesn't have to be a power of 2). Since AForge, Exocortex, NAudio and Lomont support radix 2 only, the benchmark uses arrays with sizes that are a power of 2.

The following table shows the total execution time (and the average) in milliseconds for FFTs of increasing size:

FFT size 1024 2048 4096 8192
Accord       332     0.03       658     0.07       1522     0.15       2947     0.29
AForge 335 0.03 731 0.07 1572 0.16 4065 0.41
Math.NET 390 0.04 773 0.08 1474 0.15 3461 0.35
NAudio 135 0.01 296 0.03 646 0.06 1435 0.14
DSPLib 151 0.02 341 0.03 726 0.07 1853 0.19
Exocortex 185 0.02 409 0.04 920 0.09 2743 0.27
Lomont 126 0.01 274 0.03 614 0.06 2465 0.25
Exocortex (real) 79 0.01 171 0.02 370 0.04 788 0.08
Lomont (real) 76 0.01 163 0.02 355 0.04 777 0.08
FFTW 29 - 65 0.01 163 0.02 662 0.07
FFTW (real) 16 - 35 - 81 0.01 221 0.02
FFTWF (real) 11 - 23 - 55 0.01 120 0.01

In the above table, each FFT is actually called 10000 times (the repeat value from the user interface was chosen as 200, and it is multiplied by 50 inner iterations). The benchmark was run on an AMD Phenom II X2 550 processor (3.1 GHz).

The next chart shows the benchmark result for the different FFTs of size 2048, 4096 and 8192:

Interpreting FFT Results

The benchmark application contains a util called FFT Explorer. You can open it by clicking on the leftmost icon of the benchmark window.

The FFT Explorer lets you select the FFT implementation, an input signal and the FFT size. Three graphs will display the input signal, the spectrum computed by the selected FFT and the signal computed by the inverse FFT.

Let's have a look at an example signal of the SignalGenerator class. The generated signal is a simple sine wave with frequency 1.0 Hz and amplitude 20.0:

public static double[] Sine(int n)
{
    const int FS = 64; // sampling rate

    return MathNet.Numerics.Generate.Sinusoidal(n, FS, 1.0, 20.0);
}

Let the FFT frame size be n = 256. With a sampling rate of 64 Hz, our periodic signal will be repeated exactly four times over the selected window. Be aware that all values are chosen to have an exact match between signal period, sampling rate and FFT size. This way, we won't have to deal with spectral leakage.

Each bin of the FFT output is spaced by the frequency resolution (sampling rate) / (FFT size), which in our case is 64 / 256 = 0.25. So we expect a peek corresponding to our 1.0 Hz signal to be in bin number 4 (since 1.0 = 4 * 0.25).

Due to the nature of the DFT, the spectrum of the signal will get scaled by n = 256, so if no further scaling is done, we expect the value to be 20.0 * 256 / 2 = 2560. We divide by 2, since the amplitude is distributed across two bins. The second bin is located at index 256 - 4 = 252 and will have the same magnitude, because, for real valued input signals, the FFT output is (conjugate) symmetric (across n/2, the bin corresponding to the Nyquist frequency).

The actual values of the peek won't be consistent between FFT implementations, since there's no common scaling convention. If the FFT size is n, then some implementations scale the FFT by 1/n, some scale the inverse FFT by 1/n and some scale both by 1/sqrt(n). Some don't scale at all (like FFTW).

The following table shows the amplitudes computed by the different FFTs for the above example:

  Accord.NET AForge.NET Exocortex.DSP Math.NET Numerics NAudio Lomont FFTW
Value: 2560 10 2560 160 10 160 2560

You can see that AForge.NET and NAudio scale by 1/n and that Math.NET and Lomont scale by 1/sqrt(n) (both Math.NET and Lomont allow the user to change scaling conventions; the values computed above and used in the benchmark represent the default settings).

Conclusion

Obviously and not completely unexpected, FFTW is the clear winner. So, if using a native DLL is an option for you, go for it. Looking at the managed code, both Exocortex and Lomont seem to be a good choice. For complex valued data, Lomont is slightly faster. For real valued signals, both perform the same. NAudio also performs pretty well, but uses a custom Complex type and does not support real valued input.

History

  • 2016-05-15 - Initial version
  • 2016-06-14 - Add information requested in the comments
  • 2018-09-02 - Update libraries, include DSPLib and fix benchmark (thanks to I'm Chris, see comments)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Studied math and computer science at the university of Dortmund.

MCTS .NET Framework 4, Windows Applications

You may also be interested in...

Comments and Discussions

 
QuestionSharpfftw.dll Pin
15-Oct-18 9:34
member15-Oct-18 9:34 
SuggestionMath.NET Intel MKL builds Pin
basarugur21-Mar-18 1:00
memberbasarugur21-Mar-18 1:00 
BugBad rounding leads to incorrect results? Pin
I'm Chris4-Mar-18 0:45
memberI'm Chris4-Mar-18 0:45 
GeneralRe: Bad rounding leads to incorrect results? Pin
Eric Ouellet20-Mar-18 7:20
professionalEric Ouellet20-Mar-18 7:20 
GeneralRe: Bad rounding leads to incorrect results? Pin
Christian Woltering1-Sep-18 12:53
memberChristian Woltering1-Sep-18 12:53 
QuestionIt would be helpful to expand FFT abbreviation at least once at the beginning of the article. Pin
Roman Ivantsov13-Jun-16 18:46
memberRoman Ivantsov13-Jun-16 18:46 
QuestionNumber of repeats Pin
Gwannoes6-Jun-16 4:44
memberGwannoes6-Jun-16 4:44 
AnswerRe: Number of repeats Pin
Christian Woltering6-Jun-16 7:05
memberChristian Woltering6-Jun-16 7:05 
GeneralRe: Number of repeats Pin
Gwannoes6-Jun-16 20:50
memberGwannoes6-Jun-16 20:50 
QuestionMix Radix Pin
Mark C. Malburg16-May-16 5:15
memberMark C. Malburg16-May-16 5:15 
AnswerRe: Mix Radix Pin
Christian Woltering16-May-16 8:45
memberChristian Woltering16-May-16 8:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180920.1 | Last Updated 14 Jun 2016
Article Copyright 2016 by Christian Woltering
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid