start with 400G analysis work
This commit is contained in:
409
Classes/04_DSP/Equalizer/ML_MLSE_DUOBINARY.m
Normal file
409
Classes/04_DSP/Equalizer/ML_MLSE_DUOBINARY.m
Normal file
@@ -0,0 +1,409 @@
|
||||
classdef ML_MLSE_DUOBINARY < handle
|
||||
% ---------------------------------------------------------------------
|
||||
% W. Lanneer and Y. Lefevre,
|
||||
% “Machine Learning-Based Pre-Equalizers for Maximum Likelihood
|
||||
% Sequence Estimation in High-Speed PONs,” EUSIPCO 2023
|
||||
% ---------------------------------------------------------------------
|
||||
% This implementation reproduces the closed-loop ML-based
|
||||
% pre-equalizer training for MLSE, supporting both training and
|
||||
% detection (decision-directed) modes.
|
||||
% ---------------------------------------------------------------------
|
||||
|
||||
properties
|
||||
sps
|
||||
order
|
||||
e
|
||||
e_tr
|
||||
error
|
||||
|
||||
len_tr
|
||||
mu_tr
|
||||
epochs_tr
|
||||
|
||||
dd_mode
|
||||
mu_dd
|
||||
epochs_dd
|
||||
adaptive_mu
|
||||
|
||||
constellation
|
||||
L
|
||||
alpha
|
||||
DIR
|
||||
DIR_flip
|
||||
trellis_states
|
||||
traceback_depth
|
||||
delta
|
||||
|
||||
% Internal variables
|
||||
S
|
||||
Nf
|
||||
nStates
|
||||
nFeasible
|
||||
combs
|
||||
first_sym
|
||||
last_sym
|
||||
valid
|
||||
valid_to_idx
|
||||
valid_from_idx
|
||||
w
|
||||
|
||||
% Fast lookup
|
||||
nSym
|
||||
key_table
|
||||
trans_index
|
||||
true_to_state_idx
|
||||
|
||||
% Debug metrics
|
||||
ber = []
|
||||
ber_dd = []
|
||||
ce = ones(1,1)
|
||||
end
|
||||
|
||||
methods
|
||||
function obj = ML_MLSE_DUOBINARY(options)
|
||||
arguments(Input)
|
||||
options.sps = 2;
|
||||
options.order = 15;
|
||||
options.len_tr = 4096;
|
||||
options.mu_tr = 0.001;
|
||||
options.epochs_tr = 5;
|
||||
options.dd_mode = 1;
|
||||
options.mu_dd = 1e-5;
|
||||
options.epochs_dd = 5;
|
||||
options.adaptive_mu = 1;
|
||||
options.delta = 0;
|
||||
options.traceback_depth = 1024;
|
||||
options.L = 1;
|
||||
end
|
||||
|
||||
fn = fieldnames(options);
|
||||
for n = 1:numel(fn)
|
||||
obj.(fn{n}) = options.(fn{n});
|
||||
end
|
||||
|
||||
obj.e = zeros(obj.order,1);
|
||||
obj.error = 0;
|
||||
end
|
||||
|
||||
% ==============================================================
|
||||
% PROCESS
|
||||
% ==============================================================
|
||||
function [X,X_viterbi] = process(obj, X, D)
|
||||
% Normalize input RMS
|
||||
X = X.normalize("mode","rms");
|
||||
obj.constellation = sort(unique(D.signal),'ascend');
|
||||
obj.nSym = numel(obj.constellation);
|
||||
|
||||
if length(X)/length(D) ~= obj.sps
|
||||
warning('Signal length does not fit to reference!');
|
||||
end
|
||||
|
||||
% --- Parameters
|
||||
obj.S = obj.nSym;
|
||||
obj.Nf = obj.order * obj.sps;
|
||||
obj.nStates = obj.S^obj.L;
|
||||
obj.nFeasible = obj.nStates * obj.S;
|
||||
|
||||
% --- Trellis mapping
|
||||
obj.trellis_states = reshape(obj.constellation,1,[]);
|
||||
pre_comb_mat = repmat(obj.trellis_states, obj.L, 1);
|
||||
pre_comb_cell = mat2cell(pre_comb_mat, ones(1,obj.L), size(pre_comb_mat,2));
|
||||
obj.combs = fliplr(combvec(pre_comb_cell{:}).');
|
||||
obj.first_sym = obj.combs(:,1);
|
||||
obj.last_sym = obj.combs(:,end);
|
||||
obj.nStates = size(obj.combs,1);
|
||||
|
||||
% --- Valid transitions
|
||||
obj.valid = false(obj.nStates);
|
||||
for from = 1:obj.nStates
|
||||
for to = 1:obj.nStates
|
||||
if all(obj.combs(to,2:end) == obj.combs(from,1:end-1))
|
||||
obj.valid(to,from) = true;
|
||||
end
|
||||
end
|
||||
end
|
||||
[obj.valid_to_idx,obj.valid_from_idx] = find(obj.valid);
|
||||
|
||||
% --- Initialize weights
|
||||
if isempty(obj.w) || any(size(obj.w) ~= [obj.Nf+1,obj.nFeasible])
|
||||
% obj.w = randn(obj.Nf+1,obj.nFeasible);
|
||||
obj.w = zeros(obj.Nf+1,obj.nFeasible);
|
||||
end
|
||||
|
||||
% --- Fast lookup tables
|
||||
[~, sym_idx_mat] = ismember(obj.combs, obj.constellation);
|
||||
key_vals = 1 + sum((sym_idx_mat - 1) .* (obj.nSym .^ (0:obj.L-1)), 2);
|
||||
max_key = obj.nSym^obj.L;
|
||||
obj.key_table = zeros(max_key,1,'uint32');
|
||||
obj.key_table(key_vals) = 1:obj.nStates;
|
||||
|
||||
obj.trans_index = sparse(obj.nStates,obj.nStates);
|
||||
for i = 1:length(obj.valid_from_idx)
|
||||
f = obj.valid_from_idx(i);
|
||||
t = obj.valid_to_idx(i);
|
||||
obj.trans_index(t,f) = i;
|
||||
end
|
||||
|
||||
% ==============================================================
|
||||
% TRAINING
|
||||
% ==============================================================
|
||||
fprintf('\n--- Training mode ---\n');
|
||||
obj.equalize(X.signal, D.signal, obj.mu_tr, obj.epochs_tr, obj.len_tr, true);
|
||||
obj.e_tr = obj.e;
|
||||
|
||||
% ==============================================================
|
||||
% DECISION-DIRECTED / TESTING
|
||||
% ==============================================================
|
||||
fprintf('--- Decision-directed / detection mode ---\n');
|
||||
[y, y_vit] = obj.equalize(X.signal, D.signal, obj.mu_dd, obj.epochs_dd, X.length, false);
|
||||
|
||||
X_viterbi = X;
|
||||
X.signal = y;
|
||||
X_viterbi.signal = y_vit;
|
||||
end
|
||||
|
||||
% ==============================================================
|
||||
% EQUALIZE
|
||||
% ==============================================================
|
||||
function [y,y_ref] = equalize(obj,x,d,mu,epochs,N,training)
|
||||
debug = 0;
|
||||
showPlots = 0;
|
||||
y = zeros(N,1);
|
||||
nSymbols = ceil(N/obj.sps);
|
||||
|
||||
for epoch = 1:epochs
|
||||
pm = zeros(obj.nStates,1);
|
||||
pred = zeros(nSymbols,obj.nStates,'uint32');
|
||||
pm_sto = nan(obj.nStates,nSymbols,'like',pm);
|
||||
CE_accum = 0;
|
||||
|
||||
start_sample = 1;
|
||||
end_sample = N;
|
||||
start_symbol = 1 + floor((start_sample - 1)/obj.sps);
|
||||
|
||||
% --- initialize true state
|
||||
if numel(d) >= obj.L && start_symbol >= obj.L
|
||||
init_seq = d(start_symbol-obj.L+1:start_symbol);
|
||||
key_init = obj.seq2key(init_seq);
|
||||
true_to_state_idx = obj.key_table(key_init);
|
||||
if true_to_state_idx==0, true_to_state_idx=1; end
|
||||
else
|
||||
true_to_state_idx = uint32(1);
|
||||
end
|
||||
|
||||
for sample = start_sample:obj.sps:end_sample
|
||||
symbol = (sample - start_sample)/obj.sps + 1;
|
||||
sym_idx = start_symbol + (symbol - 1);
|
||||
|
||||
% --- Observation window (with delta)
|
||||
i1 = sample - obj.Nf + 1 + obj.delta;
|
||||
i2 = sample + obj.delta;
|
||||
buf = x(max(1,i1):min(length(x),i2));
|
||||
padL = max(0,1 - i1);
|
||||
padR = max(0,i2 - length(x));
|
||||
yk = [zeros(padL,1); buf(:); zeros(padR,1)];
|
||||
yk = [yk;1];
|
||||
|
||||
% --- Branch metrics
|
||||
c_hat = (yk.' * obj.w).';
|
||||
pm = pm - min(pm);
|
||||
v_tilde = pm(obj.valid_from_idx) + c_hat;
|
||||
|
||||
% --- allocate once
|
||||
if epoch==1 && symbol==1
|
||||
obj.true_to_state_idx = ones(ceil(N/obj.sps),1,'uint32');
|
||||
end
|
||||
|
||||
% --- previous "to" becomes "from"
|
||||
if symbol>1
|
||||
true_from_state_idx = obj.true_to_state_idx(symbol-1);
|
||||
else
|
||||
true_from_state_idx = 1;
|
||||
end
|
||||
|
||||
% --- compute or reuse "to" state
|
||||
if epoch==1
|
||||
if sym_idx>=obj.L
|
||||
key_to = obj.seq2key(d(sym_idx-obj.L+1:sym_idx));
|
||||
state_idx = obj.key_table(key_to);
|
||||
if state_idx==0
|
||||
state_idx = true_from_state_idx;
|
||||
end
|
||||
obj.true_to_state_idx(symbol) = state_idx;
|
||||
else
|
||||
obj.true_to_state_idx(symbol) = true_from_state_idx;
|
||||
end
|
||||
end
|
||||
true_to_state_idx = obj.true_to_state_idx(symbol);
|
||||
|
||||
% --- fast Dirac creation
|
||||
dirac = zeros(obj.nFeasible,1);
|
||||
trans_idx = obj.trans_index(true_to_state_idx,true_from_state_idx);
|
||||
if trans_idx~=0
|
||||
dirac(trans_idx)=1;
|
||||
end
|
||||
|
||||
% --- ensure valid (from,to)
|
||||
if ~any(dirac)
|
||||
mask = obj.valid_from_idx==true_from_state_idx & ...
|
||||
obj.valid_to_idx ==true_to_state_idx;
|
||||
if any(mask)
|
||||
dirac(mask) = 1;
|
||||
else
|
||||
idx = find(obj.valid_from_idx==true_from_state_idx,1,'first');
|
||||
dirac(idx) = 1;
|
||||
obj.true_to_state_idx(symbol) = obj.valid_to_idx(idx);
|
||||
end
|
||||
end
|
||||
|
||||
% ===================================================================
|
||||
% TRAINING MODE (weight update)
|
||||
% ===================================================================
|
||||
if training
|
||||
% --- Softmax and CE
|
||||
v_shift = -(v_tilde - min(v_tilde));
|
||||
v_shift = min(v_shift,100);
|
||||
expv = exp(v_shift);
|
||||
p = expv./(sum(expv)+eps);
|
||||
CE_symbol(symbol) = -log(p(dirac==1)+eps);
|
||||
|
||||
% --- CE smoothing and adaptive μ
|
||||
if sym_idx>obj.L
|
||||
CE_smooth(symbol)=0.01*CE_symbol(symbol)+0.99*CE_symbol(symbol-1);
|
||||
else
|
||||
CE_smooth(symbol)=CE_symbol(symbol);
|
||||
end
|
||||
CE_accum=CE_accum+CE_symbol(symbol);
|
||||
|
||||
% --- Gradient update
|
||||
dmp=(dirac-p)';
|
||||
dL_Dw=(yk).*dmp;
|
||||
if sym_idx>=obj.L
|
||||
if obj.adaptive_mu
|
||||
mu_eff=CE_smooth(symbol);
|
||||
mu_eff=max(min(mu_eff,0.2),1e-4);
|
||||
else
|
||||
mu_eff=mu;
|
||||
end
|
||||
obj.w=obj.w - mu_eff.*dL_Dw;
|
||||
end
|
||||
end
|
||||
|
||||
% ===================================================================
|
||||
% DECODING MODE (Viterbi only)
|
||||
% ===================================================================
|
||||
% Compare-Select (always executed)
|
||||
vmat=inf(obj.nStates,obj.nStates);
|
||||
vmat(obj.valid)=v_tilde;
|
||||
[pm_next,pred(symbol,:)]=min(vmat,[],2);
|
||||
pm_next=pm_next-min(pm_next);
|
||||
pm=pm_next;
|
||||
pm_sto(:,symbol)=pm;
|
||||
end
|
||||
|
||||
% --- Traceback
|
||||
[~,s_end]=min(pm);
|
||||
vpath=zeros(symbol,1,'uint32');
|
||||
vpath(symbol)=s_end;
|
||||
for n=symbol:-1:2
|
||||
vpath(n-1)=pred(n,vpath(n));
|
||||
end
|
||||
|
||||
y_ref=d(start_symbol:end);
|
||||
y=obj.first_sym(vpath);
|
||||
|
||||
% --- BER/CE reporting and plots
|
||||
if training
|
||||
err=sum(y~=y_ref(1:length(y)));
|
||||
ser=err/length(y);
|
||||
[ber, ~] = obj.calculateDuobinaryBer(y, y_ref);
|
||||
if isfinite(ber)
|
||||
fprintf('Epoch %d - BER: %.2e\n',epoch,ber);
|
||||
obj.ber(epoch)=ber;
|
||||
else
|
||||
fprintf('Epoch %d - SER: %.2e\n',epoch,ser);
|
||||
obj.ber(epoch)=ser;
|
||||
end
|
||||
obj.ce(epoch)=CE_accum/symbol;
|
||||
|
||||
if debug && mod(epoch,10)==1 && showPlots
|
||||
figure(10);clf
|
||||
subplot(3,2,1:2);
|
||||
imagesc(obj.w);axis xy;colorbar;title('Filter W');
|
||||
subplot(3,2,3);
|
||||
vtilde_mat=NaN(obj.nStates,obj.nStates);
|
||||
vtilde_mat(obj.valid)=v_tilde;
|
||||
imagesc(vtilde_mat);axis xy;colorbar;title('Path Metrics (v\_tilde)');
|
||||
subplot(3,2,4);
|
||||
plot(1:symbol,pm_sto);title('Path Metric Evolution');
|
||||
subplot(3,2,5);hold on;
|
||||
scatter(1:symbol,CE_symbol,1,'.');
|
||||
scatter(1:symbol,CE_smooth,1,'.');
|
||||
title('Cross Entropy');
|
||||
subplot(3,2,6);hold on;
|
||||
yyaxis left
|
||||
scatter(1:length(obj.ce),obj.ce,10,'s','filled');
|
||||
ylabel('Cross Entropy');
|
||||
yyaxis right
|
||||
scatter(1:length(obj.ber),obj.ber,10,'d','filled');
|
||||
set(gca,'YScale','log');
|
||||
ylabel('BER (log)');
|
||||
xlabel('Epoch');grid on;
|
||||
title('Convergence');
|
||||
drawnow;
|
||||
end
|
||||
else
|
||||
[ber, ser] = obj.calculateDuobinaryBer(y, y_ref);
|
||||
if isfinite(ber)
|
||||
fprintf('DD epoch %d - BER: %.2e\n',epoch,ber);
|
||||
obj.ber_dd(epoch)=ber;
|
||||
else
|
||||
fprintf('DD epoch %d - SER: %.2e\n',epoch,ser);
|
||||
obj.ber_dd(epoch)=ser;
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
methods (Access=private)
|
||||
% ==============================================================
|
||||
% Helper: convert a detected precoded sequence back to PAM data
|
||||
% ==============================================================
|
||||
function data = invertDuobinaryPrecoder(obj, precoded)
|
||||
encoded = Duobinary().encode(precoded,"M",obj.S);
|
||||
encoded_signal = Signal(encoded);
|
||||
decoded_signal = Duobinary().decode(encoded_signal,"M",obj.S);
|
||||
data = decoded_signal.signal;
|
||||
end
|
||||
|
||||
% ==============================================================
|
||||
% Helper: calculate BER after inverting the duobinary precoder
|
||||
% ==============================================================
|
||||
function [ber, ser] = calculateDuobinaryBer(obj, detected, reference)
|
||||
n = min(numel(detected), numel(reference));
|
||||
detected = detected(1:n);
|
||||
reference = reference(1:n);
|
||||
|
||||
ser = sum(detected ~= reference) / n;
|
||||
|
||||
detected_data = obj.invertDuobinaryPrecoder(detected);
|
||||
reference_data = obj.invertDuobinaryPrecoder(reference);
|
||||
mapper = PAMmapper(obj.S, 0);
|
||||
detected_bits = mapper.demap(detected_data);
|
||||
reference_bits = mapper.demap(reference_data);
|
||||
[~,~,ber,~] = calc_ber(reference_bits, detected_bits, ...
|
||||
"skip_front",10,"skip_end",10,"returnErrorLocation",1);
|
||||
end
|
||||
|
||||
% ==============================================================
|
||||
% Helper: Sequence → key (always scalar)
|
||||
% ==============================================================
|
||||
function key = seq2key(obj, seq)
|
||||
[~, idx] = ismember(flip(seq), obj.constellation);
|
||||
pow = (obj.nSym .^ (0:obj.L-1)).';
|
||||
key = 1 + sum((idx(:) - 1) .* pow);
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -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));
|
||||
|
||||
Reference in New Issue
Block a user