-
Notifications
You must be signed in to change notification settings - Fork 7
/
time_frequence.py
190 lines (136 loc) · 5.8 KB
/
time_frequence.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import numpy as np
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.nn.functional as F
import scipy.signal
class ifft(nn.Module):
def __init__(self, nfft=1024):
super(ifft, self).__init__()
assert nfft % 2 == 0
self.nfft = int(nfft)
self.n_freq = n_freq = int(nfft / 2)
real_kernels, imag_kernels, self.ac_cof = _get_ifft_kernels(nfft)
self.real_conv = nn.Conv2d(1, nfft, (n_freq, 1), stride=1, padding=0, bias=False)
self.imag_conv = nn.Conv2d(1, nfft, (n_freq, 1), stride=1, padding=0, bias=False)
self.real_conv.weight.data.copy_(real_kernels)
self.imag_conv.weight.data.copy_(imag_kernels)
self.real_model = nn.Sequential(self.real_conv)
self.imag_model = nn.Sequential(self.imag_conv)
def forward(self, magn, phase, ac=None):
assert magn.size()[2] == phase.size()[2] == self.n_freq
output = self.real_model(magn) - self.imag_model(phase)
if ac is not None:
output = output + ac * self.ac_cof
return output / self.nfft
def _get_ifft_kernels(nfft):
nfft = int(nfft)
assert nfft % 2 == 0
def kernel_fn(time, freq):
return np.exp(1j * (2 * np.pi * time * freq) / 1024.)
kernels = np.fromfunction(kernel_fn, (int(nfft), int(nfft/2+1)), dtype=np.float64)
kernels = np.zeros((1024, 513)) * 1j
'''
for i in range(1024):
for j in range(513):
kernels[i, j] = kernel_fn(i, j)
'''
ac_cof = float(np.real(kernels[0, 0]))
kernels = 2 * kernels[:, 1:]
kernels[:, -1] = kernels[:, -1] / 2.0
real_kernels = np.real(kernels)
imag_kernels = np.imag(kernels)
real_kernels = torch.from_numpy(real_kernels[:, np.newaxis, :, np.newaxis])
imag_kernels = torch.from_numpy(imag_kernels[:, np.newaxis, :, np.newaxis])
return real_kernels, imag_kernels, ac_cof
class istft(nn.Module):
def __init__(self, nfft=1024, hop_length=512):
super(istft, self).__init__()
assert nfft % 2 == 0
assert hop_length <= nfft
self.hop_length = hop_length
self.nfft = int(nfft)
self.n_freq = n_freq = int(nfft / 2)
self.real_kernels, self.imag_kernels, self.ac_cof = _get_istft_kernels(nfft)
trans_kernels = np.zeros((nfft, nfft), np.float64)
np.fill_diagonal(trans_kernels, np.ones((nfft, ), dtype=np.float64))
self.trans_kernels = nn.Parameter(torch.from_numpy(trans_kernels[:, np.newaxis, np.newaxis, :]).float())
def forward(self, magn, phase, ac):
'''
batch None frequency frame
'''
assert magn.size()[2] == phase.size()[2] == self.n_freq
nfft = self.nfft
hop = self.hop_length
# complex conjugate
phase = -1. * phase
real_part = F.conv2d(magn, self.real_kernels)
imag_part = F.conv2d(phase, self.imag_kernels)
output = real_part - imag_part
ac = ac.unsqueeze(1)
ac = float(self.ac_cof) * ac.expand_as(output)
output = output + ac
output = output / float(self.nfft)
output = F.conv_transpose2d(output, self.trans_kernels, stride=self.hop_length)
output = output.squeeze(1)
output = output.squeeze(1)
return output
def _get_istft_kernels(nfft):
nfft = int(nfft)
assert nfft % 2 == 0
def kernel_fn(time, freq):
return np.exp(1j * (2 * np.pi * time * freq) / nfft)
kernels = np.fromfunction(kernel_fn, (int(nfft), int(nfft/2+1)), dtype=np.float64)
ac_cof = float(np.real(kernels[0, 0]))
kernels = 2 * kernels[:, 1:]
kernels[:, -1] = kernels[:, -1] / 2.0
real_kernels = np.real(kernels)
imag_kernels = np.imag(kernels)
real_kernels = nn.Parameter(torch.from_numpy(real_kernels[:, np.newaxis, :, np.newaxis]).float())
imag_kernels = nn.Parameter(torch.from_numpy(imag_kernels[:, np.newaxis, :, np.newaxis]).float())
return real_kernels, imag_kernels, ac_cof
class stft(nn.Module):
def __init__(self, nfft=1024, hop_length=512, window="hanning"):
super(stft, self).__init__()
assert nfft % 2 == 0
self.hop_length = hop_length
self.n_freq = n_freq = nfft//2 + 1
self.real_kernels, self.imag_kernels = _get_stft_kernels(nfft, window)
def forward(self, sample):
sample = sample.unsqueeze(1)
sample = sample.unsqueeze(1)
magn = F.conv2d(sample, self.real_kernels, stride=self.hop_length)
phase = F.conv2d(sample, self.imag_kernels, stride=self.hop_length)
magn = magn.permute(0, 2, 1, 3)
phase = phase.permute(0, 2, 1, 3)
# complex conjugate
phase = -1 * phase[:,:,1:,:]
ac = magn[:,:,0,:]
magn = magn[:,:,1:,:]
return magn, phase, ac
def _get_stft_kernels(nfft, window):
nfft = int(nfft)
assert nfft % 2 == 0
def kernel_fn(freq, time):
return np.exp(-1j * (2 * np.pi * time * freq) / float(nfft))
kernels = np.fromfunction(kernel_fn, (nfft//2+1, nfft), dtype=np.float64)
if window == "hanning":
win_cof = scipy.signal.get_window("hanning", nfft)[np.newaxis, :]
else:
win_cof = np.ones((1, nfft), dtype=np.float64)
kernels = kernels[:, np.newaxis, np.newaxis, :] * win_cof
real_kernels = nn.Parameter(torch.from_numpy(np.real(kernels)).float())
imag_kernels = nn.Parameter(torch.from_numpy(np.imag(kernels)).float())
return real_kernels, imag_kernels
if __name__ == "__main__":
signal = np.random.rand(1024 * 10)
signal = signal - np.mean(signal)
signal = signal[np.newaxis, :]
model = stft(window="retangle")
real, imag, ac = model.forward(Variable(torch.from_numpy(signal).float()))
real = real.data.numpy()
imag = imag.data.numpy()
ac = ac.data.numpy()
print(ac)