forked from onolab-tmu/code_2020ICASSP_five
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfive.py
More file actions
182 lines (152 loc) · 6.48 KB
/
five.py
File metadata and controls
182 lines (152 loc) · 6.48 KB
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
# Copyright (c) 2018-2019 Robin Scheibler
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""
FIVE: Fast Independent Vector Extraction
=====================================================
This algorithm extracts one source independent from a minimum energy background
using an iterative algorithm that attempts to maximize the SINR at every step.
References
----------
.. [1] R. Scheibler and N. Ono, Fast Independent Vector Extraction by Iterative
SINR Maximization, arXiv, 2019.
"""
import numpy as np
from pyroomacoustics.bss.common import projection_back
from scipy import linalg
def five(
X,
n_iter=3,
proj_back=True,
W0=None,
model="laplace",
init_eig=False,
return_filters=False,
callback=None,
callback_checkpoints=[],
cost_callback=None,
):
"""
This algorithm extracts one source independent from a minimum energy background.
The separation is done in the time-frequency domain and the FFT length
should be approximately equal to the reverberation time. The residual
energy in the background is minimized.
Two different statistical models (Laplace or time-varying Gauss) can
be used by specifying the keyword argument `model`. The performance of Gauss
model is higher in good conditions (few sources, low noise), but Laplace
(the default) is more robust in general.
Parameters
----------
X: ndarray (nframes, nfrequencies, nchannels)
STFT representation of the signal
n_iter: int, optional
The number of iterations (default 3)
proj_back: bool, optional
Scaling on first mic by back projection (default True)
W0: ndarray (nfrequencies, nsrc, nchannels), optional
Initial value for demixing matrix
model: str
The model of source distribution 'gauss' or 'laplace' (default)
init_eig: bool, optional (default ``False``)
If ``True``, and if ``W0 is None``, then the weights are initialized
using the principal eigenvectors of the covariance matrix of the input
data. When ``False``, the demixing matrices are initialized with identity
matrix.
return_filters: bool
If true, the function will return the demixing matrix too
callback: func
A callback function called every 10 iterations, allows to monitor
convergence
callback_checkpoints: list of int
A list of epoch number when the callback should be called
cost_callback: func
When this callback function is specified, it will be called with
the value of the cost function as argument
Returns
-------
Returns an (nframes, nfrequencies, 1) array. Also returns
the demixing matrix (nfrequencies, nchannels, nsources)
if ``return_values`` keyword is True.
"""
n_frames, n_freq, n_chan = X.shape
def tensor_H(A):
return np.conj(A.swapaxes(1, 2))
# default to determined case
n_src = 1
if model not in ["laplace", "gauss"]:
raise ValueError("Model should be either " "laplace" " or " "gauss" ".")
# covariance matrix of input signal (n_freq, n_chan, n_chan)
Cx = np.mean(X[:, :, :, None] * np.conj(X[:, :, None, :]), axis=0)
# We will need the inverse square root of Cx
e_val, e_vec = np.linalg.eigh(Cx)
Q_H = e_vec[:, :, ::-1] * np.sqrt(e_val[:, None, ::-1])
eps = 1e-10
V = np.zeros((n_freq, n_chan, n_chan), dtype=X.dtype)
r_inv = np.zeros((n_src, n_frames))
# Things are more efficient when the frequencies are over the first axis
Y = np.zeros((n_freq, n_src, n_frames), dtype=X.dtype)
X_original = X.copy()
# pre-whiten the input signal
X = np.linalg.solve(Q_H, X.transpose([1, 2, 0]))
# initialize the output signal
if init_eig:
# Principal component
Y = X[:, :1, :].copy()
else:
# First microphone
Y = X_original[:, :, :1].transpose([1, 2, 0]).copy()
for epoch in range(n_iter):
if callback is not None and epoch in callback_checkpoints:
Y_tmp = Y.transpose([2, 0, 1])
if proj_back:
z = projection_back(Y_tmp, X_original[:, :, 0])
callback(Y_tmp * np.conj(z[None, :, :]))
else:
callback(Y_tmp)
# Update now the demixing matrix
# shape: (n_frames, n_src)
if model == "laplace":
r_inv = 1.0 / np.maximum(eps, 2.0 * np.linalg.norm(Y[:, 0, :], axis=0))
elif model == "gauss":
r_inv = 1.0 / np.maximum(
eps, (np.linalg.norm(Y[:, 0, :], axis=0) ** 2) / n_freq
)
# Compute Auxiliary Variable
# shape: (n_freq, n_chan, n_chan)
V[:, :, :] = np.matmul(
(X * r_inv[None, None, :]), np.conj(X.swapaxes(1, 2)) / n_frames
)
# Solve the Eigenvalue problem
# We only need the smallest eigenvector and eigenvalue,
# so we could solve this more efficiently, but it is faster to
# just solve everything rather than wrap this in a for loop
lambda_, R = np.linalg.eigh(V)
# Update the output signal
# note: eigenvalues are in ascending order, we use the smallest
Y[:, :, :] = np.matmul(np.conj(R[:, :, :1]).transpose([0, 2, 1]) / np.sqrt(lambda_[:, None, :1]), X)
if return_filters and epoch == n_iter - 1:
W = R
Y = Y.transpose([2, 0, 1]).copy()
if proj_back:
z = projection_back(Y, X_original[:, :, 0])
Y *= np.conj(z[None, :, :])
if return_filters:
return Y, W @ np.linalg.inv(Q_H)
else:
return Y