Few more scripts to evaluate new ML-MLSE Equalizer - which is not better!!! :-(

This commit is contained in:
silas (home)
2025-11-03 08:17:39 +01:00
parent 09f345aa91
commit 39bc8243fc
7 changed files with 443 additions and 319 deletions

View File

@@ -53,6 +53,8 @@ classdef ML_MLSE < handle
state_dict % containers.Map: key(sequence)->state index
key_fmt = '%.8g_'; % key format for sequence strings
nSym % |constellation|
ber = []
end
methods
@@ -182,7 +184,7 @@ classdef ML_MLSE < handle
% ==============================================================
% FFE + Whitening + ML-Based Branch Metric Estimation + Viterbi
% ==============================================================
debug = 0;
debug = 1;
% --- Input padding and preallocation
y = zeros(N,1);
@@ -199,19 +201,34 @@ classdef ML_MLSE < handle
pred = zeros(nSymbols, obj.nStates, 'uint32');
pm_sto = nan(obj.nStates, nSymbols,'like',pm);
% --- Initialize "true" trellis state for training (shift-register style)
% expect sequences in chronological order [x_{k-L+1}:x_k], but combs rows are [x_k, x_{k-1}, ...]
if numel(d) >= obj.L
init_seq = d(1:obj.L); % [x_1 ... x_L]
true_to_state_idx = obj.state_dict(obj.seq_key(flip(init_seq))); % flip to [x_L, x_{L-1}, ...]
%%% START IDX
if training
max_start = length(x) - ( (ceil(N/obj.sps)-1)*obj.sps + 1 );
max_start = max(1, max_start); % safety
start_sample = randi([1, max_start], 1); %rnd training; not really good
start_sample = 1;
end_sample = start_sample + (ceil(N/obj.sps)-1)*obj.sps;
else
true_to_state_idx = 1;
start_sample = 1;
end_sample = N;
end
start_symbol = 1 + floor((start_sample - 1)/obj.sps); % ABSOLUTE symbol index
if numel(d) >= obj.L && start_symbol >= obj.L
init_seq = d(start_symbol-obj.L+1 : start_symbol); % [d_k-L+1 ... d_k]
true_to_state_idx = obj.state_dict(obj.seq_key(flip(init_seq))); % [d_k ... d_k-L+1]
else
% Not enough history fall back to state 1
true_to_state_idx = uint32(1);
end
symbol = 0;
for sample = 1:obj.sps:N
for sample = start_sample:obj.sps:end_sample
symbol = symbol + 1;
k = symbol;
sym_idx = start_symbol + (symbol - 1);
% --- Build Δ-delayed observation window y_k
i1 = sample - obj.Nf + 1 + obj.delta;
@@ -232,78 +249,72 @@ classdef ML_MLSE < handle
v_tilde = pm(obj.valid_from_idx) + c_hat; % [nFeasible×1]
% ===== Gradient update (Algorithm 1) =====
% if training
if k > obj.L
% shift-register: previous "to" becomes current "from"
true_from_state_idx = true_to_state_idx;
% current "to" from data window
curr_seq = d(k-obj.L+1:k);
key_to = obj.seq_key(flip(curr_seq));
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
true_from_state_idx = 1;
true_to_state_idx = true_to_state_idx; % keep init
end
if 0
disp(['FROM: state',char(num2str(true_from_state_idx)),' : symbol transition', char(num2str(obj.combs(true_from_state_idx,:)))]);
disp(['TO: state',char(num2str(true_to_state_idx)),' : symbol transition', char(num2str(obj.combs(true_to_state_idx,:)))]);
end
% Dirac delta over correct extended transition (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; % This Dirac delta function δ(s = s k, s = s k1) = 1 if the extended state (s, s) corresponds to the true realized states (s k, s k1), and is zero otherwise.
% softmax over -v_tilde (numerically safe shift)
p = exp(-(v_tilde - max(v_tilde)));
p = p./sum(p); % found in formula (9) and (19)
% gradient term (t - p)
dmp = (dirac - p)'; % 1×nFeasible
if mod(symbol,128) == 1 && debug
% --- Normalize and compute probabilities
v_norm = v_tilde - max(v_tilde);
probs_lin = exp(-v_norm); probs_lin = probs_lin ./ sum(probs_lin);
probs_log = -v_norm;
% --- Map back into nStates×nStates grid
probs_mat = nan(obj.nStates, obj.nStates);
probs_logmat = nan(obj.nStates, obj.nStates);
probs_mat(obj.valid) = probs_lin;
probs_logmat(obj.valid) = probs_log;
% --- Identify the current true transition
[to_idx, from_idx] = find(obj.valid);
cur_idx = find(dirac==1);
cur_to = to_idx(cur_idx);
cur_from = from_idx(cur_idx);
figure(11); clf;
imagesc(probs_logmat); axis xy; colorbar;
xlabel('From state'); ylabel('To state'); set(gca,'FontSize',10);
hold on;
plot(cur_from, cur_to, 'rs', 'MarkerSize', 10, 'LineWidth', 2, 'MarkerFaceColor', 'none');
hold off;
end
if 1 %training
% dmp is large for the correct transition -> update emphasizes that branch
dL_Dw = dmp .* (yk); % CE/(w) - formula (10)
% only start with updates when we are inside the signal
if k > obj.L
obj.w = obj.w - mu * dL_Dw; % (Nf+1)×nFeasible
% 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;
end
% Dirac delta over correct extended transition (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;
% softmax over -v_tilde (numerically safe shift)
p = exp(-(v_tilde - max(v_tilde)));
p = p./(sum(p)+eps);
% gradient term (t - p)
dmp = (dirac - p)'; % 1×nFeasible
% Per-feature gradient; implicit expansion gives (Nf+1)×nFeasible
dL_Dw = (yk) .* dmp;
% Start updates only when the ABSOLUTE symbol index has L history
if sym_idx >= obj.L
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
end
% if debug && epoch > 2
% figure(100);
% subplot(4,1,1);
% heatmap(p');
% title('Probs')
% subplot(4,1,2);
% heatmap(dmp);
% title('Update')
% subplot(4,1,3);
% heatmap(dL_Dw);
% title('Update')
% subplot(4,1,4);
% heatmap(bj.w);
% title('Update')
%
% end
end
% --- Compare-Select (matrix form, min of costs)
v_tilde_mat = inf(obj.nStates, obj.nStates);
v_tilde_mat(obj.valid) = v_tilde;
@@ -327,10 +338,21 @@ classdef ML_MLSE < handle
y_vit = obj.first_sym(viterbi_path);
y = obj.first_sym(viterbi_path);
if 1 %debug || training
err = sum(y ~= d(1:length(y)));
ser = err./length(y);
fprintf('Epoch: %d - SER: %.1e \n',epoch, ser);
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;
% ser = err./length(y);
% fprintf('Epoch: %d - SER: %.1e \n',epoch, ser);
figure(10);
subplot(2,2,1:2);
@@ -347,6 +369,8 @@ classdef ML_MLSE < handle
scatter(1:symbol,pm_sto,1,'.')
% plot(1:symbol,pm_sto,'LineStyle','none')
title('Path Metric Winners')
drawnow
end
end
end