Caution
You're reading an old version of this documentation. If you want up-to-date information, please have a look at 0.9.1.
librosa.core.griffinlim¶
- librosa.core.griffinlim(S, n_iter=32, hop_length=None, win_length=None, window='hann', center=True, dtype=<class 'numpy.float32'>, length=None, pad_mode='reflect', momentum=0.99, init='random', random_state=None)[source]¶
Approximate magnitude spectrogram inversion using the “fast” Griffin-Lim algorithm [1] [2].
Given a short-time Fourier transform magnitude matrix (S), the algorithm randomly initializes phase estimates, and then alternates forward- and inverse-STFT operations. Note that this assumes reconstruction of a real-valued time-domain signal, and that S contains only the non-negative frequencies (as computed by core.stft).
- 1(1,2)
Perraudin, N., Balazs, P., & Søndergaard, P. L. “A fast Griffin-Lim algorithm,” IEEE Workshop on Applications of Signal Processing to Audio and Acoustics (pp. 1-4), Oct. 2013.
- 2
D. W. Griffin and J. S. Lim, “Signal estimation from modified short-time Fourier transform,” IEEE Trans. ASSP, vol.32, no.2, pp.236–243, Apr. 1984.
- Parameters
- Snp.ndarray [shape=(n_fft / 2 + 1, t), non-negative]
An array of short-time Fourier transform magnitudes as produced by core.stft.
- n_iterint > 0
The number of iterations to run
- hop_lengthNone or int > 0
The hop length of the STFT. If not provided, it will default to n_fft // 4
- win_lengthNone or int > 0
The window length of the STFT. By default, it will equal n_fft
- windowstring, tuple, number, function, or np.ndarray [shape=(n_fft,)]
- centerboolean
If True, the STFT is assumed to use centered frames. If False, the STFT is assumed to use left-aligned frames.
- dtypenp.dtype
Real numeric type for the time-domain signal. Default is 32-bit float.
- lengthNone or int > 0
If provided, the output y is zero-padded or clipped to exactly length samples.
- pad_modestring
If center=True, the padding mode to use at the edges of the signal. By default, STFT uses reflection padding.
- momentumnumber >= 0
The momentum parameter for fast Griffin-Lim. Setting this to 0 recovers the original Griffin-Lim method [1]. Values near 1 can lead to faster convergence, but above 1 may not converge.
- initNone or ‘random’ [default]
If ‘random’ (the default), then phase values are initialized randomly according to random_state. This is recommended when the input S is a magnitude spectrogram with no initial phase estimates.
If None, then the phase is initialized from S. This is useful when an initial guess for phase can be provided, or when you want to resume Griffin-Lim from a previous output.
- random_stateNone, int, or np.random.RandomState
If int, random_state is the seed used by the random number generator for phase initialization.
If np.random.RandomState instance, the random number generator itself.
If None, defaults to the current np.random object.
- Returns
- ynp.ndarray [shape=(n,)]
time-domain signal reconstructed from S
Examples
A basic STFT inverse example
>>> y, sr = librosa.load(librosa.util.example_audio_file(), duration=5, offset=30) >>> # Get the magnitude spectrogram >>> S = np.abs(librosa.stft(y)) >>> # Invert using Griffin-Lim >>> y_inv = librosa.griffinlim(S) >>> # Invert without estimating phase >>> y_istft = librosa.istft(S)
Wave-plot the results
>>> import matplotlib.pyplot as plt >>> plt.figure() >>> ax = plt.subplot(3,1,1) >>> librosa.display.waveplot(y, sr=sr, color='b') >>> plt.title('Original') >>> plt.xlabel('') >>> plt.subplot(3,1,2, sharex=ax, sharey=ax) >>> librosa.display.waveplot(y_inv, sr=sr, color='g') >>> plt.title('Griffin-Lim reconstruction') >>> plt.xlabel('') >>> plt.subplot(3,1,3, sharex=ax, sharey=ax) >>> librosa.display.waveplot(y_istft, sr=sr, color='r') >>> plt.title('Magnitude-only istft reconstruction') >>> plt.tight_layout() >>> plt.show()