From c6d527a3076a484f2ea5378ca5d749f53d16ba1c Mon Sep 17 00:00:00 2001 From: Eric Teunis de Boone Date: Fri, 8 Apr 2022 15:16:12 +0200 Subject: [PATCH] Simu: Let Signal have default final positions --- simulations/lib/signals/digitisedsignal.py | 9 ++- simulations/lib/signals/signal.py | 68 +++++++++++++++++----- 2 files changed, 59 insertions(+), 18 deletions(-) diff --git a/simulations/lib/signals/digitisedsignal.py b/simulations/lib/signals/digitisedsignal.py index 59d683d..ea276de 100755 --- a/simulations/lib/signals/digitisedsignal.py +++ b/simulations/lib/signals/digitisedsignal.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 + import numpy as np import scipy.interpolate as interp @@ -14,7 +15,7 @@ class DigitisedSignal(Signal): Model an arbitrary digitised signal that can be translated to another position and time. """ - def __init__(self, signal, sample_rate, t_0 = 0, x_0 = 0, periodic=True, interp1d_kw = None, velocity=None): + def __init__(self, signal, sample_rate, t_0 = 0, x_0 = 0, periodic=True, interp1d_kw = None, velocity=None, t_f = None, x_f = None): """ Initialise by saving the raw signal @@ -37,8 +38,12 @@ class DigitisedSignal(Signal): Dictionary will be entered in as **kwargs. velocity : float, optional Defaults to the speed of light in m/s. + t_f : float, optional + Default time that this signal is received. + x_f : float, optional + Default Location that this signal is received. """ - super().__init__(t_0=t_0, x_0=x_0, velocity=velocity) + super().__init__(t_0=t_0, x_0=x_0, velocity=velocity, t_f=t_f, x_f=x_f) self.raw = np.asarray(signal) self.periodic = periodic diff --git a/simulations/lib/signals/signal.py b/simulations/lib/signals/signal.py index 095dd65..3e97e48 100644 --- a/simulations/lib/signals/signal.py +++ b/simulations/lib/signals/signal.py @@ -11,7 +11,7 @@ class Signal(): Super object, cannot be used directly. """ - def __init__(self, t_0 = 0, x_0 = 0, velocity=None): + def __init__(self, t_0 = 0, x_0 = 0, velocity=None, t_f = None, x_f = None): """ Parameters ---------- @@ -21,12 +21,27 @@ class Signal(): Location that this signal is sent out from. velocity : float, optional Defaults to the speed of light in m/s. + t_f : float, optional + Default time that this signal is received. + x_f : float, optional + Default Location that this signal is received. """ + if t_0 is None: + raise ValueError("t_0 cannot be None") + if x_0 is None: + raise ValueError("x_0 cannot be None") + + self.x_0 = np.asarray(x_0) # m + self.t_0 = np.asarray(t_0) # s self.velocity = 299792458 if velocity is None else velocity # m / s - self.x_0 = x_0 # m - self.t_0 = t_0 # s + # Default final positions + t_f = np.asarray(t_f) if t_f is not None else None + x_f = np.asarray(x_f) if x_f is not None else None + + self.x_f = x_f + self.t_f = t_f def __call__(self, t_f = None, x_f = None, **kwargs): """ @@ -43,7 +58,7 @@ class Signal(): raise NotImplementedError - def spatial_time_offset(self, x_f, x_0=None, velocity=None): + def spatial_time_offset(self, x_f=None, x_0=None, velocity=None): """ Calculate the time offset caused by a spatial distance. """ @@ -52,15 +67,27 @@ class Signal(): if x_0 is None: x_0 = self.x_0 + if x_f is None: + x_f = self.x_f + + ## make sure they are arrays + x_0 = np.asarray(x_0) if x_0 is not None else None + x_f = np.asarray(x_f) if x_f is not None else None return np.sqrt( np.sum((x_f - x_0)**2, axis=-1) )/velocity - def temporal_time_offset(self, t_f, t_0=None): + def temporal_time_offset(self, t_f=None, t_0=None): """ Calculate the time offset caused by a temporal distance. """ if t_0 is None: t_0 = self.t_0 + if t_f is None: + t_f = self.t_f + + ## make sure they are arrays + t_0 = np.asarray(t_0) if t_0 is not None else None + t_f = np.asarray(t_f) if t_f is not None else None return t_f - t_0 @@ -76,26 +103,35 @@ class Signal(): Returns: the time shift """ + # Get default values + ## starting point + if t_0 is None: + t_0 = self.t_0 + if x_0 is None: + x_0 = self.x_0 - ## spatial offset + ## final point + if x_f is None: + x_f = self.x_f + if t_f is None: + t_f = self.t_f + + ## make sure they are arrays + t_0 = np.asarray(t_0) if t_0 is not None else None + x_0 = np.asarray(x_0) if x_0 is not None else None + t_f = np.asarray(t_f) if t_f is not None else None + x_f = np.asarray(x_f) if x_f is not None else None + + # 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 + # 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