-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
294 lines (237 loc) · 6.39 KB
/
utils.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
import numpy as np
import scipy.signal as sig
# Perfect Sweep
def perfect_sweep(N):
"""
generate_PerfectSweep returns a periodic perfect sweep
Parametrs
---------
N : int
length of the perfect sequence / sample
Returns
p : array
perfect_sweep
"""
m = np.arange(0, np.ceil(N/2+1))
P_half = np.exp(-1j * 2 * np.pi / N * m**2)
return np.real(np.fft.irfft(P, n=N))
def perfect_sequence_randomphase(N):
"""
Parametrs
---------
N : int
length of the perfect sequence / sample
Returns
p : array
perfect_sweep
"""
m = np.arange(0, np.ceil(N/2+1))
phase = 2 * np.pi * np.random.random(len(m))
phase[0] = 0
P_half = np.exp(-1j * phase)
if (N % 2) == 0:
P_half[-1] = 1
return np.fft.irfft(P_half, n=N)
def cconv(x, y, N=None):
return np.fft.irfft( np.fft.rfft(x, n=N) * np.fft.rfft(y, n=N), n=N)
def cxcorr(x, y, N=None):
return np.fft.irfft(np.fft.rfft(x) * np.fft.rfft(np.roll(y[::-1],1)))
def time_reverse(x):
N = len(x)
return np.roll(x,-1)[N-1::-1]
def db(x):
return 20*np.log10(np.abs(x))
def lagr_poly(ni, n):
"""Lagrange polynomail of order n
Parameters
----------
ni : array
Sequences
n : scalar
input
Returns
-------
h : array
Lagrange polynomial
Notes
-----
"""
N = len(ni)
h = np.zeros(N)
for m in range(N):
nm = ni[m]
idx = np.concatenate([np.arange(0, m), np.arange(m+1, N)])
h[m] = np.prod((n - ni[idx])/(nm - ni[idx]))
return h
def fdfilt_lagr(tau, Lf, fs):
"""
Parameters
----------
tau : delay / s
Lf : length of the filter / sample
fs : sampling rate / Hz
Returns
-------
h : (Lf)
nonzero filter coefficients
ni : time index of the first element of h
n0 : time index of the center of h
"""
d = tau * fs
if Lf % 2 == 0:
n0 = np.ceil(d)
Lh = int(Lf/2)
idx = np.arange(n0-Lh, n0+Lh).astype(int)
elif Lf % 2 == 1:
n0 = np.round(d)
Lh = int(np.floor(Lf/2))
idx = np.arange(n0-Lh, n0+Lh+1).astype(int)
else:
print('Invalid value of Lf. Must be an integer')
return lagr_poly(idx, d), idx[0], n0
def fdfilt_sinc(tau, Lf, fs, beta=8.6):
"""
Parameters
----------
tau : delay / s
Lf : length of the filter / sample
fs : sampling rate / Hz
Returns
-------
h : (Lf)
nonzero filter coefficients
ni : time index of the first element of h
n0 : time index of the center of h
"""
d = tau * fs
w = np.kaiser(Lf, beta)
if Lf % 2 == 0:
n0 = np.ceil(d)
Lh = int(Lf/2)
idx = np.arange(n0-Lh, n0+Lh).astype(int)
elif Lf % 2 == 1:
n0 = np.round(d)
Lh = int(np.floor(Lf/2))
idx = np.arange(n0-Lh, n0+Lh+1).astype(int)
else:
print('Invalid value of Lf. Must be an integer')
return np.sinc(idx - d) * w, idx[0], n0
def fdfilter(xi, yi, x, order, type='lagrange'):
"""
Lagrange interpolation
Parameters
----------
xi :
in accending order
yi :
x :
[xmin, xmax]
Return
------
yi :
"""
N = order+1
if N%2 == 0:
Nhalf = N/2
n0 = np.searchsorted(xi, x)
idx = np.linspace(n0-Nhalf, n0+Nhalf, num=N, endpoint=False).astype(int)
elif N%2 == 1:
Nhalf = (N-1)/2
n0 = np.argmin(np.abs(xi-x))
idx = np.linspace(n0-Nhalf, n0+Nhalf+1, num=N, endpoint=False).astype(int)
else:
print('order must be an integer')
return np.dot(yi[idx], lagr_poly(xi[idx], x))
def fractional_delay(delay, Lf, fs, type):
"""
fractional delay filter
Parameters
----------
delay : array
time-varying delay in sample
Lf : int
length of the fractional delay filter
Returns
-------
waveform : array (Lf)
nonzero coefficients
shift : array (Lf)
indices of the first nonzero coefficient
offset : array (Lf)
indices of the center of the filter
"""
L = len(delay)
waveform = np.zeros((L, Lf))
shift = np.zeros(L)
offset = np.zeros(L)
if type == 'sinc':
for n in range(L):
htemp, ni, n0 = fdfilt_sinc(delay[n], Lf, fs=fs)
waveform[n, :] = htemp
shift[n] = ni
offset[n] = n0
elif type == 'lagrange':
for n in range(L):
htemp, ni, n0 = fdfilt_lagr(delay[n], Lf, fs=fs)
waveform[n, :] = htemp
shift[n] = ni
offset[n] = n0
else:
print('unknown type')
return waveform, shift, offset
def construct_ir_matrix(waveform, shift, Nh):
"""
Convert 'waveform' and 'shift' into an IR matrix
Parameters
----------
waveform : array
nonzero elements of the IRs
shift : array
indices of the first nonzero coefficients
Nh : int
length of each IRs
Returns
-------
h :
IRs
H :
TFs
Ho :
CHT spectrum
"""
L, Lf = waveform.shape
h = np.zeros((L, Nh))
for n in range(L):
idx = (np.arange(shift[n], shift[n] + Lf)).astype(int)
h[n, idx] = waveform[n,:]
H = np.fft.fft(h)
Ho = (1/L) * np.roll(np.fft.fft(H, axis=0), int(L/2), axis=0)
return h, H, Ho
def captured_signal(waveform, shift, p):
"""
Apply time-varying delay to a perfect sweep
Parameters
----------
waveform : array
nonzero filter coefficients
shift : array
indices of the first nonzero coefficients
p : array
periodic excitation signal
Returns
-------
s : array
captured signal
"""
return time_varying_delay(waveform, shift, p)
def time_varying_delay(waveform, shift, p):
"""
Apply a time varying delay to an input sequence
"""
L, Lf = waveform.shape
N = len(p)
s = np.zeros(L)
for n in range(L):
idx = np.arange(shift[n], shift[n]+Lf).astype(int)
s[n] = np.dot(p[np.mod(n - idx, N)], waveform[n, :])
return s