start with 400G analysis work

This commit is contained in:
Silas Oettinghaus
2026-07-20 10:22:11 +02:00
parent cae81c0dae
commit 125d8508ca
43 changed files with 4089 additions and 123 deletions

View File

@@ -0,0 +1,608 @@
%% BER over SIR from saved MPI simulation warehouses grouped by linewidth
% Loads a saved simulation warehouse and plots one BER-vs-SIR curve per
% linewidth for each algorithm.
% clear;
% clc;
%% Load data
resultFile = "";
if resultFile == ""
resultDir = fullfile(fileparts(mfilename("fullpath")), "results");
files = dir(fullfile(resultDir, "mpi_simulation_*.mat"));
if isempty(files)
error("PLOT_mpi_simulation_linewidth_vs_sir:NoResultFiles", ...
"No mpi_simulation_*.mat files found in %s.", resultDir);
end
[~, newestIdx] = max([files.datenum]);
resultFile = fullfile(files(newestIdx).folder, files(newestIdx).name);
end
loaded = load(resultFile, "wh", "simulation_config");
wh = loaded.wh;
fprintf("Loaded MPI simulation warehouse:\n%s\n", resultFile);
wh.showInfo;
%% Plot settings
useBoundedLines = true;
usePolyfit = true;
polyfitOrderMax = 4;
boundaryPolyfitOrderMax = 4;
fecBerThreshold = 3.8e-3;
maxBerForPlot = 0.1;
crossingSirWindow = [15 40];
boundMode = "fitStd";
algorithmMarkers = {'o','square','diamond','^','v','>','<','pentagram'};
%% Collect and clean
cleanData = collectMpiSimulationBerRows(wh);
cleanData = normalizeBerColumnName(cleanData);
cleanData = cleanData(isfinite(cleanData.BER) & cleanData.BER > 0 & ...
cleanData.BER < maxBerForPlot, :);
if isempty(cleanData)
warning("PLOT_mpi_simulation_linewidth_vs_sir:NoRows", ...
"No valid BER rows remain for plotting.");
return
end
if all(~isfinite(cleanData.laser_linewidth))
if isfield(loaded, "simulation_config") && isfield(loaded.simulation_config, "laser_linewidth")
cleanData.laser_linewidth(:) = loaded.simulation_config.laser_linewidth;
else
cleanData.laser_linewidth(:) = 0;
end
end
cleanData.clean_keep = true(height(cleanData), 1);
groupId = findgroups(cleanData.storage_name, cleanData.block_update, ...
cleanData.laser_linewidth, cleanData.sir);
for curGroup = unique(groupId(isfinite(groupId))).'
rowMask = groupId == curGroup;
berValues = cleanData.BER(rowMask);
if nnz(rowMask) > 3
cleanData.clean_keep(rowMask) = ~isoutlier(berValues);
end
end
cleanData = cleanData(cleanData.clean_keep, :);
groupVars = ["storage_name", "algorithm", "block_update", "laser_linewidth", "sir"];
summaryTable = groupsummary(cleanData, groupVars, {"mean", "min", "max"}, "BER");
summaryTable = sortrows(summaryTable, groupVars);
summaryTable.sir_exact = summaryTable.sir;
summaryTable = addBerStdBounds(summaryTable, cleanData, groupVars);
selectedBlockUpdates = unique(cleanData.block_update(isfinite(cleanData.block_update))).';
selectedAlgorithms = unique(cleanData.storage_name, "stable").';
selectedLinewidths = unique(cleanData.laser_linewidth(isfinite(cleanData.laser_linewidth))).';
selectedLinewidths = sort(selectedLinewidths);
requiredSirRows = table();
for blockIdx = 1:numel(selectedBlockUpdates)
blockUpdate = selectedBlockUpdates(blockIdx);
for algIdx = 1:numel(selectedAlgorithms)
storageName = selectedAlgorithms(algIdx);
algorithmName = string(summaryTable.algorithm(find(summaryTable.storage_name == storageName, 1, "first")));
for linewidthIdx = 1:numel(selectedLinewidths)
laserLinewidth = selectedLinewidths(linewidthIdx);
curveMask = summaryTable.storage_name == storageName & ...
summaryTable.block_update == blockUpdate & ...
summaryTable.laser_linewidth == laserLinewidth;
sirValues = summaryTable.sir_exact(curveMask).';
meanBer = summaryTable.mean_BER(curveMask).';
[~, ~, requiredSir, fitOrder] = fitBerAtFec( ...
sirValues, meanBer, polyfitOrderMax, fecBerThreshold, crossingSirWindow);
newRow = table(storageName, algorithmName, blockUpdate, ...
laserLinewidth, requiredSir, fitOrder, nnz(isfinite(sirValues) & isfinite(meanBer)), ...
'VariableNames', {'storage_name', 'algorithm', 'block_update', ...
'laser_linewidth', 'required_sir', 'fit_order', 'n_points'});
requiredSirRows = [requiredSirRows; newRow]; %#ok<AGROW>
end
end
end
fprintf("Cleaned to %d simulation BER rows across %d storage/block/linewidth/SIR groups.\n", ...
height(cleanData), height(summaryTable));
disp(groupcounts(cleanData, ["storage_name", "block_update", "laser_linewidth"]));
disp(requiredSirRows);
%% Plot BER vs SIR, linewidth as curve family
for blockIdx = 1:numel(selectedBlockUpdates)
blockUpdate = selectedBlockUpdates(blockIdx);
figure();
clf;
tiledlayout(numel(selectedAlgorithms), 1, "TileSpacing", "compact");
for algIdx = 1:numel(selectedAlgorithms)
storageName = selectedAlgorithms(algIdx);
nexttile;
hold on;
for linewidthIdx = 1:numel(selectedLinewidths)
laserLinewidth = selectedLinewidths(linewidthIdx);
lineColor = linewidthColor(linewidthIdx, numel(selectedLinewidths));
marker = algorithmMarker(storageName, algorithmMarkers);
displayName = sprintf("%s, %s", ...
algorithmDisplayName(storageName), linewidthLabel(laserLinewidth));
rawMask = cleanData.storage_name == storageName & ...
cleanData.block_update == blockUpdate & ...
cleanData.laser_linewidth == laserLinewidth;
curveMask = summaryTable.storage_name == storageName & ...
summaryTable.block_update == blockUpdate & ...
summaryTable.laser_linewidth == laserLinewidth;
if ~any(curveMask)
continue
end
scatterRows = cleanData(rawMask, :);
scatter(scatterRows.sir, scatterRows.BER, ...
22, ...
"Marker", ".", ...
"MarkerEdgeColor", lineColor, ...
"MarkerFaceColor", lineColor, ...
"HandleVisibility", "off");
sirValues = summaryTable.sir_exact(curveMask).';
meanBer = summaryTable.mean_BER(curveMask).';
boundCenterBer = summaryTable.std_center_BER(curveMask).';
boundLowerBer = summaryTable.std_lower_BER(curveMask).';
boundUpperBer = summaryTable.std_upper_BER(curveMask).';
valid = isfinite(sirValues) & isfinite(meanBer) & meanBer > 0;
if useBoundedLines && exist("boundedline", "file") && any(valid)
[xBand, centerBand, yBounds] = berStdBounds( ...
sirValues, boundCenterBer, boundLowerBer, boundUpperBer, ...
boundMode, boundaryPolyfitOrderMax);
[hl, hp] = boundedline(xBand, centerBand, yBounds, ...
'alpha', 'transparency', 0.08, ...
'cmap', lineColor, ...
'nan', 'fill', ...
'orientation', 'vert');
set(hl, "LineStyle", "none", "LineWidth", 1, "Marker", "none", ...
"HandleVisibility", "off", "DisplayName", displayName);
set(hp, "LineStyle", "-", "HandleVisibility", "off", "Marker", "none");
end
plot(sirValues(valid), meanBer(valid), ...
"LineStyle", "-", ...
"Marker", marker, ...
"MarkerSize", 3, ...
"LineWidth", 1, ...
"Color", lineColor, ...
"MarkerFaceColor", "w", ...
"MarkerEdgeColor", lineColor, ...
"DisplayName", displayName, ...
"HandleVisibility", "on");
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", lineColor, ...
"HandleVisibility", "off");
end
end
end
yline(2.2e-4, "LineWidth", 1, "LineStyle", "--", "HandleVisibility", "off");
yline(fecBerThreshold, "LineWidth", 1, "LineStyle", "--", "HandleVisibility", "off");
yline(2e-2, "LineWidth", 1, "LineStyle", "--", "HandleVisibility", "off");
title(sprintf("%s, block update %.0f", algorithmDisplayName(storageName), blockUpdate));
xlabel("SIR (dB)");
ylabel("BER");
set(gca, "YScale", "log");
ylim([9e-5, maxBerForPlot]);
xlim(crossingSirWindow);
grid on;
box on;
legend("Location", "northeast", "Interpreter", "none");
if exist("beautifyBERplot", "file")
beautifyBERplot("logscale", true, "setcolors", false, ...
"setmarkers", false, "changemarkers", false);
end
end
end
%% Plot required SIR at FEC over linewidth
for blockIdx = 1:numel(selectedBlockUpdates)
blockUpdate = selectedBlockUpdates(blockIdx);
figure();
clf;
hold on;
for algIdx = 1:numel(selectedAlgorithms)
storageName = selectedAlgorithms(algIdx);
algColor = algorithmColor(storageName);
marker = algorithmMarker(storageName, algorithmMarkers);
displayName = algorithmDisplayName(storageName);
rowMask = requiredSirRows.storage_name == storageName & ...
requiredSirRows.block_update == blockUpdate;
x = requiredSirRows.laser_linewidth(rowMask).';
y = requiredSirRows.required_sir(rowMask).';
[x, sortIdx] = sort(x);
y = y(sortIdx);
valid = isfinite(x) & isfinite(y);
if ~any(valid)
continue
end
plot(x(valid), y(valid), ...
"LineWidth", 1.4, ...
"LineStyle", "-", ...
"Marker", marker, ...
"MarkerSize", 5, ...
"Color", algColor, ...
"MarkerFaceColor", "w", ...
"MarkerEdgeColor", algColor, ...
"DisplayName", char(displayName));
end
set(gca, "XScale", "log");
xticks(selectedLinewidths);
xticklabels(arrayfun(@linewidthLabel, selectedLinewidths, "UniformOutput", false));
ylim(crossingSirWindow);
grid on;
box on;
xlabel("Laser linewidth");
ylabel(sprintf("Required SIR at BER = %.1e (dB)", fecBerThreshold));
title(sprintf("Required SIR over linewidth, block update %.0f", blockUpdate));
legend("Location", "best", "Interpreter", "none");
if exist("beautifyBERplot", "file")
beautifyBERplot("logscale", false, "setcolors", false, ...
"setmarkers", false, "changemarkers", false);
end
end
%% Local helpers
function data = collectMpiSimulationBerRows(wh)
storageNames = string(fieldnames(wh.sto));
data = table();
for storageIdx = 1:numel(storageNames)
storageName = storageNames(storageIdx);
storage = wh.sto.(char(storageName));
for linIdx = 1:numel(storage)
package = storage{linIdx};
ber = extractPackageBer(package);
if ~isfinite(ber)
continue
end
[physValues, physNames] = wh.getPhysIndicesByLinIndex(linIdx);
phys = struct();
for physIdx = 1:numel(physNames)
phys.(char(physNames{physIdx})) = physValues{physIdx};
end
algorithm = extractPackageAlgorithm(package, storageName);
newRow = table( ...
storageName, ...
algorithm, ...
readPhysValue(phys, "sir", NaN), ...
readPhysValue(phys, "block_update", NaN), ...
readPhysValue(phys, "random_key", NaN), ...
readPhysValue(phys, "laser_linewidth", NaN), ...
ber, ...
'VariableNames', {'storage_name', 'algorithm', 'sir', ...
'block_update', 'random_key', 'laser_linewidth', 'BER'});
data = [data; newRow]; %#ok<AGROW>
end
end
end
function data = normalizeBerColumnName(data)
variableNames = string(data.Properties.VariableNames);
if ismember("BER", variableNames)
return
end
if ismember("ber", variableNames)
data.Properties.VariableNames(variableNames == "ber") = {'BER'};
return
end
error("PLOT_mpi_simulation_linewidth_vs_sir:MissingBerColumn", ...
"Could not find a BER or ber column in the simulation BER table.");
end
function value = readPhysValue(phys, name, defaultValue)
if isfield(phys, name)
value = phys.(name);
else
value = defaultValue;
end
end
function ber = extractPackageBer(package)
ber = NaN;
if isempty(package)
return
end
if iscell(package)
package = package{1};
end
if isstruct(package) && isfield(package, "metrics")
metrics = package.metrics;
if isstruct(metrics) && isfield(metrics, "BER")
ber = metrics.BER;
elseif isobject(metrics) && isprop(metrics, "BER")
ber = metrics.BER;
end
end
end
function algorithm = extractPackageAlgorithm(package, fallbackName)
algorithm = fallbackName;
if isempty(package)
return
end
if iscell(package)
package = package{1};
end
if isstruct(package) && isfield(package, "mpi_reduction_config") && ...
isfield(package.mpi_reduction_config, "algorithm")
algorithm = string(package.mpi_reduction_config.algorithm);
end
end
function summaryTable = addBerStdBounds(summaryTable, cleanData, groupVars)
nGroups = height(summaryTable);
stdCenter = NaN(nGroups, 1);
stdLower = NaN(nGroups, 1);
stdUpper = NaN(nGroups, 1);
for groupIdx = 1:nGroups
rowMask = true(height(cleanData), 1);
for varIdx = 1:numel(groupVars)
varName = groupVars(varIdx);
rowMask = rowMask & cleanData.(char(varName)) == summaryTable.(char(varName))(groupIdx);
end
[stdCenter(groupIdx), stdLower(groupIdx), stdUpper(groupIdx)] = ...
logBerMeanStdInterval(cleanData.BER(rowMask));
end
summaryTable.std_center_BER = stdCenter;
summaryTable.std_lower_BER = stdLower;
summaryTable.std_upper_BER = stdUpper;
end
function [centerBer, lowerBer, upperBer] = logBerMeanStdInterval(berValues)
berValues = berValues(isfinite(berValues) & berValues > 0);
if isempty(berValues)
centerBer = NaN;
lowerBer = NaN;
upperBer = NaN;
return
end
logBer = log10(berValues(:));
centerLog = mean(logBer, "omitnan");
stdLog = std(logBer, 0, "omitnan");
centerBer = 10 .^ centerLog;
lowerBer = 10 .^ (centerLog - stdLog);
upperBer = 10 .^ (centerLog + stdLog);
end
function [xBand, centerBand, yBounds] = berStdBounds(sirValues, centerBer, lowerBer, upperBer, boundMode, maxOrder)
if boundMode == "directStd"
[xBand, centerBand, yBounds] = directBerBounds(sirValues, centerBer, lowerBer, upperBer);
return
end
[xBand, centerBand, yBounds] = fittedBerBounds(sirValues, centerBer, lowerBer, upperBer, maxOrder);
end
function [xBand, centerBand, yBounds] = directBerBounds(sirValues, centerBer, lowerBer, upperBer)
valid = isfinite(sirValues) & isfinite(centerBer) & isfinite(lowerBer) & ...
isfinite(upperBer) & centerBer > 0 & lowerBer > 0 & upperBer > 0;
xBand = sirValues(valid).';
centerBand = centerBer(valid).';
lowerBand = lowerBer(valid).';
upperBand = upperBer(valid).';
[xBand, orderIdx] = sort(xBand(:));
centerBand = centerBand(orderIdx);
lowerBand = lowerBand(orderIdx);
upperBand = upperBand(orderIdx);
lowerTmp = min(lowerBand, upperBand);
upperBand = max(lowerBand, upperBand);
lowerBand = lowerTmp;
centerBand = min(max(centerBand, lowerBand), upperBand);
yBounds = [max(centerBand - lowerBand, 0), max(upperBand - centerBand, 0)];
end
function [xBand, centerBand, yBounds] = fittedBerBounds(sirValues, meanBer, minBer, maxBer, maxOrder)
valid = isfinite(sirValues) & isfinite(meanBer) & isfinite(minBer) & ...
isfinite(maxBer) & meanBer > 0 & minBer > 0 & maxBer > 0;
x = sirValues(valid);
yMean = meanBer(valid);
yMin = minBer(valid);
yMax = maxBer(valid);
if numel(x) < 2
xBand = x(:);
centerBand = yMean(:);
yBounds = [max(yMean(:) - yMin(:), 0), max(yMax(:) - yMean(:), 0)];
return
end
[x, orderIdx] = sort(x(:));
yMean = yMean(orderIdx);
yMin = yMin(orderIdx);
yMax = yMax(orderIdx);
xBand = linspace(min(x), max(x), 300).';
fitOrder = min(maxOrder, numel(unique(x)) - 1);
if fitOrder < 1
centerBand = interp1(x, yMean, xBand, "linear", "extrap");
lowerBand = interp1(x, yMin, xBand, "linear", "extrap");
upperBand = interp1(x, yMax, xBand, "linear", "extrap");
else
centerBand = fitLogBer(x, yMean, xBand, fitOrder);
lowerBand = fitLogBer(x, yMin, xBand, fitOrder);
upperBand = fitLogBer(x, yMax, xBand, fitOrder);
end
lowerTmp = min(lowerBand, upperBand);
upperBand = max(lowerBand, upperBand);
lowerBand = lowerTmp;
centerBand = min(max(centerBand, lowerBand), upperBand);
yBounds = [max(centerBand - lowerBand, 0), max(upperBand - centerBand, 0)];
end
function yFit = fitLogBer(x, y, xFit, fitOrder)
coeff = polyfit(x, log10(y), fitOrder);
yFit = 10 .^ polyval(coeff, xFit);
end
function [xFit, yFit, requiredSir, fitOrder] = fitBerAtFec( ...
sirValues, meanBer, polyfitOrderMax, fecBerThreshold, crossingSirWindow)
xFit = NaN;
yFit = NaN;
requiredSir = NaN;
fitOrder = NaN;
valid = isfinite(sirValues) & isfinite(meanBer) & meanBer > 0 & ...
sirValues >= crossingSirWindow(1) & sirValues <= crossingSirWindow(2);
if nnz(valid) < 2
return
end
sirValues = sirValues(valid);
meanBer = meanBer(valid);
[sirValues, sortIdx] = sort(sirValues);
meanBer = meanBer(sortIdx);
fitOrder = min(polyfitOrderMax, nnz(valid) - 1);
fitCoeff = polyfit(sirValues, log10(meanBer), fitOrder);
xFit = linspace(max(min(sirValues), crossingSirWindow(1)), ...
min(max(sirValues), crossingSirWindow(2)), 300);
yFit = 10 .^ polyval(fitCoeff, xFit);
thresholdMask = isfinite(yFit) & yFit <= fecBerThreshold;
if ~any(thresholdMask)
return
end
firstThresholdIdx = find(thresholdMask, 1, "first");
if firstThresholdIdx == 1
requiredSir = xFit(firstThresholdIdx);
return
end
xPair = xFit(firstThresholdIdx - 1:firstThresholdIdx);
yPair = log10(yFit(firstThresholdIdx - 1:firstThresholdIdx));
if all(isfinite(yPair)) && diff(yPair) ~= 0
requiredSir = interp1(yPair, xPair, log10(fecBerThreshold), ...
"linear", "extrap");
else
requiredSir = xFit(firstThresholdIdx);
end
if requiredSir < crossingSirWindow(1) || requiredSir > crossingSirWindow(2)
requiredSir = NaN;
end
end
function label = algorithmDisplayName(algorithmName)
algorithmName = string(algorithmName);
switch algorithmName
case {"plain_ffe", "conventional_ffe"}
label = "FFE only";
case "a2_tracked_levels"
label = "ACT";
case "a2_residual"
label = "L-DCA";
case "a1_moving_average"
label = "DCA";
case "dc_tracking"
label = "DCT";
otherwise
label = algorithmName;
end
end
function color = algorithmColor(algorithmName)
algorithmName = string(algorithmName);
switch algorithmName
case {"plain_ffe", "conventional_ffe"}
color = [0.3467 0.5360 0.6907];
case "a2_tracked_levels"
color = [0.9153 0.2816 0.2878];
case "a2_residual"
color = [0.4416 0.7490 0.4322];
case "a1_moving_average"
color = [1.0000 0.5984 0.2000];
case "dc_tracking"
color = [0.6769 0.4447 0.7114];
otherwise
color = [0 0 0];
end
end
function color = linewidthColor(linewidthIdx, nLinewidths)
if nLinewidths <= 1
color = [0.3467 0.5360 0.6907];
return
end
if exist("cbrewer2", "file")
colors = cbrewer2("Set1", max(nLinewidths, 3));
else
colors = lines(nLinewidths);
end
color = colors(linewidthIdx, :);
end
function label = linewidthLabel(laserLinewidth)
if abs(laserLinewidth) >= 1e6
label = sprintf("%.3g MHz", laserLinewidth * 1e-6);
elseif abs(laserLinewidth) >= 1e3
label = sprintf("%.3g kHz", laserLinewidth * 1e-3);
else
label = sprintf("%.3g Hz", laserLinewidth);
end
end
function marker = algorithmMarker(algorithmName, algorithmMarkers)
algorithmOrder = ["conventional_ffe", "dc_tracking", "a2_tracked_levels", ...
"a2_residual", "a1_moving_average"];
markerIdx = find(algorithmOrder == string(algorithmName), 1);
if isempty(markerIdx)
markerIdx = 1;
end
marker = algorithmMarkers{mod(markerIdx - 1, numel(algorithmMarkers)) + 1};
end

