-
Notifications
You must be signed in to change notification settings - Fork 1
/
AuxTDE.py
253 lines (195 loc) · 6.61 KB
/
AuxTDE.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
243
244
245
246
247
248
249
250
251
252
253
# -*- coding: utf-8 -*-
import numpy as np
import single_AuxTDE
import TDE
from functions.STFT import mSTFT
def AuxTDE(
x,
frlen=None,
frsft=None,
n_iter_t=5,
n_iter_a=5,
n_epoch=2,
a0=None,
tau0=None,
average=True,
):
"""
Auxilliary function based estimation of sub-sample time delays.
parameters
----------
x: array_like (n_sample, n_ch)
Multichannel observations
frlen: float, optional
FFT frame length for STFT (default: n_sample)
frsft: float, optional
Proportion of frame shift (default: half overlap)
n_iter_t: int, optional
The number of iterations for tau updates
n_iter_a: int, optional
The number of iterations for a updates
n_epoch: int, optional
The number of epochs for alternating updates of tau and a
a0: float, optional
The initial estimates of the amplitude.
If None, 1 is used for all frequencies.
tau0: float, optional
The initial estimates of the time delays.
If None, GCC with quadratic interpolation is used.
average: bool, optional
If true, frequency-independent amplitude is estimated.
return
----------
tau: array_like (n_iter_t + 1, n_ch)
The sub-sample precision time delay estimates
tau[0, :] are the initial estimates.
"""
# Check error and get parameters
n_samples, n_ch = x.shape
if n_samples < n_ch:
x = x.T
n_samples, n_ch = x.shape
if frlen is None:
frlen = n_samples
if frsft is None:
frsft = frlen // 2
# STFT
wnd = np.ones(frlen)
X = mSTFT(x, frlen, frsft, wnd, zp=False).transpose(2, 0, 1)
n_freq, n_ch, n_frame = X.shape
# compute variables/parameters
w = 2.0 * np.pi * np.arange(0, n_freq) / frlen
V = calc_SCM(X)
A = np.abs(V)
phi = np.angle(V / A)
A /= frlen
A[1:-1, :, :] *= 2
## main
# initialization
tau = init_tau(x) if tau0 is None else np.copy(tau0)
a = np.ones([n_freq, n_ch, 1]) / np.sqrt(n_ch) if a0 is None else np.copy(a0)
sigma = np.ones([n_ch])
f_update_a = update_a_mean if average else update_a_freq
# alternating updates
for epoch in range(n_epoch):
weighted_A = A / (sigma[:, np.newaxis] @ sigma[np.newaxis, :])
# time delay estimation
for iter in range(n_iter_t):
tau[1:] = update_tau(a, tau, w, weighted_A, phi)
# amplitude estimation
for iter in range(n_iter_a):
a = f_update_a(a, tau, w, weighted_A, phi)
# update sigma
sigma = update_sigma(a, tau, X, w)
# deal with the minus time delay
mask = tau > (n_samples / 2)
tau -= n_samples * mask
return tau
def update_tau(a, tau, w, A, phi):
w2 = w ** 2
tdiff = tau[:, np.newaxis].T - tau[:, np.newaxis]
n_ch = tau.shape[0]
# update phase estimates (auxiliary variables)
theta = w[:, np.newaxis, np.newaxis] * tdiff[np.newaxis, :, :] + phi
# round to within 2pi
theta -= np.round(theta / (2 * np.pi)) * 2 * np.pi
# update coefficients of auxiliary function
B = a @ a.swapaxes(1, 2) * A * np.sinc(theta / np.pi)
C = w2[:, np.newaxis, np.newaxis] * (
np.identity(n_ch)[np.newaxis, :, :] * np.sum(B, axis=1)[:, np.newaxis, :] - B
)
c = w[:, np.newaxis] * np.sum(B * theta, axis=1)
# update time delay estimates
retval = tau[1:] - np.linalg.inv(np.sum(C[:, 1:, 1:], axis=0)) @ (
np.sum(c[:, 1:], axis=0)
)
return retval
def update_a_freq(a, tau, w, A, phi):
# update auxiliary variables
tdiff = tau[:, np.newaxis].T - tau[:, np.newaxis]
theta = w[:, np.newaxis, np.newaxis] * tdiff[np.newaxis, :, :] + phi
# update amplitudes
V_prime = np.real(A * np.exp(-1 * 1j * theta))
a = max_and_norm(V_prime @ a)
return a
def update_a_mean(a, tau, w, A, phi):
# update auxiliary variables
tdiff = tau[:, np.newaxis].T - tau[:, np.newaxis]
theta = w[:, np.newaxis, np.newaxis] * tdiff[np.newaxis, :, :] + phi
# update amplitudes
V_prime = np.mean(np.real(A * np.exp(-1 * 1j * theta)), axis=0)
a = max_and_norm(V_prime @ a)
return a
def max_and_norm(vec):
vec = np.maximum(vec, 1e-10)
return vec / np.linalg.norm(vec, axis=1)[:, np.newaxis, :]
def update_sigma(a, tau, X, w):
g = calcRTF(a, tau, w)
tmp = X - g * (g.conj().swapaxes(1, 2) @ X) / (g.conj().swapaxes(1, 2) @ g)
tmp2 = np.real(tmp.conj() * tmp)
tmp2[1:-1, :, :] *= 2
sigma2 = np.mean(tmp2, axis=(0, 2))
return np.sqrt(sigma2)
def calcRTF(a, tau, w):
return a * np.exp(
-1j * w[:, np.newaxis, np.newaxis] * tau[np.newaxis, :, np.newaxis]
)
def calc_SCM(X):
# compute spatial covariance matrix
V = X @ X.conj().swapaxes(1, 2)
return V / X.shape[2]
def init_tau(x, is_naive=False):
# initialization
n_samples, n_ch = x.shape
tau = np.zeros([n_ch])
tau_naive = np.zeros([n_ch])
# compute cross-spectrum
X = np.fft.rfft(x, axis=0)
# compute time delay estimates
for ch in range(1, n_ch):
tau[ch], tau_naive[ch] = TDE.GCC_with_parafit(
X[:, 0] * np.conj(X[:, ch]), ret_GCC=True
)
return tau_naive if is_naive else tau
def cost_function(a, tau, A, phi, w):
amat = a * a.swapaxes(1, 2)
tdiff = tau[:, np.newaxis].T - tau[:, np.newaxis]
cost = np.sum(
amat * A * np.cos(w[:, np.newaxis, np.newaxis] * tdiff[np.newaxis, :, :] + phi)
)
return cost
def cost_function_mat(a, V):
cost = np.sum(a.swapaxes(1, 2) @ V @ a)
return cost
def auxiliary_function(a, tau, init_tau, A, phi, w):
amat = a * a.swapaxes(1, 2)
tdiff = tau[:, np.newaxis].T - tau[:, np.newaxis]
tdiff = tdiff[np.newaxis, :, :]
tdiff_0 = init_tau[:, np.newaxis].T - init_tau[:, np.newaxis]
tdiff_0 = tdiff_0[np.newaxis, :, :]
w = w[:, np.newaxis, np.newaxis]
# phase estimates (auxiliary variables)
theta = w * tdiff_0 + phi
# round to within 2pi
n = np.round(theta / (2 * np.pi))
theta -= 2 * n * np.pi
# main
Q = np.sum(
amat
* A
* (
(-0.5 * np.sinc(theta / np.pi)) * (w * tdiff + phi - 2 * n * np.pi) ** 2
+ np.cos(theta)
+ theta * np.sin(theta) / 2
)
)
return Q
def PW_AuxTDE(x, frlen=None, n_iter=10, ret_all=False):
n_samples, n_ch = x.shape
tau = np.zeros([n_ch, n_iter + 1])
x_2ch = np.zeros([n_samples, 2])
x_2ch[:, 0] = x[:, 0]
for ch in range(1, n_ch):
x_2ch[:, 1] = x[:, ch]
tau[ch, :] = single_AuxTDE.AuxTDE(x_2ch, frlen, n_iter=n_iter, ret_all=True)
return tau if ret_all else tau[:, -1]