Update frequency cut arg and fit precision and range

This commit is contained in:
linarphy 2024-05-31 17:50:29 +02:00
parent e391f463c4
commit 1e4dc0975b
No known key found for this signature in database
GPG key ID: E61920135EFF2295
2 changed files with 72 additions and 32 deletions

View file

@ -7,12 +7,16 @@ install(__name__)
logger = getLogger(__name__) logger = getLogger(__name__)
def show_projection(experiment: "Experiment") -> None: def show_projection(
experiment: "Experiment",
start_frequency: float | None = None,
end_frequency: float | None = None,
) -> None:
""" """
Show projection data with matplotlib Show projection data with matplotlib
""" """
logger.debug(_("showing experiment result")) logger.debug(_("showing experiment result"))
from matplotlib.pyplot import loglog, show, legend, close from matplotlib.pyplot import loglog, show, legend, close, vlines
excited = experiment.signals["excited"].psd().sqrt() excited = experiment.signals["excited"].psd().sqrt()
reference = experiment.signals["reference"].psd().sqrt() reference = experiment.signals["reference"].psd().sqrt()
@ -28,6 +32,24 @@ def show_projection(experiment: "Experiment") -> None:
(experiment.projection + reference).y, (experiment.projection + reference).y,
label="sum reference + excited", label="sum reference + excited",
) # type: ignore[ReportUnusedCallResult] ) # type: ignore[ReportUnusedCallResult]
if start_frequency is not None:
vlines(
[
start_frequency,
],
min(experiment.projection.y),
max(experiment.projection.y),
color = 'k',
) # type: ignore[ReportUnusedCallResult]
if end_frequency is not None:
vlines(
[
end_frequency,
],
min(experiment.projection.y),
max(experiment.projection.y),
color = 'k',
) # type: ignore[ReportUnusedCallResult]
legend() # type: ignore[ReportUnusedCallResult] legend() # type: ignore[ReportUnusedCallResult]
show() show()
close() close()

View file

@ -1,7 +1,7 @@
from pathlib import Path from pathlib import Path
from tomllib import load from tomllib import load
from numpy import argmin, float64, pi from numpy import argmin, float64, pi, log10
from numpy.core.function_base import logspace from numpy.core.function_base import logspace
from numpy.core.multiarray import array from numpy.core.multiarray import array
from numpy.typing import NDArray from numpy.typing import NDArray
@ -295,9 +295,11 @@ class Experiment:
def fit_factors( def fit_factors(
self, self,
start: float = 15, scatter_min: float = 1e-20,
end: float = 100, scatter_max: float = 1,
x: NDArray[float64] | None = None, start_frequency: float = 15,
end_frequency: float = 100,
precision: int = 3,
) -> dict[str, float]: ) -> dict[str, float]:
""" """
Find the best factor (first order only) to get the projection Find the best factor (first order only) to get the projection
@ -311,10 +313,14 @@ class Experiment:
excited, excited,
reference, reference,
phase, phase,
) = self.get_factors(start=start, end=end) ) = self.get_factors(start=start_frequency, end=end_frequency)
if x is None: for index in range(precision):
x = logspace(-10, 0, 1000) logger.debug(
_("search for a local minimum")
)
x = logspace(log10(scatter_min), log10(scatter_max), 1000)
y = array( y = array(
[ [
@ -339,7 +345,19 @@ class Experiment:
] ]
) )
pre_scatter_factor: float = x[argmin(y)] if argmin(y) == 0:
logger.warning(_("smaller than current range allows"))
scatter_max: float = x[1]
elif argmin(y) == len(x) - 1:
logger.warning(_("bigger than current range allows"))
scatter_min: float = x[-2]
else:
scatter_min: float = x[argmin(y) - 1]
scatter_max: float = x[argmin(y) + 1]
logger.debug(_("local minimum found"))
pre_scatter_factor: float = scatter_min / 2 + scatter_max / 2
logger.info( logger.info(
_("found a scattering factor of {factor}").format( _("found a scattering factor of {factor}").format(