FFTSharp is a .NET library for signal processing that provides functions to perform fast Fourier transforms (FFT) and other signal processing algorithms.
The Fourier Transform is a mathematical tool that allows converting a signal in the time domain to its representation in the frequency domain. Even if the operation doesn’t sound familiar, you have probably used it, for example, when using an equalizer to adjust the “bass and treble” of an audio player.
In the case of sampled (discretized) signals, the equivalent is the DFT (discrete Fourier transform). Meanwhile, the FFT (Fast Fourier Transform) is an algorithm for efficiently calculating the DFT and its inverse computationally.
FFT is a widely used technique in signal processing, with applications in many cases. It is used from processing audio, images, or video signals to the analysis of seismic and astronomical data.
FFTSharp allows us to implement these functionalities easily in a .NET application, including:
- High-precision and high-speed Fast Fourier Transforms (FFT)
- Functions to calculate power spectra, phase spectra, and other related calculations
- Functions to apply analysis and filtering windows to signals
- Functions to perform convolution and correlation operations
It is designed to be easy to use and offer fast and accurate performance. Therefore, it is an excellent choice for when we need to perform signal processing. Typically audio and images, although it is useful for any series of data.
How to Use FFTSharp
We can easily add the library to a .NET project through the corresponding NuGet package.
Install-Package FftSharp
Now we can use FFTSharp on a series of data as follows.
double[] data = { 1, 2, 3, 4, 5, 4, 3, 2, 1 };
var spectrum = FftSharp.FFO.Power(data);
for (int i = 0; i < spectrum.Length; i++)
{
Console.WriteLine($"Frequency: {i}, Power: {spectrum[i]}");
}
Here is an example of how to use it on a sampled audio wave, along with the ScottPlot library to plot the data.
// sample audio with tones at 2, 10, and 20 kHz plus white noise
double[] signal = FftSharp.SampleData.SampleAudio1();
int sampleRate = 48_000;
// plot the sample audio
var plt = new ScottPlot.Plot(400, 200);
plt.AddSignal(signal, sampleRate / 1000.0);
plt.YLabel("Amplitude");
plt.Margins(0);
plt.SaveFig("time-series.png");
The project page explains the different options and provides more examples. They also provide a Demo application in the repository to check the functionality of the options in simulated sound, and another using real audio obtained with a microphone.
FFTSharp is Open Source under the MIT license, and all the code and documentation is available in the project’s repository at https://github.com/swharden/FftSharp.