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

@@ -30,6 +30,9 @@ classdef VNLE < handle
optmize_mus = 0;
mu_optimization
mu_optimization_iter = 0;
mu_optimization_len
plot_mu_optimization = 0
mu_optimization_fignum = 3020;
x_norm
ce
@@ -55,6 +58,9 @@ classdef VNLE < handle
options.decide = false;
options.save_debug = 0;
options.optmize_mus = 0;
options.mu_optimization_len = 2^15;
options.plot_mu_optimization = 0;
options.mu_optimization_fignum = 3020;
end
@@ -110,7 +116,6 @@ classdef VNLE < handle
lbdesc = [num2str(obj.order),' tap FFE'];
X = X.logbookentry(lbdesc); % append to logbook
N = X;
N = X - D;
@@ -138,13 +143,18 @@ classdef VNLE < handle
ones(1,obj.ce(3))*mu(3) ]);
end
x = x(:);
d = d(:);
x = [zeros(floor(obj.order(1)/2),1); x; zeros(obj.order(1),1)];
n_symbols = floor(N / obj.sps);
y = zeros(n_symbols,1);
d_hat = zeros(n_symbols,1);
if showviz
f = figure(111);
figure(111);
subplot(2,2,1:2);
hold on
a = scatter(1:numel(x),x,1,'.');
scatter(1:numel(x),x,1,'.');
a2 = scatter(1,1,1,'.');
a3 = scatter(1,1,2,'.');
a4 = xline(1);
@@ -215,40 +225,50 @@ classdef VNLE < handle
mu_range = [1e-5, 1e-2];
mu_dc_range = [1e-5, 1e-1];
vars = [optimizableVariable("mu_tr",mu_range,"Transform","log"), ...
optimizableVariable("mu_dd",mu_range,"Transform","log")];
[x_opt,d_opt,N_opt] = obj.optimizationSignals(x,d);
vars = obj.muOptimizableVariables("mu_tr",mu_range);
vars = [vars, obj.muOptimizableVariables("mu_dd",mu_range)];
optimize_mu_dc = obj.mu_dc ~= 0;
if optimize_mu_dc
vars = [vars, optimizableVariable("mu_dc",mu_dc_range,"Transform","log")];
end
obj.mu_optimization_iter = 0;
obj.mu_optimization = bayesopt(@(p)obj.muObjective(p,x,d),vars, ...
"MaxObjectiveEvaluations",10, ...
fprintf("VNLE mu opt uses %d samples / %d symbols\n",N_opt,numel(d_opt));
obj.mu_optimization = bayesopt(@(p)obj.muObjective(p,x_opt,d_opt),vars, ...
"MaxObjectiveEvaluations",20, ...
"AcquisitionFunctionName","expected-improvement-plus", ...
"IsObjectiveDeterministic",false, ...
"Verbose",0, ...
"PlotFcn",[]);
obj.mu_tr = obj.mu_optimization.XAtMinObjective.mu_tr;
obj.mu_dd = obj.mu_optimization.XAtMinObjective.mu_dd;
obj.mu_tr = obj.muVectorFromParams(obj.mu_optimization.XAtMinObjective,"mu_tr");
obj.mu_dd = obj.muVectorFromParams(obj.mu_optimization.XAtMinObjective,"mu_dd");
if optimize_mu_dc
obj.mu_dc = obj.mu_optimization.XAtMinObjective.mu_dc;
end
objective_db = 10*log10(obj.mu_optimization.MinObjective);
if optimize_mu_dc
fprintf("\nVNLE mu opt done: mu_tr=%9.3e, mu_dd=%9.3e, mu_dc=%9.3e, MSE=%9.3e, MSE_dB=%7.2f dB\n", ...
obj.mu_tr,obj.mu_dd,obj.mu_dc,obj.mu_optimization.MinObjective,objective_db);
fprintf("\nVNLE mu opt done: mu_tr=[%s], mu_dd=[%s], mu_dc=%9.3e, BER=%9.3e\n", ...
obj.formatMuVector(obj.mu_tr),obj.formatMuVector(obj.mu_dd), ...
obj.mu_dc,obj.mu_optimization.MinObjective);
else
fprintf("\nVNLE mu opt done: mu_tr=%9.3e, mu_dd=%9.3e, MSE=%9.3e, MSE_dB=%7.2f dB\n", ...
obj.mu_tr,obj.mu_dd,obj.mu_optimization.MinObjective,objective_db);
fprintf("\nVNLE mu opt done: mu_tr=[%s], mu_dd=[%s], BER=%9.3e\n", ...
obj.formatMuVector(obj.mu_tr),obj.formatMuVector(obj.mu_dd), ...
obj.mu_optimization.MinObjective);
end
if obj.plot_mu_optimization
obj.plotMuOptimization();
end
end
function objective = muObjective(obj,params,x,d)
old_debug = obj.save_debug;
old_mu_dc = obj.mu_dc;
obj.save_debug = 1;
old_state = obj.captureObjectiveState();
cleanup = onCleanup(@()obj.restoreObjectiveState(old_state));
obj.save_debug = 0;
optimize_mu_dc = ismember("mu_dc",string(params.Properties.VariableNames));
if optimize_mu_dc
obj.mu_dc = params.mu_dc;
@@ -257,34 +277,217 @@ classdef VNLE < handle
obj.e = zeros(sum(obj.ce),1);
obj.e_dc = 0;
obj.debug_struct = struct();
obj.equalize(x,d,params.mu_tr,obj.epochs_tr,obj.len_tr,1,0);
obj.equalize(x,d,params.mu_dd,obj.epochs_dd,numel(x),0,0);
muTrCandidate = obj.muVectorFromParams(params,"mu_tr");
muDdCandidate = obj.muVectorFromParams(params,"mu_dd");
N_tr = min(obj.len_tr,numel(x));
obj.equalize(x,d,muTrCandidate,obj.epochs_tr,N_tr,1,0);
[signal,~] = obj.equalize(x,d,muDdCandidate,obj.epochs_dd,numel(x),0,0);
objective = mean(obj.debug_struct.error(end,:),"omitnan");
[ber,errors] = obj.berObjective(signal,d);
objective = ber;
if ~isfinite(objective)
objective = inf;
end
objective_db = 10*log10(objective);
obj.mu_optimization_iter = obj.mu_optimization_iter + 1;
if optimize_mu_dc
fprintf("\rVNLE mu opt %02d: mu_tr=%9.3e, mu_dd=%9.3e, mu_dc=%9.3e, MSE=%9.3e, MSE_dB=%7.2f dB", ...
obj.mu_optimization_iter,params.mu_tr,params.mu_dd,params.mu_dc,objective,objective_db);
fprintf("\rVNLE mu opt %02d: mu_tr=[%s], mu_dd=[%s], mu_dc=%9.3e, BER=%9.3e, errors=%d", ...
obj.mu_optimization_iter,obj.formatMuVector(muTrCandidate), ...
obj.formatMuVector(muDdCandidate),params.mu_dc,ber,errors);
else
fprintf("\rVNLE mu opt %02d: mu_tr=%9.3e, mu_dd=%9.3e, MSE=%9.3e, MSE_dB=%7.2f dB", ...
obj.mu_optimization_iter,params.mu_tr,params.mu_dd,objective,objective_db);
fprintf("\rVNLE mu opt %02d: mu_tr=[%s], mu_dd=[%s], BER=%9.3e, errors=%d", ...
obj.mu_optimization_iter,obj.formatMuVector(muTrCandidate), ...
obj.formatMuVector(muDdCandidate),ber,errors);
end
obj.save_debug = old_debug;
obj.mu_dc = old_mu_dc;
clear cleanup
end
function [x_opt,d_opt,N_opt] = optimizationSignals(obj,x,d,opt_len)
if nargin < 4
opt_len = obj.mu_optimization_len;
end
N_available = min(numel(x),numel(d) * obj.sps);
if isempty(opt_len) || opt_len <= 0 || isinf(opt_len)
N_opt = N_available;
else
N_opt = min(N_available,max(obj.len_tr,opt_len));
end
N_opt = obj.sps * floor(N_opt / obj.sps);
N_opt = max(obj.sps,N_opt);
n_symbols = N_opt / obj.sps;
x_opt = x(1:N_opt);
d_opt = d(1:n_symbols);
end
function [ber,errors] = berObjective(~,signal,d)
M = numel(unique(d));
mapper = PAMmapper(M,0);
eq_signal_sd = Signal(signal);
eq_signal_hd = mapper.quantize(eq_signal_sd);
tx_symbols = Signal(d);
rx_bits = mapper.demap(eq_signal_hd);
tx_bits = mapper.demap(tx_symbols);
skip_front = min(1000,max(0,floor(numel(rx_bits.signal) / 4)));
[~,errors,ber,~] = calc_ber(rx_bits.signal,tx_bits.signal, ...
"skip_front",skip_front, ...
"skip_end",0, ...
"returnErrorLocation",1);
end
function vars = muOptimizableVariables(obj,prefix,mu_range)
vars = optimizableVariable.empty;
activeOrders = find(obj.order > 0);
for idx = 1:numel(activeOrders)
orderIdx = activeOrders(idx);
varName = sprintf("%s_%d",prefix,orderIdx);
vars = [vars, optimizableVariable(varName,mu_range,"Transform","log")]; %#ok<AGROW>
end
end
function mu = muVectorFromParams(obj,params,prefix)
mu = zeros(1,3);
for orderIdx = 1:numel(mu)
varName = sprintf("%s_%d",prefix,orderIdx);
if ismember(varName,string(params.Properties.VariableNames))
mu(orderIdx) = params.(varName);
elseif numel(obj.(char(prefix))) >= orderIdx
mu(orderIdx) = obj.(char(prefix))(orderIdx);
else
mu(orderIdx) = obj.(char(prefix))(1);
end
end
end
function plotMuOptimization(obj)
if isempty(obj.mu_optimization)
return
end
X = obj.mu_optimization.XTrace;
objective = obj.mu_optimization.ObjectiveTrace;
objective = objective(:);
valid = isfinite(objective);
if isempty(X) || ~any(valid)
return
end
var_names = X.Properties.VariableNames;
n_vars = numel(var_names);
eval_idx = (1:numel(objective)).';
objective_plot = obj.positiveObjectiveForLogPlot(objective);
best_plot = obj.positiveObjectiveForLogPlot(cummin(objective));
figure(obj.mu_optimization_fignum);
clf;
t = tiledlayout(2,2,"TileSpacing","compact","Padding","compact");
title(t,"VNLE Bayesian mu optimization");
nexttile;
h_candidate = semilogy(eval_idx,objective_plot,"o-","DisplayName","candidate");
obj.addOptimizationDataTips(h_candidate,X,objective,objective_plot,eval_idx,var_names);
hold on;
h_best_trace = semilogy(eval_idx,best_plot,"k-","LineWidth",1.2,"DisplayName","best so far");
h_best_trace.DataTipTemplate.DataTipRows(end+1) = dataTipTextRow("Evaluation",eval_idx);
h_best_trace.DataTipTemplate.DataTipRows(end+1) = dataTipTextRow("Best BER",best_plot);
grid on;
xlabel("Evaluation");
ylabel("BER");
legend("Location","best");
if n_vars < 2
return
end
pairs = nchoosek(1:n_vars,2);
n_pair_plots = min(size(pairs,1),3);
[~,best_idx] = min(objective);
for pair_idx = 1:n_pair_plots
nexttile;
x_name = var_names{pairs(pair_idx,1)};
y_name = var_names{pairs(pair_idx,2)};
x_data = X.(x_name);
y_data = X.(y_name);
c_data = log10(objective_plot);
h_scatter = scatter(log10(x_data),log10(y_data),35,c_data,"filled");
obj.addOptimizationDataTips(h_scatter,X,objective,objective_plot,eval_idx,var_names);
hold on;
h_best = plot(log10(x_data(best_idx)),log10(y_data(best_idx)),"kp", ...
"MarkerSize",12, ...
"MarkerFaceColor","y", ...
"DisplayName","best");
obj.addOptimizationDataTips(h_best,X(best_idx,:),objective(best_idx),objective_plot(best_idx),eval_idx(best_idx),var_names);
grid on;
xlabel("log10(" + string(x_name) + ")");
ylabel("log10(" + string(y_name) + ")");
cb = colorbar;
cb.Label.String = "log10(BER)";
title(string(x_name) + " vs " + string(y_name));
end
end
function addOptimizationDataTips(~,plot_handle,X,objective,objective_plot,eval_idx,var_names)
plot_handle.DataTipTemplate.DataTipRows(end+1) = dataTipTextRow("Evaluation",eval_idx);
plot_handle.DataTipTemplate.DataTipRows(end+1) = dataTipTextRow("BER",objective);
plot_handle.DataTipTemplate.DataTipRows(end+1) = dataTipTextRow("BER shown",objective_plot);
for var_idx = 1:numel(var_names)
var_name = var_names{var_idx};
plot_handle.DataTipTemplate.DataTipRows(end+1) = dataTipTextRow(var_name,X.(var_name));
end
end
function objective_plot = positiveObjectiveForLogPlot(~,objective)
objective_plot = objective;
positive_values = objective(isfinite(objective) & objective > 0);
if isempty(positive_values)
floor_value = 1e-12;
else
floor_value = min(positive_values) / 10;
end
objective_plot(~isfinite(objective_plot) | objective_plot <= 0) = floor_value;
end
function state = captureObjectiveState(obj)
state.e = obj.e;
state.e_dc = obj.e_dc;
state.error = obj.error;
state.mu_dc = obj.mu_dc;
state.save_debug = obj.save_debug;
state.debug_struct = obj.debug_struct;
end
function restoreObjectiveState(obj,state)
obj.e = state.e;
obj.e_dc = state.e_dc;
obj.error = state.error;
obj.mu_dc = state.mu_dc;
obj.save_debug = state.save_debug;
obj.debug_struct = state.debug_struct;
end
function s = formatMuVector(~,mu)
s = strtrim(sprintf("%9.3e ",mu));
end
%% Functions needed During Adaption
function x_in_vnle_format = calcVNLENonlinVecs(~,x_in_block,I_2,I_3,N_,norm_)
% These are the second and third order input signal products of the VNLE EQ
% ∑ h1 x_in(k-n1) + ∑∑ h2 x_in(k-n1)*x_in(k-n2) + ∑∑∑ h3 x_in(k-n1)*x_in(k-n2)*x_in(k-n3)
x_in_block = x_in_block(:);
l1=length(x_in_block);
l2=length(I_2);
l3=length(I_3);
l2=size(I_2,1);
l3=size(I_3,1);
final_length = l1+l2+l3;
x_in_vnle_format = zeros(final_length,1);
@@ -382,6 +585,7 @@ classdef VNLE < handle
end
function powerNorm = calcPowerNormalization(~,v)
v = v(:);
powerNorm(1) = sqrt(mean(abs(v ).^2));
powerNorm(2) = sqrt(mean(abs(v.^2).^2));