ML_MLSE (before testing in depth)
minor changes here and there
This commit is contained in:
@@ -48,6 +48,11 @@ classdef ML_MLSE < handle
|
||||
valid_to_idx
|
||||
valid_from_idx
|
||||
w
|
||||
|
||||
% --- New: fast state lookup ---
|
||||
state_dict % containers.Map: key(sequence)->state index
|
||||
key_fmt = '%.8g_'; % key format for sequence strings
|
||||
nSym % |constellation|
|
||||
end
|
||||
|
||||
methods
|
||||
@@ -79,7 +84,6 @@ classdef ML_MLSE < handle
|
||||
|
||||
obj.e = zeros(obj.order,1);
|
||||
obj.error = 0;
|
||||
|
||||
end
|
||||
|
||||
function [X,X_viterbi] = process(obj, X, D)
|
||||
@@ -88,33 +92,33 @@ classdef ML_MLSE < handle
|
||||
% 1 normalize RMS
|
||||
X = X.normalize("mode","rms");
|
||||
|
||||
obj.constellation = unique(D.signal);
|
||||
% Use sorted constellation for deterministic mapping
|
||||
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
|
||||
|
||||
|
||||
% ==============================================================
|
||||
% INITIALIZATION (only before final epoch and detection mode)
|
||||
% ==============================================================
|
||||
|
||||
|
||||
% --- Parameters
|
||||
obj.S = numel(unique(D.signal)); % alphabet size
|
||||
obj.Nf = obj.order*obj.sps; % filter length
|
||||
% obj.delta = 3;%ceil(obj.Nf/2); % delay parameter
|
||||
obj.S = numel(obj.constellation); % alphabet size
|
||||
obj.Nf = obj.order*obj.sps; % filter length
|
||||
% obj.delta = 3;%ceil(obj.Nf/2); % delay parameter
|
||||
obj.nStates = obj.S^obj.L;
|
||||
obj.nFeasible = obj.nStates*obj.S;
|
||||
|
||||
% --- Trellis mapping
|
||||
obj.trellis_states = reshape(unique(D.signal),1,[]);
|
||||
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);
|
||||
obj.combs = fliplr(combvec(pre_comb_cell{:}).'); % rows: states, columns: [x_k, x_{k-1}, ...]
|
||||
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);
|
||||
@@ -130,10 +134,17 @@ classdef ML_MLSE < handle
|
||||
% --- Allocate vectors and weights
|
||||
% !! IF SHAPE FIT, then we already have smth there an we want
|
||||
% to start with the existing fitler-set
|
||||
if all(size(obj.w) ~= [obj.Nf+1,obj.nFeasible])
|
||||
if isempty(obj.w) || any(size(obj.w) ~= [obj.Nf+1,obj.nFeasible])
|
||||
obj.w = zeros(obj.Nf+1,obj.nFeasible); % filter weights per transition + bias tap
|
||||
obj.w = randn(obj.Nf+1,obj.nFeasible);
|
||||
end
|
||||
% obj.w = randn(obj.Nf+1,obj.nFeasible);
|
||||
|
||||
% --- Precompute dictionary for fast state lookup (sequence -> state)
|
||||
keys = cell(obj.nStates,1);
|
||||
for i = 1:obj.nStates
|
||||
keys{i} = obj.seq_key(obj.combs(i,:)); % combs row is already [x_k, x_{k-1}, ...]
|
||||
end
|
||||
obj.state_dict = containers.Map(keys, 1:obj.nStates);
|
||||
|
||||
% ==============================================================
|
||||
% TRAINING
|
||||
@@ -165,8 +176,6 @@ classdef ML_MLSE < handle
|
||||
X_viterbi.fs = D.fs; %change sampling frequency of outgoing signal from fdac e.g. 2 sps to symbol spaced = fsym
|
||||
lbdesc = [num2str(obj.order),'order FFE + PF + Viterbi'];
|
||||
X_viterbi = X_viterbi.logbookentry(lbdesc); % append to logbook
|
||||
|
||||
|
||||
end
|
||||
|
||||
function [y,y_vit] = equalize(obj,x,d,mu,epochs,N,training)
|
||||
@@ -174,24 +183,34 @@ classdef ML_MLSE < handle
|
||||
% FFE + Whitening + ML-Based Branch Metric Estimation + Viterbi
|
||||
% ==============================================================
|
||||
debug = 0;
|
||||
|
||||
% --- Input padding and preallocation
|
||||
y = zeros(N,1);
|
||||
|
||||
% number of symbol steps in this block
|
||||
nSymbols = ceil(N/obj.sps);
|
||||
|
||||
for epoch = 1:epochs
|
||||
|
||||
|
||||
pm = zeros(obj.nStates,1);
|
||||
% state metrics (log-domain costs): keep as column [nStates×1]
|
||||
pm = zeros(obj.nStates,1); % v_{k-1}(s′)
|
||||
c_hat = zeros(1,obj.nFeasible);
|
||||
v_tilde = zeros(1,obj.nFeasible);
|
||||
pred = zeros(N, obj.nStates, 'uint32');
|
||||
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}, ...]
|
||||
else
|
||||
true_to_state_idx = 1;
|
||||
end
|
||||
|
||||
% ==============================================================
|
||||
% RUNTIME LOOP
|
||||
% ==============================================================
|
||||
symbol = 0;
|
||||
for sample = 1:obj.sps:N
|
||||
symbol = symbol + 1;
|
||||
|
||||
k = symbol;
|
||||
|
||||
% --- Build Δ-delayed observation window y_k
|
||||
@@ -201,80 +220,66 @@ classdef ML_MLSE < handle
|
||||
padL = max(0,1 - i1);
|
||||
padR = max(0,i2 - length(x));
|
||||
yk = [zeros(padL,1); buf(:); zeros(padR,1)]; % Nf×1
|
||||
yk = [yk;1];
|
||||
yk = [yk;1];
|
||||
|
||||
% --- Predict branch metrics for all feasible transitions
|
||||
% --- Predict branch metrics for all feasible transitions: c_hat
|
||||
c_hat = (yk.' * obj.w); % [1×nFeasible]
|
||||
c_hat = c_hat.'; % [nFeasible×1]
|
||||
|
||||
% --- Extended path metrics
|
||||
% --- Extended path metrics: v_tilde = pm(from) + c_hat
|
||||
% normalize pm to avoid growth (invariant to additive const)
|
||||
pm = pm - min(pm);
|
||||
v_tilde = pm(obj.valid_from_idx) + c_hat; % [nFeasible×1]
|
||||
|
||||
% ===== Gradient update (Algorithm 1) =====
|
||||
% if training
|
||||
|
||||
% for current symbol index k -> previous (k-1) and current (k)
|
||||
if k > obj.L
|
||||
prev_seq = d(k-obj.L:k-1); % previous state symbols
|
||||
true_from_state_idx = find(ismember(obj.combs, flip(prev_seq).', 'rows'));% find state indices in trellis
|
||||
% shift-register: previous "to" becomes current "from"
|
||||
true_from_state_idx = true_to_state_idx;
|
||||
|
||||
%if ~(true_from_state_idx == true_to_state_idx), warning('Impossible state transition?!'), end
|
||||
|
||||
curr_seq = d(k-obj.L+1:k); % next state symbols
|
||||
true_to_state_idx = find(ismember(obj.combs, flip(curr_seq).', 'rows'));% find state indices in trellis
|
||||
% 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 = 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
|
||||
% true_from and true_to are (1) -> (-1)
|
||||
% thus dirac = [1,0,0,0]'
|
||||
|
||||
% 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∗ k−1) = 1 if the extended state (s, s′) corresponds to the true realized states (s∗ k, s∗ k−1), and is zero otherwise.
|
||||
|
||||
if sum(dirac) == 0
|
||||
warning('whats happening?!');
|
||||
end
|
||||
|
||||
% softmax over -v_tilde, one-hot target t
|
||||
% 4 feasible transitions: [0,0][0,1][1,0][1,1] #not in
|
||||
% order here!
|
||||
% first round:
|
||||
% v_tilde is zero
|
||||
% -> exp(-0) = 1
|
||||
% -> 1/(1+1+1+1)
|
||||
% -> 1/4
|
||||
% -> each transition is equally likely?
|
||||
% p = exp(-v_tilde);
|
||||
p = exp(-(v_tilde-max(v_tilde)));
|
||||
% softmax over -v_tilde (numerically safe shift)
|
||||
p = exp(-(v_tilde - max(v_tilde)));
|
||||
p = p./sum(p); % found in formula (9) and (19)
|
||||
|
||||
% 1-0.25 = 0.75
|
||||
% 0-0.25 = -0.25
|
||||
% what happens here at the zero? Is this some log
|
||||
% probability - larger zero is likely; lower zero is
|
||||
% unlikely? But this is based on known information (training)
|
||||
dmp = (dirac - p)';
|
||||
|
||||
% 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); % numerical stability
|
||||
probs_lin = exp(-v_norm);
|
||||
probs_lin = probs_lin ./ sum(probs_lin);
|
||||
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_mat(obj.valid) = probs_lin; % linear-space probabilities
|
||||
probs_mat = nan(obj.nStates, obj.nStates);
|
||||
probs_logmat = nan(obj.nStates, obj.nStates);
|
||||
probs_logmat(obj.valid) = probs_log; % log-domain scores
|
||||
probs_mat(obj.valid) = probs_lin;
|
||||
probs_logmat(obj.valid) = probs_log;
|
||||
|
||||
% --- Identify the current true transition
|
||||
[to_idx, from_idx] = find(obj.valid);
|
||||
@@ -282,51 +287,37 @@ classdef ML_MLSE < handle
|
||||
cur_to = to_idx(cur_idx);
|
||||
cur_from = from_idx(cur_idx);
|
||||
|
||||
% --- Plot using imagesc (supports hold)
|
||||
figure(11); clf;
|
||||
imagesc(probs_logmat);
|
||||
axis xy; % origin top-left
|
||||
% colormap(parula);
|
||||
colorbar;
|
||||
xlabel('From state');
|
||||
ylabel('To state');
|
||||
set(gca,'FontSize',10);
|
||||
|
||||
% --- Overlay current transition
|
||||
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');
|
||||
plot(cur_from, cur_to, 'rs', 'MarkerSize', 10, 'LineWidth', 2, 'MarkerFaceColor', 'none');
|
||||
hold off;
|
||||
end
|
||||
|
||||
if 1 %training
|
||||
% the correct state gets a high update, we weight this
|
||||
% with the input signal, from here the signals are not
|
||||
% "understandable"
|
||||
% dmp is large for the correct transition to update
|
||||
% only this one!
|
||||
% dmp is large for the correct transition -> update emphasizes that branch
|
||||
dL_Dw = dmp .* (yk); % ∂CE/∂(w) - formula (10)
|
||||
% from paper: We have observed in simulations that ignoring the derivative of vk−1(s′) during training yields negligible loss after convergence.
|
||||
% my note: so the update direction is the same for b and w?
|
||||
|
||||
%only start with updates when we are inside the signal
|
||||
% only start with updates when we are inside the signal
|
||||
if k > obj.L
|
||||
% actual filter training updates:
|
||||
obj.w = obj.w - mu * dL_Dw;% Nf×nFeasible
|
||||
obj.w = obj.w - mu * dL_Dw; % (Nf+1)×nFeasible
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
% compare select
|
||||
% --- Compare-Select (matrix form, min of costs)
|
||||
v_tilde_mat = inf(obj.nStates, obj.nStates);
|
||||
v_tilde_mat(obj.valid) = v_tilde;
|
||||
[pm, pred(k,:)] = min(v_tilde_mat, [], 2);
|
||||
[pm_next, pred(k,:)] = min(v_tilde_mat, [], 2);
|
||||
|
||||
% re-center to keep metrics bounded (decision-invariant)
|
||||
pm_next = pm_next - min(pm_next);
|
||||
|
||||
pm = pm_next;
|
||||
pm_sto(:,symbol) = pm;
|
||||
|
||||
end
|
||||
|
||||
[~, s_end] = max(pm);
|
||||
% --- Traceback (full; you can window with traceback_depth if desired)
|
||||
[~, s_end] = min(pm);
|
||||
viterbi_path = zeros(symbol,1,'uint32');
|
||||
viterbi_path(symbol) = s_end;
|
||||
for n = symbol:-1:2
|
||||
@@ -334,12 +325,8 @@ classdef ML_MLSE < handle
|
||||
end
|
||||
|
||||
y_vit = obj.first_sym(viterbi_path);
|
||||
y = 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);
|
||||
@@ -357,12 +344,20 @@ classdef ML_MLSE < handle
|
||||
title('Path Metrics (v_tilde)')
|
||||
|
||||
subplot(2,2,4);
|
||||
% scatter(1:N,pm_sto,1,'.')
|
||||
plot(1:symbol,pm_sto)
|
||||
scatter(1:symbol,pm_sto,1,'.')
|
||||
% plot(1:symbol,pm_sto,'LineStyle','none')
|
||||
title('Path Metric Winners')
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
methods (Access=private)
|
||||
function k = seq_key(obj, seq)
|
||||
% Build a stable key string for a sequence row vector in the *same order as combs rows* ([x_k, x_{k-1}, ...])
|
||||
% Use rounding via sprintf to avoid floating-point issues.
|
||||
% seq must be a row vector.
|
||||
k = sprintf(obj.key_fmt, seq);
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user