Fix errors, Update logging & Add Signal handling

This commit is contained in:
linarphy 2024-05-22 12:04:13 +02:00
parent 92d3d4885c
commit 3af1d68122
No known key found for this signature in database
GPG key ID: E61920135EFF2295
4 changed files with 81 additions and 48 deletions

View file

@ -1,9 +1,10 @@
from __future__ import annotations
from numpy.typing import NDArray from numpy.typing import NDArray
from numpy import arange from numpy import arange
from scipy.interpolate import CubicSpline from scipy.interpolate import CubicSpline
def interpolate_abciss(signals: tuple["Signal", ...]) -> NDArray: def interpolate_abciss(signals: tuple[Signal, ...]) -> NDArray:
""" """
return the axis that would be used by the interpolate function return the axis that would be used by the interpolate function
""" """
@ -15,7 +16,7 @@ def interpolate_abciss(signals: tuple["Signal", ...]) -> NDArray:
return arange(start, end, 1 / max(rates)) return arange(start, end, 1 / max(rates))
def interpolate(signals: tuple["Signal", ...]) -> tuple["Signal", ...]: def interpolate(signals: tuple[Signal, ...]) -> tuple[Signal, ...]:
""" """
Interpolate multiple signals with a single abciss list, which has Interpolate multiple signals with a single abciss list, which has
the smallest interval and the bigget rate the smallest interval and the bigget rate
@ -29,3 +30,6 @@ def interpolate(signals: tuple["Signal", ...]) -> tuple["Signal", ...]:
] ]
return tuple(new_signals) return tuple(new_signals)
from backscattering_analyzer.signal import Signal # no circular import

View file

