2023-07-23 13:27:23 +00:00
|
|
|
import logging
|
2023-07-22 19:08:19 +00:00
|
|
|
import numpy as np
|
|
|
|
import sympy
|
2023-07-11 09:18:29 +00:00
|
|
|
from pathlib import Path
|
2023-07-22 19:08:19 +00:00
|
|
|
from PyQt6.QtGui import QPixmap
|
|
|
|
from nqrduck.contrib.mplwidget import MplWidget
|
|
|
|
from nqrduck.helpers.signalprocessing import SignalProcessing as sp
|
2023-07-11 09:18:29 +00:00
|
|
|
from .base_spectrometer_model import BaseSpectrometerModel
|
|
|
|
|
2023-07-23 13:27:23 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2023-07-23 15:52:21 +00:00
|
|
|
|
|
|
|
class Function:
|
2023-07-22 19:08:19 +00:00
|
|
|
name: str
|
2023-07-23 15:52:21 +00:00
|
|
|
parameters: list
|
2023-07-22 19:08:19 +00:00
|
|
|
expression: str | sympy.Expr
|
|
|
|
resolution: float
|
|
|
|
start_x: float
|
|
|
|
end_x: float
|
|
|
|
|
|
|
|
def __init__(self, expr) -> None:
|
|
|
|
self.parameters = []
|
|
|
|
self.expr = expr
|
2023-07-29 14:34:03 +00:00
|
|
|
self.resolution = 1/30.72e6
|
2023-07-22 19:08:19 +00:00
|
|
|
self.start_x = -1
|
|
|
|
self.end_x = 1
|
|
|
|
|
2023-07-23 15:52:21 +00:00
|
|
|
def get_time_points(self, pulse_length: float) -> np.ndarray:
|
2023-07-22 19:08:19 +00:00
|
|
|
"""Returns the time domain points for the function with the given pulse length."""
|
|
|
|
# Get the time domain points
|
|
|
|
n = int(pulse_length / self.resolution)
|
|
|
|
t = np.linspace(0, pulse_length, n)
|
|
|
|
return t
|
2023-07-23 15:52:21 +00:00
|
|
|
|
2023-07-22 19:08:19 +00:00
|
|
|
def evaluate(self, pulse_length: float) -> np.ndarray:
|
|
|
|
"""Evaluates the function for the given pulse length."""
|
|
|
|
n = int(pulse_length / self.resolution)
|
2023-07-23 13:27:23 +00:00
|
|
|
t = np.linspace(self.start_x, self.end_x, n)
|
2023-07-22 19:08:19 +00:00
|
|
|
x = sympy.symbols("x")
|
|
|
|
|
|
|
|
found_variables = dict()
|
|
|
|
# Create a dictionary of the parameters and their values
|
|
|
|
for parameter in self.parameters:
|
|
|
|
found_variables[parameter.symbol] = parameter.value
|
|
|
|
|
|
|
|
final_expr = self.expr.subs(found_variables)
|
2023-07-23 15:52:21 +00:00
|
|
|
# If the expression is a number (does not depend on x), return an array of that number
|
2023-07-22 19:08:19 +00:00
|
|
|
if final_expr.is_number:
|
|
|
|
return np.full(t.shape, float(final_expr))
|
|
|
|
|
|
|
|
f = sympy.lambdify([x], final_expr, "numpy")
|
2023-07-23 15:52:21 +00:00
|
|
|
|
2023-07-22 19:08:19 +00:00
|
|
|
return f(t)
|
2023-07-23 15:52:21 +00:00
|
|
|
|
|
|
|
def frequency_domain_plot(self, pulse_length: float) -> MplWidget:
|
2023-07-22 19:08:19 +00:00
|
|
|
mpl_widget = MplWidget()
|
|
|
|
td = self.get_time_points(pulse_length)
|
|
|
|
yd = self.evaluate(pulse_length)
|
|
|
|
xdf, ydf = sp.fft(td, yd)
|
|
|
|
mpl_widget.canvas.ax.plot(xdf, ydf)
|
|
|
|
mpl_widget.canvas.ax.set_xlabel("Frequency in Hz")
|
|
|
|
mpl_widget.canvas.ax.set_ylabel("Magnitude")
|
2023-07-29 14:56:16 +00:00
|
|
|
mpl_widget.canvas.ax.grid(True)
|
2023-07-22 19:08:19 +00:00
|
|
|
return mpl_widget
|
|
|
|
|
2023-07-23 15:52:21 +00:00
|
|
|
def time_domain_plot(self, pulse_length: float) -> MplWidget:
|
2023-07-22 19:08:19 +00:00
|
|
|
mpl_widget = MplWidget()
|
|
|
|
td = self.get_time_points(pulse_length)
|
|
|
|
mpl_widget.canvas.ax.plot(td, self.evaluate(pulse_length))
|
|
|
|
mpl_widget.canvas.ax.set_xlabel("Time in s")
|
|
|
|
mpl_widget.canvas.ax.set_ylabel("Magnitude")
|
2023-07-29 14:56:16 +00:00
|
|
|
mpl_widget.canvas.ax.grid(True)
|
2023-07-22 19:08:19 +00:00
|
|
|
return mpl_widget
|
2023-07-24 06:21:00 +00:00
|
|
|
|
|
|
|
def get_pulse_amplitude(self, pulse_length: float) -> np.array:
|
|
|
|
"""Returns the pulse amplitude in the time domain."""
|
|
|
|
return self.evaluate(pulse_length)
|
2023-07-23 15:52:21 +00:00
|
|
|
|
|
|
|
def add_parameter(self, parameter: "Function.Parameter"):
|
2023-07-22 19:08:19 +00:00
|
|
|
self.parameters.append(parameter)
|
|
|
|
|
2023-07-23 15:52:21 +00:00
|
|
|
def to_json(self):
|
|
|
|
return {
|
|
|
|
"name": self.name,
|
|
|
|
"parameters": [parameter.to_json() for parameter in self.parameters],
|
|
|
|
"expression": str(self.expr),
|
|
|
|
"resolution": self.resolution,
|
|
|
|
"start_x": self.start_x,
|
|
|
|
"end_x": self.end_x,
|
|
|
|
}
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_json(cls, data):
|
|
|
|
for subclass in cls.__subclasses__():
|
|
|
|
if subclass.name == data["name"]:
|
|
|
|
cls = subclass
|
|
|
|
break
|
|
|
|
|
|
|
|
obj = cls()
|
|
|
|
obj.expr = data["expression"]
|
|
|
|
obj.name = data["name"]
|
|
|
|
obj.resolution = data["resolution"]
|
|
|
|
obj.start_x = data["start_x"]
|
|
|
|
obj.end_x = data["end_x"]
|
|
|
|
|
|
|
|
obj.parameters = []
|
|
|
|
for parameter in data["parameters"]:
|
|
|
|
obj.add_parameter(Function.Parameter.from_json(parameter))
|
|
|
|
|
|
|
|
return obj
|
2023-07-29 14:34:03 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def expr(self):
|
|
|
|
return self._expr
|
|
|
|
|
|
|
|
@expr.setter
|
|
|
|
def expr(self, expr):
|
|
|
|
if isinstance(expr, str):
|
|
|
|
try:
|
|
|
|
self._expr = sympy.sympify(expr)
|
|
|
|
except:
|
|
|
|
logger.error("Could not convert %s to a sympy expression", expr)
|
|
|
|
raise SyntaxError("Could not convert %s to a sympy expression" % expr)
|
|
|
|
elif isinstance(expr, sympy.Expr):
|
|
|
|
self._expr = expr
|
|
|
|
|
2023-07-23 13:27:23 +00:00
|
|
|
|
2023-07-22 19:08:19 +00:00
|
|
|
class Parameter:
|
2023-07-23 15:52:21 +00:00
|
|
|
def __init__(self, name: str, symbol: str, value: float) -> None:
|
2023-07-22 19:08:19 +00:00
|
|
|
self.name = name
|
|
|
|
self.symbol = symbol
|
|
|
|
self.value = value
|
|
|
|
self.default = value
|
|
|
|
|
2023-07-23 15:52:21 +00:00
|
|
|
def set_value(self, value: float):
|
2023-07-23 13:27:23 +00:00
|
|
|
self.value = value
|
|
|
|
logger.debug("Parameter %s set to %s", self.name, self.value)
|
|
|
|
|
2023-07-23 15:52:21 +00:00
|
|
|
def to_json(self):
|
|
|
|
return {
|
|
|
|
"name": self.name,
|
|
|
|
"symbol": self.symbol,
|
|
|
|
"value": self.value,
|
|
|
|
"default": self.default,
|
|
|
|
}
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_json(cls, data):
|
|
|
|
obj = cls(data["name"], data["symbol"], data["value"])
|
|
|
|
obj.default = data["default"]
|
|
|
|
return obj
|
|
|
|
|
2023-07-22 19:08:19 +00:00
|
|
|
class RectFunction(Function):
|
|
|
|
name = "Rectangular"
|
2023-07-23 15:52:21 +00:00
|
|
|
|
2023-07-22 19:08:19 +00:00
|
|
|
def __init__(self) -> None:
|
|
|
|
expr = sympy.sympify("1")
|
|
|
|
super().__init__(expr)
|
|
|
|
|
2023-07-23 15:52:21 +00:00
|
|
|
|
2023-07-22 19:08:19 +00:00
|
|
|
class SincFunction(Function):
|
|
|
|
name = "Sinc"
|
2023-07-23 15:52:21 +00:00
|
|
|
|
2023-07-22 19:08:19 +00:00
|
|
|
def __init__(self) -> None:
|
|
|
|
expr = sympy.sympify("sin(x * l)/ (x * l)")
|
|
|
|
super().__init__(expr)
|
|
|
|
self.add_parameter(Function.Parameter("Scale Factor", "l", 2))
|
2023-07-23 13:27:23 +00:00
|
|
|
self.start_x = -np.pi
|
|
|
|
self.end_x = np.pi
|
2023-07-22 19:08:19 +00:00
|
|
|
|
2023-07-23 15:52:21 +00:00
|
|
|
|
2023-07-22 19:08:19 +00:00
|
|
|
class GaussianFunction(Function):
|
|
|
|
name = "Gaussian"
|
2023-07-23 15:52:21 +00:00
|
|
|
|
2023-07-22 19:08:19 +00:00
|
|
|
def __init__(self) -> None:
|
|
|
|
expr = sympy.sympify("exp(-0.5 * ((x - mu) / sigma)**2)")
|
|
|
|
super().__init__(expr)
|
|
|
|
self.add_parameter(Function.Parameter("Mean", "mu", 0))
|
|
|
|
self.add_parameter(Function.Parameter("Standard Deviation", "sigma", 1))
|
2023-07-23 15:52:21 +00:00
|
|
|
self.start_x = -np.pi
|
|
|
|
self.end_x = np.pi
|
|
|
|
|
2023-07-22 19:08:19 +00:00
|
|
|
|
2023-07-23 15:52:21 +00:00
|
|
|
# class TriangleFunction(Function):
|
2023-07-22 19:08:19 +00:00
|
|
|
# def __init__(self) -> None:
|
|
|
|
# expr = sympy.sympify("triang(x)")
|
|
|
|
# super().__init__(lambda x: triang(x))
|
|
|
|
|
2023-07-23 15:52:21 +00:00
|
|
|
|
2023-07-22 19:08:19 +00:00
|
|
|
class CustomFunction(Function):
|
2023-07-23 15:52:21 +00:00
|
|
|
name = "Custom"
|
|
|
|
|
2023-07-22 19:08:19 +00:00
|
|
|
def __init__(self) -> None:
|
2023-07-23 15:52:21 +00:00
|
|
|
expr = sympy.sympify(" 2 * x**2 + 3 * x + 1")
|
|
|
|
super().__init__(expr)
|
|
|
|
|
2023-07-22 11:48:55 +00:00
|
|
|
|
|
|
|
class Option:
|
|
|
|
"""Defines options for the pulse parameters which can then be set accordingly."""
|
|
|
|
|
2023-07-23 15:52:21 +00:00
|
|
|
def __init__(self, name: str, value) -> None:
|
|
|
|
self.name = name
|
|
|
|
self.value = value
|
|
|
|
|
2023-07-11 15:50:42 +00:00
|
|
|
def set_value(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2023-07-23 15:52:21 +00:00
|
|
|
def to_json(self):
|
|
|
|
return {"name": self.name, "value": self.value, "type": self.TYPE}
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_json(cls, data) -> "Option":
|
|
|
|
for subclass in cls.__subclasses__():
|
|
|
|
if subclass.TYPE == data["type"]:
|
|
|
|
cls = subclass
|
|
|
|
break
|
|
|
|
|
|
|
|
# Check if from_json is implemented for the subclass
|
|
|
|
if cls.from_json.__func__ == Option.from_json.__func__:
|
|
|
|
obj = cls(data["name"], data["value"])
|
|
|
|
else:
|
|
|
|
obj = cls.from_json(data)
|
|
|
|
|
|
|
|
return obj
|
2023-07-20 14:06:43 +00:00
|
|
|
|
2023-07-11 15:50:42 +00:00
|
|
|
class BooleanOption(Option):
|
2023-07-22 11:48:55 +00:00
|
|
|
"""Defines a boolean option for a pulse parameter option."""
|
2023-07-23 15:52:21 +00:00
|
|
|
TYPE = "Boolean"
|
2023-07-11 15:50:42 +00:00
|
|
|
|
2023-07-20 14:06:43 +00:00
|
|
|
def set_value(self, value):
|
|
|
|
self.value = value
|
2023-07-22 11:48:55 +00:00
|
|
|
|
|
|
|
|
2023-07-11 15:50:42 +00:00
|
|
|
class NumericOption(Option):
|
2023-07-22 11:48:55 +00:00
|
|
|
"""Defines a numeric option for a pulse parameter option."""
|
2023-07-23 15:52:21 +00:00
|
|
|
TYPE = "Numeric"
|
2023-07-11 15:50:42 +00:00
|
|
|
|
|
|
|
def set_value(self, value):
|
|
|
|
self.value = float(value)
|
|
|
|
|
2023-07-22 11:48:55 +00:00
|
|
|
|
2023-07-22 19:08:19 +00:00
|
|
|
class FunctionOption(Option):
|
|
|
|
"""Defines a selection option for a pulse parameter option.
|
|
|
|
It takes different function objects."""
|
2023-07-23 15:52:21 +00:00
|
|
|
TYPE = "Function"
|
|
|
|
|
|
|
|
def __init__(self, name, functions) -> None:
|
|
|
|
super().__init__(name, functions[0])
|
2023-07-22 19:08:19 +00:00
|
|
|
self.functions = functions
|
2023-07-11 15:50:42 +00:00
|
|
|
|
2023-07-22 19:08:19 +00:00
|
|
|
def set_value(self, value):
|
|
|
|
self.value = value
|
2023-07-23 15:52:21 +00:00
|
|
|
|
|
|
|
def get_function_by_name(self, name):
|
|
|
|
for function in self.functions:
|
|
|
|
if function.name == name:
|
|
|
|
return function
|
|
|
|
raise ValueError("Function with name %s not found" % name)
|
|
|
|
|
|
|
|
def to_json(self):
|
|
|
|
return {"name": self.name, "value": self.value.to_json(), "type": self.TYPE}
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_json(cls, data):
|
|
|
|
functions = [function() for function in Function.__subclasses__()]
|
|
|
|
obj = cls(data["name"], functions)
|
|
|
|
obj.value = Function.from_json(data["value"])
|
|
|
|
return obj
|
|
|
|
|
2023-07-11 15:50:42 +00:00
|
|
|
|
2023-07-11 09:18:29 +00:00
|
|
|
class TXPulse(BaseSpectrometerModel.PulseParameter):
|
2023-07-23 15:52:21 +00:00
|
|
|
RELATIVE_AMPLITUDE = "Relative TX Amplitude"
|
|
|
|
TX_PHASE = "TX Phase"
|
|
|
|
TX_PULSE_SHAPE = "TX Pulse Shape"
|
|
|
|
|
2023-07-11 09:18:29 +00:00
|
|
|
def __init__(self, name) -> None:
|
|
|
|
super().__init__(name)
|
2023-07-23 15:52:21 +00:00
|
|
|
self.add_option(NumericOption(self.RELATIVE_AMPLITUDE, 0))
|
|
|
|
self.add_option(NumericOption(self.TX_PHASE, 0))
|
|
|
|
self.add_option(
|
|
|
|
FunctionOption(self.TX_PULSE_SHAPE, [RectFunction(), SincFunction(), GaussianFunction()]),
|
|
|
|
)
|
2023-07-11 09:18:29 +00:00
|
|
|
|
2023-07-11 15:50:42 +00:00
|
|
|
def get_pixmap(self):
|
|
|
|
self_path = Path(__file__).parent
|
2023-07-23 15:52:21 +00:00
|
|
|
if self.get_option_by_name(self.RELATIVE_AMPLITUDE).value > 0:
|
2023-07-22 11:48:55 +00:00
|
|
|
image_path = self_path / "resources/pulseparameter/TXOn.png"
|
2023-07-11 15:50:42 +00:00
|
|
|
else:
|
2023-07-22 11:48:55 +00:00
|
|
|
image_path = self_path / "resources/pulseparameter/TXOff.png"
|
2023-07-11 15:50:42 +00:00
|
|
|
pixmap = QPixmap(str(image_path))
|
|
|
|
return pixmap
|
2023-07-11 09:18:29 +00:00
|
|
|
|
2023-07-23 15:52:21 +00:00
|
|
|
|
2023-07-11 15:50:42 +00:00
|
|
|
class RXReadout(BaseSpectrometerModel.PulseParameter):
|
2023-07-23 15:52:21 +00:00
|
|
|
RX = "RX"
|
2023-07-11 11:58:07 +00:00
|
|
|
def __init__(self, name) -> None:
|
|
|
|
super().__init__(name)
|
2023-07-23 15:52:21 +00:00
|
|
|
self.add_option(BooleanOption(self.RX, False))
|
2023-07-12 15:14:16 +00:00
|
|
|
|
|
|
|
def get_pixmap(self):
|
|
|
|
self_path = Path(__file__).parent
|
2023-07-23 15:52:21 +00:00
|
|
|
if self.get_option_by_name(self.RX).value == False:
|
2023-07-12 15:14:16 +00:00
|
|
|
image_path = self_path / "resources/pulseparameter/RXOff.png"
|
|
|
|
else:
|
|
|
|
image_path = self_path / "resources/pulseparameter/RXOn.png"
|
|
|
|
pixmap = QPixmap(str(image_path))
|
|
|
|
return pixmap
|
|
|
|
|
2023-07-22 11:48:55 +00:00
|
|
|
|
2023-07-11 09:18:29 +00:00
|
|
|
class Gate(BaseSpectrometerModel.PulseParameter):
|
2023-07-23 15:52:21 +00:00
|
|
|
GATE_STATE = "Gate State"
|
2023-07-11 09:18:29 +00:00
|
|
|
def __init__(self, name) -> None:
|
|
|
|
super().__init__(name)
|
2023-07-23 15:52:21 +00:00
|
|
|
self.add_option(BooleanOption(self.GATE_STATE, False))
|
2023-07-11 09:18:29 +00:00
|
|
|
|
|
|
|
def get_pixmap(self):
|
|
|
|
self_path = Path(__file__).parent
|
2023-07-23 15:52:21 +00:00
|
|
|
if self.get_option_by_name(self.GATE_STATE).value == False:
|
2023-07-12 15:14:16 +00:00
|
|
|
image_path = self_path / "resources/pulseparameter/GateOff.png"
|
2023-07-11 15:50:42 +00:00
|
|
|
else:
|
2023-07-12 15:14:16 +00:00
|
|
|
image_path = self_path / "resources/pulseparameter/GateOn.png"
|
2023-07-11 09:18:29 +00:00
|
|
|
pixmap = QPixmap(str(image_path))
|
|
|
|
return pixmap
|