torch.ISTFTOptions
export interface ISTFTOptions {
hop_length?: number;
win_length?: number;
window?: Tensor;
center?: boolean;
normalized?: boolean;
onesided?: boolean;
length?: number;
return_complex?: boolean;
}hop_length(number)optionalwin_length(number)optionalwindow(Tensor)optionalcenter(boolean)optionalnormalized(boolean)optionalonesided(boolean)optionallength(number)optionalreturn_complex(boolean)optional
Computes the inverse Short-Time Fourier Transform (ISTFT).
Reconstructs a signal from its STFT representation using overlap-add synthesis.
Inverse of stft(). Combines windowed IFFT frames using overlap-add to recover
the original (or modified) time-domain signal. Essential for:
- Signal reconstruction: Converting spectrograms back to audio
- Spectral editing: Modifying frequency content and reconstructing
- Noise reduction: Processing STFT, zeroing noise, reconstructing clean signal
- Audio effects: Pitch shifting, time stretching, vocoding
- Audio enhancement: Denoising, declipping, speech enhancement
- Vocoders: Phase vocoder for high-quality time stretching
- Restoration: Recovering signals from spectral representations
Perfect reconstruction:
istft(stft(x)) ≈ x(with matching parameters and appropriate window)- Works when window satisfies Constant Overlap-Add (COLA) property
- Use same parameters (hop_length, win_length, window) as original stft
Overlap-add mechanism:
- Each frame's IFFT output is windowed and placed at its time position
- Overlapping regions from adjacent frames are summed (overlap-add)
- Window must be normalized (e.g., Hann window with 75% overlap)
Examples
// Perfect reconstruction: istft(stft(x)) == x
const original = torch.randn(8000);
const stft_result = torch.stft(original, 512, { hop_length: 128 });
const reconstructed = torch.istft(stft_result, 512, { hop_length: 128 });
// reconstructed closely matches original// Spectral noise reduction: suppress frequencies below threshold
const noisy_audio = torch.randn(16000);
const stft = torch.stft(noisy_audio, 512, {
hop_length: 128,
window: torch.hann_window(512)
});
const magnitude = torch.abs(stft);
// Threshold: keep only strong frequencies
const threshold = magnitude.quantile(0.8); // 80th percentile
const mask = magnitude.gt(threshold);
const real_masked = stft.select(-1, 0).mul(mask);
const imag_masked = stft.select(-1, 1).mul(mask);
const masked_stft = torch.stack([real_masked, imag_masked], -1);
const denoised = torch.istft(masked_stft, 512, {
hop_length: 128,
window: torch.hann_window(512)
});
// Denoised signal with weak frequencies suppressed// Spectral editing: boost specific frequencies
const signal = torch.randn(4096);
const stft = torch.stft(signal, 256, { hop_length: 64 });
// Extract magnitude and phase
const mag = torch.abs(stft);
const phase = torch.atan2(stft.select(-1, 1), stft.select(-1, 0));
// Boost 100 Hz component
mag.select(0, 100).mul_(2); // 2x amplification at 100 Hz
// Reconstruct with modified magnitude
const real = mag.mul(phase.cos());
const imag = mag.mul(phase.sin());
const modified_stft = torch.stack([real, imag], -1);
const enhanced = torch.istft(modified_stft, 256, { hop_length: 64 });// Batch signal reconstruction
const stft_batch = torch.randn(8, 257, 100, 2); // 8 STFTs
const signals = torch.istft(stft_batch, 512, { hop_length: 128 }); // [8, 12800]// Phase vocoder-like time stretching (simple approximation)
const signal = torch.randn(16000);
const stft = torch.stft(signal, 512, {
hop_length: 128,
window: torch.hann_window(512)
});
// Time-stretch by 1.5x: repeat frames (simple, not perfect)
const time_axis = 1; // Frequency is axis 0, time is axis 1
const stretched_stft = stft.repeat(1, 2, 1); // Double time frames (rough approximation)
const stretched = torch.istft(stretched_stft, 512, {
hop_length: 128,
window: torch.hann_window(512),
length: Math.floor(16000 * 1.5)
});
// Simple time stretching (real vocoders use phase coherence)// Spectrogram inversion: reproduce original from magnitude
const signal = torch.randn(8000);
const magnitude = torch.abs(torch.stft(signal, 256));
// Create phase (random for comparison)
const phase = torch.rand_like(magnitude).mul(2 * Math.PI);
const real = magnitude.mul(phase.cos());
const imag = magnitude.mul(phase.sin());
const stft = torch.stack([real, imag], -1);
const reconstruction = torch.istft(stft, 256, { hop_length: 64 });