Add object calibration
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include "capture_file_writer.h"
|
||||
#include "channel_bundle_support.hpp"
|
||||
#include "data_preprocessor.hpp"
|
||||
#include "locator/tcp_server.hpp"
|
||||
#include "processing_live_config.hpp"
|
||||
#include "runtime_wait.hpp"
|
||||
@@ -16,6 +17,7 @@
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cstring>
|
||||
#include <exception>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
@@ -60,11 +62,23 @@ struct Workspace {
|
||||
};
|
||||
|
||||
struct RingName {
|
||||
std::string name = "/radar_native_test_" + std::to_string(::getpid());
|
||||
RingName() { ipc::ShmRing::unlink_ring(name); }
|
||||
std::string name;
|
||||
explicit RingName(const std::string& suffix = {})
|
||||
: name("/radar_native_test_" + std::to_string(::getpid()) + suffix) {
|
||||
ipc::ShmRing::unlink_ring(name);
|
||||
}
|
||||
~RingName() { ipc::ShmRing::unlink_ring(name); }
|
||||
};
|
||||
|
||||
auto save_bundle(const Workspace& workspace, const char* name, const ipc::RawSweepCollection& collection)
|
||||
-> std::string {
|
||||
const auto path = workspace.path / name;
|
||||
const auto bytes = ipc::serialize_raw_collection(collection);
|
||||
std::ofstream(path, std::ios::binary).write(
|
||||
reinterpret_cast<const char*>(bytes.data()), static_cast<std::streamsize>(bytes.size()));
|
||||
return path.string();
|
||||
}
|
||||
|
||||
void write_u32(std::vector<std::uint8_t>& bytes, std::size_t offset, std::uint32_t value) {
|
||||
require(offset + sizeof(value) <= bytes.size(), "invalid test offset");
|
||||
std::memcpy(bytes.data() + offset, &value, sizeof(value));
|
||||
@@ -137,6 +151,22 @@ void test_configuration(const std::string& seed_path, const Workspace& workspace
|
||||
const auto save = [&](const Json& config) { std::ofstream(config_path) << config; };
|
||||
save(seed);
|
||||
const auto config = config::load_run_config(config_path.string());
|
||||
auto with_object_calibration = seed;
|
||||
with_object_calibration["preprocess"]["s21"]["object_calibration"] =
|
||||
Json{{"set_name", "reflector"}, {"bundle_path", "corrections.bin"}};
|
||||
save(with_object_calibration);
|
||||
const auto selected = config::load_run_config(config_path.string()).preprocess.s21.object_calibration;
|
||||
require(selected.set_name == "reflector" && selected.bundle_path == "corrections.bin",
|
||||
"object calibration asset did not load");
|
||||
with_object_calibration["preprocess"]["s21"].erase("object_calibration");
|
||||
save(with_object_calibration);
|
||||
require(config::load_run_config(config_path.string()).preprocess.s21.object_calibration.bundle_path.empty(),
|
||||
"absent object calibration was not disabled");
|
||||
for (const auto& invalid_asset : {Json(nullptr), Json{{"bundle_path", true}}}) {
|
||||
with_object_calibration["preprocess"]["s21"]["object_calibration"] = invalid_asset;
|
||||
save(with_object_calibration);
|
||||
expect_error([&] { (void)config::load_run_config(config_path.string()); });
|
||||
}
|
||||
for (const auto& [pointer, value] : std::array<std::pair<const char*, double>, 8>{{
|
||||
{"/radar/sweep/start_hz", -1}, {"/radar/sweep/stop_hz", 1e100},
|
||||
{"/radar/sweep/if_bandwidth_hz", 0}, {"/radar/sweep/points", 0},
|
||||
@@ -297,12 +327,8 @@ void test_csv_failures(const Workspace& workspace) {
|
||||
|
||||
void test_asset_reload_preserves_data(const Workspace& workspace) {
|
||||
const auto save = [&](const char* name, float sample) {
|
||||
const auto path = workspace.path / name;
|
||||
const auto bytes = ipc::serialize_raw_collection({.traces = {
|
||||
return save_bundle(workspace, name, {.traces = {
|
||||
{.frequency_hz = {1}, .s11 = {{sample, 0}}, .s21 = {{sample, 0}}}}});
|
||||
std::ofstream(path, std::ios::binary).write(
|
||||
reinterpret_cast<const char*>(bytes.data()), static_cast<std::streamsize>(bytes.size()));
|
||||
return path.string();
|
||||
};
|
||||
const auto open = save("open.bin", 1);
|
||||
const auto short_standard = save("short.bin", -1);
|
||||
@@ -328,6 +354,105 @@ void test_asset_reload_preserves_data(const Workspace& workspace) {
|
||||
"failed OSL reload lost previous calibration");
|
||||
}
|
||||
|
||||
void test_object_calibration(const Workspace& workspace) {
|
||||
const ipc::SweepTraceBlock first{.combo = {7, 3}, .frequency_hz = {1e9F, 2e9F},
|
||||
.s11 = {{1, 2}, {3, 4}}, .s21 = {{2, 3}, {4, 5}}, .capture_start_ns = 5, .capture_end_ns = 15};
|
||||
const ipc::SweepTraceBlock second{.combo = {1, 9}, .frequency_hz = {3e9F, 4e9F, 5e9F},
|
||||
.s11 = {{}, {}, {}}, .s21 = {{6, 7}, {8, 9}, {10, 11}}};
|
||||
auto factors_first = first;
|
||||
factors_first.s21 = {{0, 2}, {2, 0}};
|
||||
auto factors_second = second;
|
||||
factors_second.s21 = {{1, -1}, {-1, 0}, {0, 1}};
|
||||
preprocessing::S21ObjectCalibrationBundle correction;
|
||||
auto trace = first;
|
||||
correction.apply(trace);
|
||||
require(ipc::serialize_raw_collection({.traces = {trace}}) == ipc::serialize_raw_collection({.traces = {first}}),
|
||||
"disabled object calibration changed a trace");
|
||||
correction.load(save_bundle(workspace, "object.bin", {.traces = {factors_second, factors_first}}));
|
||||
correction.validate_combos({first.combo, second.combo});
|
||||
expect_error([&] { correction.validate_combos({{0, 0}}); });
|
||||
for (auto invalid : {first, second}) {
|
||||
invalid.combo = {0, 0};
|
||||
expect_error([&] { correction.apply(invalid); });
|
||||
}
|
||||
auto mismatch = second;
|
||||
mismatch.frequency_hz[1] += 1e8F; // Same endpoints and count; interior grid differs.
|
||||
expect_error([&] { correction.apply(mismatch); });
|
||||
mismatch = second;
|
||||
mismatch.s21.pop_back();
|
||||
expect_error([&] { correction.apply(mismatch); });
|
||||
auto nonfinite = factors_first;
|
||||
nonfinite.s21[0].re = std::numeric_limits<float>::quiet_NaN();
|
||||
expect_error([&] { correction.load(save_bundle(workspace, "object_nan.bin", {.traces = {nonfinite}})); });
|
||||
expect_error([&] { correction.load(save_bundle(workspace, "object_duplicate.bin", {.traces = {factors_first, factors_first}})); });
|
||||
auto expected = first;
|
||||
expected.s21 = {{-6, 4}, {8, 10}};
|
||||
trace = first;
|
||||
correction.apply(trace);
|
||||
require(ipc::serialize_raw_collection({.traces = {trace}}) == ipc::serialize_raw_collection({.traces = {expected}}),
|
||||
"object calibration changed S11, timing, pair identity, or multiplication after a failed reload");
|
||||
trace = second;
|
||||
correction.apply(trace);
|
||||
require(trace.s21[0].re == 13 && trace.s21[0].im == 1 && trace.s21[1].re == -8 && trace.s21[1].im == -9 &&
|
||||
trace.s21[2].re == -11 && trace.s21[2].im == 10, "object calibration mixed pairs or frequency grids");
|
||||
trace = first;
|
||||
trace.s21[0] = {std::numeric_limits<float>::max(), std::numeric_limits<float>::max()};
|
||||
expect_error([&] { correction.apply(trace); });
|
||||
correction.load("");
|
||||
trace = first;
|
||||
correction.apply(trace);
|
||||
require(trace.s21[0].re == 2 && trace.s21[0].im == 3, "clearing object calibration left stale factors");
|
||||
}
|
||||
|
||||
void test_object_calibration_pipeline(const Workspace& workspace) {
|
||||
const ipc::SweepTraceBlock trace{.combo = {7, 3}, .frequency_hz = {1e9F, 2e9F},
|
||||
.s11 = {{7, 3}, {8, 4}}, .s21 = {{2, 4}, {6, 8}}, .capture_start_ns = 5, .capture_end_ns = 15};
|
||||
auto standard = trace;
|
||||
standard.s21 = {{2, 0}, {2, 0}};
|
||||
auto reference = trace;
|
||||
reference.s21 = {{1, 2}, {2, 2}};
|
||||
auto factors = trace;
|
||||
factors.s21 = {{0, 2}, {5, 1}};
|
||||
preprocessing::CalibrationMaster calibration(preprocessing::make_s21_through_calibrator());
|
||||
calibration.load_s21_calibration_bundle(save_bundle(workspace, "pipeline_through.bin", {.traces = {standard}}));
|
||||
preprocessing::ReferenceMaster references;
|
||||
references.load_s21_reference_bundle(save_bundle(workspace, "pipeline_reference.bin", {.traces = {reference}}));
|
||||
references.prepare_calibrated(calibration, {trace.combo});
|
||||
config::RunConfig config;
|
||||
config.run_combos = {trace.combo};
|
||||
config.preprocess.s21.object_calibration.bundle_path =
|
||||
save_bundle(workspace, "pipeline_object.bin", {.traces = {factors}});
|
||||
config.preprocess.notch = {.enabled = true, .bands_hz = {{2e9F, 2e9F}}, .taper_type = "hard"};
|
||||
RingName raw_name("_object_raw"), pre_name("_object_pre");
|
||||
auto raw_ring = ipc::ShmRing::open_or_create(raw_name.name, 2, 4096);
|
||||
auto pre_ring = ipc::ShmRing::open_or_create(pre_name.name, 2, 4096);
|
||||
preprocessing::DataPreprocessor preprocessor(config, calibration, references, raw_ring, pre_ring);
|
||||
require(raw_ring.push(ipc::serialize_raw_collection({.collection_id = 41, .capture_start_ns = 5,
|
||||
.capture_end_ns = 15, .traces = {trace}})), "test raw ring could not hold a sweep");
|
||||
std::atomic<bool> stop{false};
|
||||
std::exception_ptr error;
|
||||
std::jthread worker([&] { try { preprocessor.run(stop); } catch (...) { error = std::current_exception(); } });
|
||||
std::vector<std::uint8_t> bytes;
|
||||
const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(2);
|
||||
while (bytes.empty() && std::chrono::steady_clock::now() < deadline) {
|
||||
(void)pre_ring.pop(bytes);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
}
|
||||
stop.store(true);
|
||||
worker.join();
|
||||
if (error) { std::rethrow_exception(error); }
|
||||
require(!bytes.empty(), "object-corrected preprocessed collection was not published");
|
||||
const auto result = ipc::deserialize_preprocessed_collection(bytes);
|
||||
const auto& output = result.traces.at(0);
|
||||
require(result.collection_id == 41 && result.capture_start_ns == 5 && result.capture_end_ns == 15 &&
|
||||
output.combo == trace.combo && output.capture_start_ns == 5 && output.capture_end_ns == 15,
|
||||
"object calibration lost capture identity or timing");
|
||||
require(output.s21[0].re == -2 && output.s21[0].im == 1 && output.s21[1].re == 0 && output.s21[1].im == 0,
|
||||
"object calibration ran before existing preprocessing or was applied more than once");
|
||||
require(output.s11[0].re == 7 && output.s11[0].im == 3 && output.s11[1].re == 0 && output.s11[1].im == 0,
|
||||
"object calibration changed S11 or bypassed the notch filter");
|
||||
}
|
||||
|
||||
void test_locator_packet_errors() {
|
||||
int sockets[2]{};
|
||||
require(::socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == 0, "socketpair failed");
|
||||
@@ -384,6 +509,8 @@ int main(int argc, char** argv) {
|
||||
test_wire_decoders();
|
||||
test_configuration(argv[1], workspace);
|
||||
test_asset_reload_preserves_data(workspace);
|
||||
test_object_calibration(workspace);
|
||||
test_object_calibration_pipeline(workspace);
|
||||
test_ring();
|
||||
test_partial_driver_open();
|
||||
test_interruptible_wait();
|
||||
|
||||
Reference in New Issue
Block a user