PyTorch torch.hann_window Function
PyTorch torch Reference Manual
torch.hann_windowIt is a function in PyTorch used to generate a Hann window. The Hann window (also known as the Hanning window) is a cosine-squared window function, commonly used for signal windowing before Fourier transform to reduce spectral leakage.
Function Definition
torch.hann_window(window_length, periodic=True, dtype=None, layout=torch.strided, device=None, requires_grad=False)
Usage Example
Example
import torch
# Create a Hann window of length 512
window = torch.hann_window(512)
print("Hann window shape:", window.shape)
print("First 5 values of the window:", window[:5])
# Used for STFT
x = torch.randn(1, 16000)
stft_result = torch.stft(x.squeeze(), n_fft=512, hop_length=160, win_length=512, window=window)
print("STFT result shape:", stft_result.shape)
# Create a Hann window of length 512
window = torch.hann_window(512)
print("Hann window shape:", window.shape)
print("First 5 values of the window:", window[:5])
# Used for STFT
x = torch.randn(1, 16000)
stft_result = torch.stft(x.squeeze(), n_fft=512, hop_length=160, win_length=512, window=window)
print("STFT result shape:", stft_result.shape)
Other Extensions