View File

@@ -0,0 +1,214 @@
function output = mpi_simulation_worker(userParameters, simulation_config)
%MPI_SIMULATION_WORKER Build one decorrelated MPI point and run the DSP recipe.
arguments
userParameters struct
simulation_config struct
end
config = applyDefaults(simulation_config);
[Scpe_sig_raw, Symbols, Tx_bits, dataTable] = buildMpiScopeSignal(userParameters, config);
output = config.recipe(Scpe_sig_raw, Symbols, Tx_bits, ...
"fsym", config.fsym, ...
"M", config.M, ...
"duob_mode", config.duob_mode, ...
"dataTable", dataTable, ...
"userParameters", userParameters, ...
"debug_plots", config.debug_plots);
end
function config = applyDefaults(config)
defaults = struct( ...
"M", 4, ...
"fsym", 112e9, ...
"mpi_path_meter", 1000, ...
"laser_linewidth", 150e3, ...
"recipe", @mpi_recipe_dev, ...
"debug_plots", false, ...
"fdac", 256e9, ...
"fadc", 256e9, ...
"kover", 16, ...
"random_key", 1, ...
"rcalpha", 0.05, ...
"duob_mode", db_mode.no_db, ...
"vbias_rel", 0.5, ...
"u_pi", 3, ...
"laser_wavelength", 1293, ...
"link_length_km", 1, ...
"rop", -9, ...
"rx_bwl", 80e9, ...
"scope_bwl", 110e9, ...
"alpha", 0);
names = fieldnames(defaults);
for nameIdx = 1:numel(names)
name = names{nameIdx};
if ~isfield(config, name) || isempty(config.(name))
config.(name) = defaults.(name);
end
end
end
function [Scpe_sig, Symbols, Tx_bits, dataTable] = buildMpiScopeSignal(userParameters, config)
sir = readUserParameter(userParameters, "sir", 30);
randomKey = readUserParameter(userParameters, "random_key", config.random_key);
laserLinewidth = readUserParameter(userParameters, "laser_linewidth", config.laser_linewidth);
config.laser_linewidth = laserLinewidth;
Pform = Pulseformer( ...
"fsym", config.fsym, ...
"fdac", 4 * config.fsym, ...
"pulse", "rrc", ...
"pulselength", 16, ...
"alpha", config.rcalpha);
vbias = -config.vbias_rel * config.u_pi;
mainSource = buildOpticalSource(Pform, config, randomKey, randomKey + 1, vbias);
interferenceSource = buildOpticalSource(Pform, config, randomKey + 100000, ...
randomKey + 100001, vbias);
Symbols = mainSource.Symbols;
Tx_bits = mainSource.Tx_bits;
Opt_sig = applyDecorrelatedMpi(mainSource.Opt_sig, interferenceSource.Opt_sig, ...
sir, config.link_length_km);
Rx_sig = Amplifier( ...
"amp_mode", "ideal_no_noise", ...
"gain_mode", "output_power", ...
"amplification_db", config.rop).process(Opt_sig);
Rx_sig = Photodiode( ...
"fsimu", config.fdac * config.kover, ...
"dark_current", 2e-08, ...
"responsivity", 1, ...
"temperature", 20, ...
"nep", 1.8e-11).process(Rx_sig);
Rx_sig.signal = real(Rx_sig.signal);
Rx_sig = Filter( ...
"filtdegree", 4, ...
"f_cutoff", config.rx_bwl, ...
"fs", config.fdac * config.kover, ...
"filterType", filtertypes.butterworth, ...
"active", true).process(Rx_sig);
Rx_sig.signal = real(Rx_sig.signal);
scopeFilter = Filter( ...
"filtdegree", 4, ...
"f_cutoff", config.scope_bwl, ...
"fs", config.fadc, ...
"filterType", filtertypes.butterworth, ...
"active", true);
Scpe_sig = Scope( ...
"fsimu", config.fdac * config.kover, ...
"fadc", config.fadc, ...
"delay", 0, ...
"fixed_delay", 0, ...
"filtertype", filtertypes.butterworth, ...
"samplingdelay", 0, ...
"rand_samplingdelay", 0, ...
"freq_offset", 0, ...
"samp_jitter", 0, ...
"adcresolution", 8, ...
"quantbuffer", 0.1, ...
"block_dc", 1, ...
"lpf_active", 1, ...
"H_lpf", scopeFilter).process(Rx_sig);
Scpe_sig.signal = real(Scpe_sig.signal);
HighpassFilter = Filter( ...
"filtdegree", 6, ...
"f_cutoff", 1e6, ...
"fs", config.fadc, ...
"filterType", filtertypes.butterworth, ...
"active", true, "lowpass",0);
Scpe_sig = HighpassFilter.process(Scpe_sig);
dataTable = table( ...
sir, ...
config.fsym, ...
config.M, ...
config.mpi_path_meter, ...
laserLinewidth, ...
randomKey, ...
'VariableNames', {'sir', 'symbolrate', 'pam_level', ...
'interference_path_length', 'laser_linewidth', 'random_key'});
end
function source = buildOpticalSource(Pform, config, sourceRandomKey, laserRandomKey, vbias)
[Digi_sig, Symbols, Tx_bits] = PAMsource( ...
"fsym", config.fsym, ...
"M", config.M, ...
"order", 18, ...
"useprbs", 0, ...
"fs_out", config.fdac, ...
"applyclipping", 0, ...
"clipfactor", 1.5, ...
"applypulseform", 1, ...
"pulseformer", Pform, ...
"randkey", sourceRandomKey, ...
"duobinary_mode", config.duob_mode, ...
"mrds_code", 0, ...
"mrds_blocklength", 512).process();
El_sig = M8199A("kover", config.kover).process(Digi_sig);
El_sig = Filter("f_cutoff",65e9,"filterType","butterworth","filtdegree",4,"fs",El_sig.fs).process(El_sig);
El_sig = El_sig.normalize("mode", "oneone");
scaling = 0.6 * (config.u_pi / 2 - abs(vbias - config.u_pi / 2));
El_sig = El_sig .* scaling;
Opt_sig = EML( ...
"mode", eml_mode.im_cosinus, ...
"power", 3, ...
"fsimu", El_sig.fs, ...
"lambda", config.laser_wavelength, ...
"bias", vbias, ...
"u_pi", config.u_pi, ...
"linewidth", config.laser_linewidth, ...
"randomkey", laserRandomKey, ...
"alpha", config.alpha).process(El_sig);
source = struct( ...
"Opt_sig", Opt_sig, ...
"Symbols", Symbols, ...
"Tx_bits", Tx_bits);
end
function Opt_sig = applyDecorrelatedMpi(main_sig, interference_sig, sir, linkLengthKm)
interference_sig = Amplifier( ...
"amp_mode", "ideal_no_noise", ...
"gain_mode", "output_power", ...
"amplification_db", main_sig.power - sir).process(interference_sig);
if numel(main_sig.signal) ~= numel(interference_sig.signal)
minLength = min(numel(main_sig.signal), numel(interference_sig.signal));
main_sig.signal = main_sig.signal(1:minLength);
interference_sig.signal = interference_sig.signal(1:minLength);
end
combined_sig = main_sig + interference_sig;
Opt_sig = Fiber( ...
"fsimu", combined_sig.fs, ...
"fiber_length", linkLengthKm, ...
"alpha", 0.3, ...
"D", 0, ...
"lambda0", 1310, ...
"gamma", 0, ...
"Dslope", 0.07).process(combined_sig);
end
function value = readUserParameter(userParameters, name, defaultValue)
if isfield(userParameters, name)
value = userParameters.(name);
else
value = defaultValue;
end
end

