added s11 collections
This commit is contained in:
@@ -8,31 +8,39 @@ import numpy as np
|
||||
|
||||
from python_app.models.dataset_model import ResultCollection, SweepCollection
|
||||
|
||||
RAW_MAGIC = 0x31574152
|
||||
PREPROC_MAGIC = 0x31525050
|
||||
RAW_MAGIC = 0x32574152
|
||||
PREPROC_MAGIC = 0x32525050
|
||||
RESULT_MAGIC = 0x314C5352
|
||||
|
||||
|
||||
def _write_interleaved_complex(buffer: bytearray, values: np.ndarray) -> None:
|
||||
"""Append complex64 array as interleaved float32 real/imag pairs."""
|
||||
interleaved = np.empty(values.size * 2, dtype="<f4")
|
||||
interleaved[0::2] = values.real.astype("<f4", copy=False)
|
||||
interleaved[1::2] = values.imag.astype("<f4", copy=False)
|
||||
buffer.extend(interleaved.tobytes())
|
||||
|
||||
|
||||
def serialize_trace_collection(collection: SweepCollection, magic: int) -> bytes:
|
||||
"""Serialize one raw/preprocessed trace collection into ring-compatible binary format."""
|
||||
buffer = bytearray()
|
||||
buffer.extend(
|
||||
struct.pack("<IQQI", magic, collection.collection_id, collection.monotonic_ns, len(collection.traces))
|
||||
)
|
||||
buffer.extend(struct.pack("<IQQI", magic, collection.collection_id, collection.monotonic_ns, len(collection.traces)))
|
||||
|
||||
for trace in collection.traces:
|
||||
freq = np.asarray(trace.frequency_hz, dtype=np.float32)
|
||||
s21 = np.asarray(trace.s21, dtype=np.complex64)
|
||||
if freq.size != s21.size:
|
||||
raise ValueError("Trace frequency and S21 sizes must match")
|
||||
# Python workflows still operate on S21 only. Emit a zero-filled S11
|
||||
# channel so C++ trace bundles keep the same wire format as runtime
|
||||
# rings while the Python layer remains unchanged.
|
||||
s11 = np.zeros(freq.size, dtype=np.complex64)
|
||||
|
||||
buffer.extend(struct.pack("<III", trace.combo.input_pos, trace.combo.output_pos, int(freq.size)))
|
||||
buffer.extend(freq.astype("<f4", copy=False).tobytes())
|
||||
|
||||
interleaved = np.empty(freq.size * 2, dtype="<f4")
|
||||
interleaved[0::2] = s21.real.astype("<f4", copy=False)
|
||||
interleaved[1::2] = s21.imag.astype("<f4", copy=False)
|
||||
buffer.extend(interleaved.tobytes())
|
||||
_write_interleaved_complex(buffer, s11)
|
||||
_write_interleaved_complex(buffer, s21)
|
||||
|
||||
return bytes(buffer)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user