jaxtfwaves 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- jaxtfwaves-0.2.0.dist-info/METADATA +144 -0
- jaxtfwaves-0.2.0.dist-info/RECORD +14 -0
- jaxtfwaves-0.2.0.dist-info/WHEEL +4 -0
- jaxtfwaves-0.2.0.dist-info/entry_points.txt +2 -0
- tfplugins/__init__.py +0 -0
- tfplugins/sobhbwaveform/__init__.py +0 -0
- tfplugins/sobhbwaveform/sobhbparams.py +758 -0
- tfplugins/sobhbwaveform/sobhbwaveform_ripple.py +474 -0
- tfwaves/__init__.py +9 -0
- tfwaves/interpolation.py +265 -0
- tfwaves/response.py +633 -0
- tfwaves/stft.py +902 -0
- tfwaves/utils.py +705 -0
- tfwaves/waveform.py +369 -0
tfwaves/utils.py
ADDED
|
@@ -0,0 +1,705 @@
|
|
|
1
|
+
#! /usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
|
|
4
|
+
"""
|
|
5
|
+
Utility functions.
|
|
6
|
+
|
|
7
|
+
This module provides utility functions for usual linear algebra operations on Numpy arrays.
|
|
8
|
+
It is extracted from the LISA GW Response package.
|
|
9
|
+
|
|
10
|
+
Authors:
|
|
11
|
+
Jean-Baptiste Bayle <j2b.bayle@gmail.com>
|
|
12
|
+
Chantal Pitte <chanpitte@gmail.com>
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from typing import Callable
|
|
16
|
+
|
|
17
|
+
import jax
|
|
18
|
+
import jax.numpy as jnp
|
|
19
|
+
from jax.scipy import special
|
|
20
|
+
from scipy.interpolate import make_interp_spline
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def jax_vectorize(fn):
|
|
24
|
+
"""Decorator that mimics np.vectorize, using vmap in JAX."""
|
|
25
|
+
|
|
26
|
+
def wrapped(x):
|
|
27
|
+
x = jnp.atleast_1d(x)
|
|
28
|
+
out = jax.vmap(fn)(x)
|
|
29
|
+
if out.shape == (1,):
|
|
30
|
+
return out[0]
|
|
31
|
+
return out
|
|
32
|
+
|
|
33
|
+
return wrapped
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def dot(a, b):
|
|
37
|
+
"""Dot product on the last axis.
|
|
38
|
+
|
|
39
|
+
Args:
|
|
40
|
+
a (array-like): input array
|
|
41
|
+
b (array-like): input array
|
|
42
|
+
"""
|
|
43
|
+
return jnp.einsum("...j, ...j", a, b)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def norm(a):
|
|
47
|
+
"""Norm on the last axis.
|
|
48
|
+
|
|
49
|
+
Args:
|
|
50
|
+
a (array-like): input array
|
|
51
|
+
"""
|
|
52
|
+
return jnp.linalg.norm(a, axis=-1)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def arrayindex(x, a):
|
|
56
|
+
"""Return the array indices for ``x`` in ``a``.
|
|
57
|
+
|
|
58
|
+
>>> a = [1, 3, 5, 7]
|
|
59
|
+
>>> indices = arrayindex([3, 7, 1], a)
|
|
60
|
+
[1, 3, 0]
|
|
61
|
+
|
|
62
|
+
Args:
|
|
63
|
+
x (array-like): elements to search for
|
|
64
|
+
a (array-like): array in which elements are searched for
|
|
65
|
+
|
|
66
|
+
Returns:
|
|
67
|
+
:obj:`ndarray`: List of array indices
|
|
68
|
+
|
|
69
|
+
Raises:
|
|
70
|
+
ValueError: if not all elements of ``x`` cannot be found in ``a``.
|
|
71
|
+
"""
|
|
72
|
+
if not jnp.all(jnp.isin(x, a)):
|
|
73
|
+
raise ValueError("cannot find all items")
|
|
74
|
+
return jnp.searchsorted(a, x)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def atleast_2d(a):
|
|
78
|
+
"""View inputs as arrays with at least two dimensions.
|
|
79
|
+
|
|
80
|
+
Contrary to numpy's function, we here add the missing dimension
|
|
81
|
+
on the last axis if needed.
|
|
82
|
+
|
|
83
|
+
>>> jnp.atleast_2d(3.0)
|
|
84
|
+
array([[3.]])
|
|
85
|
+
>>> x = jnp.arange(3.0)
|
|
86
|
+
>>> jnp.atleast_2d(x)
|
|
87
|
+
array([[0., 1., 2.]])
|
|
88
|
+
>>> jnp.atleast_2d(x).base is x
|
|
89
|
+
True
|
|
90
|
+
>>> jnp.atleast_2d(1, [1, 2], [[1, 2]])
|
|
91
|
+
[array([[1]]), array([[1, 2]]), array([[1, 2]])]
|
|
92
|
+
|
|
93
|
+
Args:
|
|
94
|
+
a (array-like): input array
|
|
95
|
+
|
|
96
|
+
Returns:
|
|
97
|
+
:obj:`ndarray`: An array with ``ndim >= 2``.
|
|
98
|
+
|
|
99
|
+
Copies are avoided where possible, and views with two or more
|
|
100
|
+
dimensions are returned.
|
|
101
|
+
"""
|
|
102
|
+
a = jnp.asanyarray(a)
|
|
103
|
+
if a.ndim == 0:
|
|
104
|
+
return a.reshape(1, 1)
|
|
105
|
+
if a.ndim == 1:
|
|
106
|
+
return a[:, jnp.newaxis]
|
|
107
|
+
return a
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
### // and % operators reversed from Chantal?
|
|
111
|
+
@jax_vectorize
|
|
112
|
+
def emitter(link):
|
|
113
|
+
"""Return emitter spacecraft index from link index.
|
|
114
|
+
|
|
115
|
+
>>> emitter(12)
|
|
116
|
+
array(2)
|
|
117
|
+
>>> emitter([12, 31, 21])
|
|
118
|
+
array([2, 1, 1])
|
|
119
|
+
>>> emitter(np.array([23, 12]))
|
|
120
|
+
array([3, 2])
|
|
121
|
+
|
|
122
|
+
Args:
|
|
123
|
+
link (int): link index
|
|
124
|
+
|
|
125
|
+
Returns:
|
|
126
|
+
int: Emitter spacecraft index.
|
|
127
|
+
"""
|
|
128
|
+
return jnp.where(
|
|
129
|
+
jnp.any(jnp.array([12, 23, 31, 13, 32, 21]) == link), link % 10, jnp.nan
|
|
130
|
+
).astype(int)
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
@jax_vectorize
|
|
134
|
+
def receiver(link):
|
|
135
|
+
"""Return receiver spacecraft index from a link index.
|
|
136
|
+
|
|
137
|
+
>>> receiver(12)
|
|
138
|
+
array(1)
|
|
139
|
+
>>> receiver([12, 31, 21])
|
|
140
|
+
array([1, 3, 2])
|
|
141
|
+
>>> receiver(np.array([23, 12]))
|
|
142
|
+
array([2, 1])
|
|
143
|
+
|
|
144
|
+
Args:
|
|
145
|
+
link (int): link index
|
|
146
|
+
|
|
147
|
+
Returns:
|
|
148
|
+
int: Emitter spacecraft index.
|
|
149
|
+
"""
|
|
150
|
+
return jnp.where(
|
|
151
|
+
jnp.any(jnp.array([12, 23, 31, 13, 32, 21]) == link), link // 10, jnp.nan
|
|
152
|
+
).astype(int)
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def asymptotic_erfi(x):
|
|
156
|
+
"""
|
|
157
|
+
Computes the asymptotic expansion of the imaginary error function
|
|
158
|
+
|
|
159
|
+
Parameters
|
|
160
|
+
----------
|
|
161
|
+
x : ndarray
|
|
162
|
+
input values
|
|
163
|
+
|
|
164
|
+
Returns
|
|
165
|
+
-------
|
|
166
|
+
erfi : ndarray
|
|
167
|
+
Approximate imaginary error function for large x
|
|
168
|
+
"""
|
|
169
|
+
|
|
170
|
+
sqrtpi = jnp.sqrt(jnp.pi)
|
|
171
|
+
x = jnp.asarray(x)
|
|
172
|
+
|
|
173
|
+
return (
|
|
174
|
+
jnp.exp(x**2) * (1 / (sqrtpi * x) + 1 / (2 * sqrtpi * x**3) + 3 / (4 * sqrtpi * x**5)) + 1j
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
@jax.jit
|
|
179
|
+
def integ(freq, f_dot, x):
|
|
180
|
+
r"""
|
|
181
|
+
Computes a primitive of $\exp{-i \pi \dot{f} x^2} \exp{-2i \pi f x}$
|
|
182
|
+
|
|
183
|
+
Parameters
|
|
184
|
+
----------
|
|
185
|
+
freq : array
|
|
186
|
+
Frequency array
|
|
187
|
+
f_dot : float
|
|
188
|
+
Waveform frequency derivative
|
|
189
|
+
x : float
|
|
190
|
+
Boundary of the time window
|
|
191
|
+
"""
|
|
192
|
+
|
|
193
|
+
freq = jnp.asarray(freq)
|
|
194
|
+
f_dot = jnp.asarray(f_dot)
|
|
195
|
+
x = jnp.asarray(x)
|
|
196
|
+
|
|
197
|
+
prefact = jnp.exp(1j * jnp.pi * freq**2 / f_dot) / jnp.sqrt(2 * f_dot)
|
|
198
|
+
|
|
199
|
+
z = jnp.sqrt(2) * (freq + f_dot * x) / jnp.sqrt(f_dot)
|
|
200
|
+
|
|
201
|
+
sz, cz = special.fresnel(z)
|
|
202
|
+
|
|
203
|
+
return prefact * (cz - 1j * sz)
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
@jax.jit
|
|
207
|
+
def integ_freq(tau, tau_prime):
|
|
208
|
+
r"""
|
|
209
|
+
Computes a primitive of $\exp{-i \pi {\tau'} \nu^2} \exp{-2i \pi t \nu}$
|
|
210
|
+
|
|
211
|
+
Parameters
|
|
212
|
+
----------
|
|
213
|
+
tau : array
|
|
214
|
+
Time array
|
|
215
|
+
tau_prime : float
|
|
216
|
+
Waveform times derivative
|
|
217
|
+
x : float
|
|
218
|
+
Boundary of the time window
|
|
219
|
+
|
|
220
|
+
"""
|
|
221
|
+
|
|
222
|
+
tau = jnp.asarray(tau)
|
|
223
|
+
tau_prime = jnp.asarray(tau_prime)
|
|
224
|
+
|
|
225
|
+
abs_tau_prime = jnp.abs(tau_prime)
|
|
226
|
+
sign_tau_prime = jnp.sign(tau_prime)
|
|
227
|
+
|
|
228
|
+
prefact = jnp.exp(-1j * sign_tau_prime * jnp.pi / 4.0) / jnp.sqrt(2)
|
|
229
|
+
z = tau * jnp.sqrt(2 / abs_tau_prime)
|
|
230
|
+
|
|
231
|
+
sz, cz = special.fresnel(z)
|
|
232
|
+
return prefact * (cz + sign_tau_prime * 1j * sz)
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
def integ_asymptotic(freq, f_dot, x):
|
|
236
|
+
r"""
|
|
237
|
+
Computes a primitive of $\exp{-i \pi \dot{f} x^2} \exp{-2i \pi f x}$
|
|
238
|
+
using the asymptotic expansion for large x.
|
|
239
|
+
|
|
240
|
+
Parameters
|
|
241
|
+
----------
|
|
242
|
+
freq : array
|
|
243
|
+
Frequency array
|
|
244
|
+
f_dot : float
|
|
245
|
+
Waveform frequency derivative
|
|
246
|
+
x : float
|
|
247
|
+
Boundary of the time window
|
|
248
|
+
|
|
249
|
+
"""
|
|
250
|
+
|
|
251
|
+
freq = jnp.asarray(freq)
|
|
252
|
+
f_dot = jnp.asarray(f_dot)
|
|
253
|
+
x = jnp.asarray(x)
|
|
254
|
+
|
|
255
|
+
prefact = -jnp.exp(1j * jnp.pi * freq**2 / f_dot + 1j * jnp.pi / 4) / (2 * jnp.sqrt(f_dot))
|
|
256
|
+
z = jnp.exp(1j * jnp.pi * 3 / 4) * jnp.sqrt(jnp.pi) * (f_dot * x + freq) / jnp.sqrt(f_dot)
|
|
257
|
+
|
|
258
|
+
return prefact * asymptotic_erfi(z)
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
@jax.jit
|
|
262
|
+
def zfreq(freq, f_dot, t_start, t_end):
|
|
263
|
+
r"""
|
|
264
|
+
Computes the Fourier transform of $w(t) \exp{-i \pi \dot{f} t^2}$
|
|
265
|
+
when w(t) is rectangular.
|
|
266
|
+
|
|
267
|
+
Parameters
|
|
268
|
+
----------
|
|
269
|
+
freq : array
|
|
270
|
+
Frequency array
|
|
271
|
+
f_dot : float
|
|
272
|
+
Waveform frequency derivative
|
|
273
|
+
t_start : float
|
|
274
|
+
Window start time
|
|
275
|
+
t_end : float
|
|
276
|
+
Window end time
|
|
277
|
+
|
|
278
|
+
"""
|
|
279
|
+
|
|
280
|
+
return integ(freq, f_dot, t_end) - integ(freq, f_dot, t_start)
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
@jax.jit
|
|
284
|
+
def ztau(tau, tau_prime, t_start, t_end):
|
|
285
|
+
r"""
|
|
286
|
+
Computes the inverse Fourier transform of $W(\nu) \exp{-i \pi \tau'(f) \nu^2}$
|
|
287
|
+
when w(t) is rectangular.
|
|
288
|
+
|
|
289
|
+
Parameters
|
|
290
|
+
----------
|
|
291
|
+
tau : array
|
|
292
|
+
Time array
|
|
293
|
+
tau_prime : float
|
|
294
|
+
Waveform time derivative
|
|
295
|
+
t_start : float
|
|
296
|
+
Window start time
|
|
297
|
+
t_end : float
|
|
298
|
+
Window end time
|
|
299
|
+
|
|
300
|
+
"""
|
|
301
|
+
|
|
302
|
+
tau_plus = tau + t_end
|
|
303
|
+
tau_minus = tau + t_start
|
|
304
|
+
|
|
305
|
+
return integ_freq(tau_plus, tau_prime) - integ_freq(tau_minus, tau_prime)
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
def multiple_dot_vect(a_mat, b_vect):
|
|
309
|
+
"""
|
|
310
|
+
Performs matrix-to-vector multiplication on a array of matrices and vectors
|
|
311
|
+
|
|
312
|
+
Parameters
|
|
313
|
+
----------
|
|
314
|
+
a : ndarray
|
|
315
|
+
series of m x n matrices (array of size p x m x n)
|
|
316
|
+
b : ndarray
|
|
317
|
+
series of n x k vectors (array of size p x n)
|
|
318
|
+
|
|
319
|
+
Returns
|
|
320
|
+
-------
|
|
321
|
+
c : ndarray
|
|
322
|
+
array of size p x m containg the dot products of all vectors
|
|
323
|
+
contained in a and b.
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
"""
|
|
327
|
+
if a_mat.ndim == 3:
|
|
328
|
+
return jnp.einsum("ijk, ik -> ij", a_mat, b_vect)
|
|
329
|
+
if a_mat.ndim == 4:
|
|
330
|
+
return jnp.einsum("nijk, nik -> nij", a_mat, b_vect)
|
|
331
|
+
if a_mat.ndim == 5:
|
|
332
|
+
return jnp.einsum("mnijk, mnik -> mnij", a_mat, b_vect)
|
|
333
|
+
return jnp.dot(a_mat, b_vect)
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
def bspline_interp(
|
|
337
|
+
t: jax.Array, data: jax.Array, k: int = 5, ext: str = "zeros"
|
|
338
|
+
) -> Callable[[jax.Array], jax.Array]:
|
|
339
|
+
"""Compute B-spline interpolants with control over extrapolation mode.
|
|
340
|
+
|
|
341
|
+
Parameters
|
|
342
|
+
----------
|
|
343
|
+
t : Array-like of shape (N,)
|
|
344
|
+
Time coordinates between which to interpolate.
|
|
345
|
+
data : Array-like of shape (N,)
|
|
346
|
+
Data samples to interpolate (the spline interpolant will pass through
|
|
347
|
+
those points).
|
|
348
|
+
k : int
|
|
349
|
+
Degree of the spline.
|
|
350
|
+
ext : str, 'zeros' or 'raise'
|
|
351
|
+
Controls the extrapolation mode for elements not in the interval
|
|
352
|
+
defined by ``t``. If set to 'zeros', return 0. If set to 'raise',
|
|
353
|
+
raise a ValueError.
|
|
354
|
+
|
|
355
|
+
Returns
|
|
356
|
+
-------
|
|
357
|
+
Callable
|
|
358
|
+
Spline interpolant as a callable object.
|
|
359
|
+
|
|
360
|
+
Raises
|
|
361
|
+
------
|
|
362
|
+
ValueError
|
|
363
|
+
If the ``t`` and ``data`` sizes are not the same.
|
|
364
|
+
If the ``ext`` parameter is not 'zeros' or 'raise'.
|
|
365
|
+
"""
|
|
366
|
+
t = jnp.asanyarray(t)
|
|
367
|
+
data = jnp.asanyarray(data)
|
|
368
|
+
|
|
369
|
+
if t.size != data.shape[0]:
|
|
370
|
+
raise ValueError("time and data sizes must be the same")
|
|
371
|
+
|
|
372
|
+
scipy_interp = make_interp_spline(t, data, k=k)
|
|
373
|
+
|
|
374
|
+
# Builds the interpolant function wth zero padding
|
|
375
|
+
def interp_zeros(x: jax.Array) -> jax.Array:
|
|
376
|
+
x = jnp.asarray(x)
|
|
377
|
+
y = jnp.zeros_like(x)
|
|
378
|
+
int_indices = jnp.logical_and(x >= t[0], x <= t[-1])
|
|
379
|
+
y[int_indices] = scipy_interp(x[int_indices])
|
|
380
|
+
return y
|
|
381
|
+
|
|
382
|
+
# Builds the interpolant function with exception raising
|
|
383
|
+
def interp_raise(x: jax.Array) -> jax.Array:
|
|
384
|
+
x = jnp.asarray(x)
|
|
385
|
+
if jnp.any(x < t[0]) or jnp.any(x > t[-1]):
|
|
386
|
+
out_of_bound = x[(x < t[0]) | (x > t[-1])][0]
|
|
387
|
+
raise ValueError(
|
|
388
|
+
f"Interpolated times out of the allowed time range [{t[0]}, {t[-1]}] "
|
|
389
|
+
f"(for example, {out_of_bound} is outside the range). Use ext='zeros' "
|
|
390
|
+
"to get 0 for out-of-range values."
|
|
391
|
+
)
|
|
392
|
+
return scipy_interp(x)
|
|
393
|
+
|
|
394
|
+
if ext == "zeros":
|
|
395
|
+
return interp_zeros
|
|
396
|
+
if ext == "raise":
|
|
397
|
+
return interp_raise
|
|
398
|
+
|
|
399
|
+
raise ValueError(f"Invalid option for 'ext': got '{ext}', expected 'zeros' or 'raise'.")
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
# Dot product from Chantal
|
|
403
|
+
@jax.jit
|
|
404
|
+
def dot_custom(a_mat, b_mat):
|
|
405
|
+
"""Dot product.
|
|
406
|
+
|
|
407
|
+
Args:
|
|
408
|
+
a_mat (array): 3D array
|
|
409
|
+
b_mat (array): 2D array
|
|
410
|
+
|
|
411
|
+
Returns:
|
|
412
|
+
array: 3D multiplied array.
|
|
413
|
+
"""
|
|
414
|
+
return jnp.einsum("ijk, kl -> ijl", a_mat, b_mat)
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
### JAX-compatible interpolation routines (from Chantal's LISARing)
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
@jax.jit
|
|
421
|
+
def hermite_interp_build(x, y, periodic: bool = False):
|
|
422
|
+
"""
|
|
423
|
+
Cubic Hermite on a uniform grid using high-order slope estimates.
|
|
424
|
+
- x_p (array): scalar or array query points
|
|
425
|
+
- y (array): values on uniform grid (N,) #TODO consider logspace
|
|
426
|
+
- x (array): coordinate of y
|
|
427
|
+
- periodic: if True, use wrapped stencils for slopes
|
|
428
|
+
"""
|
|
429
|
+
|
|
430
|
+
x = jnp.asarray(x)
|
|
431
|
+
y = jnp.asarray(y)
|
|
432
|
+
h = jnp.diff(x)[0]
|
|
433
|
+
N = y.shape[0]
|
|
434
|
+
|
|
435
|
+
def periodic_branch(_):
|
|
436
|
+
def roll(a, k):
|
|
437
|
+
return jnp.roll(a, k, axis=0)
|
|
438
|
+
|
|
439
|
+
m = (-roll(y, -2) + 8 * roll(y, -1) - 8 * roll(y, 1) + roll(y, 2)) / (12 * h)
|
|
440
|
+
return m
|
|
441
|
+
|
|
442
|
+
def nonperiodic_branch(_):
|
|
443
|
+
m = jnp.zeros_like(y)
|
|
444
|
+
m = m.at[2:-2].set((-y[4:] + 8 * y[3:-1] - 8 * y[1:-3] + y[:-4]) / (12 * h))
|
|
445
|
+
m = m.at[1].set((y[2] - y[0]) / (2 * h))
|
|
446
|
+
m = m.at[-2].set((y[-1] - y[-3]) / (2 * h))
|
|
447
|
+
if N >= 5:
|
|
448
|
+
m = m.at[0].set((-25 * y[0] + 48 * y[1] - 36 * y[2] + 16 * y[3] - 3 * y[4]) / (12 * h))
|
|
449
|
+
m = m.at[-1].set(
|
|
450
|
+
(25 * y[-1] - 48 * y[-2] + 36 * y[-3] - 16 * y[-4] + 3 * y[-5]) / (12 * h)
|
|
451
|
+
)
|
|
452
|
+
elif N >= 3:
|
|
453
|
+
m = m.at[0].set((-3 * y[0] + 4 * y[1] - y[2]) / (2 * h))
|
|
454
|
+
m = m.at[-1].set((3 * y[-1] - 4 * y[-2] + y[-3]) / (2 * h))
|
|
455
|
+
return m
|
|
456
|
+
|
|
457
|
+
m = jax.lax.cond(periodic, periodic_branch, nonperiodic_branch, operand=None)
|
|
458
|
+
return x, y, m, h
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
@jax.jit
|
|
462
|
+
def hermite_interp_eval(x, y, m, h, x_p):
|
|
463
|
+
"""Evaluate Hermite cubic spline"""
|
|
464
|
+
y0, y1, m0, m1, t = extract_y0_y1_m0_m1_t(x, y, m, h, x_p)
|
|
465
|
+
|
|
466
|
+
h00 = 2 * t**3 - 3 * t**2 + 1
|
|
467
|
+
h10 = t**3 - 2 * t**2 + t
|
|
468
|
+
h01 = -2 * t**3 + 3 * t**2
|
|
469
|
+
h11 = t**3 - t**2
|
|
470
|
+
# jax.config.update("jax_enable_x64", False)
|
|
471
|
+
return h00 * y0 + h10 * h * m0 + h01 * y1 + h11 * h * m1
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
@jax.jit
|
|
475
|
+
def extract_y0_y1_m0_m1_t(x, y, m, h, x_p):
|
|
476
|
+
"""Helper function for spline evaluation"""
|
|
477
|
+
N = y.shape[0]
|
|
478
|
+
x0 = x[0]
|
|
479
|
+
u = (x_p - x0) / h
|
|
480
|
+
idx = jnp.floor(u).astype(jnp.int32)
|
|
481
|
+
idx = jnp.clip(idx, 0, N - 2)
|
|
482
|
+
t = u - idx.astype(u.dtype)
|
|
483
|
+
|
|
484
|
+
y0 = jnp.take(y, idx)
|
|
485
|
+
y1 = jnp.take(y, idx + 1)
|
|
486
|
+
m0 = jnp.take(m, idx)
|
|
487
|
+
m1 = jnp.take(m, idx + 1)
|
|
488
|
+
|
|
489
|
+
return y0, y1, m0, m1, t
|
|
490
|
+
|
|
491
|
+
|
|
492
|
+
@jax.jit
|
|
493
|
+
def spline_2d(x, y_mat, x_eval):
|
|
494
|
+
"""Spline interpolation of 2D array on 1D domain.
|
|
495
|
+
|
|
496
|
+
Args:
|
|
497
|
+
x (array): domain
|
|
498
|
+
y_mat (array): 2D array of values
|
|
499
|
+
x_eval (array): where to evaluate spline
|
|
500
|
+
|
|
501
|
+
Returns:
|
|
502
|
+
array: evaluated interpolant
|
|
503
|
+
"""
|
|
504
|
+
# y_mat (N,M)
|
|
505
|
+
if x.shape[0] == y_mat.shape[1]: # dimension sanity check
|
|
506
|
+
y_mat = y_mat.tranpose()
|
|
507
|
+
|
|
508
|
+
def spline_eval(x, y, x_eval):
|
|
509
|
+
# build spline
|
|
510
|
+
def spline(x, y, time):
|
|
511
|
+
x, y, m, h = hermite_interp_build(x, y)
|
|
512
|
+
return hermite_interp_eval(x, y, m, h, time)
|
|
513
|
+
|
|
514
|
+
return spline(x, y, x_eval)
|
|
515
|
+
|
|
516
|
+
batched_spline_eval = jax.vmap(spline_eval, in_axes=(None, 1, jnp.newaxis)) # inner vmap over M
|
|
517
|
+
results = batched_spline_eval(x, y_mat, x_eval) # shape (M,N)
|
|
518
|
+
# Transpose back to (N, M)
|
|
519
|
+
return jnp.transpose(results, (1, 0))
|
|
520
|
+
|
|
521
|
+
|
|
522
|
+
@jax.jit
|
|
523
|
+
def spline_1d(x, y, x_eval):
|
|
524
|
+
"""1D spline interpolant
|
|
525
|
+
|
|
526
|
+
Args:
|
|
527
|
+
x (array): domain
|
|
528
|
+
y (array): array of values
|
|
529
|
+
x_eval (array): where to evaluate spline
|
|
530
|
+
|
|
531
|
+
Returns:
|
|
532
|
+
array: evaluated interpolant
|
|
533
|
+
"""
|
|
534
|
+
|
|
535
|
+
# build spline
|
|
536
|
+
def spline(x, y, time):
|
|
537
|
+
x, y, m, h = hermite_interp_build(x, y)
|
|
538
|
+
return hermite_interp_eval(x, y, m, h, time)
|
|
539
|
+
|
|
540
|
+
return spline(x, y, x_eval)
|
|
541
|
+
|
|
542
|
+
|
|
543
|
+
### Utils to compute integral of G(u,f)e^{2ipifu^2}du.
|
|
544
|
+
|
|
545
|
+
|
|
546
|
+
def _fit_affine_endpoints(y_a, y_b, a, b):
|
|
547
|
+
"""Fit affine model from two endpoint values.
|
|
548
|
+
|
|
549
|
+
Broadcasting-friendly: accepts arrays of any matching shapes.
|
|
550
|
+
|
|
551
|
+
Parameters
|
|
552
|
+
----------
|
|
553
|
+
y_a, y_b : array_like
|
|
554
|
+
Function values at a and b (any shape).
|
|
555
|
+
a, b : array_like
|
|
556
|
+
Interval endpoints (must broadcast with y_a, y_b).
|
|
557
|
+
|
|
558
|
+
Returns
|
|
559
|
+
-------
|
|
560
|
+
intercept, slope : same shape as y_a, y_b
|
|
561
|
+
"""
|
|
562
|
+
width = b - a
|
|
563
|
+
slope = (y_b - y_a) / width
|
|
564
|
+
intercept = y_a - slope * a
|
|
565
|
+
return intercept, slope
|
|
566
|
+
|
|
567
|
+
|
|
568
|
+
def _j0_from_fresnel(a, b, mu, v):
|
|
569
|
+
"""J0 = integral_a^b exp(i*pi*(u-mu)^2/v) du via Fresnel C,S.
|
|
570
|
+
|
|
571
|
+
Broadcasting-friendly.
|
|
572
|
+
"""
|
|
573
|
+
v_abs = jnp.abs(v)
|
|
574
|
+
sign_v = jnp.sign(v)
|
|
575
|
+
|
|
576
|
+
z_scale = jnp.sqrt(2.0 / v_abs)
|
|
577
|
+
za = z_scale * (a - mu)
|
|
578
|
+
zb = z_scale * (b - mu)
|
|
579
|
+
|
|
580
|
+
sa, ca = special.fresnel(za)
|
|
581
|
+
sb, cb = special.fresnel(zb)
|
|
582
|
+
|
|
583
|
+
dC = cb - ca
|
|
584
|
+
dS = sb - sa
|
|
585
|
+
|
|
586
|
+
# v>0: C + iS ; v<0: C - iS
|
|
587
|
+
return jnp.sqrt(v_abs / 2.0) * (dC + 1j * sign_v * dS)
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
def _linearized_single(alpha, beta, d0, d1, a, b, t, v, q):
|
|
591
|
+
"""Compute one integral with affine approximations.
|
|
592
|
+
|
|
593
|
+
Broadcasting-friendly: all inputs must have compatible shapes.
|
|
594
|
+
"""
|
|
595
|
+
pi = jnp.pi
|
|
596
|
+
kappa = pi / v
|
|
597
|
+
qd1 = q * d1
|
|
598
|
+
mu = t - v * qd1
|
|
599
|
+
|
|
600
|
+
j0 = _j0_from_fresnel(a, b, mu, v)
|
|
601
|
+
edge = jnp.exp(1j * kappa * (b - mu) ** 2) - jnp.exp(1j * kappa * (a - mu) ** 2)
|
|
602
|
+
j1 = mu * j0 + edge / (2j * kappa)
|
|
603
|
+
|
|
604
|
+
pref = jnp.exp(2j * pi * (q * d0) + 1j * pi * (2.0 * t * qd1 - v * qd1**2))
|
|
605
|
+
return pref * (alpha * j0 + beta * j1)
|
|
606
|
+
|
|
607
|
+
|
|
608
|
+
@jax.jit
|
|
609
|
+
def linearized_quadrature_vectorized(
|
|
610
|
+
fplus,
|
|
611
|
+
fcross,
|
|
612
|
+
demi,
|
|
613
|
+
drec,
|
|
614
|
+
t_bins,
|
|
615
|
+
f_bins,
|
|
616
|
+
window_T,
|
|
617
|
+
tau_f,
|
|
618
|
+
dtau_f,
|
|
619
|
+
phase_amp,
|
|
620
|
+
):
|
|
621
|
+
"""Vectorized 4-channel replacement using broadcasting.
|
|
622
|
+
|
|
623
|
+
Parameters
|
|
624
|
+
----------
|
|
625
|
+
fplus, fcross : (t_bins.size + 1, 6)
|
|
626
|
+
Pre-evaluated modulation at time bin edges for 6 spacecraft.
|
|
627
|
+
Rows are consecutive time edges, one more than number of bins.
|
|
628
|
+
demi, drec : (t_bins.size + 1, 6)
|
|
629
|
+
Pre-evaluated delays at time bin edges for 6 spacecraft.
|
|
630
|
+
t_bins : (n_time_bins,)
|
|
631
|
+
Time bin centers.
|
|
632
|
+
window_T : scalar or (n_time_bins,)
|
|
633
|
+
STFT window duration.
|
|
634
|
+
tau_f : (2*di+1, n_time_bins)
|
|
635
|
+
t = tau(f) for each frequency and time bin.
|
|
636
|
+
dtau_f : (2*di+1, n_time_bins)
|
|
637
|
+
v = tau'(f) for each frequency and time bin.
|
|
638
|
+
phase_amp : (2*di+1, n_time_bins, 6)
|
|
639
|
+
External prefactor for each (frequency, time, spacecraft).
|
|
640
|
+
f_bins : (2*di+1, n_time_bins)
|
|
641
|
+
Factor q used in exp(2 i pi q d(u)).
|
|
642
|
+
Use q=1 when d is already phase-like.
|
|
643
|
+
Use q=f when d is a physical delay.
|
|
644
|
+
|
|
645
|
+
Returns
|
|
646
|
+
-------
|
|
647
|
+
(4, 2*di+1, n_time_bins, 6) complex array ordered as:
|
|
648
|
+
[plusemi, plusrec, crossemi, crossrec]
|
|
649
|
+
"""
|
|
650
|
+
|
|
651
|
+
# Interval bounds for each time bin: (n_time,)
|
|
652
|
+
a_bins = t_bins - 0.5 * window_T
|
|
653
|
+
b_bins = t_bins + 0.5 * window_T
|
|
654
|
+
|
|
655
|
+
# Slice consecutive edge samples: (n_time, 6)
|
|
656
|
+
y_a_plus = fplus[:-1, :]
|
|
657
|
+
y_b_plus = fplus[1:, :]
|
|
658
|
+
y_a_cross = fcross[:-1, :]
|
|
659
|
+
y_b_cross = fcross[1:, :]
|
|
660
|
+
y_a_demi = demi[:-1, :]
|
|
661
|
+
y_b_demi = demi[1:, :]
|
|
662
|
+
y_a_drec = drec[:-1, :]
|
|
663
|
+
y_b_drec = drec[1:, :]
|
|
664
|
+
|
|
665
|
+
# Fit affine models using broadcasting
|
|
666
|
+
# Add dimension for broadcasting with spacecraft: (n_time, 1)
|
|
667
|
+
a_broadcast = a_bins[:, None]
|
|
668
|
+
b_broadcast = b_bins[:, None]
|
|
669
|
+
|
|
670
|
+
# Fit for all (time, spacecraft) pairs at once: output (n_time, 6)
|
|
671
|
+
alpha_p, beta_p = _fit_affine_endpoints(y_a_plus, y_b_plus, a_broadcast, b_broadcast)
|
|
672
|
+
alpha_c, beta_c = _fit_affine_endpoints(y_a_cross, y_b_cross, a_broadcast, b_broadcast)
|
|
673
|
+
d0_e, d1_e = _fit_affine_endpoints(y_a_demi, y_b_demi, a_broadcast, b_broadcast)
|
|
674
|
+
d0_r, d1_r = _fit_affine_endpoints(y_a_drec, y_b_drec, a_broadcast, b_broadcast)
|
|
675
|
+
|
|
676
|
+
# Prepare for broadcasting over (freq, time, spacecraft)
|
|
677
|
+
# Affine params: (n_time, 6) -> (1, n_time, 6)
|
|
678
|
+
alpha_p = alpha_p[None, :, :]
|
|
679
|
+
beta_p = beta_p[None, :, :]
|
|
680
|
+
alpha_c = alpha_c[None, :, :]
|
|
681
|
+
beta_c = beta_c[None, :, :]
|
|
682
|
+
d0_e = d0_e[None, :, :]
|
|
683
|
+
d1_e = d1_e[None, :, :]
|
|
684
|
+
d0_r = d0_r[None, :, :]
|
|
685
|
+
d1_r = d1_r[None, :, :]
|
|
686
|
+
|
|
687
|
+
# Interval bounds: (n_time,) -> (1, n_time, 1)
|
|
688
|
+
a = a_bins[None, :, None]
|
|
689
|
+
b = b_bins[None, :, None]
|
|
690
|
+
|
|
691
|
+
# t, v: (n_freq, n_time) -> (n_freq, n_time, 1)
|
|
692
|
+
t = tau_f[:, :, None]
|
|
693
|
+
v = dtau_f[:, :, None]
|
|
694
|
+
|
|
695
|
+
# q factor in exp(2 i pi q d)
|
|
696
|
+
q = f_bins[:, :, None]
|
|
697
|
+
|
|
698
|
+
# Compute all 4 channels via broadcasting: output (n_freq, n_time, 6) each
|
|
699
|
+
plusemi = _linearized_single(alpha_p, beta_p, d0_e, d1_e, a, b, t, v, q) * phase_amp
|
|
700
|
+
plusrec = _linearized_single(alpha_p, beta_p, d0_r, d1_r, a, b, t, v, q) * phase_amp
|
|
701
|
+
crossemi = _linearized_single(alpha_c, beta_c, d0_e, d1_e, a, b, t, v, q) * phase_amp
|
|
702
|
+
crossrec = _linearized_single(alpha_c, beta_c, d0_r, d1_r, a, b, t, v, q) * phase_amp
|
|
703
|
+
|
|
704
|
+
# Stack to (4, n_freq, n_time, 6)
|
|
705
|
+
return jnp.stack([plusemi, plusrec, crossemi, crossrec], axis=0)
|