GriffinLim<T>

Models & Types · AiDotNet.Diffusion.Audio

Griffin-Lim algorithm for audio reconstruction from magnitude spectrograms.

For Beginners

When we compute a spectrogram, we get both magnitude (how loud each frequency is) and phase (where in its cycle each frequency is). For visualization and some ML tasks, we often discard phase and keep only magnitude.

But to play back audio, we need phase information! Griffin-Lim solves this by:

  1. Starting with random phase
  2. Converting to audio (ISTFT)
  3. Converting back to spectrogram (STFT)
  4. Keeping the new phase but forcing the original magnitude
  5. Repeating until convergence

With momentum acceleration, convergence is typically achieved in 30-60 iterations.

Usage:

var griffinLim = new GriffinLim<float>(
nFft: 2048,
hopLength: 512,
iterations: 60
);

// From a magnitude spectrogram (e.g., generated by AI)
var magnitudeSpectrogram = Tensor<float>.CreateRandom(86, 1025); // [frames, nFft/2 + 1]
var audio = griffinLim.Reconstruct(magnitudeSpectrogram);

How It Works

The Griffin-Lim algorithm iteratively estimates the phase of a signal given only its magnitude spectrogram. This is useful when you have a spectrogram (e.g., from a generative model like Riffusion) but need to reconstruct audio.

Constructors

Constructor Summary
GriffinLim(ShortTimeFourierTransform<T> stft, int iterations, double momentum, int? seed) Initializes Griffin-Lim with an existing STFT processor.
GriffinLim(int nFft, int? hopLength, IWindowFunction<T> windowFunction, int iterations, double momentum, int? seed) Initializes a new Griffin-Lim processor.

Properties

Property Summary
ShortTimeFourierTransform<T> STFT { get; } Gets the STFT processor.
double Momentum { get; } Gets the momentum factor.
int Iterations { get; } Gets the number of iterations.

Methods

Method Summary
T ComputeSpectralConvergence(Tensor<T> targetMagnitude, Tensor<T> signal) Estimates the spectral convergence error.
Tensor<T> Reconstruct(Tensor<T> magnitude, int? length) Reconstructs audio from a magnitude spectrogram using Griffin-Lim.
Tensor<T> ReconstructFromMel(Tensor<T> melSpec, MelSpectrogram<T> melProcessor, int? length) Reconstructs audio from a Mel spectrogram.
Tensor<T> ReconstructWithProgress(Tensor<T> magnitude, Action<int, double> progressCallback, int? length) Reconstructs audio with progress callback.