Spaces:
Build error
Build error
Commit
·
7c8fb33
1
Parent(s):
af68200
Upload STFT.py
Browse files
STFT.py
ADDED
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Taken from ESPNet
|
3 |
+
"""
|
4 |
+
|
5 |
+
import torch
|
6 |
+
from torch.functional import stft as torch_stft
|
7 |
+
from torch_complex.tensor import ComplexTensor
|
8 |
+
|
9 |
+
from Utility.utils import make_pad_mask
|
10 |
+
|
11 |
+
|
12 |
+
class STFT(torch.nn.Module):
|
13 |
+
|
14 |
+
def __init__(self, n_fft=512, win_length=None, hop_length=128, window="hann", center=True, normalized=False,
|
15 |
+
onesided=True):
|
16 |
+
super().__init__()
|
17 |
+
self.n_fft = n_fft
|
18 |
+
if win_length is None:
|
19 |
+
self.win_length = n_fft
|
20 |
+
else:
|
21 |
+
self.win_length = win_length
|
22 |
+
self.hop_length = hop_length
|
23 |
+
self.center = center
|
24 |
+
self.normalized = normalized
|
25 |
+
self.onesided = onesided
|
26 |
+
self.window = window
|
27 |
+
|
28 |
+
def extra_repr(self):
|
29 |
+
return (f"n_fft={self.n_fft}, "
|
30 |
+
f"win_length={self.win_length}, "
|
31 |
+
f"hop_length={self.hop_length}, "
|
32 |
+
f"center={self.center}, "
|
33 |
+
f"normalized={self.normalized}, "
|
34 |
+
f"onesided={self.onesided}")
|
35 |
+
|
36 |
+
def forward(self, input_wave, ilens=None):
|
37 |
+
"""
|
38 |
+
STFT forward function.
|
39 |
+
Args:
|
40 |
+
input_wave: (Batch, Nsamples) or (Batch, Nsample, Channels)
|
41 |
+
ilens: (Batch)
|
42 |
+
Returns:
|
43 |
+
output: (Batch, Frames, Freq, 2) or (Batch, Frames, Channels, Freq, 2)
|
44 |
+
"""
|
45 |
+
bs = input_wave.size(0)
|
46 |
+
|
47 |
+
if input_wave.dim() == 3:
|
48 |
+
multi_channel = True
|
49 |
+
# input: (Batch, Nsample, Channels) -> (Batch * Channels, Nsample)
|
50 |
+
input_wave = input_wave.transpose(1, 2).reshape(-1, input_wave.size(1))
|
51 |
+
else:
|
52 |
+
multi_channel = False
|
53 |
+
|
54 |
+
# output: (Batch, Freq, Frames, 2=real_imag)
|
55 |
+
# or (Batch, Channel, Freq, Frames, 2=real_imag)
|
56 |
+
if self.window is not None:
|
57 |
+
window_func = getattr(torch, f"{self.window}_window")
|
58 |
+
window = window_func(self.win_length, dtype=input_wave.dtype, device=input_wave.device)
|
59 |
+
else:
|
60 |
+
window = None
|
61 |
+
|
62 |
+
complex_output = torch_stft(input=input_wave,
|
63 |
+
n_fft=self.n_fft,
|
64 |
+
win_length=self.win_length,
|
65 |
+
hop_length=self.hop_length,
|
66 |
+
center=self.center,
|
67 |
+
window=window,
|
68 |
+
normalized=self.normalized,
|
69 |
+
onesided=self.onesided,
|
70 |
+
return_complex=True)
|
71 |
+
output = torch.view_as_real(complex_output)
|
72 |
+
# output: (Batch, Freq, Frames, 2=real_imag)
|
73 |
+
# -> (Batch, Frames, Freq, 2=real_imag)
|
74 |
+
output = output.transpose(1, 2)
|
75 |
+
if multi_channel:
|
76 |
+
# output: (Batch * Channel, Frames, Freq, 2=real_imag)
|
77 |
+
# -> (Batch, Frame, Channel, Freq, 2=real_imag)
|
78 |
+
output = output.view(bs, -1, output.size(1), output.size(2), 2).transpose(1, 2)
|
79 |
+
|
80 |
+
if ilens is not None:
|
81 |
+
if self.center:
|
82 |
+
pad = self.win_length // 2
|
83 |
+
ilens = ilens + 2 * pad
|
84 |
+
|
85 |
+
olens = (ilens - self.win_length) // self.hop_length + 1
|
86 |
+
output.masked_fill_(make_pad_mask(olens, output, 1), 0.0)
|
87 |
+
else:
|
88 |
+
olens = None
|
89 |
+
|
90 |
+
return output, olens
|
91 |
+
|
92 |
+
def inverse(self, input, ilens=None):
|
93 |
+
"""
|
94 |
+
Inverse STFT.
|
95 |
+
Args:
|
96 |
+
input: Tensor(batch, T, F, 2) or ComplexTensor(batch, T, F)
|
97 |
+
ilens: (batch,)
|
98 |
+
Returns:
|
99 |
+
wavs: (batch, samples)
|
100 |
+
ilens: (batch,)
|
101 |
+
"""
|
102 |
+
istft = torch.functional.istft
|
103 |
+
|
104 |
+
if self.window is not None:
|
105 |
+
window_func = getattr(torch, f"{self.window}_window")
|
106 |
+
window = window_func(self.win_length, dtype=input.dtype, device=input.device)
|
107 |
+
else:
|
108 |
+
window = None
|
109 |
+
|
110 |
+
if isinstance(input, ComplexTensor):
|
111 |
+
input = torch.stack([input.real, input.imag], dim=-1)
|
112 |
+
assert input.shape[-1] == 2
|
113 |
+
input = input.transpose(1, 2)
|
114 |
+
|
115 |
+
wavs = istft(input, n_fft=self.n_fft, hop_length=self.hop_length, win_length=self.win_length, window=window, center=self.center,
|
116 |
+
normalized=self.normalized, onesided=self.onesided, length=ilens.max() if ilens is not None else ilens)
|
117 |
+
|
118 |
+
return wavs, ilens
|