work on MPI

This commit is contained in:
Silas Oettinghaus
2026-07-13 15:25:45 +02:00
parent a6cf742121
commit ef0a74cb7f
16 changed files with 2894 additions and 705 deletions

View File

@@ -0,0 +1,237 @@
%% Simple BER over SIR plot from the recombined warehouses
% This script intentionally keeps the mechanics visible:
% 1) load the run_id warehouse and the grouped config warehouse
% 2) loop over occupied config cells
% 3) read the physical coordinates of the cell
% 4) extract all BER values stored in that cell
% 5) plot individual BER dots and one larger mean marker per SIR
clear; clc;
scriptDir = fileparts(mfilename("fullpath"));
runWhFile = fullfile(scriptDir, "combined_by_run_id_results.mat");
configWhFile = fullfile(scriptDir, "combined_by_config_results.mat");
runWhData = load(runWhFile);
configWhData = load(configWhFile);
wh_run_id_combined = runWhData.wh_run_id_combined;
wh_config_combined = configWhData.wh_config_combined;
algorithmStorageNames = configWhData.algorithmStorageNames;
fprintf("Loaded run_id warehouse:\n %s\n", runWhFile);
wh_run_id_combined.showInfo;
fprintf("\nLoaded config-grouped warehouse:\n %s\n", configWhFile);
wh_config_combined.showInfo;
%% Plot settings
pathLengthToPlot = 1; % use 300 to see duplicate run_ids per config; set 1000 for the previous path
selectedPamLevels = 4;%wh_config_combined.parameter.pam_level.values;
selectedAlgorithms = algorithmStorageNames([1,5]);
useBoundedLines = true; % switch uncertainty bands on/off here
usePolyfit = true; % switch fitted dashed trend lines on/off here
polyfitOrderMax = 4;
% SIR vs BER: one subplot per PAM level, all algorithms compared
if exist("linspecer", "file")
algColors = linspecer(numel(selectedAlgorithms));
else
algColors = lines(numel(selectedAlgorithms));
end
figure(); clf;
tiledlayout(numel(selectedPamLevels), 1, "TileSpacing", "compact");
for pamIdx = 1:numel(selectedPamLevels)
pamLevel = selectedPamLevels(pamIdx);
nexttile; hold on;
for algIdx = 1:numel(selectedAlgorithms)
algorithmName = selectedAlgorithms{algIdx};
algColor = algColors(algIdx, :);
berTable = buildBerTable(wh_config_combined, algorithmName, pathLengthToPlot);
berTable = berTable(berTable.pam_level == pamLevel, :);
% berTable(berTable.sir == 23,:) = [];
if isempty(berTable)
continue
end
sirValues = unique(berTable.sir(:).');
sirValues = sort(sirValues);
meanBer = nan(size(sirValues));
minBer = nan(size(sirValues));
maxBer = nan(size(sirValues));
for sirIdx = 1:numel(sirValues)
sirValue = sirValues(sirIdx);
sirRows = berTable(berTable.sir == sirValue, :);
berValues = [sirRows.ber_values{:}];
berValues = berValues(isfinite(berValues));
berValues = berValues(berValues<0.1);
berValues = rmoutliers(berValues);
if isempty(berValues)
continue
end
scatter(repmat(sirValue, size(berValues)), berValues, ...
30, ...
"Marker", ".", ...
"MarkerEdgeColor", algColor, ...
"MarkerFaceColor", algColor, ...
"HandleVisibility", "off");
meanBer(sirIdx) = mean(berValues, "omitnan");
minBer(sirIdx) = min(berValues);
maxBer(sirIdx) = max(berValues);
end
valid = isfinite(sirValues) & isfinite(meanBer);
if ~any(valid)
continue
end
if useBoundedLines && exist("boundedline", "file")
yLower = max(meanBer - minBer, 0);
yUpper = max(maxBer - meanBer, 0);
yBounds = [yLower(:), yUpper(:)];
[hl, hp] = boundedline(sirValues(valid).', meanBer(valid).', yBounds(valid, :), ...
'alpha', 'transparency', 0.08, ...
'cmap', algColor, ...
'nan', 'fill', ...
'orientation', 'vert');
set(hl, "LineStyle", "none", "Marker", "none", "HandleVisibility", "off");
set(hp, "LineStyle", "none", "HandleVisibility", "off");
end
scatter(sirValues(valid), meanBer(valid), ...
20, ...
"Marker", "o", ...
"MarkerEdgeColor", algColor, ...
"MarkerFaceColor", algColor, ...
"LineWidth", 1, ...
"DisplayName", algorithmName);
if usePolyfit
fitMask = valid & meanBer > 0;
if nnz(fitMask) >= 2
fitOrder = min(polyfitOrderMax, nnz(fitMask) - 1);
fitCoeff = polyfit(sirValues(fitMask), log10(meanBer(fitMask)), fitOrder);
xFit = linspace(min(sirValues(fitMask)), max(sirValues(fitMask)), 300);
yFit = 10 .^ polyval(fitCoeff, xFit);
plot(xFit, yFit, ...
"LineStyle", "--", ...
"LineWidth", 1.1, ...
"Color", algColor, ...
"HandleVisibility", "off");
end
end
end
title(sprintf("PAM %.0f, path %.0f m", pamLevel, pathLengthToPlot));
xlabel("SIR (dB)");
ylabel("BER");
set(gca, "YScale", "log");
xlim([15,45]);
grid on;
box on;
legend("Location", "best", "Interpreter", "none");
end
%% Local helpers
function berTable = buildBerTable(wh, storageName, pathLength)
rows = struct( ...
"pam_level", {}, ...
"symbolrate", {}, ...
"interference_path_length", {}, ...
"sir", {}, ...
"block_update", {}, ...
"run_ids", {}, ...
"n_run_ids", {}, ...
"ber_values", {}, ...
"n_ber_values", {});
for linIdx = 1:numel(wh.sto.(storageName))
storedValue = wh.sto.(storageName){linIdx};
if isempty(storedValue)
continue
end
phys = linearIndexToStruct(wh, linIdx);
if phys.interference_path_length ~= pathLength
continue
end
berValues = extractBerValues(storedValue);
if isempty(berValues)
continue
end
runIds = wh.sto.run_id{linIdx};
if isempty(runIds)
runIds = NaN;
end
row = struct();
row.pam_level = phys.pam_level;
row.symbolrate = phys.symbolrate;
row.interference_path_length = phys.interference_path_length;
row.sir = phys.sir;
row.block_update = phys.block_update;
row.run_ids = {runIds};
row.n_run_ids = numel(runIds);
row.ber_values = {berValues};
row.n_ber_values = numel(berValues);
rows(end+1) = row; %#ok<AGROW>
end
if isempty(rows)
berTable = struct2table(rows);
else
berTable = struct2table(rows);
berTable = sortrows(berTable, ["pam_level", "sir", "symbolrate"]);
end
end
function physStruct = linearIndexToStruct(wh, linIdx)
[physValues, physNames] = wh.getPhysIndicesByLinIndex(linIdx);
physStruct = struct();
for paramIdx = 1:numel(physNames)
physStruct.(char(physNames{paramIdx})) = physValues{paramIdx};
end
end
function berValues = extractBerValues(value)
berValues = [];
if isstruct(value)
if isfield(value, "metrics") && hasBer(value.metrics)
berValues(end+1) = value.metrics.BER;
end
elseif iscell(value)
for valueIdx = 1:numel(value)
berValues = [berValues, extractBerValues(value{valueIdx})]; %#ok<AGROW>
end
end
end
function tf = hasBer(metrics)
tf = (isstruct(metrics) && isfield(metrics, "BER")) || ...
(isobject(metrics) && isprop(metrics, "BER"));
end

View File

@@ -0,0 +1,539 @@
% === DSP settings ===
dsp_options = struct();
dsp_options.mode = "run_id";
dsp_options.recipe = @mpi_recipe_dev;
dsp_options.append_to_db = false;
dsp_options.start_occurence = 1;
dsp_options.max_occurences = 15;
dsp_options.debug_plots = false;
dsp_options.database_type = "mysql";
dsp_options.dataBase = "labor";
dsp_options.storage_path = "W:\labdata\ECOC Silas\ecoc_2025";
dsp_options.server = "192.168.178.192";
dsp_options.port = 3306;
dsp_options.user = "silas";
dsp_options.password = "silas";
db = DBHandler("dataBase", [dsp_options.dataBase], ...
"type", dsp_options.database_type, ...
"server", dsp_options.server, ...
"user", dsp_options.user, "password", dsp_options.password);
scriptDir = fileparts(mfilename("fullpath"));
whpam4_loaded = load(fullfile(scriptDir, "pam_4_results.mat"));
whpam6_loaded = load(fullfile(scriptDir, "pam_6_results.mat"));
whpam8_loaded = load(fullfile(scriptDir, "pam_8_results.mat"));
whpam4 = whpam4_loaded.obj;
whpam6 = whpam6_loaded.obj;
whpam8 = whpam8_loaded.obj;
whList = {whpam4, whpam6, whpam8};
algorithmStorageNames = fieldnames(whpam4.sto);
%% Build a lossless run_id warehouse and cache run metadata
pamformats = [4, 6, 8];
baudrates = [112e9, 96e9, 72e9];
runMetaTable = queryRunMetadata(db, whList, pamformats, baudrates);
wh_run_id_combined = mergeDataStoragesByUnion(whList);
wh_run_id_combined = addRunMetadataStorages(wh_run_id_combined, runMetaTable);
runIdSavePath = fullfile(scriptDir, "combined_by_run_id_results.mat");
save(runIdSavePath, "wh_run_id_combined", "runMetaTable", "algorithmStorageNames", '-v7.3');
fprintf("Saved run_id-combined warehouse:\n %s\n", runIdSavePath);
wh_run_id_combined.showInfo;
%% Build a grouped config warehouse for analysis views
configFields = ["pam_level", "symbolrate", "interference_path_length", "sir"];
wh_config_combined = buildConfigWarehouse( ...
wh_run_id_combined, runMetaTable, algorithmStorageNames, configFields);
configSavePath = fullfile(scriptDir, "combined_by_config_results.mat");
save(configSavePath, "wh_config_combined", "runMetaTable", "algorithmStorageNames", '-v7.3');
fprintf("Saved config-grouped warehouse:\n %s\n", configSavePath);
wh_config_combined.showInfo;
%% BER over SIR at fixed path length
plot_options = struct();
plot_options.path_length = 1000;
plot_options.use_boundedlines = true;
plot_options.boundedline_alpha = 0.10;
plot_options.polyfit_order_max = 3;
plot_options.fec_ber_threshold = 2e-2;
plot_options.figure_base = 7100;
plot_options.xlim = [15 35];
plotBerOverSirForPath(wh_config_combined, algorithmStorageNames, plot_options);
%% Local functions
function runMetaTable = queryRunMetadata(db, whList, pamformats, baudrates)
metaTables = {};
metadataFields = db.getTableFieldNames('Runs');
for whIdx = 1:numel(whList)
curWh = whList{whIdx};
curRunIds = curWh.parameter.run_id.values(:);
fp = QueryFilter();
fp.where('Runs', 'run_id', 'GREATER_EQUAL', 3153);
fp.where('Runs', 'symbolrate', 'EQUALS', baudrates(whIdx));
fp.where('Runs', 'fiber_length', 'EQUALS', 0);
fp.where('Runs', 'db_mode', 'EQUALS', '"no_db"');
fp.where('Runs', 'pam_level', 'EQUALS', pamformats(whIdx));
[dataTable, ~] = db.queryDB(fp, metadataFields);
dataTable.run_id = numericColumn(dataTable.run_id);
dataTable = dataTable(ismember(dataTable.run_id, curRunIds), :);
missingRunIds = setdiff(curRunIds, dataTable.run_id);
if ~isempty(missingRunIds)
warning("recombine_warehouses:MissingMetadata", ...
"%d run_id(s) from PAM %.0f warehouse were not found in the metadata query.", ...
numel(missingRunIds), pamformats(whIdx));
end
metaTables{end+1} = dataTable; %#ok<AGROW>
end
runMetaTable = vertcat(metaTables{:});
[~, uniqueIdx] = unique(numericColumn(runMetaTable.run_id), "stable");
runMetaTable = runMetaTable(uniqueIdx, :);
numericFields = ["run_id", "pam_level", "symbolrate", "interference_path_length", "sir"];
for fieldIdx = 1:numel(numericFields)
fieldName = numericFields(fieldIdx);
if ismember(fieldName, string(runMetaTable.Properties.VariableNames))
runMetaTable.(char(fieldName)) = numericColumn(runMetaTable.(char(fieldName)));
end
end
[~, sortIdx] = sort(runMetaTable.run_id);
runMetaTable = runMetaTable(sortIdx, :);
end
function x = numericColumn(x)
if isnumeric(x)
x = double(x);
elseif iscell(x)
x = str2double(string(x));
elseif isstring(x) || ischar(x) || iscategorical(x)
x = str2double(string(x));
else
x = double(x);
end
x = x(:);
end
function whMerged = mergeDataStoragesByUnion(whList)
templateWh = whList{1};
paramNames = cellstr(templateWh.fn);
for whIdx = 2:numel(whList)
if ~isequal(sort(cellstr(whList{whIdx}.fn)), sort(paramNames))
error("recombine_warehouses:ParameterMismatch", ...
"All input warehouses must use the same parameter names.");
end
end
mergedParams = struct();
for paramIdx = 1:numel(paramNames)
paramName = paramNames{paramIdx};
values = [];
for whIdx = 1:numel(whList)
newValues = whList{whIdx}.parameter.(paramName).values(:).';
values = [values, newValues]; %#ok<AGROW>
end
values = unique(values, "stable");
if isnumeric(values)
values = sort(values);
end
mergedParams.(paramName) = values;
end
whMerged = DataStorage(mergedParams);
for whIdx = 1:numel(whList)
curWh = whList{whIdx};
curStorageNames = fieldnames(curWh.sto);
for storageIdx = 1:numel(curStorageNames)
storageName = curStorageNames{storageIdx};
if ~isfield(whMerged.sto, storageName)
whMerged.addStorage(storageName);
end
for linIdx = 1:numel(curWh.sto.(storageName))
storedValue = curWh.sto.(storageName){linIdx};
if isempty(storedValue)
continue
end
targetLinIdx = mapLinearIndex(curWh, whMerged, linIdx);
existingValue = whMerged.sto.(storageName){targetLinIdx};
whMerged.sto.(storageName){targetLinIdx} = mergeStoredValue(existingValue, storedValue);
end
end
end
end
function wh = addRunMetadataStorages(wh, runMetaTable)
metadataStorageNames = ["meta_pam_level", "meta_symbolrate", ...
"meta_interference_path_length", "meta_sir"];
metadataTableFields = ["pam_level", "symbolrate", "interference_path_length", "sir"];
for fieldIdx = 1:numel(metadataStorageNames)
if ~isfield(wh.sto, metadataStorageNames(fieldIdx))
wh.addStorage(char(metadataStorageNames(fieldIdx)));
end
end
for linIdx = 1:wh.getLastLinIndice()
physStruct = linearIndexToStruct(wh, linIdx);
if ~isfield(physStruct, "run_id")
continue
end
metaIdx = find(runMetaTable.run_id == physStruct.run_id, 1);
if isempty(metaIdx)
continue
end
for fieldIdx = 1:numel(metadataStorageNames)
wh.sto.(char(metadataStorageNames(fieldIdx))){linIdx} = ...
runMetaTable.(char(metadataTableFields(fieldIdx)))(metaIdx);
end
end
end
function whConfig = buildConfigWarehouse(whRun, runMetaTable, algorithmStorageNames, configFields)
runParamNames = cellstr(whRun.fn);
extraParamNames = setdiff(string(runParamNames), "run_id", "stable");
configParams = struct();
for fieldIdx = 1:numel(configFields)
fieldName = configFields(fieldIdx);
values = unique(runMetaTable.(char(fieldName))(:).', "stable");
if isnumeric(values)
values = sort(values);
end
configParams.(char(fieldName)) = values;
end
for paramIdx = 1:numel(extraParamNames)
paramName = extraParamNames(paramIdx);
configParams.(char(paramName)) = whRun.parameter.(char(paramName)).values;
end
whConfig = DataStorage(configParams);
for storageIdx = 1:numel(algorithmStorageNames)
whConfig.addStorage(algorithmStorageNames{storageIdx});
end
whConfig.addStorage("run_id");
for storageIdx = 1:numel(algorithmStorageNames)
storageName = algorithmStorageNames{storageIdx};
for linIdx = 1:numel(whRun.sto.(storageName))
storedValue = whRun.sto.(storageName){linIdx};
if isempty(storedValue)
continue
end
sourcePhys = linearIndexToStruct(whRun, linIdx);
metaIdx = find(runMetaTable.run_id == sourcePhys.run_id, 1);
if isempty(metaIdx)
continue
end
targetArgs = configTargetArgs(whConfig, sourcePhys, runMetaTable, metaIdx, configFields);
targetLinIdx = whConfig.getIndicesByPhys(targetArgs);
existingValue = whConfig.sto.(storageName){targetLinIdx};
whConfig.sto.(storageName){targetLinIdx} = mergeStoredValue(existingValue, storedValue);
end
end
for linIdx = 1:whRun.getLastLinIndice()
sourcePhys = linearIndexToStruct(whRun, linIdx);
metaIdx = find(runMetaTable.run_id == sourcePhys.run_id, 1);
if isempty(metaIdx)
continue
end
targetArgs = configTargetArgs(whConfig, sourcePhys, runMetaTable, metaIdx, configFields);
targetLinIdx = whConfig.getIndicesByPhys(targetArgs);
whConfig.sto.run_id{targetLinIdx} = mergeUniqueNumeric( ...
whConfig.sto.run_id{targetLinIdx}, sourcePhys.run_id);
end
end
function targetArgs = configTargetArgs(whConfig, sourcePhys, runMetaTable, metaIdx, configFields)
targetArgs = cell(1, numel(whConfig.fn));
for paramIdx = 1:numel(whConfig.fn)
paramName = string(whConfig.fn(paramIdx));
if any(configFields == paramName)
targetArgs{paramIdx} = runMetaTable.(char(paramName))(metaIdx);
else
targetArgs{paramIdx} = sourcePhys.(char(paramName));
end
end
end
function targetLinIdx = mapLinearIndex(sourceWh, targetWh, sourceLinIdx)
sourcePhys = linearIndexToStruct(sourceWh, sourceLinIdx);
targetArgs = cell(1, numel(targetWh.fn));
for paramIdx = 1:numel(targetWh.fn)
paramName = char(targetWh.fn(paramIdx));
targetArgs{paramIdx} = sourcePhys.(paramName);
end
targetLinIdx = targetWh.getIndicesByPhys(targetArgs);
end
function physStruct = linearIndexToStruct(wh, linIdx)
[physValues, physNames] = wh.getPhysIndicesByLinIndex(linIdx);
physStruct = struct();
for paramIdx = 1:numel(physNames)
physStruct.(char(physNames{paramIdx})) = physValues{paramIdx};
end
end
function out = mergeStoredValue(existingValue, newValue)
if isempty(existingValue)
out = newValue;
return
end
existingPackages = asPackageCell(existingValue);
newPackages = asPackageCell(newValue);
out = [existingPackages(:); newPackages(:)].';
end
function packages = asPackageCell(value)
if isempty(value)
packages = {};
elseif iscell(value)
packages = value(:).';
else
packages = {value};
end
end
function out = mergeUniqueNumeric(existingValue, newValue)
if isempty(existingValue)
out = newValue;
else
out = unique([existingValue(:); newValue(:)].', "stable");
end
end
function plotBerOverSirForPath(whConfig, algorithmStorageNames, plotOptions)
paramNames = string(whConfig.fn);
if ~any(paramNames == "pam_level") || ~any(paramNames == "interference_path_length") || ~any(paramNames == "sir")
error("recombine_warehouses:MissingPlotParameters", ...
"The config warehouse must contain pam_level, interference_path_length, and sir parameters.");
end
pamLevels = whConfig.parameter.pam_level.values;
hasBlockUpdate = any(paramNames == "block_update");
if hasBlockUpdate
blockUpdates = whConfig.parameter.block_update.values;
else
blockUpdates = NaN;
end
if exist("linspecer", "file")
curveColors = linspecer(max(numel(algorithmStorageNames), 1));
else
curveColors = lines(max(numel(algorithmStorageNames), 1));
end
figureIdx = plotOptions.figure_base;
for blockIdx = 1:numel(blockUpdates)
for pamIdx = 1:numel(pamLevels)
pamLevel = pamLevels(pamIdx);
figureIdx = figureIdx + 1;
figure(figureIdx); clf; hold on
for storageIdx = 1:numel(algorithmStorageNames)
storageName = algorithmStorageNames{storageIdx};
curveColor = curveColors(storageIdx, :);
filter = struct();
filter.pam_level = pamLevel;
filter.interference_path_length = plotOptions.path_length;
if hasBlockUpdate
filter.block_update = blockUpdates(blockIdx);
end
[sirValues, berGroups] = collectBerBySir(whConfig, storageName, filter);
if isempty(sirValues)
continue
end
[sirValues, sortIdx] = sort(sirValues);
berGroups = berGroups(sortIdx);
berAvg = nan(1, numel(sirValues));
berMin = nan(1, numel(sirValues));
berMax = nan(1, numel(sirValues));
for sirIdx = 1:numel(sirValues)
rawBer = berGroups{sirIdx};
rawBer = rawBer(isfinite(rawBer));
if isempty(rawBer)
continue
end
scatter(repmat(sirValues(sirIdx), size(rawBer)), rawBer, ...
12, ...
"Marker", ".", ...
"MarkerEdgeColor", curveColor, ...
"MarkerFaceColor", curveColor, ...
"HandleVisibility", "off");
berAvg(sirIdx) = mean(rawBer, "omitnan");
berMin(sirIdx) = min(rawBer);
berMax(sirIdx) = max(rawBer);
end
validAvg = isfinite(sirValues) & isfinite(berAvg);
if ~any(validAvg)
continue
end
if plotOptions.use_boundedlines && exist("boundedline", "file")
yLower = max(berAvg - berMin, 0);
yUpper = max(berMax - berAvg, 0);
yBounds = [yLower(:), yUpper(:)];
[hl, hp] = boundedline(sirValues(:), berAvg(:), yBounds, ...
"alpha", "transparency", plotOptions.boundedline_alpha, ...
"cmap", curveColor, ...
"nan", "fill", ...
"orientation", "vert");
set(hl, "LineStyle", "none", "Marker", "none", "HandleVisibility", "off");
set(hp, "HandleVisibility", "off", "LineStyle", "none");
end
scatter(sirValues(validAvg), berAvg(validAvg), ...
54, ...
"Marker", "o", ...
"MarkerEdgeColor", curveColor, ...
"MarkerFaceColor", "none", ...
"LineWidth", 1.2, ...
"DisplayName", storageName);
fitMask = validAvg & berAvg > 0;
if nnz(fitMask) >= 2
fitOrder = min(plotOptions.polyfit_order_max, nnz(fitMask) - 1);
fitCoeff = polyfit(sirValues(fitMask), log10(berAvg(fitMask)), fitOrder);
xFit = linspace(min(sirValues(fitMask)), max(sirValues(fitMask)), 300);
yFit = 10 .^ polyval(fitCoeff, xFit);
plot(xFit, yFit, ...
"LineWidth", 1.2, ...
"LineStyle", "--", ...
"Marker", "none", ...
"Color", curveColor, ...
"HandleVisibility", "off");
end
end
if hasBlockUpdate
title(sprintf("BER over SIR, PAM %.0f, path %.0f m, block update = %g", ...
pamLevel, plotOptions.path_length, blockUpdates(blockIdx)));
else
title(sprintf("BER over SIR, PAM %.0f, path %.0f m", ...
pamLevel, plotOptions.path_length));
end
xlabel("SIR (dB)");
ylabel("BER");
xlim(plotOptions.xlim);
grid on
box on
if exist("beautifyBERplot", "file")
beautifyBERplot("logscale", true, "setcolors", false, "setmarkers", false);
else
set(gca, "YScale", "log");
end
legend("Location", "best", "Interpreter", "none");
end
end
end
function [sirValues, berGroups] = collectBerBySir(whConfig, storageName, filter)
sirValues = [];
berGroups = {};
for linIdx = 1:numel(whConfig.sto.(storageName))
storedValue = whConfig.sto.(storageName){linIdx};
if isempty(storedValue)
continue
end
physStruct = linearIndexToStruct(whConfig, linIdx);
if ~matchesFilter(physStruct, filter)
continue
end
berValues = extractBerValues(storedValue);
if isempty(berValues)
continue
end
sirValue = physStruct.sir;
sirIdx = find(sirValues == sirValue, 1);
if isempty(sirIdx)
sirValues(end+1) = sirValue; %#ok<AGROW>
berGroups{end+1} = berValues; %#ok<AGROW>
else
berGroups{sirIdx} = [berGroups{sirIdx}, berValues]; %#ok<AGROW>
end
end
end
function tf = matchesFilter(physStruct, filter)
tf = true;
filterFields = fieldnames(filter);
for fieldIdx = 1:numel(filterFields)
fieldName = filterFields{fieldIdx};
if ~isfield(physStruct, fieldName) || physStruct.(fieldName) ~= filter.(fieldName)
tf = false;
return
end
end
end
function berValues = extractBerValues(value)
berValues = [];
if isstruct(value)
if isfield(value, "metrics") && isfield(value.metrics, "BER")
berValues(end+1) = value.metrics.BER;
end
elseif iscell(value)
for valueIdx = 1:numel(value)
berValues = [berValues, extractBerValues(value{valueIdx})]; %#ok<AGROW>
end
end
end

View File

@@ -3,8 +3,8 @@ dsp_options = struct();
dsp_options.mode = "run_id";
dsp_options.recipe = @mpi_recipe_dev;
dsp_options.append_to_db = false;
dsp_options.start_occurence = 2;
dsp_options.max_occurences = 1;
dsp_options.start_occurence = 1;
dsp_options.max_occurences = 15;
dsp_options.debug_plots = false;
dsp_options.database_type = "mysql";
@@ -23,95 +23,58 @@ db = DBHandler("dataBase", [dsp_options.dataBase],...
"user", dsp_options.user, "password", dsp_options.password);
%%
fp = QueryFilter();
fp.where('Runs','run_id','GREATER_EQUAL',3153);
fp.where('Runs', 'symbolrate', 'EQUALS', 112e9);
fp.where('Runs', 'fiber_length', 'EQUALS', 0);
fp.where('Runs', 'interference_path_length', 'EQUALS', 1000);
fp.where('Runs', 'sir', 'LESS_EQUAL', 20);
fp.where('Runs', 'db_mode', 'EQUALS', '"no_db"');
% fp.where('Runs', 'is_mpi', 'EQUALS', 1);
fp.where('Runs', 'pam_level', 'EQUALS', 4);
% fp.where('Runs', 'v_bias', 'EQUALS', 2.65);
[dataTable,~] = db.queryDB(fp, db.getTableFieldNames('Runs'));
pamformats = [4,6,8];
baudrates = [112e9,96e9,72e9];
% [~, uniqueSirRows] = unique(dataTable.sir, "stable");
% dataTable = dataTable(uniqueSirRows, :);
for i = 1:3
B = baudrates(i);
M = pamformats(i);
fp = QueryFilter();
fp.where('Runs','run_id','GREATER_EQUAL',3153);
fp.where('Runs', 'symbolrate', 'EQUALS', B); % 72 96 112
fp.where('Runs', 'fiber_length', 'EQUALS', 0);
fp.where('Runs', 'interference_path_length', 'EQUALS', 1000);
fp.where('Runs', 'sir', 'LESS_EQUAL', 20);
fp.where('Runs', 'db_mode', 'EQUALS', '"no_db"');
% fp.where('Runs', 'is_mpi', 'EQUALS', 1);
fp.where('Runs', 'pam_level', 'EQUALS', M);
% fp.where('Runs', 'v_bias', 'EQUALS', 2.65);
[dataTable,~] = db.queryDB(fp, db.getTableFieldNames('Runs'));
[~, sortIdx] = sort(dataTable.sir, 'descend');
dataTable = dataTable(sortIdx, :);
run_ids = dataTable.run_id;
%% === Warehouse setup ===
dsp_options.userParameters = struct();
dsp_options.userParameters.block_update = 1;%[1,2,4,8,16,32,64,112,224,448,512,1024,2048,2048*2,2048*4];%%logspace(-3.8,-1,22);%[linspace(2,4096,22)];
wh = DataStorage(dsp_options.userParameters);
%%
n_realizations = (dsp_options.max_occurences - dsp_options.start_occurence + 1);
n_userparams = prod(wh.dim);
n_run_ids = numel(run_ids);
parallel_jobs = n_userparams * n_run_ids;
queried_jobs = n_realizations * n_userparams * n_run_ids;
fprintf("-> [ %d run_id(s) × %d userParam combination(s) = %d parallel job(s) ] × %d realizations = %d total jobs \n", ...
n_run_ids, n_userparams, parallel_jobs, n_realizations, queried_jobs);
%% === Run ===
% submitJobs returns the raw per-job results and the filled Warehouse. For a
% single run_id and remove_dc = [0, 1], results is a 2-by-1 cell array.
[results, wh] = submitJobs(run_ids, dsp_options, processingMode.serial, ...
"wh", wh, ...
"waitbar", true);
% savepath = fullfile("C:","Users","Silas","Documents","MATLAB","imdd_simulation","projects","Diss","MPI_revisit","algorithms",sprintf("pam_%d_results.mat",M));
% save(wh,savepath)
[~, sortIdx] = sort(dataTable.sir, 'descend');
dataTable = dataTable(sortIdx, :);
dataTable = dataTable(1, :);
run_ids = dataTable.run_id;
if isempty(run_ids)
error("run_minimal_recipe:MissingRunIds", ...
"Set run_ids to one or more known run IDs before running this example.");
end
%% === Warehouse setup ===
dsp_options.userParameters = struct();
% dsp_options.userParameters.smoothing_length = [0,linspace(100,1000,10),2500,5000,10000];
wh = DataStorage(dsp_options.userParameters);
%%
n_realizations = (dsp_options.max_occurences - dsp_options.start_occurence + 1);
n_userparams = prod(wh.dim);
n_run_ids = numel(run_ids);
parallel_jobs = n_userparams * n_run_ids;
queried_jobs = n_realizations * n_userparams * n_run_ids;
fprintf("-> [ %d run_id(s) × %d userParam combination(s) = %d parallel job(s) ] × %d realizations = %d total jobs \n", ...
n_run_ids, n_userparams, parallel_jobs, n_realizations, queried_jobs);
%% === Run ===
% submitJobs returns the raw per-job results and the filled Warehouse. For a
% single run_id and remove_dc = [0, 1], results is a 2-by-1 cell array.
[results, wh] = submitJobs(run_ids, dsp_options, processingMode.serial, ...
"wh", wh, ...
"waitbar", true);
%% Analyze
storageNames = fieldnames(wh.sto);
result = wh.sto.(storageNames{1});
result = result(:).';
% Each stored entry is a package cell containing one or more occurrences.
ber_all = cell(1,numel(result));
for result_idx = 1:numel(result)
packageCell = result{result_idx};
if isempty(packageCell)
ber_all{result_idx} = NaN;
continue
end
ber_values = nan(1,numel(packageCell));
for package_idx = 1:numel(packageCell)
if isstruct(packageCell{package_idx}) && isfield(packageCell{package_idx},"metrics")
ber_values(package_idx) = packageCell{package_idx}.metrics.BER;
end
end
ber_all{result_idx} = ber_values;
end
maxPackages = max(cellfun(@numel,ber_all));
ber_mat = nan(maxPackages,numel(ber_all));
for k = 1:numel(ber_all)
ber_mat(1:numel(ber_all{k}),k) = ber_all{k};
end
ber = mean(ber_mat,1,"omitnan");
x = dataTable.sir(:).';
if numel(x) ~= numel(ber)
x = 1:numel(ber);
end
figure(2026); clf; hold on
plot(x,ber);
for k = 1:numel(x)
scatter(repmat(x(k),maxPackages,1),ber_mat(:,k))
end
beautifyBERplot("logscale",true,"setcolors",false,"setmarkers",true);

View File

@@ -0,0 +1,73 @@
% === DSP settings ===
dsp_options = struct();
dsp_options.mode = "run_id";
dsp_options.recipe = @mpi_recipe_dev;
dsp_options.append_to_db = false;
dsp_options.start_occurence = 1;
dsp_options.max_occurences = 15;
dsp_options.debug_plots = false;
dsp_options.database_type = "mysql";
dsp_options.dataBase = "labor";
dsp_options.storage_path = "W:\labdata\ECOC Silas\ecoc_2025";
dsp_options.server = "192.168.178.192";
dsp_options.port = 3306;
dsp_options.user = "silas";
dsp_options.password = "silas";
db = DBHandler("dataBase", [dsp_options.dataBase],...
"type", dsp_options.database_type,...
"server", dsp_options.server,...
"user", dsp_options.user, "password", dsp_options.password);
%%
fp = QueryFilter();
fp.where('Runs','run_id','GREATER_EQUAL',3153);
fp.where('Runs', 'symbolrate', 'EQUALS', 72e9); % 72 96 112
fp.where('Runs', 'fiber_length', 'EQUALS', 0);
% fp.where('Runs', 'interference_path_length', 'EQUALS', 1000);
% fp.where('Runs', 'sir', 'LESS_EQUAL', 50);
fp.where('Runs', 'db_mode', 'EQUALS', '"no_db"');
% fp.where('Runs', 'is_mpi', 'EQUALS', 1);
fp.where('Runs', 'pam_level', 'EQUALS', 8);
% fp.where('Runs', 'v_bias', 'EQUALS', 2.65);
[dataTable,~] = db.queryDB(fp, db.getTableFieldNames('Runs'));
% [~, uniqueSirRows] = unique(dataTable.sir, "stable");
% dataTable = dataTable(uniqueSirRows, :);
[~, sortIdx] = sort(dataTable.sir, 'descend');
dataTable = dataTable(sortIdx, :);
% dataTable = dataTable(1, :);
run_ids = dataTable.run_id;
if isempty(run_ids)
error("run_minimal_recipe:MissingRunIds", ...
"Set run_ids to one or more known run IDs before running this example.");
end
%% === Warehouse setup ===
dsp_options.userParameters = struct();
dsp_options.userParameters.block_update = [1,2,4,8,16,32,64,112,224,448,512,1024,2048,2048*2,2048*4];%%logspace(-3.8,-1,22);%[linspace(2,4096,22)];
wh = DataStorage(dsp_options.userParameters);
%%
n_realizations = (dsp_options.max_occurences - dsp_options.start_occurence + 1);
n_userparams = prod(wh.dim);
n_run_ids = numel(run_ids);
parallel_jobs = n_userparams * n_run_ids;
queried_jobs = n_realizations * n_userparams * n_run_ids;
fprintf("-> [ %d run_id(s) × %d userParam combination(s) = %d parallel job(s) ] × %d realizations = %d total jobs \n", ...
n_run_ids, n_userparams, parallel_jobs, n_realizations, queried_jobs);
%% === Run ===
% submitJobs returns the raw per-job results and the filled Warehouse. For a
% single run_id and remove_dc = [0, 1], results is a 2-by-1 cell array.
[results, wh] = submitJobs(run_ids, dsp_options, processingMode.parallel, ...
"wh", wh, ...
"waitbar", true);

View File

@@ -0,0 +1,360 @@
%% Analyze
% === DSP settings ===
dsp_options = struct();
dsp_options.mode = "run_id";
dsp_options.recipe = @mpi_recipe_dev;
dsp_options.append_to_db = false;
dsp_options.start_occurence = 1;
dsp_options.max_occurences = 15;
dsp_options.debug_plots = false;
dsp_options.database_type = "mysql";
dsp_options.dataBase = "labor";
dsp_options.storage_path = "W:\labdata\ECOC Silas\ecoc_2025";
dsp_options.server = "192.168.178.192";
dsp_options.port = 3306;
dsp_options.user = "silas";
dsp_options.password = "silas";
db = DBHandler("dataBase", [dsp_options.dataBase],...
"type", dsp_options.database_type,...
"server", dsp_options.server,...
"user", dsp_options.user, "password", dsp_options.password);
%%
fp = QueryFilter();
fp.where('Runs','run_id','GREATER_EQUAL',3153);
fp.where('Runs', 'symbolrate', 'EQUALS', 72e9); % 72 96 112
fp.where('Runs', 'fiber_length', 'EQUALS', 0);
fp.where('Runs', 'interference_path_length', 'EQUALS', 1000);
% fp.where('Runs', 'sir', 'LESS_EQUAL', 50);
fp.where('Runs', 'db_mode', 'EQUALS', '"no_db"');
% fp.where('Runs', 'is_mpi', 'EQUALS', 1);
fp.where('Runs', 'pam_level', 'EQUALS', 8);
% fp.where('Runs', 'v_bias', 'EQUALS', 2.65);
[dataTable,~] = db.queryDB(fp, db.getTableFieldNames('Runs'));
% [~, uniqueSirRows] = unique(dataTable.sir, "stable");
% dataTable = dataTable(uniqueSirRows, :);
[~, sortIdx] = sort(dataTable.sir, 'descend');
dataTable = dataTable(sortIdx, :);
% dataTable = dataTable(1, :);
run_ids = dataTable.run_id;
%%
wh_ = load("C:\Users\Silas\Documents\MATLAB\imdd_simulation\projects\Diss\MPI_revisit\parallelization_analysis\wh_block_update_pam8_combined.mat");
wh = wh_.wh;
% wh_ = load("C:\Users\Silas\Documents\MATLAB\imdd_simulation\projects\Diss\MPI_revisit\parallelization_analysis\wh_block_update_pam4_short_blocks.mat");
% wh2 = wh_.wh;
%
% wh = mergeDataStorages(wh1,wh2);
%%
storageNames = fieldnames(wh.sto);
ber_by_storage = struct();
ber_mat_by_storage = struct();
sir_values = dataTable.sir(:).';
block_updates = wh.parameter.block_update.values;
storage_parameter_names = cellstr(wh.fn);
block_axis = find(strcmp(storage_parameter_names,"block_update"),1);
if exist('linspecer','file')
curve_colors = linspecer(max(numel(storageNames),1));
else
curve_colors = lines(max(numel(storageNames),1));
end
fec_ber_threshold = 2e-2;%3.8e-3;
fit_coeff_by_storage = struct();
required_sir_fec_by_storage = struct();
for storage_idx = 1:numel(storageNames)
storageName = storageNames{storage_idx};
fit_coeff_by_storage.(storageName) = cell(1,numel(block_updates));
required_sir_fec_by_storage.(storageName) = nan(1,numel(block_updates));
end
for block_idx = 1:numel(block_updates)
block_update = block_updates(block_idx);
figure(3000 + block_idx); clf; hold on
for storage_idx = 1:numel(storageNames)
storageName = storageNames{storage_idx};
curve_color = curve_colors(storage_idx,:);
result = wh.sto.(storageName);
if ~isempty(block_axis) && size(result,block_axis) == numel(block_updates)
result_subs = repmat({':'},1,ndims(result));
result_subs{block_axis} = block_idx;
result_for_block = result(result_subs{:});
result_for_block = result_for_block(:).';
else
result_for_block = result(:).';
end
% Each stored entry is a package cell containing one or more occurrences.
ber_all = cell(1,numel(result_for_block));
for result_idx = 1:numel(result_for_block)
packageCell = result_for_block{result_idx};
if isempty(packageCell)
ber_all{result_idx} = NaN;
continue
end
ber_values = nan(1,numel(packageCell));
for package_idx = 1:numel(packageCell)
if isstruct(packageCell{package_idx}) && isfield(packageCell{package_idx},"metrics")
ber_values(package_idx) = packageCell{package_idx}.metrics.BER;
end
end
ber_all{result_idx} = ber_values;
end
maxPackages = max(cellfun(@numel,ber_all));
ber_mat = nan(maxPackages,numel(ber_all));
for k = 1:numel(ber_all)
ber_mat(1:numel(ber_all{k}),k) = ber_all{k};
end
% Replace outliers in ber_mat with NaN (operate column-wise).
for col = 1:size(ber_mat,2)
colData = ber_mat(:,col);
if all(isnan(colData)); continue; end
mask = isoutlier(colData);
ber_mat(mask,col) = NaN;
end
ber = mean(ber_mat,1,"omitnan");
ber_min = nan(1,numel(ber));
ber_max = nan(1,numel(ber));
for k = 1:numel(ber)
ber_here = ber_mat(:,k);
ber_here = ber_here(isfinite(ber_here));
if isempty(ber_here)
continue
end
ber_min(k) = min(ber_here);
ber_max(k) = max(ber_here);
end
x = sir_values;
if numel(x) ~= numel(ber)
x = 1:numel(ber);
end
ber_by_storage.(storageName){block_idx} = ber;
ber_mat_by_storage.(storageName){block_idx} = ber_mat;
y_lower = max(ber - ber_min,0);
y_upper = max(ber_max - ber,0);
y_bounds = [y_lower(:), y_upper(:)];
[hl, hp] = boundedline(x(:),ber(:),y_bounds, ...
'alpha', 'transparency', 0.1, ...
'cmap', curve_color, ...
'nan', 'fill', ...
'orientation', 'vert');
set(hl, ...
'LineWidth',1.4, ...
'LineStyle','none', ...
'Marker','o', ...
'Markersize',5,...
'Color',curve_color, ...
'DisplayName',storageName);
set(hp, ...
'HandleVisibility','off', ...
'LineStyle','none');
for k = 1:numel(x)
scatter(repmat(x(k),maxPackages,1),ber_mat(:,k), ...
16, ...
'Marker','.', ...
'MarkerEdgeColor',curve_color, ...
'MarkerFaceColor',curve_color, ...
'HandleVisibility','off');
end
fit_mask = isfinite(x) & isfinite(ber) & ber > 0;
if nnz(fit_mask) >= 2
fit_order = min(3,nnz(fit_mask)-1);
fit_coeff = polyfit(x(fit_mask),log10(ber(fit_mask)),fit_order);
x_fit = linspace(min(x(fit_mask)),max(x(fit_mask)),300);
y_fit = 10.^polyval(fit_coeff,x_fit);
fit_coeff_by_storage.(storageName){block_idx} = fit_coeff;
sir_req = NaN;
threshold_mask = isfinite(y_fit) & y_fit <= fec_ber_threshold;
if any(threshold_mask)
first_threshold_idx = find(threshold_mask,1,"first");
if first_threshold_idx == 1
sir_req = x_fit(first_threshold_idx);
else
x_pair = x_fit(first_threshold_idx-1:first_threshold_idx);
y_pair = log10(y_fit(first_threshold_idx-1:first_threshold_idx));
if all(isfinite(y_pair)) && diff(y_pair) ~= 0
sir_req = interp1(y_pair,x_pair,log10(fec_ber_threshold), ...
"linear","extrap");
else
sir_req = x_fit(first_threshold_idx);
end
end
end
required_sir_fec_by_storage.(storageName)(block_idx) = sir_req;
plot(x_fit,y_fit, ...
'LineWidth',1.2, ...
'LineStyle','--', ...
'Marker','none', ...
'Color',curve_color, ...
'HandleVisibility','off');
end
end
title(sprintf("BER over SIR, block update = %g",block_update));
xlabel("SIR (dB)");
ylabel("BER");
xlim([15 35]);
beautifyBERplot("logscale",true,"setcolors",false,"setmarkers",false);
legend("Location","best","Interpreter","none");
end
%% Required SIR to reach FEC over parallelization
figure(5000); clf; hold on
for storage_idx = 1:numel(storageNames)
storageName = storageNames{storage_idx};
curve_color = curve_colors(storage_idx,:);
required_sir = required_sir_fec_by_storage.(storageName);
valid = isfinite(block_updates) & isfinite(required_sir);
if ~any(valid)
continue
end
plot(block_updates(valid),required_sir(valid), ...
'LineWidth',1.4, ...
'LineStyle','-', ...
'Marker','o', ...
'MarkerSize',5, ...
'Color',curve_color, ...
'DisplayName',storageName);
end
set(gca,'XScale','log');
xticks(block_updates);
xticklabels(string(block_updates));
grid on
box on
xlabel("Parallelization / block update");
ylabel(sprintf("Required SIR at BER = %.1e (dB)",fec_ber_threshold));
title("Required SIR to reach FEC over parallelization");
legend("Location","best","Interpreter","none");
%%
function whMerged = mergeDataStorages(varargin)
% mergeDataStorages merges compatible DataStorage objects by physical indices.
% Existing duplicate entries are concatenated when both values are package cells.
whList = varargin;
templateWh = whList{1};
paramNames = cellstr(templateWh.fn);
mergedParams = struct();
for param_idx = 1:numel(paramNames)
paramName = paramNames{param_idx};
values = templateWh.parameter.(paramName).values(:).';
for wh_idx = 2:numel(whList)
curWh = whList{wh_idx};
if ~isequal(sort(cellstr(curWh.fn)),sort(paramNames))
error("mergeDataStorages:ParameterMismatch", ...
"All warehouses must use the same parameter names.");
end
if ~isfield(curWh.parameter,paramName)
error("mergeDataStorages:ParameterMismatch", ...
"Warehouse %d does not contain parameter '%s'.",wh_idx,paramName);
end
newValues = curWh.parameter.(paramName).values(:).';
for value_idx = 1:numel(newValues)
if ~ismember(newValues(value_idx),values)
values(end+1) = newValues(value_idx); %#ok<AGROW>
end
end
end
if strcmp(paramName,"block_update") && isnumeric(values)
values = sort(values);
end
mergedParams.(paramName) = values;
end
whMerged = DataStorage(mergedParams);
for wh_idx = 1:numel(whList)
curWh = whList{wh_idx};
curStorageNames = fieldnames(curWh.sto);
for storage_idx = 1:numel(curStorageNames)
storageName = curStorageNames{storage_idx};
if ~isfield(whMerged.sto,storageName)
whMerged.addStorage(storageName);
end
for lin_idx = 1:numel(curWh.sto.(storageName))
storedValue = curWh.sto.(storageName){lin_idx};
if isempty(storedValue)
continue
end
[physValues,physNames] = curWh.getPhysIndicesByLinIndex(lin_idx);
physNames = cellfun(@char,physNames,'UniformOutput',false);
targetSubscripts = cell(1,numel(whMerged.fn));
for param_idx = 1:numel(whMerged.fn)
paramName = char(whMerged.fn(param_idx));
sourceParamIdx = find(strcmp(physNames,paramName),1);
if isempty(sourceParamIdx)
error("mergeDataStorages:ParameterMismatch", ...
"Storage entry is missing parameter '%s'.",paramName);
end
targetSubscripts{param_idx} = whMerged.getIndexByPhys(paramName,physValues{sourceParamIdx});
end
if isscalar(targetSubscripts)
targetLinIdx = targetSubscripts{1};
else
targetLinIdx = sub2ind(whMerged.getStorageSize(),targetSubscripts{:});
end
existingValue = whMerged.sto.(storageName){targetLinIdx};
whMerged.sto.(storageName){targetLinIdx} = mergeStoredValue(existingValue,storedValue);
end
end
end
end
function out = mergeStoredValue(existingValue,newValue)
if isempty(existingValue)
out = newValue;
elseif iscell(existingValue) && iscell(newValue)
out = [existingValue(:); newValue(:)].';
else
out = newValue;
end
end

View File

@@ -127,7 +127,9 @@ for sirval = [20,30,50]
pngTikzPath = "04_Experimental_Evaluation/tikz/mpi/" + exportName + "-1.png";
figure(sirval + pathlen);
exportNakedPlotWithTikz(gca, pngFile, tikzFile, pngTikzPath, 150);
exportLevelScatterTikz(gca, pngFile, tikzFile, pngTikzPath, ...
"resolutionDpi", 150, ...
"generatedBy", "plot_mpi_timesignal.m");
end
end
@@ -145,309 +147,3 @@ end
% var_slope = p(1);
% var_offset = p(2);c
function exportNakedPlotWithTikz(sourceAx, pngFile, tikzFile, pngTikzPath, resolutionDpi)
outputDir = fileparts(pngFile);
if ~exist(outputDir, "dir")
mkdir(outputDir);
end
exportScatterLayerPng(sourceAx, pngFile, resolutionDpi);
linePlots = collectTikzLinePlots(sourceAx);
textAnnotations = collectTikzTextAnnotations(sourceAx);
writeTikzPngAxis(tikzFile, pngTikzPath, sourceAx, linePlots, textAnnotations);
end
function exportScatterLayerPng(sourceAx, pngFile, resolutionDpi)
sourceFig = ancestor(sourceAx, "figure");
xLimits = sourceAx.XLim;
yLimits = sourceAx.YLim;
scatterHandles = findGraphicsObjectsByType(sourceAx, "scatter");
exportFig = figure( ...
"Visible", "off", ...
"Color", "white", ...
"Units", sourceFig.Units, ...
"Position", sourceFig.Position);
cleanupFigure = onCleanup(@() close(exportFig));
exportAx = copyobj(sourceAx, exportFig);
exportAx.Units = "normalized";
exportAx.Position = [0 0 1 1];
exportAx.XLim = xLimits;
exportAx.YLim = yLimits;
exportAx.XLimMode = "manual";
exportAx.YLimMode = "manual";
stripAxesToScatterOnly(exportAx);
hideAxesInkForRasterExport(exportAx);
fprintf("Exporting scatter PNG with XLim=[%g %g], YLim=[%g %g], scatter objects=%d: %s\n", ...
xLimits(1), xLimits(2), yLimits(1), yLimits(2), numel(scatterHandles), pngFile);
drawnow;
exportFig.PaperPositionMode = "auto";
print(exportFig, char(pngFile), "-dpng", sprintf("-r%d", resolutionDpi));
end
function stripAxesToScatterOnly(ax)
plotObjects = findall(ax);
for objIdx = 1:numel(plotObjects)
obj = plotObjects(objIdx);
if obj == ax || ~isvalid(obj)
continue
end
objType = string(get(obj, "Type"));
if lower(objType) ~= "scatter"
delete(obj);
end
end
end
function hideAxesInkForRasterExport(ax)
ax.Visible = "on";
ax.Color = "white";
ax.Box = "off";
ax.XGrid = "off";
ax.YGrid = "off";
ax.XMinorGrid = "off";
ax.YMinorGrid = "off";
ax.XTick = [];
ax.YTick = [];
ax.XColor = "white";
ax.YColor = "white";
ax.Title.String = "";
ax.XLabel.String = "";
ax.YLabel.String = "";
end
function handles = findGraphicsObjectsByType(parentHandle, objectType)
allHandles = findall(parentHandle);
matches = false(size(allHandles));
for handleIdx = 1:numel(allHandles)
currentHandle = allHandles(handleIdx);
if ~isvalid(currentHandle)
continue
end
matches(handleIdx) = lower(string(get(currentHandle, "Type"))) == lower(string(objectType));
end
handles = allHandles(matches);
end
function linePlots = collectTikzLinePlots(sourceAx)
lineHandles = findGraphicsObjectsByType(sourceAx, "line");
lineHandles = flipud(lineHandles(:));
linePlots = repmat(struct( ...
"xData", [], ...
"yData", [], ...
"color", [], ...
"lineWidth", []), numel(lineHandles), 1);
validLineCount = 0;
for lineIdx = 1:numel(lineHandles)
lineHandle = lineHandles(lineIdx);
xData = lineHandle.XData(:);
yData = lineHandle.YData(:);
validSamples = isfinite(xData) & isfinite(yData);
if ~any(validSamples)
continue
end
validLineCount = validLineCount + 1;
linePlots(validLineCount).xData = xData(validSamples);
linePlots(validLineCount).yData = yData(validSamples);
linePlots(validLineCount).color = lineHandle.Color;
linePlots(validLineCount).lineWidth = lineHandle.LineWidth;
end
linePlots = linePlots(1:validLineCount);
end
function textAnnotations = collectTikzTextAnnotations(sourceAx)
textHandles = findGraphicsObjectsByType(sourceAx, "text");
textHandles = flipud(textHandles(:));
xLimits = sourceAx.XLim;
yLimits = sourceAx.YLim;
textAnnotations = repmat(struct( ...
"x", [], ...
"y", [], ...
"text", "", ...
"anchor", "center"), numel(textHandles), 1);
validTextCount = 0;
for textIdx = 1:numel(textHandles)
textHandle = textHandles(textIdx);
labelText = string(textHandle.String);
if strlength(strtrim(labelText)) == 0
continue
end
textPosition = textHandle.Position;
if textPosition(1) < xLimits(1) || textPosition(1) > xLimits(2) || ...
textPosition(2) < yLimits(1) || textPosition(2) > yLimits(2)
continue
end
validTextCount = validTextCount + 1;
textAnnotations(validTextCount).x = textPosition(1);
textAnnotations(validTextCount).y = textPosition(2);
textAnnotations(validTextCount).text = labelText;
textAnnotations(validTextCount).anchor = matlabTextAlignmentToTikzAnchor( ...
textHandle.HorizontalAlignment, textHandle.VerticalAlignment);
end
textAnnotations = textAnnotations(1:validTextCount);
end
function writeTikzPngAxis(tikzFile, pngTikzPath, sourceAx, linePlots, textAnnotations)
outputDir = fileparts(tikzFile);
if ~exist(outputDir, "dir")
mkdir(outputDir);
end
xLimits = sourceAx.XLim;
yLimits = sourceAx.YLim;
fid = fopen(tikzFile, "w");
if fid < 0
error("plot_mpi_timesignal:TikzOpenFailed", ...
"Could not open TikZ export file: %s", tikzFile);
end
cleanupFile = onCleanup(@() fclose(fid));
axisOptions = {
" every axis/.append style={font=\scriptsize},"
"width=\fwidth,"
"height=\fheight,"
"at={(0\fwidth,0\fheight)},"
"scale only axis,"
"axis on top,"
"xmin=" + formatPgfNumber(xLimits(1)) + ","
"xmax=" + formatPgfNumber(xLimits(2)) + ","
"xlabel style={font=\color{white!15!black}\small},"
"xlabel={Time in $\mu$s},"
"ymin=" + formatPgfNumber(yLimits(1)) + ","
"ymax=" + formatPgfNumber(yLimits(2)) + ","
"ylabel style={font=\color{white!15!black}\small},"
"ylabel={Normalized Amplitude},"
"axis background/.style={fill=white},"
"xmajorgrids,"
"ymajorgrids,"
"grid style={line width=0.4pt, dotted, color=black!20},"
"scaled ticks=false,"
"tick label style={/pgf/number format/fixed, /pgf/number format/1000 sep={}},"
"legend columns=1"
};
fprintf(fid, "%% This file was generated by plot_mpi_timesignal.m.\n");
fprintf(fid, "%%\n");
for lineIdx = 1:numel(linePlots)
fprintf(fid, "%s\n", formatTikzColorDefinition(lineIdx, linePlots(lineIdx).color));
end
if ~isempty(linePlots)
fprintf(fid, "%%\n");
end
fprintf(fid, "%s\n\n", "\begin{tikzpicture}");
fprintf(fid, "%s\n", "\begin{axis}[%");
for optionIdx = 1:numel(axisOptions)
fprintf(fid, "%s\n", axisOptions{optionIdx});
end
fprintf(fid, "%s\n", "]");
graphicsLine = sprintf( ...
"\\addplot [forget plot] graphics [xmin=%s, xmax=%s, ymin=%s, ymax=%s] {%s};", ...
formatPgfNumber(xLimits(1)), ...
formatPgfNumber(xLimits(2)), ...
formatPgfNumber(yLimits(1)), ...
formatPgfNumber(yLimits(2)), ...
char(strrep(pngTikzPath, "\", "/")));
fprintf(fid, "%s\n\n", graphicsLine);
writeTikzLinePlots(fid, linePlots);
writeTikzTextAnnotations(fid, textAnnotations);
fprintf(fid, "%s\n\n", "\end{axis}");
fprintf(fid, "%s", "\end{tikzpicture}%");
end
function valueText = formatPgfNumber(value)
valueText = string(sprintf("%.15g", value));
end
function colorDefinition = formatTikzColorDefinition(colorIdx, rgbColor)
colorDefinition = sprintf( ...
"\\definecolor{mycolor%d}{rgb}{%.5f,%.5f,%.5f}%%", ...
colorIdx, rgbColor(1), rgbColor(2), rgbColor(3));
end
function writeTikzLinePlots(fid, linePlots)
for lineIdx = 1:numel(linePlots)
fprintf(fid, "\\addplot [color=mycolor%d, line width=%.1fpt, forget plot]\n", ...
lineIdx, linePlots(lineIdx).lineWidth);
fprintf(fid, " table[row sep=crcr]{%%\n");
for sampleIdx = 1:numel(linePlots(lineIdx).xData)
fprintf(fid, "%s\t%s\\\\\n", ...
formatPgfNumber(linePlots(lineIdx).xData(sampleIdx)), ...
formatPgfNumber(linePlots(lineIdx).yData(sampleIdx)));
end
fprintf(fid, "};\n\n");
end
end
function writeTikzTextAnnotations(fid, textAnnotations)
for textIdx = 1:numel(textAnnotations)
fprintf(fid, "\\node[font=\\scriptsize, anchor=%s, fill=white, draw=black!40, rounded corners=2pt, inner sep=2pt]\n", ...
textAnnotations(textIdx).anchor);
fprintf(fid, " at (axis cs:%s,%s) {%s};\n\n", ...
formatPgfNumber(textAnnotations(textIdx).x), ...
formatPgfNumber(textAnnotations(textIdx).y), ...
escapeTikzText(textAnnotations(textIdx).text));
end
end
function anchor = matlabTextAlignmentToTikzAnchor(horizontalAlignment, verticalAlignment)
horizontalAlignment = string(horizontalAlignment);
verticalAlignment = string(verticalAlignment);
if horizontalAlignment == "left"
horizontalAnchor = "west";
elseif horizontalAlignment == "right"
horizontalAnchor = "east";
else
horizontalAnchor = "";
end
if verticalAlignment == "top"
verticalAnchor = "north";
elseif verticalAlignment == "bottom"
verticalAnchor = "south";
else
verticalAnchor = "";
end
if strlength(horizontalAnchor) > 0 && strlength(verticalAnchor) > 0
anchor = verticalAnchor + " " + horizontalAnchor;
elseif strlength(horizontalAnchor) > 0
anchor = horizontalAnchor;
elseif strlength(verticalAnchor) > 0
anchor = verticalAnchor;
else
anchor = "center";
end
end
function textOut = escapeTikzText(textIn)
textOut = char(textIn);
% textOut = strrep(textOut, "\", "\textbackslash{}");
textOut = strrep(textOut, "{", "\{");
textOut = strrep(textOut, "}", "\}");
textOut = strrep(textOut, "%", "\%");
textOut = strrep(textOut, "&", "\&");
textOut = strrep(textOut, "#", "\#");
textOut = strrep(textOut, "_", "\_");
% textOut = strrep(textOut, "$", "\$");
end

View File

@@ -1,49 +1,10 @@
% dsp_options.database_type = 'mysql';
% dsp_options.dataBase = 'labor';
% dsp_options.storage_path = 'Z:\2025\ECOC Silas\ecoc_2025\';
% database = DBHandler("dataBase", [dsp_options.dataBase], "type", dsp_options.database_type);
% filterParams = database.tables;
% filterParams.Runs.loop_id = 209;
% % filterParams.Configurations = struct( ...
% % 'symbolrate', 112e9, ... %[224,336,360,390,420,448]
% % 'fiber_length', 0, ...
% % 'db_mode', '"no_db"', ...
% % 'interference_attenuation', [], ...
% % 'interference_path_length', 1000, ...
% % 'is_mpi', 1, ...
% % 'pam_level', 4, ...
% % 'wavelength', 1310, ...
% % 'precomp_amp', [], ...
% % 'signal_attenuation', [], ...
% % 'v_awg', [], ...
% % 'v_bias', [] ...
% % );
%
% % if 1
% % % filterParams.EqualizerParameters.dc_buffer_len = 1;
% % filterParams.EqualizerParameters.ffe_buffer_len = 1;
% % filterParams.EqualizerParameters.smoothing_buffer_len = 4096;
% % filterParams.EqualizerParameters.smoothing_buffer_update = 224;
% % filterParams.EqualizerParameters.DCmu = 0;
% % end
% a = database.getTableFieldNames('Runs');
% b = database.getTableFieldNames('Results');
% c = database.getTableFieldNames('EqualizerParameters');
% d = [a;b;c];
%
% [dataTable,~] = database.queryDB(filterParams, d);
%
% selectedFields = {'Configurations.run_id' 'Runs.loop_id' 'Runs.date_of_run' 'Runs.rx_raw_path' 'Runs.bitrate' 'Runs.v_bias' 'Runs.v_awg' 'Runs.precomp_amp' 'Runs.symbolrate' 'Runs.pam_level'...
% 'Runs.db_mode' 'Runs.rop_attenuation' 'Runs.is_mpi' 'Runs.interference_attenuation' 'Runs.interference_path_length' 'Runs.signal_attenuation' ...
% 'EqualizerParameters.equalizer_structure' 'EqualizerParameters.diff_precode' 'EqualizerParameters.eq_id' 'EqualizerParameters.dc_buffer_len' 'EqualizerParameters.ffe_buffer_len' 'EqualizerParameters.smoothing_buffer_len' 'EqualizerParameters.smoothing_buffer_update' 'EqualizerParameters.DCmu' 'Measurements.power_pd_in' ...
% 'Measurements.power_mpi_interference' 'Measurements.power_mpi_signal' 'Results.BER' 'Results.BER_precoded' 'Results.EVM' 'Results.SNR' 'Results.GMI' 'Results.Alpha' 'Results.date_of_processing'};
db = DBHandler("type","mysql","dataBase",'labor');
fp = QueryFilter();
% fp.where('mpi_superview', 'loop_id','EQUALS', 209);
% fp.where('mpi_superview','run_id','GREATER_EQUAL',3153);
fp.where('mpi_superview', 'symbolrate','EQUALS', 112e9);
fp.where('mpi_superview', 'pam_level','EQUALS', 4);
fn = [db.getTableFieldNames('mpi_superview')];
@@ -67,7 +28,7 @@ figure()
tiledlayout(1, 1, 'TileSpacing', 'compact', 'Padding', 'compact');
y_here = 0;
figcnt = 0;
for int_len = 1000%[0,50,300,1000]
for int_len = 1%[0,50,300,1000]
figcnt = figcnt+1;
% figure(int_len+1);
nexttile;
@@ -139,7 +100,7 @@ for int_len = 1000%[0,50,300,1000]
% Modify values in 'interference_path_length' where the condition is met
dataTable.interference_path_length(dataTable.interference_path_length < 101 & dataTable.interference_path_length > 1) = 50;
dataTable.interference_path_length(dataTable.interference_path_length == 1) = 0;
% dataTable.interference_path_length(dataTable.interference_path_length == 1) = 0;
dataTable = dataTable(dataTable.interference_path_length == int_len, :);