diff --git a/src/nqrduck_spectrometer/measurement.py b/src/nqrduck_spectrometer/measurement.py index 5e15550..997397d 100644 --- a/src/nqrduck_spectrometer/measurement.py +++ b/src/nqrduck_spectrometer/measurement.py @@ -1,4 +1,5 @@ import logging +import numpy as np from nqrduck.helpers.signalprocessing import SignalProcessing as sp logger = logging.getLogger(__name__) @@ -25,6 +26,29 @@ class Measurement(): self.fdx, self.fdy = sp.fft(tdx, tdy, frequency_shift) self.IF_frequency = IF_frequency + # Data saving and loading + + def to_json(self): + """Converts the measurement to a json-compatible format.""" + return { + "tdx": [[x.real, x.imag] for x in self.tdx], # Convert complex numbers to list + "tdy": [[x.real, x.imag] for x in self.tdy], # Convert complex numbers to list + "target_frequency": self.target_frequency, + "IF_frequency": self.IF_frequency + } + + @classmethod + def from_json(cls, json): + """Converts the json format to a measurement.""" + tdx = np.array([complex(x[0], x[1]) for x in json["tdx"]]) + tdy = np.array([complex(y[0], y[1]) for y in json["tdy"]]) + return cls( + tdx, + tdy, + json["target_frequency"], + json["IF_frequency"] + ) + # Measurement data @property def tdx(self):