m-thesis-introduction/simulations/lib/signals/signal.py

101 lines
2.7 KiB
Python

"""
Define the super Signal class
"""
import numpy as np
class Signal():
"""
An arbitrary signal that can be translated to another position and time.
Note that position can be of any length.
Super object, cannot be used directly.
"""
def __init__(self, t_0 = 0, x_0 = 0, velocity=None):
"""
Parameters
----------
t_0 : float, optional
Time that this signal is sent out.
x_0 : float, optional
Location that this signal is sent out from.
velocity : float, optional
Defaults to the speed of light in m/s.
"""
self.velocity = 299792458 if velocity is None else velocity # m / s
self.x_0 = x_0 # m
self.t_0 = t_0 # s
def __call__(self, t_f = None, x_f = None, **kwargs):
"""
Allow this class to be used as a function.
"""
return self._translate(t_f, x_f, **kwargs)[0]
def _translate(self, t_f = None, x_f = None, t_0 = None, x_0 = None, velocity = None):
"""
Translate the signal from (t_0, x_0) to (t_f, x_f) with optional velocity.
Returns the signal at (t_f, x_f)
"""
raise NotImplementedError
def spatial_time_offset(self, x_f, x_0=None, velocity=None):
"""
Calculate the time offset caused by a spatial distance.
"""
if velocity is None:
velocity = self.velocity
if x_0 is None:
x_0 = self.x_0
return np.sum(np.sqrt( (x_f - x_0)**2 )/velocity)
def temporal_time_offset(self, t_f, t_0=None):
"""
Calculate the time offset caused by a temporal distance.
"""
if t_0 is None:
t_0 = self.t_0
return t_f - t_0
def total_time_offset(self, t_f = None, x_f = None, t_0 = None, x_0 = None, velocity = None):
"""
Calculate how much time shifting is needed to go from (t_0, x_0) to (t_f, x_f).
Convention:
(t_0, x_0) < (t_f, x_0) gives a positive time shift,
(t_0, x_0) != (t_0, x_f) gives a negative time shift
Returns:
the time shift
"""
## spatial offset
if x_f is None:
spatial_time_offset = 0
else:
x_f = np.asarray(x_f)
if x_0 is None:
x_0 = self.x_0
spatial_time_offset = self.spatial_time_offset(x_f, x_0=x_0, velocity=velocity)
## temporal offset
if t_f is None:
temporal_time_offset = 0
else:
t_f = np.asarray(t_f)
if t_0 is None:
t_0 = self.t_0
temporal_time_offset = self.temporal_time_offset(t_f, t_0=t_0)
return temporal_time_offset - spatial_time_offset