-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathradial.py
400 lines (322 loc) · 10.8 KB
/
radial.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
from __future__ import division
import numpy as np
from scipy import special
from .. import util
def spherical_pw(N, k, r, setup):
r"""Radial coefficients for a plane wave.
Computes the radial component of the spherical harmonics expansion of a
plane wave impinging on a spherical array.
.. math::
\mathring{P}_n(k) = 4 \pi i^n b_n(kr)
Parameters
----------
N : int
Maximum order.
k : (M,) array_like
Wavenumber.
r : float
Radius of microphone array.
setup : {'open', 'card', 'rigid'}
Array configuration (open, cardioids, rigid).
Returns
-------
bn : (M, N+1) numpy.ndarray
Radial weights for all orders up to N and the given wavenumbers.
"""
kr = util.asarray_1d(k*r)
n = np.arange(N+1)
bn = weights(N, kr, setup)
return 4*np.pi * (1j)**n * bn
def spherical_ps(N, k, r, rs, setup):
r"""Radial coefficients for a point source.
Computes the radial component of the spherical harmonics expansion of a
point source impinging on a spherical array.
.. math::
\mathring{P}_n(k) = 4 \pi (-i) k h_n^{(2)}(k r_s) b_n(kr)
Parameters
----------
N : int
Maximum order.
k : (M,) array_like
Wavenumber.
r : float
Radius of microphone array.
rs : float
Distance of source.
setup : {'open', 'card', 'rigid'}
Array configuration (open, cardioids, rigid).
Returns
-------
bn : (M, N+1) numpy.ndarray
Radial weights for all orders up to N and the given wavenumbers.
"""
k = util.asarray_1d(k)
krs = k*rs
n = np.arange(N+1)
bn = weights(N, k*r, setup)
if len(k) == 1:
bn = bn[np.newaxis, :]
for i, x in enumerate(krs):
hn = special.spherical_jn(n, x) - 1j * special.spherical_yn(n, x)
bn[i, :] = bn[i, :] * 4*np.pi * (-1j) * hn * k[i]
return np.squeeze(bn)
def weights(N, kr, setup):
r"""Radial weighing functions.
Computes the radial weighting functions for diferent array types
(cf. eq.(2.62), Rafaely 2015).
For instance for an rigid array
.. math::
b_n(kr) = j_n(kr) - \frac{j_n^\prime(kr)}{h_n^{(2)\prime}(kr)}h_n^{(2)}(kr)
Parameters
----------
N : int
Maximum order.
kr : (M,) array_like
Wavenumber * radius.
setup : {'open', 'card', 'rigid'}
Array configuration (open, cardioids, rigid).
Returns
-------
bn : (M, N+1) numpy.ndarray
Radial weights for all orders up to N and the given wavenumbers.
"""
kr = util.asarray_1d(kr)
n = np.arange(N+1)
bns = np.zeros((len(kr), N+1), dtype=complex)
for i, x in enumerate(kr):
jn = special.spherical_jn(n, x)
if setup == 'open':
bn = jn
elif setup == 'card':
bn = jn - 1j * special.spherical_jn(n, x, derivative=True)
elif setup == 'rigid':
jnd = special.spherical_jn(n, x, derivative=True)
hn = jn - 1j * special.spherical_yn(n, x)
hnd = jnd - 1j * special.spherical_yn(n, x, derivative=True)
bn = jn - jnd/hnd*hn
else:
raise ValueError('setup must be either: open, card or rigid')
bns[i, :] = bn
return np.squeeze(bns)
def regularize(dn, a0, method):
"""Regularization (amplitude limitation) of radial filters.
Amplitude limitation of radial filter coefficients, methods according
to (cf. Rettberg, Spors : DAGA 2014)
Parameters
----------
dn : numpy.ndarray
Values to be regularized
a0 : float
Parameter for regularization (not required for all methods)
method : {'none', 'discard', 'softclip', 'Tikh', 'wng'}
Method used for regularization/amplitude limitation
(none, discard, hardclip, Tikhonov, White Noise Gain).
Returns
-------
dn : numpy.ndarray
Regularized values.
hn : array_like
"""
idx = np.abs(dn) > a0
if method == 'none':
hn = np.ones_like(dn)
elif method == 'discard':
hn = np.ones_like(dn)
hn[idx] = 0
elif method == 'hardclip':
hn = np.ones_like(dn)
hn[idx] = a0 / np.abs(dn[idx])
elif method == 'softclip':
scaling = np.pi / 2
hn = a0 / abs(dn)
hn = 2 / np.pi * np.arctan(scaling * hn)
elif method == 'Tikh':
a0 = np.sqrt(a0 / 2)
alpha = (1 - np.sqrt(1 - 1/(a0**2))) / (1 + np.sqrt(1 - 1/(a0**2)))
hn = 1 / (1 + alpha**2 * np.abs(dn)**2)
# hn = 1 / (1 + alpha**2 * np.abs(dn))
elif method == 'wng':
hn = 1/(np.abs(dn)**2)
# hn = hn/np.max(hn)
else:
raise ValueError('method must be either: none, ' +
'discard, hardclip, softclip, Tikh or wng')
# dn[0, 1:] = dn[1, 1:]
dn = dn * hn
if not np.isfinite(dn).all():
raise UserWarning("Filter not finite")
return dn, hn
def diagonal_mode_mat(bk):
"""Diagonal matrix of radial coefficients for all modes/wavenumbers.
Parameters
----------
bk : (M, N+1) numpy.ndarray
Vector containing values for all wavenumbers :math:`M` and modes up to
order :math:`N`
Returns
-------
Bk : (M, (N+1)**2, (N+1)**2) numpy.ndarray
Multidimensional array containing diagnonal matrices with input
vector on main diagonal.
"""
bk = repeat_n_m(bk)
if len(bk.shape) == 1:
bk = bk[np.newaxis, :]
K, N = bk.shape
Bk = np.zeros([K, N, N], dtype=complex)
for k in range(K):
Bk[k, :, :] = np.diag(bk[k, :])
return np.squeeze(Bk)
def repeat_n_m(v):
"""Repeat elements in a vector .
Returns a vector with the elements of the vector *v* repeated *n* times,
where *n* denotes the position of the element in *v*. The function can
be used to order the coefficients in the vector according to the order of
spherical harmonics. If *v* is a matrix, it is treated as a stack of
vectors residing in the last index and broadcast accordingly.
Parameters
----------
v : (,N+1) numpy.ndarray
Input vector or stack of input vectors.
Returns
-------
: (,(N+1)**2) numpy.ndarray
Vector or stack of vectors containing repetated values.
"""
krlist = [np.tile(v, (2*i+1, 1)).T for i, v in enumerate(v.T.tolist())]
return np.squeeze(np.concatenate(krlist, axis=-1))
def circular_pw(N, k, r, setup):
r"""Radial coefficients for a plane wave.
Computes the radial component of the circular harmonics expansion of a
plane wave impinging on a circular array.
.. math::
\mathring{P}_n(k) = i^{-n} b_n(kr)
Parameters
----------
N : int
Maximum order.
k : (M,) array_like
Wavenumber.
r : float
Radius of microphone array.
setup : {'open', 'card', 'rigid'}
Array configuration (open, cardioids, rigid).
Returns
-------
bn : (M, N+1) numpy.ndarray
Radial weights for all orders up to N and the given wavenumbers.
"""
kr = util.asarray_1d(k*r)
n = np.roll(np.arange(-N, N+1), -N)
bn = circ_radial_weights(N, kr, setup)
return (1j)**(n) * bn
def circular_ls(N, k, r, rs, setup):
r"""Radial coefficients for a line source.
Computes the radial component of the circular harmonics expansion of a
line source impinging on a circular array.
.. math::
\mathring{P}_n(k) = \frac{-i}{4} H_n^{(2)}(k r_s) b_n(kr)
Parameters
----------
N : int
Maximum order.
k : (M,) array_like
Wavenumber.
r : float
Radius of microphone array.
rs : float
Distance of source.
setup : {'open', 'card', 'rigid'}
Array configuration (open, cardioids, rigid).
Returns
-------
bn : (M, N+1) numpy.ndarray
Radial weights for all orders up to N and the given wavenumbers.
"""
k = util.asarray_1d(k)
krs = k*rs
n = np.roll(np.arange(-N, N+1), -N)
bn = circ_radial_weights(N, k*r, setup)
if len(k) == 1:
bn = bn[np.newaxis, :]
for i, x in enumerate(krs):
Hn = special.hankel2(n, x)
bn[i, :] = bn[i, :] * -1j/4 * Hn
return np.squeeze(bn)
def circ_radial_weights(N, kr, setup):
r"""Radial weighing functions.
Computes the radial weighting functions for diferent array types
For instance for an rigid array
.. math::
b_n(kr) = J_n(kr) - \frac{J_n^\prime(kr)}{H_n^{(2)\prime}(kr)}H_n^{(2)}(kr)
Parameters
----------
N : int
Maximum order.
kr : (M,) array_like
Wavenumber * radius.
setup : {'open', 'card', 'rigid'}
Array configuration (open, cardioids, rigid).
Returns
-------
bn : (M, N+1) numpy.ndarray
Radial weights for all orders up to N and the given wavenumbers.
"""
kr = util.asarray_1d(kr)
n = np.arange(N+1)
Bns = np.zeros((len(kr), N+1), dtype=complex)
for i, x in enumerate(kr):
Jn = special.jv(n, x)
if setup == 'open':
bn = Jn
elif setup == 'card':
bn = Jn - 1j * special.jvp(n, x, n=1)
elif setup == 'rigid':
Jnd = special.jvp(n, x, n=1)
Hn = special.hankel2(n, x)
Hnd = special.h2vp(n, x)
bn = Jn - Jnd/Hnd*Hn
else:
raise ValueError('setup must be either: open, card or rigid')
Bns[i, :] = bn
Bns = np.concatenate((Bns, (Bns*(-1)**np.arange(N+1))[:, :0:-1]), axis=-1)
return np.squeeze(Bns)
def circ_diagonal_mode_mat(bk):
"""Diagonal matrix of radial coefficients for all modes/wavenumbers.
Parameters
----------
bk : (M, N+1) numpy.ndarray
Vector containing values for all wavenumbers :math:`M` and modes up to
order :math:`N`
Returns
-------
Bk : (M, 2*N+1, 2*N+1) numpy.ndarray
Multidimensional array containing diagnonal matrices with input
vector on main diagonal.
"""
if len(bk.shape) == 1:
bk = bk[np.newaxis, :]
K, N = bk.shape
Bk = np.zeros([K, N, N], dtype=complex)
for k in range(K):
Bk[k, :, :] = np.diag(bk[k, :])
return np.squeeze(Bk)
def mirror_vec(v):
"""Mirror elements in a vector.
Returns a vector of length *2*len(v)-1* with symmetric elements.
The first *len(v)* elements are the same as *v* and the last *len(v)-1*
elements are *v[:0:-1]*. The function can be used to order the circular
harmonic coefficients. If *v* is a matrix, it is treated as a stack of
vectors residing in the last index and broadcast accordingly.
Parameters
----------
v : (, N+1) numpy.ndarray
Input vector of stack of input vectors
Returns
-------
: (, 2*N+1) numpy.ndarray
Vector of stack of vectors containing mirrored elements
"""
if len(v.shape) == 1:
v = v[np.newaxis, :]
return np.concatenate((v, v[:, :0:-1]), axis=1)