bundles / scipy 1.17.1 / scipy / signal / _spectral_py / istft
function
scipy.signal._spectral_py:istft
Signature
def istft ( Zxx , fs = 1.0 , window = hann_periodic , nperseg = None , noverlap = None , nfft = None , input_onesided = True , boundary = True , time_axis = -1 , freq_axis = -2 , scaling = spectrum ) Summary
Perform the inverse Short Time Fourier transform (legacy function).
Parameters
Zxx: array_likeSTFT of the signal to be reconstructed. If a purely real array is passed, it will be cast to a complex data type.
fs: float, optionalSampling frequency of the time series. Defaults to 1.0.
window: str or tuple or array_like, optionalDesired window to use. If
windowis a string or tuple, it is passed to get_window to generate the window values, which are DFT-even by default. See get_window for a list of windows and required parameters. Ifwindowis array_like it will be used directly as the window and its length must be nperseg. Defaults to a periodic Hann window. Must match the window used to generate the STFT for faithful inversion.nperseg: int, optionalNumber of data points corresponding to each STFT segment. This parameter must be specified if the number of data points per segment is odd, or if the STFT was padded via
nfft > nperseg. IfNone, the value depends on the shape ofZxxandinput_onesided. Ifinput_onesidedisTrue,nperseg=2*(Zxx.shape[freq_axis] - 1). Otherwise,nperseg=Zxx.shape[freq_axis]. Defaults toNone.noverlap: int, optionalNumber of points to overlap between segments. If
None, half of the segment length. Defaults toNone. When specified, the COLA constraint must be met (see Notes below), and should match the parameter used to generate the STFT. Defaults toNone.nfft: int, optionalNumber of FFT points corresponding to each STFT segment. This parameter must be specified if the STFT was padded via
nfft > nperseg. IfNone, the default values are the same as fornperseg, detailed above, with one exception: ifinput_onesidedis True andnperseg==2*Zxx.shape[freq_axis] - 1,nfftalso takes on that value. This case allows the proper inversion of an odd-length unpadded STFT usingnfft=None. Defaults toNone.input_onesided: bool, optionalIf
True, interpret the input array as one-sided FFTs, such as is returned bystftwithreturn_onesided=Trueand numpy.fft.rfft. IfFalse, interpret the input as a a two-sided FFT. Defaults toTrue.boundary: bool, optionalSpecifies whether the input signal was extended at its boundaries by supplying a non-
Noneboundaryargument tostft. Defaults toTrue.time_axis: int, optionalWhere the time segments of the STFT is located; the default is the last axis (i.e.
axis=-1).freq_axis: int, optionalWhere the frequency axis of the STFT is located; the default is the penultimate axis (i.e.
axis=-2).scaling: {'spectrum', 'psd'}The default 'spectrum' scaling allows each frequency line of
Zxxto be interpreted as a magnitude spectrum. The 'psd' option scales each line to a power spectral density - it allows to calculate the signal's energy by numerically integrating overabs(Zxx)**2.
Returns
t: ndarrayArray of output data times.
x: ndarrayiSTFT of
Zxx.
Notes
In order to enable inversion of an STFT via the inverse STFT with istft, the signal windowing must obey the constraint of "nonzero overlap add" (NOLA):
This ensures that the normalization factors that appear in the denominator of the overlap-add reconstruction equation
are not zero. The NOLA constraint can be checked with the check_NOLA function.
An STFT which has been modified (via masking or otherwise) is not guaranteed to correspond to an exactly realizible signal. This function implements the iSTFT via the least-squares estimation algorithm detailed in [2], which produces a signal that minimizes the mean squared error between the STFT of the returned signal and the modified STFT.
Array API Standard Support
istft is not in-scope for support of Python Array API Standard compatible backends other than NumPy.
See dev-arrayapi for more information.
Examples
import numpy as np from scipy import signal import matplotlib.pyplot as plt rng = np.random.default_rng()✓
fs = 1024 N = 10*fs nperseg = 512 amp = 2 * np.sqrt(2) noise_power = 0.001 * fs / 2 time = np.arange(N) / float(fs) carrier = amp * np.sin(2*np.pi*50*time) noise = rng.normal(scale=np.sqrt(noise_power), size=time.shape) x = carrier + noise✓
f, t, Zxx = signal.stft(x, fs=fs, nperseg=nperseg)
✓plt.figure() plt.pcolormesh(t, f, np.abs(Zxx), vmin=0, vmax=amp, shading='gouraud') plt.ylim([f[1], f[-1]]) plt.title('STFT Magnitude') plt.ylabel('Frequency [Hz]') plt.xlabel('Time [sec]')✗
plt.yscale('log') plt.show()✓

Zxx = np.where(np.abs(Zxx) >= amp/10, Zxx, 0) _, xrec = signal.istft(Zxx, fs)✓
plt.figure() plt.plot(time, x, time, xrec, time, carrier) plt.xlim([2, 2.1]) plt.xlabel('Time [sec]') plt.ylabel('Signal') plt.legend(['Carrier + Noise', 'Filtered via STFT', 'True Carrier'])✗
plt.show()
✓
plt.figure() plt.plot(time, x, time, xrec, time, carrier) plt.xlim([0, 0.1]) plt.xlabel('Time [sec]') plt.ylabel('Signal') plt.legend(['Carrier + Noise', 'Filtered via STFT', 'True Carrier'])✗
plt.show()
✓
See also
- ShortTimeFFT
Newer STFT/ISTFT implementation providing more features.
- check_COLA
Check whether the Constant OverLap Add (COLA) constraint is met
- check_NOLA
Check whether the Nonzero Overlap Add (NOLA) constraint is met
- stft
Short Time Fourier Transform
Aliases
-
scipy.signal.istft