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.
tfwaves/waveform.py ADDED
@@ -0,0 +1,369 @@
1
+ #! /usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+
4
+ """
5
+ Module for time and frequency domain waveforms.
6
+ Authors:
7
+ Quentin Baghi <quentin.baghi@protonmail.com>
8
+ Saptarshi Ghosh <saptarshi.ghosh@apc.in2p3.fr>
9
+ Gael Servignat <servignat@apc.in2p3.fr>
10
+ """
11
+
12
+ import jax.numpy as jnp
13
+ from scipy import special
14
+
15
+
16
+ class Waveform:
17
+ """
18
+ A class representing a waveform in time domain.
19
+ This is a base class that can be extended for specific waveform implementations.
20
+ """
21
+
22
+ def __init__(self, *args, **kwargs):
23
+ """
24
+ Initialize the waveform with given parameters.
25
+ This method should be overridden by subclasses to set specific attributes.
26
+ """
27
+
28
+ def update_parameters(self, *args, **kwargs):
29
+ """
30
+ Update the waveform parameters.
31
+ This method should be overridden by subclasses to implement specific logic.
32
+ """
33
+ raise NotImplementedError("This method should be implemented by subclasses.")
34
+
35
+ def compute_amplitudes(self, *args, **kwargs):
36
+ """
37
+ Compute the amplitudes from the waveform parameters.
38
+ Placeholder for actual implementation.
39
+ """
40
+ raise NotImplementedError("This method should be implemented by subclasses.")
41
+
42
+ def compute_phases(self, *args, **kwargs):
43
+ """
44
+ Compute the phases from the waveform parameters.
45
+ Placeholder for actual implementation.
46
+ """
47
+ raise NotImplementedError("This method should be implemented by subclasses.")
48
+
49
+ def compute_strain(self, *args, **kwargs):
50
+ """
51
+ Compute the complex strain in the source frame.
52
+ Placeholder for actual implementation.
53
+ """
54
+ raise NotImplementedError("This method should be implemented by subclasses.")
55
+
56
+ def compute_amplitudes_and_phases_derivatives(self, *args, **kwargs):
57
+ """
58
+ Compute amplitudes, phases, frequencies, and frequency derivatives all in a row
59
+
60
+ Parameters
61
+ ----------
62
+ tau : ndarray
63
+ STFT times
64
+
65
+ Returns
66
+ -------
67
+ a_tau, phi_tau, f_tau, df_tau
68
+ phases, frequencies, frequency derivatives
69
+ """
70
+
71
+ raise NotImplementedError("This method should be implemented by subclasses.")
72
+
73
+ def compute_polarization_angle(self, *args, **kwargs):
74
+ """
75
+ Compute the polarization angle. May not be relevant for all waveforms.
76
+ Placeholder for actual implementation.
77
+ """
78
+ raise NotImplementedError("This method should be implemented by subclasses.")
79
+
80
+
81
+ class TDWaveform(Waveform):
82
+ """
83
+ A class representing a waveform in time domain.
84
+ This is a base class that can be extended for specific waveform implementations.
85
+ """
86
+
87
+ def compute_frequencies(self, *args, **kwargs):
88
+ """
89
+ Compute arm responses from the waveform.
90
+ Placeholder for actual implementation.
91
+ """
92
+ raise NotImplementedError("This method should be implemented by subclasses.")
93
+
94
+ def compute_frequencies_derivatives(self, *args, **kwargs):
95
+ """
96
+ Compute the derivatives of the frequencies from the waveform.
97
+ Placeholder for actual implementation.
98
+ """
99
+ raise NotImplementedError("This method should be implemented by subclasses.")
100
+
101
+ def compute_amplitudes_and_phases_derivatives(self, *args, **kwargs):
102
+ """
103
+ Compute amplitudes, phases, frequencies, and frequency derivatives all in a row
104
+
105
+ Parameters
106
+ ----------
107
+ tau : ndarray
108
+ STFT times
109
+
110
+ Returns
111
+ -------
112
+ a_tau, phi_tau, f_tau, df_tau
113
+ phases, frequencies, frequency derivatives
114
+ """
115
+
116
+ return (
117
+ self.compute_amplitudes(*args, **kwargs),
118
+ self.compute_phases(*args, **kwargs),
119
+ self.compute_frequencies(*args, **kwargs),
120
+ self.compute_frequencies_derivatives(*args, **kwargs),
121
+ )
122
+
123
+
124
+ class FDWaveform(Waveform):
125
+ """
126
+ A class representing a waveform in time domain.
127
+ This is a base class that can be extended for specific waveform implementations.
128
+ """
129
+
130
+ def __init__(self, t_obs, *args, **kwargs):
131
+ """
132
+ Initialize the linear chirp waveform with given parameters.
133
+
134
+ t_obs : float
135
+ Total time of observation of the signal.
136
+ """
137
+ super().__init__(*args, **kwargs)
138
+
139
+ self.t_obs = t_obs
140
+
141
+ def compute_times(self, *args, **kwargs):
142
+ """
143
+ Compute arm responses from the waveform.
144
+ Placeholder for actual implementation.
145
+ """
146
+ raise NotImplementedError("This method should be implemented by subclasses.")
147
+
148
+ def compute_times_derivatives(self, *args, **kwargs):
149
+ """
150
+ Compute the derivatives of the frequencies from the waveform.
151
+ Placeholder for actual implementation.
152
+ """
153
+ raise NotImplementedError("This method should be implemented by subclasses.")
154
+
155
+ def compute_amplitudes_and_phases_derivatives(self, *args, **kwargs):
156
+ """
157
+ Compute amplitudes, phases, times, and times derivatives all in a row
158
+
159
+ Parameters
160
+ ----------
161
+ freq : ndarray
162
+ STFT frequencies
163
+
164
+ Returns
165
+ -------
166
+ a_tau, phi_tau, tau_f, dtau_f
167
+ phases, times, times derivatives
168
+ """
169
+
170
+ return (
171
+ self.compute_amplitudes(*args, **kwargs),
172
+ self.compute_phases(*args, **kwargs),
173
+ self.compute_times(*args, **kwargs),
174
+ self.compute_times_derivatives(*args, **kwargs),
175
+ )
176
+
177
+
178
+ class LinearChirp(TDWaveform):
179
+ """
180
+ A class representing a linear chirp waveform.
181
+ This is a specific implementation of the Waveform class.
182
+ """
183
+
184
+ def __init__(self, *args, f0=None, fdot0=None, **kwargs):
185
+ """
186
+ Initialize the linear chirp waveform with given parameters.
187
+ """
188
+ super().__init__(*args, **kwargs)
189
+ # Frequency and frequency derivative parameters
190
+ self.f0 = f0
191
+ self.fdot0 = fdot0
192
+ # Count the number of modes
193
+ if jnp.ndim(f0) == 0:
194
+ self.n_modes = 1
195
+ else:
196
+ self.n_modes = len(f0)
197
+ # Make the frequency and its derivative arrays
198
+ self.f0 = f0[:, jnp.newaxis] # shape (n_modes, 1)
199
+ self.fdot0 = fdot0[:, jnp.newaxis] # shape (n_modes, 1)
200
+
201
+ def update_parameters(self, *args, **kwargs):
202
+ """
203
+ Update the parameters of the linear chirp waveform.
204
+ This method sets the initial frequency and frequency derivative.
205
+ """
206
+
207
+ self.f0 = args[0]
208
+ self.fdot0 = args[1]
209
+
210
+ def compute_amplitudes(self, *args, **kwargs):
211
+ """
212
+ Compute the amplitudes for the linear chirp waveform.
213
+ Returns an array of size n_modes x n_times
214
+ """
215
+ # The first argument is time
216
+ tau = args[0]
217
+ if self.n_modes > 1:
218
+ return jnp.ones((self.n_modes, len(tau)))
219
+ # Implement the logic for computing amplitudes for linear chirp
220
+ return jnp.ones_like(tau)
221
+
222
+ def compute_phases(self, *args, **kwargs):
223
+ """
224
+ Construct the phase and amplitude splines for the linear chirp waveform.
225
+ """
226
+ # The first argument is time
227
+ tau = args[0]
228
+ # Implement the logic for computing phases and amplitudes for linear chirp
229
+ return 2 * jnp.pi * self.f0 * tau + jnp.pi * self.fdot0 * tau**2
230
+
231
+ def compute_frequencies(self, *args, **kwargs):
232
+ """
233
+ Compute the frequencies for the linear chirp waveform.
234
+ """
235
+ # The first argument is time
236
+ tau = args[0]
237
+ # Implement the logic for computing frequencies for linear chirp
238
+ return self.f0 + self.fdot0 * tau
239
+
240
+ def compute_frequencies_derivatives(self, *args, **kwargs):
241
+ """
242
+ Compute the derivatives of the frequencies for the linear chirp waveform.
243
+ """
244
+ # The first argument is time
245
+ tau = args[0]
246
+ if self.n_modes > 1:
247
+ return self.fdot0 * jnp.ones((self.n_modes, len(tau)))
248
+ # Implement the logic for computing frequency derivatives for linear chirp
249
+ return self.fdot0 * jnp.ones_like(tau)
250
+
251
+ def compute_strain(self, *args, **kwargs):
252
+ """
253
+ Compute the complex strain in the source frame for the linear chirp waveform.
254
+ """
255
+ # The first argument is time
256
+ tau = args[0]
257
+ # Implement the logic for computing strain for linear chirp
258
+ phases = self.compute_phases(tau)
259
+ amplitudes = self.compute_amplitudes(tau)
260
+ if self.n_modes > 1:
261
+ return jnp.sum(amplitudes * jnp.exp(-1j * phases), axis=0)
262
+ return amplitudes * jnp.exp(-1j * phases)
263
+
264
+ def compute_polarization_angle(self, *args, **kwargs):
265
+ """
266
+ Return an arbitrary polarization angle.
267
+
268
+ Returns
269
+ -------
270
+ psi : float
271
+ Polarization angle in radians.
272
+ """
273
+ return 0.0
274
+
275
+
276
+ class FDLinearChirp(FDWaveform):
277
+ """
278
+ Representation of a linear chirp in frequency domain.
279
+ This inherits from the more general FDWaveform class.
280
+ """
281
+
282
+ def __init__(self, *args, f0=None, fdot0=None, **kwargs):
283
+ """
284
+ Initialize the linear chirp waveform with given parameters.
285
+ """
286
+ super().__init__(*args, **kwargs)
287
+ # Frequency and frequency derivative parameters
288
+ self.f0 = f0
289
+ self.fdot0 = fdot0
290
+ self.freq = jnp.array([self.f0, self.f0 + self.t_obs * self.fdot0])
291
+
292
+ def update_parameters(self, *args, **kwargs):
293
+ """
294
+ Update the parameters of the linear chirp waveform.
295
+ This method sets the initial frequency and frequency derivative.
296
+ """
297
+
298
+ self.f0 = args[0]
299
+ self.fdot0 = args[1]
300
+
301
+ def compute_amplitudes(self, *args, **kwargs):
302
+ """
303
+ Compute the amplitudes for the linear chirp waveform.
304
+ """
305
+ # The first argument is time
306
+ freq = args[0]
307
+ # Implement the logic for computing amplitudes for linear chirp
308
+ return jnp.ones_like(freq) / jnp.sqrt(self.fdot0)
309
+
310
+ def compute_phases(self, *args, **kwargs):
311
+ """
312
+ Compute the phase for the linear chirp waveform.
313
+ """
314
+ # The first argument is time
315
+ freq = args[0]
316
+ return -jnp.pi / 4.0 + jnp.pi / self.fdot0 * (self.f0 - freq) ** 2.0
317
+
318
+ def compute_times(self, *args, **kwargs):
319
+ """
320
+ Compute the frequencies for the linear chirp waveform.
321
+ """
322
+ # The first argument is time
323
+ freq = args[0]
324
+ # Implement the logic for computing times for linear chirp
325
+ return (freq - self.f0) / self.fdot0
326
+
327
+ def compute_times_derivatives(self, *args, **kwargs):
328
+ """
329
+ Compute the derivatives of the frequencies for the linear chirp waveform.
330
+ """
331
+ # The first argument is time
332
+ freq = args[0]
333
+ # Implement the logic for computing times derivatives for linear chirp
334
+ return jnp.ones_like(freq) / self.fdot0
335
+
336
+ def compute_strain(self, *args, **kwargs):
337
+ """
338
+ Compute the complex strain in the source frame for the linear chirp waveform.
339
+ """
340
+ # The first argument is time
341
+ freq = args[0]
342
+ # Implement the logic for computing strain for linear chirp
343
+ phases = self.compute_phases(freq)
344
+ amplitudes = self.compute_amplitudes(freq)
345
+ return amplitudes * jnp.exp(-1j * phases)
346
+
347
+ def compute_windowing_factor(self, *args):
348
+ """
349
+ Compute the erf factor due to windowing the signal in [0,T] interval.
350
+ """
351
+ # The first argument is time
352
+ freq = args[0]
353
+
354
+ sqrtminusi = jnp.exp(-1j * jnp.pi / 4.0)
355
+ diff_freq = (self.f0 - freq) / self.fdot0
356
+ arg_erf_plus = jnp.sqrt(jnp.pi * self.fdot0) * sqrtminusi * (self.t_obs + diff_freq)
357
+ arg_erf_minus = jnp.sqrt(jnp.pi * self.fdot0) * sqrtminusi * diff_freq
358
+ return jnp.asarray(0.5 * (special.erf(arg_erf_plus) - special.erf(arg_erf_minus)))
359
+
360
+ def compute_polarization_angle(self, *args, **kwargs):
361
+ """
362
+ Return an arbitrary polarization angle.
363
+
364
+ Returns
365
+ -------
366
+ psi : float
367
+ Polarization angle in radians.
368
+ """
369
+ return 0.0