-
Notifications
You must be signed in to change notification settings - Fork 3
/
MaskingMethods.py
242 lines (208 loc) · 8.17 KB
/
MaskingMethods.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# -*- coding: utf-8 -*-
__author__ = 'S.I. Mimilakis'
__copyright__ = 'MacSeNet'
import numpy as np
from scipy.fftpack import fft, ifft
from TFMethods import TimeFrequencyDecomposition as TF
realmin = np.finfo(np.double).tiny
class FrequencyMasking:
"""Class containing various time-frequency masking methods, for processing Time-Frequency representations.
"""
def __init__(self, mX, sTarget, nResidual, psTarget = [], pnResidual = [], alpha = 1.2, method = 'Wiener'):
self._mX = mX
self._eps = np.finfo(np.float).eps
self._sTarget = sTarget
self._nResidual = nResidual
self._pTarget = psTarget
self._pY = pnResidual
self._mask = []
self._Out = []
self._alpha = alpha
self._method = method
def __call__(self, reverse = False):
if (self._method == 'Phase'):
if not self._pTarget.size or not self._pTarget.size:
raise ValueError('Phase-sensitive masking cannot be performed without phase information.')
else:
FrequencyMasking.phaseSensitive(self)
if not(reverse) :
FrequencyMasking.applyMask(self)
else :
FrequencyMasking.applyReverseMask(self)
elif (self._method == 'IRM'):
FrequencyMasking.IRM(self)
if not(reverse) :
FrequencyMasking.applyMask(self)
else :
FrequencyMasking.applyReverseMask(self)
elif (self._method == 'IBM'):
FrequencyMasking.IBM(self)
if not(reverse) :
FrequencyMasking.applyMask(self)
else :
FrequencyMasking.applyReverseMask(self)
elif (self._method == 'UBBM'):
FrequencyMasking.UBBM(self)
if not(reverse) :
FrequencyMasking.applyMask(self)
else :
FrequencyMasking.applyReverseMask(self)
elif (self._method == 'Wiener'):
FrequencyMasking.Wiener(self)
if not(reverse) :
FrequencyMasking.applyMask(self)
else :
FrequencyMasking.applyReverseMask(self)
elif (self._method == 'alphaWiener'):
FrequencyMasking.alphaHarmonizableProcess(self)
if not(reverse) :
FrequencyMasking.applyMask(self)
else :
FrequencyMasking.applyReverseMask(self)
return self._Out
def IRM(self):
"""
Computation of Ideal Amplitude Ratio Mask. As appears in :
H Erdogan, John R. Hershey, Shinji Watanabe, and Jonathan Le Roux,
"Phase-sensitive and recognition-boosted speech separation using deep recurrent neural networks,"
in ICASSP 2015, Brisbane, April, 2015.
Args:
sTarget: (2D ndarray) Magnitude Spectrogram of the target component
nResidual: (2D ndarray) Magnitude Spectrogram of the residual component
Returns:
mask: (2D ndarray) Array that contains time frequency gain values
"""
print('Ideal Amplitude Ratio Mask')
self._mask = np.divide(self._sTarget, (self._eps + self._sTarget + self._nResidual))
def IBM(self):
"""
Computation of Ideal Binary Mask.
Args:
sTarget: (2D ndarray) Magnitude Spectrogram of the target component
nResidual: (2D ndarray) Magnitude Spectrogram of the residual component
Returns:
mask: (2D ndarray) Array that contains time frequency gain values
"""
print('Ideal Binary Mask')
theta = 0.5
mask = np.divide(self._sTarget ** self._alpha, (self._eps + self._nResidual ** self._alpha))
bg = np.where(mask >= theta)
sm = np.where(mask < theta)
mask[bg[0],bg[1]] = 1.
mask[sm[0], sm[1]] = 0.
self._mask = mask
def UBBM(self):
"""
Computation of Upper Bound Binary Mask. As appears in :
- J.J. Burred, "From Sparse Models to Timbre Learning: New Methods for Musical Source Separation", PhD Thesis,
TU Berlin, 2009.
Args:
sTarget: (2D ndarray) Magnitude Spectrogram of the target component
nResidual: (2D ndarray) Magnitude Spectrogram of the residual component (Should not contain target source!)
Returns:
mask: (2D ndarray) Array that contains time frequency gain values
"""
print('Upper Bound Binary Mask')
mask = 20. * np.log(self._eps + np.divide((self._eps + (self._sTarget ** self._alpha)),
((self._eps + (self._nResidual ** self._alpha)))))
bg = np.where(mask >= 0)
sm = np.where(mask < 0)
mask[bg[0],bg[1]] = 1.
mask[sm[0], sm[1]] = 0.
self._mask = mask
def Wiener(self):
"""
Computation of Wiener-like Mask. As appears in :
H Erdogan, John R. Hershey, Shinji Watanabe, and Jonathan Le Roux,
"Phase-sensitive and recognition-boosted speech separation using deep recurrent neural networks,"
in ICASSP 2015, Brisbane, April, 2015.
Args:
sTarget: (2D ndarray) Magnitude Spectrogram of the target component
nResidual: (2D ndarray) Magnitude Spectrogram of the residual component
Returns:
mask: (2D ndarray) Array that contains time frequency gain values
"""
print('Wiener-like Mask')
localsTarget = self._sTarget ** 2.
numElements = len(self._nResidual)
if numElements > 1:
localnResidual = self._nResidual[0] ** 2. + localsTarget
for indx in range(1, numElements):
localnResidual += self._nResidual[indx] ** 2.
else :
localnResidual = self._nResidual[0] ** 2. + localsTarget
self._mask = np.divide((localsTarget + self._eps), (self._eps + localnResidual))
def alphaHarmonizableProcess(self):
"""
Computation of alpha harmonizable Wiener like mask, as appears in :
A. Liutkus, R. Badeau, "Generalized Wiener filtering with fractional power spectrograms",
40th International Conference on Acoustics, Speech and Signal Processing (ICASSP),
Apr 2015, Brisbane, Australia.
Args:
sTarget: (2D ndarray) Magnitude Spectrogram of the target component
nResidual: (2D ndarray) Magnitude Spectrogram of the residual component or a list
of 2D ndarrays which will be summed
Returns:
mask: (2D ndarray) Array that contains time frequency gain values
"""
print('Harmonizable Process with alpha:', str(self._alpha))
localsTarget = self._sTarget ** self._alpha
numElements = len(self._nResidual)
if numElements > 1:
localnResidual = self._nResidual[0] ** self._alpha + localsTarget
for indx in range(1, numElements):
localnResidual += self._nResidual[indx] ** self._alpha
else :
localnResidual = self._nResidual[0] ** self._alpha + localsTarget
self._mask = np.divide((localsTarget + self._eps), (self._eps + localnResidual))
def phaseSensitive(self):
"""
Computation of Phase Sensitive Mask. As appears in :
H Erdogan, John R. Hershey, Shinji Watanabe, and Jonathan Le Roux,
"Phase-sensitive and recognition-boosted speech separation using deep recurrent neural networks,"
in ICASSP 2015, Brisbane, April, 2015.
Args:
mTarget: (2D ndarray) Magnitude Spectrogram of the target component
pTarget: (2D ndarray) Phase Spectrogram of the output component
mY: (2D ndarray) Magnitude Spectrogram of the output component
pY: (2D ndarray) Phase Spectrogram of the output component
Returns:
mask: (2D ndarray) Array that contains time frequency gain values
"""
print('Phase Sensitive Masking.')
# Compute Phase Difference
Theta = (self._pTarget - self._pY)
self._mask = 2./ (1. + np.exp(-np.multiply(np.divide(self._sTarget, self._eps + self._nResidual), np.cos(Theta)))) - 1.
def applyMask(self):
""" Compute the filtered output spectrogram.
Args:
mask: (2D ndarray) Array that contains time frequency gain values
mX: (2D ndarray) Input Magnitude Spectrogram
Returns:
Y: (2D ndarray) Filtered version of the Magnitude Spectrogram
"""
self._Out = np.multiply(self._mask, self._mX)
def applyReverseMask(self):
""" Compute the filtered output spectrogram, reversing the gain values.
Args:
mask: (2D ndarray) Array that contains time frequency gain values
mX: (2D ndarray) Input Magnitude Spectrogram
Returns:
Y: (2D ndarray) Filtered version of the Magnitude Spectrogram
"""
self._Out = np.multiply( (1. - self._mask), self._mX)
if __name__ == "__main__":
# Small test
kSin = (0.5 * np.cos(np.arange(4096) * (1000.0 * (3.1415926 * 2.0) / 44100)))
noise = (np.random.uniform(-0.25,0.25,4096))
# Noisy observation
obs = (kSin + noise)
kSinX = fft(kSin, 4096)
noisX = fft(noise, 4096)
obsX = fft(obs, 4096)
# Wiener Case
mask = FrequencyMasking(np.abs(obsX), np.abs(kSinX), [np.abs(noisX)], [], [], alpha = 2., method = 'alphaWiener')
sinhat = mask()
noisehat = mask(reverse = True)
# Access the mask if needed
ndmask = mask._mask