librosa.core.istft

librosa.core.istft(stft_matrix, hop_length=None, win_length=None, window=’hann’, center=True, dtype=<class ‘numpy.float32’>, length=None)[source]

Inverse short-time Fourier transform (ISTFT).

Converts a complex-valued spectrogram stft_matrix to time-series y by minimizing the mean squared error between stft_matrix and STFT of y as described in [1].

In general, window function, hop length and other parameters should be same as in stft, which mostly leads to perfect reconstruction of a signal from unmodified stft_matrix.

[1]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:
stft_matrix : np.ndarray [shape=(1 + n_fft/2, t)]

STFT matrix from stft

hop_length : int > 0 [scalar]

Number of frames between STFT columns. If unspecified, defaults to win_length / 4.

win_length : int <= n_fft = 2 * (stft_matrix.shape[0] - 1)

When reconstructing the time series, each frame is windowed and each sample is normalized by the sum of squared window according to the window function (see below).

If unspecified, defaults to n_fft.

window : string, tuple, number, function, np.ndarray [shape=(n_fft,)]
center : boolean
  • If True, D is assumed to have centered frames.
  • If False, D is assumed to have left-aligned frames.
dtype : numeric type

Real numeric type for y. Default is 32-bit float.

length : int > 0, optional

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

Returns:
y : np.ndarray [shape=(n,)]

time domain signal reconstructed from stft_matrix

See also

stft
Short-time Fourier Transform

Notes

This function caches at level 30.

Examples

>>> y, sr = librosa.load(librosa.util.example_audio_file())
>>> D = librosa.stft(y)
>>> y_hat = librosa.istft(D)
>>> y_hat
array([ -4.812e-06,  -4.267e-06, ...,   6.271e-06,   2.827e-07], dtype=float32)

Exactly preserving length of the input signal requires explicit padding. Otherwise, a partial frame at the end of y will not be represented.

>>> n = len(y)
>>> n_fft = 2048
>>> y_pad = librosa.util.fix_length(y, n + n_fft // 2)
>>> D = librosa.stft(y_pad, n_fft=n_fft)
>>> y_out = librosa.istft(D, length=n)
>>> np.max(np.abs(y - y_out))
1.4901161e-07