Perform STFT and convert to magnitude spectrogram. Args: x (Tensor): Input signal tensor (B, T). fft_size (int): FFT size. hop_size (int): Hop size. win_length (int): Window length. window (str): Window function type. Returns: Tensor: Magnitu
(x, fft_size, hop_size, win_length, window)
| 10 | |
| 11 | |
| 12 | def stft(x, fft_size, hop_size, win_length, window): |
| 13 | """Perform STFT and convert to magnitude spectrogram. |
| 14 | |
| 15 | Args: |
| 16 | x (Tensor): Input signal tensor (B, T). |
| 17 | fft_size (int): FFT size. |
| 18 | hop_size (int): Hop size. |
| 19 | win_length (int): Window length. |
| 20 | window (str): Window function type. |
| 21 | |
| 22 | Returns: |
| 23 | Tensor: Magnitude spectrogram (B, #frames, fft_size // 2 + 1). |
| 24 | |
| 25 | """ |
| 26 | x_stft = torch.stft(x, fft_size, hop_size, win_length, window) |
| 27 | real = x_stft[..., 0] |
| 28 | imag = x_stft[..., 1] |
| 29 | |
| 30 | # NOTE(kan-bayashi): clamp is needed to avoid nan or inf |
| 31 | return torch.sqrt(torch.clamp(real ** 2 + imag ** 2, min=1e-7)).transpose(2, 1) |
| 32 | |
| 33 | |
| 34 | class SpectralConvergengeLoss(torch.nn.Module): |