AlexK-PL commited on
Commit
94dd089
·
1 Parent(s): ba7bb68

Delete stft.py

Browse files
Files changed (1) hide show
  1. stft.py +0 -140
stft.py DELETED
@@ -1,140 +0,0 @@
1
- """
2
- BSD 3-Clause License
3
-
4
- Copyright (c) 2017, Prem Seetharaman
5
- All rights reserved.
6
-
7
- * Redistribution and use in source and binary forms, with or without
8
- modification, are permitted provided that the following conditions are met:
9
-
10
- * Redistributions of source code must retain the above copyright notice,
11
- this list of conditions and the following disclaimer.
12
-
13
- * Redistributions in binary form must reproduce the above copyright notice, this
14
- list of conditions and the following disclaimer in the
15
- documentation and/or other materials provided with the distribution.
16
-
17
- * Neither the name of the copyright holder nor the names of its
18
- contributors may be used to endorse or promote products derived from this
19
- software without specific prior written permission.
20
-
21
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22
- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23
- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24
- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
25
- ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26
- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27
- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
28
- ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30
- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
- """
32
-
33
- import torch
34
- import numpy as np
35
- import torch.nn.functional as F
36
- from torch.autograd import Variable
37
- from scipy.signal import get_window
38
- from librosa.util import pad_center, tiny
39
- from audio_processing import window_sumsquare
40
-
41
-
42
- class STFT(torch.nn.Module):
43
- """adapted from Prem Seetharaman's https://github.com/pseeth/pytorch-stft"""
44
- def __init__(self, filter_length=800, hop_length=200, win_length=800,
45
- window='hann'):
46
- super(STFT, self).__init__()
47
- self.filter_length = filter_length
48
- self.hop_length = hop_length
49
- self.win_length = win_length
50
- self.window = window
51
- self.forward_transform = None
52
- scale = self.filter_length / self.hop_length
53
- fourier_basis = np.fft.fft(np.eye(self.filter_length))
54
-
55
- cutoff = int((self.filter_length / 2 + 1))
56
- fourier_basis = np.vstack([np.real(fourier_basis[:cutoff, :]),
57
- np.imag(fourier_basis[:cutoff, :])])
58
-
59
- forward_basis = torch.FloatTensor(fourier_basis[:, None, :])
60
- inverse_basis = torch.FloatTensor(
61
- np.linalg.pinv(scale * fourier_basis).T[:, None, :])
62
-
63
- if window is not None:
64
- assert(filter_length >= win_length)
65
- # get window and zero center pad it to filter_length
66
- fft_window = get_window(window, win_length, fftbins=True)
67
- fft_window = pad_center(fft_window, filter_length)
68
- fft_window = torch.from_numpy(fft_window).float()
69
-
70
- # window the bases
71
- forward_basis *= fft_window
72
- inverse_basis *= fft_window
73
-
74
- self.register_buffer('forward_basis', forward_basis.float())
75
- self.register_buffer('inverse_basis', inverse_basis.float())
76
-
77
- def transform(self, input_data):
78
- num_batches = input_data.size(0)
79
- num_samples = input_data.size(1)
80
-
81
- self.num_samples = num_samples
82
-
83
- # similar to librosa, reflect-pad the input
84
- input_data = input_data.view(num_batches, 1, num_samples)
85
- input_data = F.pad(
86
- input_data.unsqueeze(1),
87
- (int(self.filter_length / 2), int(self.filter_length / 2), 0, 0),
88
- mode='reflect')
89
- input_data = input_data.squeeze(1)
90
-
91
- forward_transform = F.conv1d(
92
- input_data,
93
- Variable(self.forward_basis, requires_grad=False),
94
- stride=self.hop_length,
95
- padding=0)
96
-
97
- cutoff = int((self.filter_length / 2) + 1)
98
- real_part = forward_transform[:, :cutoff, :]
99
- imag_part = forward_transform[:, cutoff:, :]
100
-
101
- magnitude = torch.sqrt(real_part**2 + imag_part**2)
102
- phase = torch.autograd.Variable(
103
- torch.atan2(imag_part.data, real_part.data))
104
-
105
- return magnitude, phase
106
-
107
- def inverse(self, magnitude, phase):
108
- recombine_magnitude_phase = torch.cat(
109
- [magnitude*torch.cos(phase), magnitude*torch.sin(phase)], dim=1)
110
-
111
- inverse_transform = F.conv_transpose1d(
112
- recombine_magnitude_phase,
113
- Variable(self.inverse_basis, requires_grad=False),
114
- stride=self.hop_length,
115
- padding=0)
116
-
117
- if self.window is not None:
118
- window_sum = window_sumsquare(
119
- self.window, magnitude.size(-1), hop_length=self.hop_length,
120
- win_length=self.win_length, n_fft=self.filter_length,
121
- dtype=np.float32)
122
- # remove modulation effects
123
- approx_nonzero_indices = torch.from_numpy(
124
- np.where(window_sum > tiny(window_sum))[0])
125
- window_sum = torch.autograd.Variable(
126
- torch.from_numpy(window_sum), requires_grad=False)
127
- inverse_transform[:, :, approx_nonzero_indices] /= window_sum[approx_nonzero_indices]
128
-
129
- # scale by hop ratio
130
- inverse_transform *= float(self.filter_length) / self.hop_length
131
-
132
- inverse_transform = inverse_transform[:, :, int(self.filter_length/2):]
133
- inverse_transform = inverse_transform[:, :, :-int(self.filter_length/2):]
134
-
135
- return inverse_transform
136
-
137
- def forward(self, input_data):
138
- self.magnitude, self.phase = self.transform(input_data)
139
- reconstruction = self.inverse(self.magnitude, self.phase)
140
- return reconstruction