@ -41,8 +41,9 @@ class Analyzer:
file = self.settings.bench_file() file = self.settings.bench_file()
self.settings.log("loading bench movement") self.settings.log("loading bench movement")
try: try:
data = loadtxt(file).T
self.bench_movement = Signal( self.bench_movement = Signal(
*loadtxt(file).T, self.settings data[0], data[1], self.settings
) )
except OSError: except OSError:
raise Exception("{file} does not exist".format(file=file)) raise Exception("{file} does not exist".format(file=file))
@ -54,8 +55,9 @@ class Analyzer:
file = self.settings.mirror_file() file = self.settings.mirror_file()
self.settings.log("loading mirror movement") self.settings.log("loading mirror movement")
try: try:
data = loadtxt(file).T
self.mirror_movement = Signal( self.mirror_movement = Signal(
*loadtxt(file).T, self.settings data[0], data[1], self.settings
) )
except OSError: except OSError:
raise Exception("{file} does not exist".format(file=file)) raise Exception("{file} does not exist".format(file=file))
@ -65,9 +67,10 @@ class Analyzer:
Load excited h(t) Load excited h(t)
""" """
file = self.settings.data_file() file = self.settings.data_file()
self.setings.log("loading excited h(t)") self.settings.log("loading excited h(t)")
try: try:
self.data_signal = Signal(*loadtxt(file).T, self.settings) data = loadtxt(file).T
self.data_signal = Signal(data[0], data[1], self.settings)
except OSError: except OSError:
raise Exception("{file} does not exist".format(file=file)) raise Exception("{file} does not exist".format(file=file))
@ -78,8 +81,9 @@ class Analyzer:
file = self.settings.reference_file() file = self.settings.reference_file()
self.settings.log("loading reference h(t)") self.settings.log("loading reference h(t)")
try: try:
data = loadtxt(file).T
self.reference_signal = Signal( self.reference_signal = Signal(
*loadtxt(file).T, self.settings data[0], data[1], self.settings
) )
except OSError: except OSError:
raise Exception("{file} does not exist".format(file=file)) raise Exception("{file} does not exist".format(file=file))
@ -95,16 +99,14 @@ class Analyzer:
coupling_values = self.modelisation[ coupling_values = self.modelisation[
self.settings.coupling_name() self.settings.coupling_name()
] ]
self.coupling = array( self.coupling = [
[ Signal(
Signal( self.modelisation["freq"][0],
self.modelisation["freq"][0], coupling,
coupling, self.settings,
self.settings, )
) for coupling in coupling_values
for coupling in coupling_values ]
]
)
except OSError: except OSError:
raise Exception("{file} does not exist".format(file=file)) raise Exception("{file} does not exist".format(file=file))
@ -130,7 +132,11 @@ class Analyzer:
excitation excitation
""" """
result = zeros( result = zeros(
len(interpolate_abciss(self.movement, self.coupling[0])) len(
interpolate_abciss(
(self.movement.psd(), self.coupling[0].abs())
)
)
) )
# frequencies depends of psd result, which we do not have yet # frequencies depends of psd result, which we do not have yet
@ -150,25 +156,12 @@ class Analyzer:
# no need to redefine it each time but simpler here # no need to redefine it each time but simpler here
frequencies = factor_n.x frequencies = factor_n.x
"""
result += (
sqrt(self.settings.scattering_factor[index])
* self.settings.wavelength / ( 4 * pi )
* sqrt(
coupling_n[1]**2 * factor_n[1]
+ coupling_d[1]**2 * factor_d[1]
)**2
)
"""
result += ( result += (
sqrt(self.settings.scattering_factor[index]) sqrt(self.settings.scattering_factor[index])
* self.settings.power_in * self.settings.power_in
/ self.settings.power_out / self.settings.power_out
* ( * (coupling_n * factor_n + coupling_d * factor_d).y
coupling_n[1] * factor_n[1]
+ coupling_d[1] * factor_d[1]
).y
) )
return array( return array(
[ [

View file

@ -182,4 +182,4 @@ class Settings:
def log(self, message) -> None: def log(self, message) -> None:
if self.verbose: if self.verbose:
self.console.log(message) self.console.log(message, _stack_offset=2)

View file

@ -64,22 +64,27 @@ class Signal:
freq_y = rfft(self.y) freq_y = rfft(self.y)
index_to_remove = where(abs(freq_x) > cutoff) index_to_remove = where(abs(freq_x) > cutoff)
freq_y[index_to_remove] = 0 freq_y[index_to_remove] = 0
signal = Signal(
return Signal(
self.x, self.x,
irfft(freq_y), irfft(freq_y),
self.settings, self.settings,
) )
return signal
def plot(self) -> None:
"""
Plot the signal
"""
import matplotlib.pyplot as plt
plt.plot(self.x, self.y)
plt.show()
def abs(self) -> Signal: def abs(self) -> Signal:
""" """
Abs of the signal (hacky way) Abs of the signal (alias)
""" """
return Signal( return self.__abs__()
self.x,
abs(self.y),
self.settings,
)
def cos(self) -> Signal: def cos(self) -> Signal:
""" """
@ -106,11 +111,14 @@ class Signal:
Substract float or a signal to another Substract float or a signal to another
""" """
if isinstance(other, Signal): if isinstance(other, Signal):
signal_1, signal_2 = interpolate((self, other)) if len(other) != len(self):
signal_1, signal_2 = interpolate((self, other))
else:
signal_1, signal_2 = self, other
return Signal( return Signal(
signal_1.frequencies, signal_1.frequencies,
signal_2.value - signal_1.value, signal_2.y - signal_1.y,
self.settings, self.settings,
) )
else: else:
@ -125,11 +133,14 @@ class Signal:
Add a float or a signal to another Add a float or a signal to another
""" """
if isinstance(other, Signal): if isinstance(other, Signal):
signal_1, signal_2 = interpolate((self, other)) if len(other) != len(self):
signal_1, signal_2 = interpolate((self, other))
else:
signal_1, signal_2 = self, other
return Signal( return Signal(
signal_1.x, signal_1.x,
signal_1 + signal_2, signal_1.y + signal_2.y,
self.settings, self.settings,
) )
else: else:
@ -144,17 +155,20 @@ class Signal:
Multiply a signal by a value or another signal Multiply a signal by a value or another signal
""" """
if isinstance(other, Signal): if isinstance(other, Signal):
signal_1, signal_2 = interpolate((self, other)) if len(other) != len(self):
signal_1, signal_2 = interpolate((self, other))
else:
signal_1, signal_2 = self, other
return Signal( return Signal(
signal_1.frequencies, signal_1.frequencies,
signal_1.value * signal_2.value, signal_1.y * signal_2.y,
self.settings, self.settings,
) )
else: else:
return Signal( return Signal(
self.x, self.x,
other * self.value, other * self.y,
self.settings, self.settings,
) )
@ -168,3 +182,25 @@ class Signal:
self.y, self.y,
] ]
) )
def __abs__(self) -> Signal:
"""
Absolute value of the signal
"""
return Signal(
self.x,
abs(self.y),
self.settings,
)
def __getitem__(self, key) -> NDArray:
"""
Get an element of the signal
"""
return self.y[key]
def __len__(self) -> int:
"""
Get length of a signal
"""
return len(self.x)