Added to and from json.

This commit is contained in:
jupfi 2023-12-16 09:55:39 +01:00
parent 50fb662e90
commit b93967666d

View file

@ -1,4 +1,5 @@
import logging import logging
import numpy as np
from nqrduck.helpers.signalprocessing import SignalProcessing as sp from nqrduck.helpers.signalprocessing import SignalProcessing as sp
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -25,6 +26,29 @@ class Measurement():
self.fdx, self.fdy = sp.fft(tdx, tdy, frequency_shift) self.fdx, self.fdy = sp.fft(tdx, tdy, frequency_shift)
self.IF_frequency = IF_frequency 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 # Measurement data
@property @property
def tdx(self): def tdx(self):