-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil_fa23.py
More file actions
369 lines (306 loc) · 10.1 KB
/
Copy pathutil_fa23.py
File metadata and controls
369 lines (306 loc) · 10.1 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
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
# see https://github.com/spatialaudio/daga2026_dode
# Nara Hahn (https://github.com/narahahn) is the author of the code below
# which originates from the project
# N. Hahn, F. Schultz, S. Spors (2023):
# Accurate time-domain simulation of spherical microphone arrays.
# Forum Acusticum, Torino, pp. 599–606.
# https://dael.euracoustics.org/confs/fa2023/data/articles/001255.pdf
# https://www.doi.org/10.61782/fa.2023.1255
# only the relevant code parts were adopted
import numpy as np
from scipy.signal import freqz
from scipy.special import spherical_jn, spherical_yn
def spherical_hn2(n, z, derivative=False):
return spherical_jn(n, z, derivative) - 1j * spherical_yn(n, z, derivative)
def hn2_poly(n, derivative=False):
"""Bessel polynomial of n-th order.
Polynomial that characterizes the spherical Hankel functions.
The coefficients are computed by using the recurrence relation.
The returned array has a length of n+1. The first coefficient is always 1.
Parameters
----------
n : int
Bessel polynomial order.
"""
if derivative:
return derivative_hn2_poly(n)
else:
beta = np.zeros(n + 1)
beta[n] = 1
for k in range(n-1, -1, -1):
beta[k] = beta[k+1] * (2*n-k) * (k+1) / (n-k) / 2
return beta
def decrease_hn2_poly_order_by_one(beta):
"""Bessel polynomial of order decreased by 1.
"""
n = len(beta)-1
alpha = np.zeros(n)
for k in range(n-1):
alpha[k] = beta[k+1] * (k+1) / (2*n-k-1)
alpha[-1] = 1
return alpha
def increase_hn2_poly_order_by_one(beta):
"""Bessel polynomial of order increased by 1.
"""
n = len(beta)
alpha = np.zeros(n+1)
for k in range(n):
alpha[k+1] = beta[k] * (2*n-k-1) / (k+1)
alpha[0] = alpha[1]
return alpha
def derivative_hn2_poly(n):
"""Polynomial characterizing the derivative of the spherical Hankel func.
"""
gamma = hn2_poly(n+1)
gamma[:-1] -= n * decrease_hn2_poly_order_by_one(gamma)
return gamma
def derivative_hn2_poly_2(n):
"""
Polynomial characterizing the derivative of the spherical Hankel functions.
An alternative recurrence relation is used.
"""
if n == 0:
gamma = hn2_poly(1)
else:
beta0 = hn2_poly(n)
beta1 = hn2_poly(n-1)
gamma = np.zeros(n+2)
gamma[0] = (n+1)*beta0[0]
gamma[1] = (n+1)*beta0[1]
gamma[-1] = 1
for k in range(2, n+1):
gamma[k] = (n+1)*beta0[k] + beta1[k-2]
return gamma
def s_zeros_hn2_poly(n):
return np.roots(hn2_poly(n)[::-1])
def s_zeros_derivative_hn2_poly(n):
return np.zeros(n)
def s_poles_derivative_hn2_poly(n):
return np.roots(derivative_hn2_poly(n)[::-1])
def s_zpk_hn2_poly(n):
s_zeros = s_zeros_derivative_hn2_poly(n)
s_poles = s_poles_derivative_hn2_poly(n)
return s_zeros, s_poles, 1
def s_zpk_ps_rigid_sphere(n):
s_zeros = s_zeros_hn2_poly(n)
s_poles = s_poles_derivative_hn2_poly(n)
return s_zeros, s_poles, 1
def phaseshift_timedelay(delay, w):
return np.exp(-1j * 2 * np.pi * w * delay)
def phaseshift_sampledelay(n, w, fs):
return phaseshift_timedelay(delay=n/fs, w=w)
def log_frequency(fmin, fmax, num_f, endpoint=True):
return np.logspace(np.log10(fmin), np.log10(fmax), num=num_f,
endpoint=endpoint)
def impulse_invariance(r, p, k, L_fir, n_center, fs, mode, window=None):
"""
Impulse invariance method.
Parameters
----------
r : array_like
Residues.
p : array_like
Poles.
k : float
Direct throughput.
L_fir : int
FIR length.
n_center : int
Sample index at which the IIR fitler begins (pre-delay).
fs : int
Sampling frequency in Hertz.
mode : string
{"uncorrected", "corrected", "dcmatched", "bandlimited",
"dcmbandlimited"}.
window : array_like, optional
Tapering window.
Returns
-------
IIR : list
List of FOS and SOS section filters.
FIR : array_like
FIR coefficients.
n_center : int
Sample index at which the IIR fitler begins (pre-delay).
"""
# p_cplx, p_real = cplxreal(p)
# num_real = len(p_real)
# r_cplx, r_real = r[num_real::2], r[:num_real]
tol = 1e-10
idx_real = (np.abs(np.imag(p)) < tol)
p_real = p[idx_real]
r_real = r[idx_real]
# num_real = len(p_real)
p_cplx = p[~idx_real]
r_cplx = r[~idx_real]
idx_sort = np.argsort(p_cplx.real)
p_cplx = p_cplx[idx_sort[::2]]
r_cplx = r_cplx[idx_sort[::2]]
filters_real = [fos_real_pole(ri, pi, L_fir, n_center, fs, mode, window)
for (ri, pi) in zip(r_real, p_real)]
filters_cplx = [sos_cplx_poles(ri, pi, L_fir, n_center, fs, mode, window)
for (ri, pi) in zip(r_cplx, p_cplx)]
IIR = []
FIR = np.zeros(L_fir)
for filt in filters_real:
IIR.append((filt[0], filt[1]))
FIR += filt[2]
for filt in filters_cplx:
IIR.append((filt[0], filt[1]))
FIR += filt[2]
if len(k) == 1:
FIR[n_center] += k[0]
return IIR, FIR, n_center
def impulse_invariance_least_square(
residues, poles, gain, Lfir, n0, f_control, fs):
"""
Band-limited impulse invariance method using least squares fit.
"""
T = 1/fs
w = 2*np.pi*f_control
jw = 1j*w
# z1 = np.exp(-1j*w*T)
# IIR part: computed by using the conventional impulse invariance method
IIR, _, _ = impulse_invariance(
residues, poles, gain, 1, 0, fs, mode='uncorrected')
H_iir = np.zeros_like(f_control, dtype=complex)
for iir in IIR:
H_iir += freqz(iir[0], iir[1], worN=f_control, fs=fs)[1]
H_iir *= phaseshift_sampledelay(n0, f_control, fs)
H_ref = np.zeros_like(f_control, dtype=complex)
for (r, p) in zip(residues, poles):
H_ref += r/(jw - p)
H_ref *= phaseshift_sampledelay(n0, f_control, fs)
a = H_ref - H_iir # difference between the target spectrum and IIR model
# Discrete-Time Fourier transform matrix
W = np.exp(-jw[:, np.newaxis]*np.arange(Lfir)*T)
# FIR coefficients
FIR = np.linalg.inv((np.conjugate(W.T)@W).real) \
@ (np.conjugate(W.T)@a).real
# FIR = np.linalg.lstsq(W, a)
return IIR, FIR, n0
def fos_real_pole(r, p, L_fir, n_center, fs, mode, window=None):
"""
Impulse invariance method applied to a first-order section filter
with a real pole.
Parameters
----------
r : float
residue.
p : float
pole.
L_fir : int
FIR length.
n_center : int
Sample index where the IIR fitler begins (pre-delay).
fs : int
Sampling frequency in Hertz.
mode : string
{"uncorrected", "corrected", "dcmatched", "bandlimited",
"dcmbandlimited"}.
window : array_like, optional
Tapering window.
Returns
-------
list
Numerator coefficients.
list
Denominator coefficients.
FIR : array_like
FIR coefficients.
"""
T = 1/fs
rd = r.real*T
pd = np.exp(p.real*T)
# First-order section
b0 = rd
a0 = 1.
a1 = -pd
# if mode in {'bandlimited', 'dcmbandlimited'}:
# n = np.arange(L_fir) - n_center
# blexres = bandlimited_decaying_exponential(r, p, n/fs, fs,
# residual=True)
# FIR = T * blexres.real
# if window is not None:
# FIR *= window
# if mode == 'dcmbandlimited':
# FIR[n_center] -= (r/p + rd/(1-pd)).real + np.sum(FIR)
if mode == 'corrected':
FIR = np.zeros(L_fir)
FIR[n_center] = -0.5 * rd
elif mode == 'uncorrected':
FIR = np.zeros(L_fir)
elif mode == 'dcmatched':
FIR = np.zeros(L_fir)
FIR[n_center] = -(r/p + rd/(1-pd)).real
elif mode == 'nyquistmatched':
FIR = np.zeros(L_fir)
FIR[n_center] = np.abs(r/(1j*np.pi*fs-p)) - rd/(1+pd)
else:
FIR = np.zeros(L_fir)
return [b0], [a0, a1], FIR
def sos_cplx_poles(r, p, L_fir, n_center, fs, mode, window=None):
"""
Impulse invariance method applied to a second-order section filter
with complex conjugate poles.
Parameters
----------
r : complex
One from the complex conjugate pole. This must corresponds to the
pole given in the second argument.
r: array_like
One from the complex conjugate residue. This must corresponds to the
residue given in the first argument.
L_fir : int
FIR length.
n_center : int
Sample index where the IIR fitler begins (pre-delay).
fs : int
Sampling frequency in Hertz.
mode : string
{"uncorrected", "corrected", "dcmatched", "bandlimited",
"dcmbandlimited"}.
window : array_like, optional
Tapering window.
Returns
-------
list
Numerator coefficients.
list
Denominator coefficients.
FIR : array_like
FIR coefficients.
"""
T = 1/fs
rd = r*T
pd = np.exp(p*T)
# Second-order section
b0 = 2 * rd.real
b1 = -2 * (rd.conj() * pd).real
a0 = 1.
a1 = -2 * np.exp(p.real*T) * np.cos(p.imag*T)
# a2 = np.exp(2 * p.real * T)
a2 = (pd * pd.conj()).real
# if mode in {'bandlimited', 'dcmbandlimited'}:
# n = np.arange(L_fir) - n_center
# blexres = bandlimited_decaying_sinusoid(r, p, n/fs, fs,
# residual=True)
# FIR = T * blexres
# if window is not None:
# FIR *= window
# if mode == 'dcmbandlimited':
# FIR[n_center] -= 2 * (r/p + rd/(1-pd)).real + np.sum(FIR)
if mode == 'corrected':
FIR = np.zeros(L_fir)
FIR[n_center] = - rd.real
elif mode == 'uncorrected':
FIR = np.zeros(L_fir)
elif mode == 'dcmatched':
FIR = np.zeros(L_fir)
FIR[n_center] = -2 * (r/p + rd/(1-pd)).real
elif mode == 'nyquistmatched':
FIR = np.zeros(L_fir)
FIR[n_center] = \
(np.abs(r/(1j*np.pi*fs-p) + r.conj()/(1j*np.pi*fs-p.conj()))
- rd/(1+pd) - rd.conj()/(1+pd.conj())).real
return [b0, b1], [a0, a1, a2], FIR