现在位置: 首页 > PyTorch 教程 > 正文

PyTorch torch.hann_window 函数


Pytorch torch 参考手册 Pytorch torch 参考手册

torch.hann_window 是 PyTorch 中用于生成 Hann 窗口的函数。Hann 窗口(也称为 Hanning 窗口)是一种余弦平方窗函数,常用于傅里叶变换前的信号窗处理,以减少频谱泄漏。

函数定义

torch.hann_window(window_length, periodic=True, dtype=None, layout=torch.strided, device=None, requires_grad=False)

使用示例

实例

import torch

# 创建长度为 512 的 Hann 窗口
window = torch.hann_window(512)
print("Hann 窗口形状:", window.shape)
print("窗口前5个值:", window[:5])

# 用于 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结果形状:", stft_result.shape)

Pytorch torch 参考手册 Pytorch torch 参考手册