def istft(stft_matrix, frame_length, step_length=None,
window='hann', padding=False):
"""
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`
frame_length: int
number of samples point for 1 frame
step_length: int
number of samples point for 1 step (when shifting the frames)
If unspecified, defaults `frame_length / 4`.
window : string, tuple, number, function, np.ndarray [shape=(n_fft,)]
- a window specification (string, tuple, or number);
see `scipy.signal.get_window`
- a window function, such as `scipy.signal.hanning`
- a user-specified window vector of length `n_fft`
padding: boolean
- If `True`, the signal `y` is padded so that frame
`D[:, t]` is centered at `y[t * step_length]`.
- If `False`, then `D[:, t]` begins at `y[t * step_length]`
Returns
-------
y : np.ndarray [shape=(n,), dtype=float32]
time domain signal reconstructed from `stft_matrix`
"""
# ====== check arguments ====== #
frame_length = int(frame_length)
if step_length is None:
step_length = frame_length // 4
else:
step_length = int(step_length)
nfft = 2 * (stft_matrix.shape[1] - 1)
# ====== use scipy here ====== #
try:
from scipy.signal.spectral import istft as _istft
except ImportError:
raise RuntimeError("`istft` requires scipy version >= 0.19")
return _istft(stft_matrix, fs=1.0, window=window,
nperseg=frame_length, noverlap=frame_length - step_length, nfft=nfft,
input_onesided=True, boundary=padding,
time_axis=0, freq_axis=-1)[-1]
评论列表
文章目录