View File

@@ -0,0 +1,476 @@
%RUN_MPI_SIMULATION_RECIPE Queue MPI simulation points and store DSP packages.
clear;
% clc;
%% Fixed experiment-like simulation setup
simulation_config = struct();
simulation_config.M = 4;
simulation_config.fsym = 112e9;
simulation_config.mpi_path_meter = 1000;
simulation_config.laser_linewidth = 150e3;%[150e3 150e3 250e3 500e3 750e3 1e6 5e6 10e6 20e6 50e6];
simulation_config.laser_linewidths = simulation_config.laser_linewidth;
simulation_config.recipe = @mpi_recipe_dev;
simulation_config.debug_plots = true;
simulation_config.waitbar = true;
simulation_config.processing_mode = processingMode.parallel;
simulation_config.num_workers = 0;
simulation_config.random_keys = 1:10;
simulation_config.fdac = 256e9;
simulation_config.fadc = 256e9;
simulation_config.kover = 16;
simulation_config.random_key = 1;
simulation_config.rcalpha = 0.05;
simulation_config.duob_mode = db_mode.no_db;
simulation_config.vbias_rel = 0.5;
simulation_config.u_pi = 3;
simulation_config.laser_wavelength = 1310;
simulation_config.link_length_km = 0.5;
simulation_config.rop = -7.5;
simulation_config.rx_bwl = 70e9;
simulation_config.scope_bwl = 70e9;
simulation_config.alpha = 0;
%% Warehouse sweep setup
sweep_params = struct();
sweep_params.sir = 15:3:45;
sweep_params.block_update = 1;
sweep_params.random_key = simulation_config.random_keys;
sweep_params.laser_linewidth = simulation_config.laser_linewidths;
wh = DataStorage(sweep_params);
fprintf("Requested %d MPI simulation job(s).\n", wh.getLastLinIndice());
%% Run queued simulations
[results, wh] = submitMpiSimulationJobs(wh, simulation_config, ...
"mode", simulation_config.processing_mode, ...
"waitbar", simulation_config.waitbar, ...
"numWorkers", simulation_config.num_workers);
%% Save result artifact
result_dir = fullfile(fileparts(mfilename("fullpath")), "results");
if ~exist(result_dir, "dir")
mkdir(result_dir);
end
timestamp = string(datetime("now", "Format", "yyyyMMdd_HHmmss"));
result_file = fullfile(result_dir, "mpi_simulation_" + timestamp + ".mat");
save(result_file, "wh", "simulation_config", "results");
fprintf("Saved MPI simulation warehouse to:\n%s\n", result_file);
%% BER inspection
plotMpiSimulationBer(wh);
function plotMpiSimulationBer(wh)
storageNames = string(fieldnames(wh.sto));
if isempty(storageNames)
warning("run_mpi_simulation_recipe:NoStorage", ...
"The warehouse does not contain any stored DSP packages.");
return
end
useBoundedLines = true;
usePolyfit = true;
polyfitOrderMax = 4;
boundaryPolyfitOrderMax = 4;
fecBerThreshold = 3.8e-3;
maxBerForPlot = 0.1;
boundMode = "fitStd";
algorithmMarkers = {'o','square','diamond','^','v','>','<','pentagram'};
cleanData = collectMpiSimulationBerRows(wh);
cleanData = normalizeBerColumnName(cleanData);
cleanData = cleanData(isfinite(cleanData.BER) & cleanData.BER > 0 & ...
cleanData.BER < maxBerForPlot, :);
if isempty(cleanData)
warning("run_mpi_simulation_recipe:NoBerRows", ...
"No valid BER rows remain for plotting.");
return
end
cleanData.clean_keep = true(height(cleanData), 1);
groupId = findgroups(cleanData.storage_name, cleanData.block_update, cleanData.sir);
for curGroup = unique(groupId(isfinite(groupId))).'
rowMask = groupId == curGroup;
berValues = cleanData.BER(rowMask);
if nnz(rowMask) > 3
cleanData.clean_keep(rowMask) = ~isoutlier(berValues);
end
end
cleanData = cleanData(cleanData.clean_keep, :);
groupVars = ["storage_name", "algorithm", "block_update", "sir"];
summaryTable = groupsummary(cleanData, groupVars, {"mean", "min", "max"}, "BER");
summaryTable = sortrows(summaryTable, groupVars);
summaryTable.sir_exact = summaryTable.sir;
summaryTable = addBerStdBounds(summaryTable, cleanData, groupVars);
selectedBlockUpdates = unique(cleanData.block_update(isfinite(cleanData.block_update))).';
selectedAlgorithms = unique(cleanData.storage_name, "stable").';
fprintf("Cleaned to %d simulation BER rows across %d storage/block/SIR groups.\n", ...
height(cleanData), height(summaryTable));
disp(groupcounts(cleanData, ["storage_name", "block_update"]));
figure();
clf;
tiledlayout(numel(selectedBlockUpdates), 1, "TileSpacing", "compact");
for blockIdx = 1:numel(selectedBlockUpdates)
blockUpdate = selectedBlockUpdates(blockIdx);
nexttile; hold on;
for algIdx = 1:numel(selectedAlgorithms)
storageName = selectedAlgorithms(algIdx);
algColor = algorithmColor(storageName);
marker = algorithmMarker(storageName, algorithmMarkers);
displayName = algorithmDisplayName(storageName);
rawMask = cleanData.storage_name == storageName & ...
cleanData.block_update == blockUpdate;
curveMask = summaryTable.storage_name == storageName & ...
summaryTable.block_update == blockUpdate;
if ~any(curveMask)
continue
end
scatterRows = cleanData(rawMask, :);
scatter(scatterRows.sir, scatterRows.BER, ...
26, ...
"Marker", ".", ...
"MarkerEdgeColor", algColor, ...
"MarkerFaceColor", algColor, ...
"HandleVisibility", "off");
sirValues = summaryTable.sir_exact(curveMask).';
meanBer = summaryTable.mean_BER(curveMask).';
boundCenterBer = summaryTable.std_center_BER(curveMask).';
boundLowerBer = summaryTable.std_lower_BER(curveMask).';
boundUpperBer = summaryTable.std_upper_BER(curveMask).';
valid = isfinite(sirValues) & isfinite(meanBer) & meanBer > 0;
if useBoundedLines && exist("boundedline", "file") && any(valid)
[xBand, centerBand, yBounds] = berStdBounds( ...
sirValues, boundCenterBer, boundLowerBer, boundUpperBer, ...
boundMode, boundaryPolyfitOrderMax);
[hl, hp] = boundedline(xBand, centerBand, yBounds, ...
'alpha', 'transparency', 0.1, ...
'cmap', algColor, ...
'nan', 'fill', ...
'orientation', 'vert');
set(hl, "LineStyle", "none", "LineWidth", 1, "Marker", "none", ...
"HandleVisibility", "off", "DisplayName", char(displayName));
set(hp, "LineStyle", "-", "HandleVisibility", "off", "Marker", "none");
end
plot(sirValues(valid), meanBer(valid), ...
"LineStyle", "-", ...
"Marker", marker, ...
"MarkerSize", 3, ...
"LineWidth", 1, ...
"Color", algColor, ...
"MarkerFaceColor", "w", ...
"MarkerEdgeColor", algColor, ...
"DisplayName", char(displayName), ...
"HandleVisibility", "on");
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
yline(2.2e-4, "LineWidth", 1, "LineStyle", "--", "HandleVisibility", "off");
yline(fecBerThreshold, "LineWidth", 1, "LineStyle", "--", "HandleVisibility", "off");
yline(2e-2, "LineWidth", 1, "LineStyle", "--", "HandleVisibility", "off");
title(sprintf("Simulated MPI, block update %.0f", blockUpdate));
xlabel("SIR (dB)");
ylabel("BER");
set(gca, "YScale", "log");
ylim([9e-5, maxBerForPlot]);
xlim([min(cleanData.sir) - 1, max(cleanData.sir) + 1]);
grid on;
box on;
legend("Location", "northeast", "Interpreter", "none");
if exist("beautifyBERplot", "file")
beautifyBERplot("logscale", true, "setcolors", false, "setmarkers", false);
end
end
end
function data = collectMpiSimulationBerRows(wh)
storageNames = string(fieldnames(wh.sto));
data = table();
for storageIdx = 1:numel(storageNames)
storageName = storageNames(storageIdx);
storage = wh.sto.(char(storageName));
for linIdx = 1:numel(storage)
package = storage{linIdx};
ber = extractPackageBer(package);
if ~isfinite(ber)
continue
end
[physValues, physNames] = wh.getPhysIndicesByLinIndex(linIdx);
phys = struct();
for physIdx = 1:numel(physNames)
phys.(char(physNames{physIdx})) = physValues{physIdx};
end
algorithm = extractPackageAlgorithm(package, storageName);
newRow = table( ...
storageName, ...
algorithm, ...
readPhysValue(phys, "sir", NaN), ...
readPhysValue(phys, "block_update", NaN), ...
readPhysValue(phys, "random_key", NaN), ...
readPhysValue(phys, "laser_linewidth", NaN), ...
ber, ...
'VariableNames', {'storage_name', 'algorithm', 'sir', ...
'block_update', 'random_key', 'laser_linewidth', 'BER'});
data = [data; newRow]; %#ok<AGROW>
end
end
end
function data = normalizeBerColumnName(data)
variableNames = string(data.Properties.VariableNames);
if ismember("BER", variableNames)
return
end
if ismember("ber", variableNames)
data.Properties.VariableNames(variableNames == "ber") = {'BER'};
return
end
error("run_mpi_simulation_recipe:MissingBerColumn", ...
"Could not find a BER or ber column in the simulation BER table.");
end
function value = readPhysValue(phys, name, defaultValue)
if isfield(phys, name)
value = phys.(name);
else
value = defaultValue;
end
end
function ber = extractPackageBer(package)
ber = NaN;
if isempty(package)
return
end
if iscell(package)
package = package{1};
end
if isstruct(package) && isfield(package, "metrics")
metrics = package.metrics;
if isstruct(metrics) && isfield(metrics, "BER")
ber = metrics.BER;
elseif isobject(metrics) && isprop(metrics, "BER")
ber = metrics.BER;
end
end
end
function algorithm = extractPackageAlgorithm(package, fallbackName)
algorithm = fallbackName;
if isempty(package)
return
end
if iscell(package)
package = package{1};
end
if isstruct(package) && isfield(package, "mpi_reduction_config") && ...
isfield(package.mpi_reduction_config, "algorithm")
algorithm = string(package.mpi_reduction_config.algorithm);
end
end
function summaryTable = addBerStdBounds(summaryTable, cleanData, groupVars)
nGroups = height(summaryTable);
stdCenter = NaN(nGroups, 1);
stdLower = NaN(nGroups, 1);
stdUpper = NaN(nGroups, 1);
for groupIdx = 1:nGroups
rowMask = true(height(cleanData), 1);
for varIdx = 1:numel(groupVars)
varName = groupVars(varIdx);
rowMask = rowMask & cleanData.(char(varName)) == summaryTable.(char(varName))(groupIdx);
end
[stdCenter(groupIdx), stdLower(groupIdx), stdUpper(groupIdx)] = ...
logBerMeanStdInterval(cleanData.BER(rowMask));
end
summaryTable.std_center_BER = stdCenter;
summaryTable.std_lower_BER = stdLower;
summaryTable.std_upper_BER = stdUpper;
end
function [centerBer, lowerBer, upperBer] = logBerMeanStdInterval(berValues)
berValues = berValues(isfinite(berValues) & berValues > 0);
if isempty(berValues)
centerBer = NaN;
lowerBer = NaN;
upperBer = NaN;
return
end
logBer = log10(berValues(:));
centerLog = mean(logBer, "omitnan");
stdLog = std(logBer, 0, "omitnan");
centerBer = 10 .^ centerLog;
lowerBer = 10 .^ (centerLog - stdLog);
upperBer = 10 .^ (centerLog + stdLog);
end
function [xBand, centerBand, yBounds] = berStdBounds(sirValues, centerBer, lowerBer, upperBer, boundMode, maxOrder)
if boundMode == "directStd"
[xBand, centerBand, yBounds] = directBerBounds(sirValues, centerBer, lowerBer, upperBer);
return
end
[xBand, centerBand, yBounds] = fittedBerBounds(sirValues, centerBer, lowerBer, upperBer, maxOrder);
end
function [xBand, centerBand, yBounds] = directBerBounds(sirValues, centerBer, lowerBer, upperBer)
valid = isfinite(sirValues) & isfinite(centerBer) & isfinite(lowerBer) & ...
isfinite(upperBer) & centerBer > 0 & lowerBer > 0 & upperBer > 0;
xBand = sirValues(valid).';
centerBand = centerBer(valid).';
lowerBand = lowerBer(valid).';
upperBand = upperBer(valid).';
[xBand, orderIdx] = sort(xBand(:));
centerBand = centerBand(orderIdx);
lowerBand = lowerBand(orderIdx);
upperBand = upperBand(orderIdx);
lowerTmp = min(lowerBand, upperBand);
upperBand = max(lowerBand, upperBand);
lowerBand = lowerTmp;
centerBand = min(max(centerBand, lowerBand), upperBand);
yBounds = [max(centerBand - lowerBand, 0), max(upperBand - centerBand, 0)];
end
function [xBand, centerBand, yBounds] = fittedBerBounds(sirValues, meanBer, minBer, maxBer, maxOrder)
valid = isfinite(sirValues) & isfinite(meanBer) & isfinite(minBer) & ...
isfinite(maxBer) & meanBer > 0 & minBer > 0 & maxBer > 0;
x = sirValues(valid);
yMean = meanBer(valid);
yMin = minBer(valid);
yMax = maxBer(valid);
if numel(x) < 2
xBand = x(:);
centerBand = yMean(:);
yBounds = [max(yMean(:) - yMin(:), 0), max(yMax(:) - yMean(:), 0)];
return
end
[x, orderIdx] = sort(x(:));
yMean = yMean(orderIdx);
yMin = yMin(orderIdx);
yMax = yMax(orderIdx);
xBand = linspace(min(x), max(x), 300).';
fitOrder = min(maxOrder, numel(unique(x)) - 1);
if fitOrder < 1
centerBand = interp1(x, yMean, xBand, "linear", "extrap");
lowerBand = interp1(x, yMin, xBand, "linear", "extrap");
upperBand = interp1(x, yMax, xBand, "linear", "extrap");
else
centerBand = fitLogBer(x, yMean, xBand, fitOrder);
lowerBand = fitLogBer(x, yMin, xBand, fitOrder);
upperBand = fitLogBer(x, yMax, xBand, fitOrder);
end
lowerTmp = min(lowerBand, upperBand);
upperBand = max(lowerBand, upperBand);
lowerBand = lowerTmp;
centerBand = min(max(centerBand, lowerBand), upperBand);
yBounds = [max(centerBand - lowerBand, 0), max(upperBand - centerBand, 0)];
end
function yFit = fitLogBer(x, y, xFit, fitOrder)
coeff = polyfit(x, log10(y), fitOrder);
yFit = 10 .^ polyval(coeff, xFit);
end
function label = algorithmDisplayName(algorithmName)
algorithmName = string(algorithmName);
switch algorithmName
case {"plain_ffe", "conventional_ffe"}
label = "FFE only";
case "a2_tracked_levels"
label = "ACT";
case "a2_residual"
label = "L-DCA";
case "a1_moving_average"
label = "DCA";
case "dc_tracking"
label = "DCT";
otherwise
label = algorithmName;
end
end
function color = algorithmColor(algorithmName)
algorithmName = string(algorithmName);
switch algorithmName
case {"plain_ffe", "conventional_ffe"}
color = [0.3467 0.5360 0.6907];
case "a2_tracked_levels"
color = [0.9153 0.2816 0.2878];
case "a2_residual"
color = [0.4416 0.7490 0.4322];
case "a1_moving_average"
color = [1.0000 0.5984 0.2000];
case "dc_tracking"
color = [0.6769 0.4447 0.7114];
otherwise
color = [0 0 0];
end
end
function marker = algorithmMarker(algorithmName, algorithmMarkers)
algorithmOrder = ["conventional_ffe", "dc_tracking", "a2_tracked_levels", ...
"a2_residual", "a1_moving_average"];
markerIdx = find(algorithmOrder == string(algorithmName), 1);
if isempty(markerIdx)
markerIdx = 1;
end
marker = algorithmMarkers{mod(markerIdx - 1, numel(algorithmMarkers)) + 1};
end

