mirror of
https://gitlab.science.ru.nl/mthesis-edeboone/m-thesis-introduction.git
synced 2024-12-22 11:33:32 +01:00
Rename TravelSignal to DigitisedSignal
This commit is contained in:
parent
19ccea30ca
commit
c4062481d4
5 changed files with 104 additions and 73 deletions
|
@ -1,6 +1,6 @@
|
||||||
from . import signal
|
from . import signals
|
||||||
from . import location
|
from . import location
|
||||||
from .util import *
|
from .util import *
|
||||||
|
|
||||||
|
|
||||||
TravelSignal = signal.TravelSignal
|
TravelSignal = signals.DigitisedSignal
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
from .travelsignal import *
|
|
2
simulations/lib/signals/__init__.py
Normal file
2
simulations/lib/signals/__init__.py
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
from .signal import *
|
||||||
|
from .digitisedsignal import *
|
|
@ -1,17 +1,13 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""
|
|
||||||
Define the TravelSignal class.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import scipy.interpolate as interp
|
import scipy.interpolate as interp
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from _signal import *
|
from .signal import *
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from ._signal import *
|
from signal import *
|
||||||
|
|
||||||
class TravelSignal:
|
class DigitisedSignal(Signal):
|
||||||
"""
|
"""
|
||||||
Model an arbitrary digitised signal that can be translated to another position and time.
|
Model an arbitrary digitised signal that can be translated to another position and time.
|
||||||
"""
|
"""
|
||||||
|
@ -40,6 +36,7 @@ class TravelSignal:
|
||||||
velocity : float, optional
|
velocity : float, optional
|
||||||
Defaults to the speed of light in m/s.
|
Defaults to the speed of light in m/s.
|
||||||
"""
|
"""
|
||||||
|
super().__init__(t_0=t_0, x_0=x_0, velocity=velocity)
|
||||||
|
|
||||||
self.raw = np.asarray(signal)
|
self.raw = np.asarray(signal)
|
||||||
self.periodic = periodic
|
self.periodic = periodic
|
||||||
|
@ -48,11 +45,6 @@ class TravelSignal:
|
||||||
self.sample_length = len(self.raw)
|
self.sample_length = len(self.raw)
|
||||||
self.time_length = self.sample_length*sample_rate # s
|
self.time_length = self.sample_length*sample_rate # s
|
||||||
|
|
||||||
self.velocity = 299792458 if velocity is None else velocity # m / s
|
|
||||||
|
|
||||||
self.x_0 = x_0 # m
|
|
||||||
self.t_0 = t_0 # s
|
|
||||||
|
|
||||||
# choose interpolation method
|
# choose interpolation method
|
||||||
if not interp1d_kw:
|
if not interp1d_kw:
|
||||||
self.interp_f = None
|
self.interp_f = None
|
||||||
|
@ -83,12 +75,6 @@ class TravelSignal:
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return self.sample_length
|
return self.sample_length
|
||||||
|
|
||||||
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):
|
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.
|
Translate the signal from (t_0, x_0) to (t_f, x_f) with optional velocity.
|
||||||
|
@ -149,53 +135,6 @@ class TravelSignal:
|
||||||
|
|
||||||
return amplitude, total_time_offset
|
return amplitude, total_time_offset
|
||||||
|
|
||||||
def spatial_time(self, x_f, velocity=None, x_0=None):
|
|
||||||
"""
|
|
||||||
Calculate the time offset caused by a spatial difference.
|
|
||||||
"""
|
|
||||||
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 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(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 = t_f - t_0
|
|
||||||
|
|
||||||
return temporal_time_offset - spatial_time_offset
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
from scipy.stats import norm
|
from scipy.stats import norm
|
||||||
|
@ -210,17 +149,17 @@ if __name__ == "__main__":
|
||||||
|
|
||||||
signal = norm.pdf(time, time[len(time)//2], (time[-1] - time[0])/10)
|
signal = norm.pdf(time, time[len(time)//2], (time[-1] - time[0])/10)
|
||||||
|
|
||||||
mysignal = TravelSignal(signal, sample_rate, t_0 = t_offset, periodic=True)
|
mysignal = DigitisedSignal(signal, sample_rate, t_0 = t_offset, periodic=True)
|
||||||
mysignal2 = TravelSignal(signal, sample_rate, t_0 = t_offset, periodic=False)
|
mysignal2 = DigitisedSignal(signal, sample_rate, t_0 = t_offset, periodic=False)
|
||||||
|
|
||||||
fig, ax = plt.subplots(1, 1, figsize=(16,4))
|
fig, ax = plt.subplots(1, 1, figsize=(16,4))
|
||||||
ax.set_title("Raw and TravelSignal")
|
ax.set_title("Raw and DigitisedSignal")
|
||||||
ax.set_ylabel("Amplitude")
|
ax.set_ylabel("Amplitude")
|
||||||
ax.set_xlabel("Time")
|
ax.set_xlabel("Time")
|
||||||
|
|
||||||
ax.plot(time, signal, label='Raw signal')
|
ax.plot(time, signal, label='Raw signal')
|
||||||
ax.plot(time2, mysignal(time2) +0.5, '.-', label='TravelSignal(periodic)+0.5')
|
ax.plot(time2, mysignal(time2) +0.5, '.-', label='DigitisedSignal(periodic)+0.5')
|
||||||
ax.plot(time2, mysignal2(time2)-0.5, '.-', label='TravelSignal-0.5')
|
ax.plot(time2, mysignal2(time2)-0.5, '.-', label='DigitisedSignal-0.5')
|
||||||
|
|
||||||
ax.legend()
|
ax.legend()
|
||||||
|
|
91
simulations/lib/signals/signal.py
Normal file
91
simulations/lib/signals/signal.py
Normal file
|
@ -0,0 +1,91 @@
|
||||||
|
"""
|
||||||
|
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(self, x_f, velocity=None, x_0=None):
|
||||||
|
"""
|
||||||
|
Calculate the time offset caused by a spatial difference.
|
||||||
|
"""
|
||||||
|
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 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(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 = t_f - t_0
|
||||||
|
|
||||||
|
return temporal_time_offset - spatial_time_offset
|
Loading…
Reference in a new issue