IntroductionIn this short article, we will compare a couple of Fast Fourier Transform (FFT) implementations for the .NET platform. The contestants are:
[td]
| Accord | | | | | | | |
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.
BenchmarkI 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.
Hide Copy Code
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:
[td]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 ResultsThe 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:
Hide Copy Code
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:
[td]
| 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).
ConclusionObviously 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)