View File

@@ -0,0 +1,87 @@
function [results, wh] = submitMpiSimulationJobs(wh, simulation_config, options)
%SUBMITMPISIMULATIONJOBS Execute MPI simulation warehouse points via runBatch.
arguments
wh DataStorage
simulation_config struct
options.mode = processingMode.serial
options.waitbar (1,1) logical = true
options.numWorkers (1,1) double {mustBeNonnegative, mustBeInteger} = 0
options.idleTimeout (1,1) double {mustBePositive} = 300
options.cancelExistingQueue (1,1) logical = true
end
nJobs = wh.getLastLinIndice();
jobs = repmat(struct("args", {{}}, "label", "", "meta", struct()), 1, nJobs);
for linIdx = 1:nJobs
userParameters = buildUserParameters(wh, linIdx);
jobs(linIdx).args = {userParameters, simulation_config};
jobs(linIdx).label = buildJobLabel(userParameters, linIdx);
jobs(linIdx).meta.lin_idx = linIdx;
jobs(linIdx).meta.userParameters = userParameters;
end
results = runBatch(@mpi_simulation_worker, jobs, ...
"mode", options.mode, ...
"waitbar", options.waitbar, ...
"waitbarMessage", "Processing MPI simulations...", ...
"numWorkers", options.numWorkers, ...
"idleTimeout", options.idleTimeout, ...
"cancelExistingQueue", options.cancelExistingQueue, ...
"resultHandler", @storeResult, ...
"errorHandler", @handleError);
function userParameters = buildUserParameters(storageWh, linIdx)
userParameters = struct();
if isempty(storageWh.getDimension())
return
end
[values, names] = storageWh.getPhysIndicesByLinIndex(linIdx);
for paramIdx = 1:numel(names)
userParameters.(char(names{paramIdx})) = values{paramIdx};
end
end
function label = buildJobLabel(userParameters, linIdx)
label = sprintf("MPI sim job %d", linIdx);
if isfield(userParameters, "sir")
label = sprintf("%s, SIR %g dB", label, userParameters.sir);
end
if isfield(userParameters, "block_update")
label = sprintf("%s, block %g", label, userParameters.block_update);
end
end
function storeResult(val, job, ~)
if isempty(val) || ~isstruct(val)
return
end
storageNames = fieldnames(val);
for storageIdx = 1:numel(storageNames)
storageName = storageNames{storageIdx};
if isempty(val.(storageName))
continue
end
ensureStorage(storageName);
wh.addValueToStorageByLinIdx(val.(storageName), storageName, job.meta.lin_idx);
end
end
function ensureStorage(storageName)
if ~isfield(wh.sto, storageName)
wh.addStorage(storageName);
end
end
function handleError(ME, job, ~)
fprintf("[%s] ERROR [%s]: %s\n", job.label, ME.identifier, ME.message);
for st = ME.stack'
fprintf(" %s:%d (%s)\n", st.file, st.line, st.name);
end
fprintf("Full report:\n%s\n", getReport(ME, "extended"));
end
end