Coverage for src/signal_edges/signal/voltage_signal.py: 79%
38 statements
« prev ^ index » next coverage.py v7.4.3, created at 2024-04-21 11:16 +0000
« prev ^ index » next coverage.py v7.4.3, created at 2024-04-21 11:16 +0000
1"""The voltage signal class, :class:`.VoltageSignal`, is a specialized signal for timestamps and voltages, shipped
2as part of |ProjectName| and used throughout the examples in the documentation. It is also available to use from the
3user code."""
5from typing import Literal
7import numpy as np
8import numpy.typing as npt
10from ..exceptions import VoltageSignalError
11from ..plotter import Units
12from .edges import EdgesMixin
13from .filters import BesselFiltersMixin, ButterworthFiltersMixin, EllipticFiltersMixin
14from .signal import Signal
15from .state_levels import StateLevelsMixin
18class VoltageSignal(
19 ButterworthFiltersMixin,
20 EllipticFiltersMixin,
21 BesselFiltersMixin,
22 StateLevelsMixin,
23 EdgesMixin,
24 Signal,
25):
26 """Predefined implementation of a :class:`.Signal` that represents an analog voltage signal as captured,
27 for example, by an oscilloscope, data recorder or similar.
29 The horizontal axis of the signal contains the timestamps values and the vertical axis of the signal contains
30 the voltage values, in miliseconds or seconds and in milivolts or volts respectively."""
32 # pylint: disable=too-many-ancestors
34 ## Private API #####################################################################################################
35 def __init__(
36 self,
37 timestamps: npt.NDArray[np.float_],
38 voltages: npt.NDArray[np.float_],
39 timestamp_unit_id: Literal["ms", "s"] | None = None,
40 voltage_unit_id: Literal["mV", "V"] | None = None,
41 ) -> None:
42 """Class constructor.
44 :param timestamps: The timestamp values for the signal.
45 :param voltages: The voltage values for the signal.
46 :param timestamp_unit_id: An identifier for the timestamp units, can be ignored if not plotting.
47 :param voltage_unit_id: An identifier for the voltage units, can be ignored if not plotting."""
48 super().__init__(
49 hvalues=timestamps,
50 vvalues=voltages,
51 hunits=None if timestamp_unit_id is None else self.__get_timestamp_units(timestamp_unit_id),
52 vunits=None if voltage_unit_id is None else self.__get_voltage_units(voltage_unit_id),
53 )
55 @staticmethod
56 def __get_timestamp_units(unit_id: Literal["ms", "s"]) -> Units:
57 """Obtains timestamp unit definitions for :class:`.Plotter` from a timestamp unit identifier.
59 :param unit_id: The timestamp unit identifier.
60 :raise VoltageSignalError: The timestamp unit identifier is not recognized.
61 :return: Timestamp units."""
62 if unit_id == "ms": 62 ↛ 63line 62 didn't jump to line 63, because the condition on line 62 was never true
63 return Units("Milliseconds", "ms", "Time")
64 if unit_id == "s": 64 ↛ 66line 64 didn't jump to line 66, because the condition on line 64 was never false
65 return Units("Seconds", "s", "Time")
66 raise VoltageSignalError(f"The timestamp unit identifier '{unit_id}' is not recognized.")
68 @staticmethod
69 def __get_voltage_units(unit_id: Literal["mV", "V"]) -> Units:
70 """Obtains voltage unit definitions for :class:`.Plotter` from a voltage unit identifier.
72 :param unit_id: The voltage unit identifier.
73 :raise VoltageSignalError: The voltage unit identifier is not recognized.
74 :return: Voltage units."""
75 if unit_id == "mV": 75 ↛ 76line 75 didn't jump to line 76, because the condition on line 75 was never true
76 return Units("Millivolts", "mV", "Voltage")
77 if unit_id == "V": 77 ↛ 79line 77 didn't jump to line 79, because the condition on line 77 was never false
78 return Units("Volts", "v", "Voltage")
79 raise VoltageSignalError(f"The voltage unit identifier '{unit_id}' is not recognized.")
81 ## Protected API ###################################################################################################
83 ## Public API ######################################################################################################
84 @property
85 def timestamps(self) -> npt.NDArray[np.float_]:
86 """Getter for the timestamps.
88 :return: The timestamps."""
89 return self._hv
91 @property
92 def voltages(self) -> npt.NDArray[np.float_]:
93 """Getter for the voltages
95 :return: The voltages."""
96 return self._vv
98 @property
99 def timestamp_units(self) -> Units:
100 """Getter for the timestamp units.
102 :return: The timestamps units."""
103 return self._hunits
105 @property
106 def voltage_units(self) -> Units:
107 """Getter for the voltage units.
109 :return: The voltage units."""
110 return self._vunits