librosa.griffinlim_cqt#

librosa.griffinlim_cqt(C, *, n_iter=32, sr=22050, hop_length=512, fmin=None, bins_per_octave=12, tuning=0.0, filter_scale=1, norm=1, sparsity=0.01, window='hann', scale=True, pad_mode='constant', res_type='soxr_hq', dtype=None, length=None, momentum=0.99, init='random', rng=None, random_state=<DEPRECATED parameter>)[source]#

Approximate constant-Q magnitude spectrogram inversion using the “fast” Griffin-Lim algorithm.

Given the magnitude of a constant-Q spectrogram (C), the algorithm randomly initializes phase estimates, and then alternates forward- and inverse-CQT operations. [1]

This implementation is based on the (fast) Griffin-Lim method for Short-time Fourier Transforms, [2] but adapted for use with constant-Q spectrograms.

Parameters:
Cnp.ndarray [shape=(…, n_bins, n_frames)]

The constant-Q magnitude spectrogram

n_iterint > 0

The number of iterations to run

srnumber > 0

Audio sampling rate

hop_lengthint > 0

The hop length of the CQT

fminnumber > 0

Minimum frequency for the CQT.

If not provided, it defaults to C1.

bins_per_octaveint > 0

Number of bins per octave

tuningfloat

Tuning deviation from A440, in fractions of a bin

filter_scalefloat > 0

Filter scale factor. Small values (<1) use shorter windows for improved time resolution.

norm{inf, -inf, 0, float > 0}

Type of norm to use for basis function normalization. See librosa.util.normalize.

sparsityfloat in [0, 1)

Sparsify the CQT basis by discarding up to sparsity fraction of the energy in each basis.

Set sparsity=0 to disable sparsification.

windowstr, tuple, or function

Window specification for the basis filters. See filters.get_window for details.

scalebool

If True, scale the CQT response by square-root the length of each channel’s filter. This is analogous to norm='ortho' in FFT.

If False, do not scale the CQT. This is analogous to norm=None in FFT.

pad_modestr

Padding mode for centered frame analysis.

See also: librosa.stft and numpy.pad.

res_typestr

The resampling mode for recursive downsampling.

See librosa.resample for a list of available options.

dtypenumeric type

Real numeric type for y. Default is inferred to match the precision of the input CQT.

lengthint > 0, optional

If provided, the output y is zero-padded or clipped to exactly length samples.

momentumfloat > 0

The momentum parameter for fast Griffin-Lim. Setting this to 0 recovers the original Griffin-Lim method. 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 C is a magnitude spectrogram with no initial phase estimates.

If None, then the phase is initialized from C. This is useful when an initial guess for phase can be provided, or when you want to resume Griffin-Lim from a previous output.

rngNone, int, sequence of int, np.random.Generator, or np.random.RandomState

Pseudorandom number generator state. When rng is None, a new numpy.random.Generator is created using entropy from the operating system. Types other than numpy.random.Generator are passed to numpy.random.default_rng to instantiate a Generator.

random_stateNone, int, np.random.RandomState, or np.random.Generator

Warning

This parameter is deprecated in 1.0.0 and will be removed in 1.2.0.

If int, random_state is the seed used by the random number generator for phase initialization.

If np.random.RandomState or np.random.Generator instance, the random number generator itself.

If None, defaults to the np.random.default_rng() object.

An exception is raised if both rng and random_state are provided.

Returns:
ynp.ndarray [shape=(…, n)]

time-domain signal reconstructed from C

Examples

A basic CQT inverse example

>>> y, sr = librosa.loadx('trumpet', sr=None)
>>> # Get the CQT magnitude, 7 octaves at 36 bins per octave
>>> C = np.abs(librosa.cqt(y=y, sr=sr, bins_per_octave=36, n_bins=7*36))
>>> # Invert using Griffin-Lim
>>> y_inv = librosa.griffinlim_cqt(C, sr=sr, bins_per_octave=36)
>>> # And invert without estimating phase
>>> y_icqt = librosa.icqt(C, sr=sr, bins_per_octave=36)

Wave-plot the results

>>> import matplotlib.pyplot as plt
>>> fig, ax = plt.subplots(nrows=3, sharex=True, sharey=True)
>>> librosa.display.waveshow(y, sr=sr, color='C0', ax=ax[0])
>>> ax[0].set(title='Original', xlabel=None)
>>> ax[0].label_outer()
>>> librosa.display.waveshow(y_inv, sr=sr, color='C1', ax=ax[1])
>>> ax[1].set(title='Griffin-Lim reconstruction', xlabel=None)
>>> ax[1].label_outer()
>>> librosa.display.waveshow(y_icqt, sr=sr, color='C2', ax=ax[2])
>>> ax[2].set(title='Magnitude-only icqt reconstruction')
../../_images/librosa-griffinlim_cqt-1.png