ML Equalizer works now.

Not yet perfectly integrated into all the routines
This commit is contained in:
Silas Oettinghaus
2025-11-12 09:24:02 +01:00
parent 39bc8243fc
commit 0080cb2264
44 changed files with 5289 additions and 2868 deletions

View File

@@ -1,13 +1,41 @@
classdef ML_MLSE < handle
% Implementation of plain and simple FFE.
% 1) Training mode (stable performance when you use NLMS)
% 2) Decision directed mode
%
%LMS: mu in order of 0.0001 for acceptable convergence speed
%NLMS: mu in order of 0.01 for acceptable convergence speed
%RLS: mu is lambda -> 0.99 -> 1 (has a strong dependency on this! use a loop to find out best values)
%
% FFE("epochs_tr",5,"epochs_dd",2,"len_tr",2^13,"mu_dd",mu_dd,"mu_tr",mu_tr,"order",25,"sps",2,"decide",0, "adaption",adaption_method(adaption),"dd_mode",use_dd_mode);
% ALGORITHM DESCRIBED IN:
% W. Lanneer and Y. Lefevre, Machine Learning-Based Pre-Equalizers for
% Maximum Likelihood Sequence Estimation in High-Speed PONs,
% in 2023 31st European Signal Processing Conference
% Further ML Refs:
% https://machinelearningmastery.com/cross-entropy-for-machine-learning/
% https://docs.pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html
% The central idea is to overcome the (white-) noise assumption within the previously described
% Viterbi algorithm, more precisely a closed-loop optimization is proposed that finds a suitable
% filter-set to directly compute the branch metrics c_k (s,s^' ). These can directly be used to
% carry out the conventional Viterbi algorithm. The system consists of S^L S=F linear FIR filters,
% combined with one bias coefficient respectively. These filters take the received input samples to
% compute the branch metrics estimates (c_k ) ̂(s,s^' ) according toThe central idea is to overcome
% the (white-) noise assumption within the previously described Viterbi algorithm, more precisely
% a closed-loop optimization is proposed that finds a suitable filter-set to directly compute the
% branch metrics c_k (s,s^' ). These can directly be used to carry out the conventional Viterbi
% algorithm. The system consists of S^L S=F linear FIR filters, combined with one bias coefficient
% respectively. These filters take the received input samples to compute the branch metrics
% estimates. Finally, the usual Viterbi is carried out...
% Recommended Settings and some findings:
% Requires many training epochs. According to ML people, 100,200 or
% even up to 1000 epochs are normal for ML-convergence
% The mu parameter _can_ be adaptive - using the cross entropy and when
% analyzing the isolated training it looks very promisig. However, is
% later use I found this is not as stable as a fixed learning rate.
% mu = 0.1 worked good for me
% Longer orders/ filter length are not always better. For me order=11
% was good.
% Delay factor (delta) is good when the order is also increased. With
% order = 11, a delta of =4 shows good results
properties
sps % usually 2
@@ -24,6 +52,8 @@ classdef ML_MLSE < handle
mu_dd %weight update in dd mode
epochs_dd
adaptive_mu
constellation
L %viterbi memory length
@@ -50,11 +80,13 @@ classdef ML_MLSE < handle
w
% --- New: fast state lookup ---
true_to_state_idx
state_dict % containers.Map: key(sequence)->state index
key_fmt = '%.8g_'; % key format for sequence strings
nSym % |constellation|
ber = []
ce = ones(1,1);
end
methods
@@ -72,6 +104,8 @@ classdef ML_MLSE < handle
options.mu_dd = 1e-5;
options.epochs_dd = 5;
options.adaptive_mu = 1;
options.delta = 0;
options.traceback_depth = 1024;
@@ -180,11 +214,12 @@ classdef ML_MLSE < handle
X_viterbi = X_viterbi.logbookentry(lbdesc); % append to logbook
end
function [y,y_vit] = equalize(obj,x,d,mu,epochs,N,training)
function [y,y_ref] = equalize(obj,x,d,mu,epochs,N,training)
% ==============================================================
% FFE + Whitening + ML-Based Branch Metric Estimation + Viterbi
% ==============================================================
debug = 1;
showPlots = 1;
% --- Input padding and preallocation
y = zeros(N,1);
@@ -200,7 +235,8 @@ classdef ML_MLSE < handle
v_tilde = zeros(1,obj.nFeasible);
pred = zeros(nSymbols, obj.nStates, 'uint32');
pm_sto = nan(obj.nStates, nSymbols,'like',pm);
CE_accum = 0;
%%% START IDX
if training
@@ -210,7 +246,7 @@ classdef ML_MLSE < handle
start_sample = 1;
end_sample = start_sample + (ceil(N/obj.sps)-1)*obj.sps;
else
start_sample = 1;
start_sample = 1;%obj.len_tr;
end_sample = N;
end
@@ -251,34 +287,72 @@ classdef ML_MLSE < handle
% ===== Gradient update (Algorithm 1) =====
if 1 %training
% previous "to" becomes current "from" (shift-register)
true_from_state_idx = true_to_state_idx;
% --- Build current "to" state from ABSOLUTE symbol index
if sym_idx >= obj.L
curr_seq = d(sym_idx-obj.L+1 : sym_idx); % [d_k-L+1 ... d_k]
key_to = obj.seq_key(flip(curr_seq)); % -> [d_k ... d_k-L+1]
if isKey(obj.state_dict, key_to)
true_to_state_idx = obj.state_dict(key_to);
else
% Fall back safely (should not happen with proper constellation)
true_to_state_idx = true_from_state_idx;
end
else
% Not enough history yet for a full L-symbol state
% keep previous 'to' and 'from'
true_to_state_idx = true_to_state_idx;
true_from_state_idx = true_from_state_idx;
% --- allocate storage once
if epoch == 1 && symbol == 1
obj.true_to_state_idx = ones(ceil(N/obj.sps),1,'uint32');
end
% Dirac delta over correct extended transition (from,to)
% --- previous "to" becomes current "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
% only compute in first epoch
if sym_idx >= obj.L
key_to = obj.seq_key(flip(d(sym_idx-obj.L+1 : sym_idx)));
if isKey(obj.state_dict, key_to)
obj.true_to_state_idx(symbol) = obj.state_dict(key_to);
else
obj.true_to_state_idx(symbol) = true_from_state_idx;
end
else
obj.true_to_state_idx(symbol) = true_from_state_idx;
end
end
% --- reuse cached state from second epoch onward
true_to_state_idx = obj.true_to_state_idx(symbol);
% --- ensure valid (from,to)
dirac = zeros(obj.nFeasible,1);
dirac(obj.valid_from_idx==true_from_state_idx & ...
obj.valid_to_idx ==true_to_state_idx) = 1;
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
% softmax over -v_tilde (numerically safe shift)
p = exp(-(v_tilde - max(v_tilde)));
p = p./(sum(p)+eps);
v_shift = -(v_tilde - min(v_tilde)); % shift to small positive numbers
v_shift = min(v_shift, 100); % clamp exponent argument ( exp(50)=3e21)
expv = exp(v_shift);
p = expv ./ (sum(expv) + eps);
% for logging only:
CE_symbol(symbol) = -log(p(dirac==1) + eps);
if sym_idx > obj.L
CE_smooth(symbol) = 0.01*CE_symbol(symbol) + 0.99*CE_smooth(symbol-1);
else
if epoch > 1
CE_smooth(symbol) = obj.ce(end); %use ce from last epoch or =1 for very first round?!
else
CE_smooth(symbol) = CE_symbol(symbol);
end
end
CE_accum = CE_symbol(symbol) + CE_accum;
% gradient term (t - p)
dmp = (dirac - p)'; % 1×nFeasible
@@ -288,10 +362,14 @@ classdef ML_MLSE < handle
% Start updates only when the ABSOLUTE symbol index has L history
if sym_idx >= obj.L
if obj.adaptive_mu
mu_eff = CE_smooth(sym_idx);
mu_eff = max(min(mu_eff, 0.2), 1e-4);
else
mu_eff = mu;
end
obj.w = obj.w - ones(size(dL_Dw,1),1).*mu .* dL_Dw; % (Nf+1)×nFeasible
% obj.w = obj.w - mu * dL_Dw; % (Nf+1)×nFeasible
obj.w = obj.w - mu_eff .* dL_Dw; % (Nf+1)×nFeasible
end
% if debug && epoch > 2
@@ -335,42 +413,69 @@ classdef ML_MLSE < handle
viterbi_path(n-1) = pred(n, viterbi_path(n));
end
y_vit = obj.first_sym(viterbi_path);
y_ref = d(start_symbol:end);
y = obj.first_sym(viterbi_path);
if debug %&& training
if debug && training
sym_start = start_symbol;
sym_end = start_symbol + symbol - 1;
ref_slice = d(sym_start : sym_end);
err = sum(y ~= ref_slice(1:numel(y)));
ref_bits = PAMmapper(obj.S,0).demap(ref_slice);
eq_bits = PAMmapper(obj.S,0).demap(y);
[~, ~, ber, ~] = calc_ber(ref_bits, eq_bits, "skip_front", 10, "skip_end", 10, "returnErrorLocation", 1);
fprintf('Epoch: %d - BER: %.1e \n',epoch, ber);
obj.ber(epoch) = ber;
try
ref_bits = PAMmapper(obj.S,0).demap(ref_slice);
eq_bits = PAMmapper(obj.S,0).demap(y);
[~, ~, ber, ~] = calc_ber(ref_bits, eq_bits, "skip_front", 10, "skip_end", 10, "returnErrorLocation", 1);
fprintf('Epoch: %d - BER: %.1e \n',epoch, ber);
obj.ber(epoch) = ber;
catch
ser = err./length(y);
fprintf('Epoch: %d - SER: %.1e \n',epoch, ser);
end
% ser = err./length(y);
% fprintf('Epoch: %d - SER: %.1e \n',epoch, ser);
obj.ce(epoch) = CE_accum./symbol;
figure(10);
subplot(2,2,1:2);
heatmap(obj.w);
title('Filter')
subplot(2,2,3);
v_tildemat = NaN(obj.nStates, obj.nStates);
v_tildemat(obj.valid) = v_tilde; % log-domain scores
heatmap(v_tildemat);
title('Path Metrics (v_tilde)')
subplot(2,2,4);
scatter(1:symbol,pm_sto,1,'.')
% plot(1:symbol,pm_sto,'LineStyle','none')
title('Path Metric Winners')
drawnow
if showPlots
figure(10);clf
subplot(3,2,1:2);
heatmap(obj.w);
title('Filter')
subplot(3,2,3);
v_tildemat = NaN(obj.nStates, obj.nStates);
v_tildemat(obj.valid) = v_tilde; % log-domain scores
heatmap(v_tildemat);
title('Path Metrics (v_tilde)')
subplot(3,2,4);
scatter(1:symbol,pm_sto,1,'.')
title('Path Metric Winners')
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
% Left y-axis: Cross Entropy (linear)
yyaxis left
scatter(1:length(obj.ce), obj.ce, 10, 's', 'filled')
ylabel('Cross Entropy')
% Right y-axis: BER (logarithmic)
yyaxis right
scatter(1:length(obj.ber), obj.ber, 10, 'd', 'filled')
set(gca, 'YScale', 'log')
ylabel('BER (log scale)')
xlim([1, epochs])
xlabel('Epoch')
title('Cross Entropy // BER')
grid on
drawnow
end
end
end
end

View File

@@ -118,7 +118,7 @@ classdef MLSE < handle
elseif trellis_state_mode == 2 %simply use the states from the ref signal (should be a robust option)
obj.trellis_states = reshape(unique(data_ref),size(obj.trellis_states));
obj.trellis_states = reshape(unique(data_ref),1,length(unique(data_ref)));
elseif trellis_state_mode == 3 %use_statistical_levels