Spectrogram is a .NET library for creating spectrograms from pre-recorded signals or live audio from the sound card. Spectrogram uses FFT algorithms and window functions provided by the FftSharp project, and it targets .NET Standard 2.0 so it can be used in .NET Framework and .NET Core projects.
Spectrogram can be installed with NuGet:
https://www.nuget.org/packages/Spectrogram
This code in Program.cs was used to create the above image:
(double[] audio, int sampleRate) = Read.MP3("cant-do-that.mp3");
var spec = new Spectrogram(
signal: audio,
sampleRate: sampleRate,
fftSize: 4096,
stepSize: 500,
freqMax: 2500,
);
spec.SaveJPG("output.jpg");If you're using Spectrogram in a graphical application you may find it helpful to retrieve the output as a Bitmap suitable for applying to a Picturebox or similar control:
Bitmap bmp = spec.GetBitmap();
pictureBox1.Image = bmp;After calculating the Spectrogram (the slow step) FFT magnitudes are stored in memory so you can rapidly recalculate pixel intensities based on new parameters:
spec.Recalculate(
multiplier: 2.8,
dB: true,
cmap: new Inferno()
);
spec.SaveJPG("output2.jpg");This example demonstrates how to convert a MP3 file to a spectrogram image. A sample MP3 audio file in the data folder contains the audio track from Ken Barker's excellent piano performance of George Frideric Handel's Suite No. 5 in E major for harpsichord (The Harmonious Blacksmith). This audio file is included with permission, and the original video can be viewed on YouTube.
If you listen to the audio track while closely inspecting the spectrogram you can identify individual piano notes and chords, and may be surprised by the interesting patterns that emerge around trills and glissandos.
// TODO: update this exampleThese examples demonstrate the identical spectrogram analyzed with a variety of different colormaps.
| Colormap Name | Color Curves | Example Spectrogram |
|---|---|---|
| Argo | ![]() |
![]() |
| Blues | ![]() |
![]() |
| Cividis | ![]() |
![]() |
| Grayscale | ![]() |
![]() |
| GrayscaleReversed | ![]() |
![]() |
| Greens | ![]() |
![]() |
| Inferno | ![]() |
![]() |
| Jet | ![]() |
![]() |
| Magma | ![]() |
![]() |
| Plasma | ![]() |
![]() |
| Viridis | ![]() |
![]() |























