#!/usr/bin/python3 # -*- coding: utf-8 -*- import torch import torch.nn as nn class LearnableSigmoid1d(nn.Module): def __init__(self, in_features, beta=1): super().__init__() self.beta = beta self.slope = nn.Parameter(torch.ones(in_features)) self.slope.requiresGrad = True def forward(self, x): # x shape: [batch_size, time_steps, spec_bins] return self.beta * torch.sigmoid(self.slope * x) def mag_pha_stft(y, n_fft, hop_size, win_size, compress_factor=1.0, center=True): hann_window = torch.hann_window(win_size).to(y.device) stft_spec = torch.stft(y, n_fft, hop_length=hop_size, win_length=win_size, window=hann_window, center=center, pad_mode='reflect', normalized=False, return_complex=True) stft_spec = torch.view_as_real(stft_spec) mag = torch.sqrt(stft_spec.pow(2).sum(-1) + 1e-9) pha = torch.atan2(stft_spec[:, :, :, 1] + 1e-10, stft_spec[:, :, :, 0] + 1e-5) # Magnitude Compression mag = torch.pow(mag, compress_factor) com = torch.stack((mag*torch.cos(pha), mag*torch.sin(pha)), dim=-1) return mag, pha, com def mag_pha_istft(mag, pha, n_fft, hop_size, win_size, compress_factor=1.0, center=True): # Magnitude Decompression mag = torch.pow(mag, (1.0/compress_factor)) com = torch.complex(mag*torch.cos(pha), mag*torch.sin(pha)) hann_window = torch.hann_window(win_size).to(com.device) wav = torch.istft(com, n_fft, hop_length=hop_size, win_length=win_size, window=hann_window, center=center) return wav if __name__ == '__main__': pass