halfway merged and pulled?!
This commit is contained in:
351
Classes/04_DSP/Equalizer/FFE_DCremoval_adaptive_mu.m
Normal file
351
Classes/04_DSP/Equalizer/FFE_DCremoval_adaptive_mu.m
Normal file
@@ -0,0 +1,351 @@
|
||||
classdef FFE_DCremoval_adaptive_mu < handle
|
||||
% Implementation of plain and simple FFE.
|
||||
% 1) Training mode (stable performance when you use NLMS)
|
||||
% 2) Decision directed mode
|
||||
|
||||
% Eq = FFE("epochs_tr",5,"epochs_dd",5,"len_tr",4096*2,"mu_dd",1e-4,"mu_tr",0,"order",25,"sps",2,"decide",0);
|
||||
|
||||
properties
|
||||
sps % usually 2
|
||||
order
|
||||
e
|
||||
e_tr
|
||||
error
|
||||
|
||||
len_tr
|
||||
mu_tr
|
||||
epochs_tr
|
||||
|
||||
mu_dd
|
||||
epochs_dd
|
||||
|
||||
mu_dc
|
||||
dc_buffer_len
|
||||
|
||||
adaptive_mu_mode
|
||||
|
||||
ffe_buffer_len
|
||||
|
||||
smoothing_buffer_length
|
||||
smoothing_buffer_update
|
||||
|
||||
constellation
|
||||
|
||||
decide
|
||||
end
|
||||
|
||||
methods
|
||||
function obj = FFE_DCremoval_adaptive_mu(options)
|
||||
arguments(Input)
|
||||
|
||||
options.sps = 2;
|
||||
options.order = 15;
|
||||
|
||||
options.len_tr = 4096;
|
||||
options.mu_tr = 0;
|
||||
options.epochs_tr = 5;
|
||||
|
||||
options.mu_dd = 1e-5;
|
||||
options.epochs_dd = 5;
|
||||
|
||||
options.mu_dc = 0.05;
|
||||
options.dc_buffer_len = 1;
|
||||
|
||||
options.ffe_buffer_len = 1;
|
||||
|
||||
options.adaptive_mu_mode = 1;
|
||||
|
||||
options.smoothing_buffer_length = 0;
|
||||
options.smoothing_buffer_update = 0;
|
||||
options.decide = false;
|
||||
|
||||
end
|
||||
|
||||
assert(options.dc_buffer_len>0);
|
||||
|
||||
fn = fieldnames(options);
|
||||
for n = 1:numel(fn)
|
||||
obj.(fn{n}) = options.(fn{n});
|
||||
end
|
||||
|
||||
obj.e = zeros(obj.order,1);
|
||||
obj.error = 0;
|
||||
|
||||
obj.dc_buffer_len = floor(obj.dc_buffer_len);
|
||||
|
||||
end
|
||||
|
||||
function [X,Noi] = process(obj, X, D)
|
||||
|
||||
% actual processing of the signal (steps 1. - 3.)
|
||||
% 1 normalize RMS
|
||||
X = X.normalize("mode","rms");
|
||||
|
||||
obj.constellation = unique(D.signal);
|
||||
|
||||
% if obj.smoothing_buffer_length > 0
|
||||
% % Apply A1 filter smoothing
|
||||
% % Calculate the moving sum with the window size N1
|
||||
% moving_sum = movsum(X.signal, [obj.smoothing_buffer_length,0]);
|
||||
%
|
||||
% % Initialize the output smoothed signal
|
||||
% X.signal = X.signal - (1 / obj.smoothing_buffer_length) * moving_sum;
|
||||
% end
|
||||
|
||||
% Training Mode
|
||||
training = 1;
|
||||
obj.equalize(X.signal, D.signal,obj.mu_tr,obj.epochs_tr,obj.len_tr,training);
|
||||
obj.e_tr = obj.e;
|
||||
|
||||
% Decision Directed Mode
|
||||
N = X.length;
|
||||
training = 0;
|
||||
[signal,decision]=obj.equalize(X.signal, D.signal,obj.mu_dd,obj.epochs_dd,N,training);
|
||||
|
||||
% Output Signal
|
||||
if obj.decide
|
||||
X.signal = decision;
|
||||
else
|
||||
X.signal = signal;
|
||||
end
|
||||
X.fs = D.fs; %change sampling frequency of outgoing signal from fdac e.g. 2 sps to symbol spaced = fsym
|
||||
lbdesc = [num2str(obj.order),' tap FFE'];
|
||||
X = X.logbookentry(lbdesc); % append to logbook
|
||||
|
||||
Noi = X - D;
|
||||
|
||||
end
|
||||
|
||||
function [y,d_hat] = equalize(obj, x, d, mu_lms, epochs, N, training)
|
||||
% Equalize with adaptive DC-removal, VSS, and parallel-buffered DC updates
|
||||
% Added: FFE gradient buffering in DD mode (error buffer) with update every obj.dc_buffer_len symbols
|
||||
|
||||
arguments
|
||||
obj
|
||||
x
|
||||
d
|
||||
mu_lms % LMS step-size (or 0 for NLMS)
|
||||
epochs % number of training/DD epochs
|
||||
N % number of samples to process
|
||||
training % boolean flag: true->training mode, false->DD mode
|
||||
end
|
||||
|
||||
if isempty(obj.e)
|
||||
obj.e = zeros(obj.order,1);
|
||||
end
|
||||
|
||||
% Zero-padding for filter memory
|
||||
x = [zeros(floor(obj.order/2),1); x; zeros(obj.order,1)];
|
||||
|
||||
% Initialize storage
|
||||
numSymbols = ceil(N/obj.sps);
|
||||
y = zeros(numSymbols,1);
|
||||
d_hat = zeros(numSymbols,1);
|
||||
err = NaN(numSymbols,numel(obj.constellation));
|
||||
e_dc_save= zeros(numSymbols,1);
|
||||
|
||||
% DC-adaptation parameters
|
||||
P_err = 0; % running error power
|
||||
alpha = 0.98; % forgetting factor for error power
|
||||
err_prev = 0; % previous error sample for VSS correlation
|
||||
gamma_dc = 1e-6; % meta step-size for DC VSS
|
||||
mu_min = 1e-6; % lower bound for mu_dc
|
||||
mu_max = 3e-1; % upper bound for mu_dc
|
||||
|
||||
% DC removal buffer
|
||||
L = obj.dc_buffer_len; % buffer length
|
||||
e_dc_buf = NaN(L,1);
|
||||
e_dc_est = 0;
|
||||
|
||||
% FFE gradient buffer (DD mode only)
|
||||
L_grad = obj.ffe_buffer_len; % buffer length
|
||||
if ~training
|
||||
% each column holds one past gradient of length obj.order
|
||||
grad_buf = NaN(obj.order, L_grad);
|
||||
end
|
||||
|
||||
smth_buffer = zeros(1, obj.smoothing_buffer_length);
|
||||
smth_mean = 0;
|
||||
% Main loop
|
||||
for epoch = 1:epochs
|
||||
s = 0;
|
||||
for sample = 1:obj.sps:N
|
||||
s = s + 1;
|
||||
|
||||
if obj.smoothing_buffer_length > 0
|
||||
smth_buffer = circshift(smth_buffer,1,2);
|
||||
smth_buffer(1) = x(sample);
|
||||
if mod(s, obj.smoothing_buffer_update) == 0
|
||||
smth_mean = mean(smth_buffer);
|
||||
end
|
||||
x(sample:sample+obj.sps-1) = x(sample:sample+obj.sps-1)-smth_mean;
|
||||
end
|
||||
|
||||
U = x(obj.order+sample-1:-1:sample);
|
||||
|
||||
%-- 1) filter output with DC correction
|
||||
y(s) = e_dc_est + obj.e.'*U;
|
||||
|
||||
%-- 2) decision
|
||||
if training
|
||||
[~, idx] = min(abs(d(s) - obj.constellation));
|
||||
else
|
||||
[~, idx] = min(abs(y(s) - obj.constellation));
|
||||
end
|
||||
d_hat(s) = obj.constellation(idx);
|
||||
|
||||
%-- 3) error
|
||||
e_val = y(s) - d_hat(s);
|
||||
if epoch == epochs
|
||||
|
||||
err(s,idx) = e_val;
|
||||
true_err(s,idx) = y(s) - d(s);
|
||||
end
|
||||
|
||||
%-- 4) tap-weight update: training immediate, DD buffered
|
||||
if training
|
||||
% immediate update (LMS or NLMS)
|
||||
if mu_lms ~= 0
|
||||
obj.e = obj.e - mu_lms * e_val * U;
|
||||
else
|
||||
normU = (U.'*U) + eps;
|
||||
obj.e = obj.e - e_val * U / normU;
|
||||
end
|
||||
else
|
||||
if 0
|
||||
% buffer gradient
|
||||
if mu_lms ~= 0
|
||||
grad = e_val * U;
|
||||
else
|
||||
normU = (U.'*U) + eps;
|
||||
grad = e_val * U / normU;
|
||||
end
|
||||
% shift and insert
|
||||
grad_buf = circshift(grad_buf, 1, 2);
|
||||
grad_buf(:,1) = grad;
|
||||
% update once every L symbols
|
||||
if mod(s, L_grad) == 0
|
||||
avg_grad = mean(grad_buf, 2, 'omitnan');
|
||||
if mu_lms ~= 0
|
||||
obj.e = obj.e - mu_lms * avg_grad;
|
||||
else
|
||||
obj.e = obj.e - avg_grad;
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
%-- 5) DC adaptation
|
||||
if obj.mu_dc ~= 0
|
||||
|
||||
if obj.adaptive_mu_mode
|
||||
|
||||
% VSS for mu_dc
|
||||
delta_mu = gamma_dc * e_val * err_prev * (U.'*U);
|
||||
obj.mu_dc = min(max(obj.mu_dc + delta_mu, mu_min), mu_max);
|
||||
err_prev = e_val;
|
||||
|
||||
% DC buffer update & periodic estimate
|
||||
P_err = alpha*P_err + (1-alpha)*e_val^2;
|
||||
mu_dc_norm = obj.mu_dc / (P_err + eps);
|
||||
|
||||
else
|
||||
|
||||
% DC buffer update & periodic estimate
|
||||
% P_err = alpha*P_err + (1-alpha)*e_val^2;
|
||||
% mu_dc_norm = obj.mu_dc / (P_err + eps);
|
||||
|
||||
mu_dc_norm = obj.mu_dc;
|
||||
|
||||
end
|
||||
|
||||
e_dc_buf = circshift(e_dc_buf, 1);
|
||||
e_dc_buf(1) = e_dc_est - mu_dc_norm * e_val;
|
||||
|
||||
if mod(s, L) == 0
|
||||
e_dc_est = median(e_dc_buf, 'omitnan');
|
||||
end
|
||||
|
||||
P_err_save(s) = P_err;
|
||||
% Pcorr_save(s) = e_val * err_prev;
|
||||
Ucorr_save(s) = (U.'*U);
|
||||
mu_dc_save(s) = mu_dc_norm;
|
||||
e_dc_save(s) = e_dc_est;
|
||||
|
||||
end
|
||||
|
||||
% store instantaneous squared error
|
||||
obj.error(epoch, s) = e_val^2;
|
||||
end
|
||||
end
|
||||
|
||||
% Optional plotting in DD mode (uncomment if needed)
|
||||
if 0%~training
|
||||
|
||||
constellation = unique(d);
|
||||
lvlcol = cbrewer2('Paired', numel(constellation)*2);
|
||||
lvlcol = lvlcol(2:2:end, :);
|
||||
|
||||
true_err(true_err==0) = NaN;
|
||||
true_errmoverr = movsum(true_err, 4096, 'omitnan');
|
||||
true_errmoverr = true_errmoverr./rms(true_errmoverr);
|
||||
|
||||
moverr = movsum(err, [100,100], 'omitnan');
|
||||
moverr = moverr./rms(moverr);
|
||||
|
||||
figure(500); clf
|
||||
hold on
|
||||
% 1st subplot: true_errmoverr
|
||||
% subplot(2,2,1); hold on
|
||||
% for k = 1:4
|
||||
% scatter(1:numSymbols, true_errmoverr(:,k), 1, lvlcol(k,:), '.');
|
||||
% end
|
||||
|
||||
% scatter(1:numSymbols, Ucorr_save./rms(Ucorr_save), 1, lvlcol(1,:), '.','DisplayName','Ucorr_save');
|
||||
% scatter(1:numSymbols, Pcorr_save./rms(Pcorr_save), 1, lvlcol(1,:), '.','DisplayName','P_corr');
|
||||
% scatter(1:numSymbols, P_err_save, 1, lvlcol(1,:), '.','DisplayName','P_err');
|
||||
% scatter(1:numSymbols, mu_dc_save, 1, lvlcol(2,:), '.','DisplayName','adapted value of $\mu_{DC}$');
|
||||
% scatter(1:numSymbols, sum(moverr,2,'omitnan'), 1, lvlcol(1,:), '.','DisplayName','Mov Error $\hat{d}$ - x over all levels');
|
||||
scatter(1:numSymbols, sum(e_dc_save,2,'omitnan'), 1, lvlcol(2,:), '.','DisplayName','Est. Error that is subtracted');
|
||||
title('Moving Sum Error');
|
||||
hold off
|
||||
legend
|
||||
|
||||
% 2nd subplot: moverr
|
||||
subplot(2,2,2); hold on
|
||||
for k = 1:4
|
||||
scatter(1:numSymbols, moverr(:,k), 1, lvlcol(k,:), '.');
|
||||
end
|
||||
title('Moving Sum Error');
|
||||
hold off
|
||||
legend
|
||||
|
||||
% 3rd subplot: err
|
||||
subplot(2,2,3); hold on
|
||||
for k = 1:4
|
||||
scatter(1:numSymbols, err(:,k), 1, lvlcol(k,:), '.');
|
||||
end
|
||||
title('Error');
|
||||
hold off
|
||||
legend
|
||||
|
||||
% 4th subplot: err + obj.constellation'
|
||||
subplot(2,2,4); hold on
|
||||
for k = 1:4
|
||||
scatter(1:numSymbols, err(:,k) + obj.constellation(k), 1, lvlcol(k,:), '.');
|
||||
end
|
||||
yline(obj.constellation, '--k');
|
||||
title('Error + Constellation');
|
||||
hold off
|
||||
legend
|
||||
|
||||
sgtitle('Error Analysis Subplots');
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
240
Classes/04_DSP/Equalizer/FFE_DCremoval_level.m
Normal file
240
Classes/04_DSP/Equalizer/FFE_DCremoval_level.m
Normal file
@@ -0,0 +1,240 @@
|
||||
classdef FFE_DCremoval_level < handle
|
||||
% Implementation of plain and simple FFE.
|
||||
% 1) Training mode (stable performance when you use NLMS)
|
||||
% 2) Decision directed mode
|
||||
|
||||
% Eq = FFE("epochs_tr",5,"epochs_dd",5,"len_tr",4096*2,"mu_dd",1e-4,"mu_tr",0,"order",25,"sps",2,"decide",0);
|
||||
|
||||
properties
|
||||
sps % usually 2
|
||||
order
|
||||
e
|
||||
error
|
||||
|
||||
len_tr
|
||||
mu_tr
|
||||
epochs_tr
|
||||
|
||||
mu_dd
|
||||
epochs_dd
|
||||
|
||||
mu_dc
|
||||
dc_buffer_len
|
||||
|
||||
constellation
|
||||
|
||||
decide
|
||||
|
||||
KF_meas_noise = 0;
|
||||
KF_process_noise = 0;
|
||||
KF_state_cov = 0;
|
||||
end
|
||||
|
||||
methods
|
||||
function obj = FFE_DCremoval_level(options)
|
||||
arguments(Input)
|
||||
|
||||
options.sps = 2;
|
||||
options.order = 15;
|
||||
|
||||
options.len_tr = 4096;
|
||||
options.mu_tr = 0;
|
||||
options.epochs_tr = 5;
|
||||
|
||||
options.mu_dd = 1e-5;
|
||||
options.epochs_dd = 5;
|
||||
|
||||
options.mu_dc = 0.05;
|
||||
options.dc_buffer_len = 1;
|
||||
|
||||
options.decide = false;
|
||||
|
||||
end
|
||||
|
||||
assert(options.dc_buffer_len>0);
|
||||
|
||||
fn = fieldnames(options);
|
||||
for n = 1:numel(fn)
|
||||
obj.(fn{n}) = options.(fn{n});
|
||||
end
|
||||
|
||||
obj.e = zeros(obj.order,1);
|
||||
obj.error = 0;
|
||||
|
||||
obj.dc_buffer_len = floor(obj.dc_buffer_len);
|
||||
|
||||
end
|
||||
|
||||
function [X,Noi] = process(obj, X, D)
|
||||
|
||||
% actual processing of the signal (steps 1. - 3.)
|
||||
% 1 normalize RMS
|
||||
X = X.normalize("mode","rms");
|
||||
|
||||
obj.constellation = unique(D.signal);
|
||||
|
||||
% Training Mode
|
||||
training = 1;
|
||||
obj.equalize(X.signal, D.signal,obj.mu_tr,obj.epochs_tr,obj.len_tr,training);
|
||||
|
||||
% Decision Directed Mode
|
||||
N = X.length;
|
||||
training = 0;
|
||||
[signal,decision]=obj.equalize(X.signal, D.signal,obj.mu_dd,obj.epochs_dd,N,training);
|
||||
|
||||
% Output Signal
|
||||
if obj.decide
|
||||
X.signal = decision;
|
||||
else
|
||||
X.signal = signal;
|
||||
end
|
||||
X.fs = D.fs; %change sampling frequency of outgoing signal from fdac e.g. 2 sps to symbol spaced = fsym
|
||||
lbdesc = [num2str(obj.order),' tap FFE'];
|
||||
X = X.logbookentry(lbdesc); % append to logbook
|
||||
|
||||
Noi = X - D;
|
||||
|
||||
end
|
||||
function [y, d_hat, logs] = equalize(obj, x, d, mu_lms, epochs, N, training)
|
||||
% Equalize with Kalman-based DC removal; training epochs estimate KF noise parameters
|
||||
|
||||
arguments
|
||||
obj
|
||||
x
|
||||
d
|
||||
mu_lms % LMS step-size (or 0 for NLMS)
|
||||
epochs % number of training or DD epochs
|
||||
N % number of samples to process
|
||||
training % true => training mode (tap-training + noise estimation)
|
||||
end
|
||||
|
||||
% Zero-pad for filter memory
|
||||
x = [zeros(floor(obj.order/2),1); x; zeros(obj.order,1)];
|
||||
numSym = ceil(N/obj.sps);
|
||||
|
||||
% Pre-allocate outputs
|
||||
y = zeros(numSym,1);
|
||||
d_hat = zeros(numSym,1);
|
||||
|
||||
% --- Training: estimate noise stats and train taps ---
|
||||
if training
|
||||
% Pre-allocate error accumulator
|
||||
totalTrain = epochs * numSym;
|
||||
trainErrs = zeros(totalTrain,1);
|
||||
te_idx = 0;
|
||||
|
||||
for ep = 1:epochs
|
||||
s = 0;
|
||||
for n = 1:obj.sps:N
|
||||
s = s + 1;
|
||||
U = x(obj.order + n - 1 : -1 : n);
|
||||
|
||||
% Equalizer output (no DC correction yet)
|
||||
y(s) = obj.e.' * U;
|
||||
|
||||
% Decision based on known symbol
|
||||
[~, idx] = min(abs(d(s) - obj.constellation));
|
||||
d_hat(s) = obj.constellation(idx);
|
||||
|
||||
% Instantaneous error
|
||||
e_n = y(s) - d_hat(s);
|
||||
|
||||
% Collect error for noise estimation
|
||||
te_idx = te_idx + 1;
|
||||
trainErrs(te_idx) = e_n;
|
||||
|
||||
% Tap-weight update (LMS or NLMS)
|
||||
if mu_lms ~= 0
|
||||
obj.e = obj.e - mu_lms * e_n * U;
|
||||
else
|
||||
normU = (U.'*U) + eps;
|
||||
obj.e = obj.e - e_n * U / normU;
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
% Estimate measurement noise R and process noise Q
|
||||
R_est = var(trainErrs(1:te_idx));
|
||||
Q_est = 1e-3 * R_est; % Q/R ratio = 1e-3 (tune as needed)
|
||||
|
||||
% Store into object for DD pass
|
||||
obj.KF_meas_noise = R_est;
|
||||
obj.KF_process_noise = Q_est;
|
||||
obj.KF_state_cov = 5*R_est; % or 5*R_est for a more “eager” start
|
||||
|
||||
% No Kalman in training; return
|
||||
logs = struct();
|
||||
return;
|
||||
end
|
||||
|
||||
% --- Decision-Directed with Kalman DC tracking ---
|
||||
% Initialize Kalman state
|
||||
x_est = 0;
|
||||
P = obj.KF_state_cov; % initial P (tune in obj; e.g. 1)
|
||||
|
||||
% Logging containers
|
||||
logs.y_raw = zeros(numSym,1);
|
||||
logs.y_corr = zeros(numSym,1);
|
||||
logs.err = zeros(numSym,1);
|
||||
logs.K_gain = zeros(numSym,1);
|
||||
logs.x_est = zeros(numSym,1);
|
||||
logs.P = zeros(numSym,1);
|
||||
logs.normU = zeros(numSym,1);
|
||||
logs.tap_norm = zeros(numSym,1);
|
||||
|
||||
for ep = 1:epochs
|
||||
s = 0;
|
||||
for n = 1:obj.sps:N
|
||||
s = s + 1;
|
||||
U = x(obj.order + n - 1 : -1 : n);
|
||||
|
||||
% 1) Kalman prediction
|
||||
P = P + obj.KF_process_noise;
|
||||
x_prior = x_est;
|
||||
|
||||
% 2) raw equalizer output
|
||||
y_raw = obj.e.' * U;
|
||||
logs.y_raw(s) = y_raw;
|
||||
|
||||
% 3) DC-corrected output
|
||||
y_corr = y_raw + x_prior;
|
||||
y(s) = y_corr;
|
||||
logs.y_corr(s) = y_corr;
|
||||
|
||||
% 4) decision-directed symbol
|
||||
[~, idx] = min(abs(y_corr - obj.constellation));
|
||||
d_hat(s) = obj.constellation(idx);
|
||||
|
||||
% 5) error
|
||||
e_n = y_corr - d_hat(s);
|
||||
logs.err(s) = e_n;
|
||||
|
||||
% 6) tap-weight update (LMS/NLMS)
|
||||
if mu_lms ~= 0
|
||||
obj.e = obj.e - mu_lms * e_n * U;
|
||||
else
|
||||
normU = U.' * U + eps;
|
||||
logs.normU(s) = normU;
|
||||
obj.e = obj.e - e_n * U / normU;
|
||||
end
|
||||
|
||||
logs.tap_norm(s) = norm(obj.e);
|
||||
|
||||
% 7) Kalman update
|
||||
K_gain = P / (P + obj.KF_meas_noise);
|
||||
x_est = x_prior + K_gain * (e_n - x_prior);
|
||||
P = (1 - K_gain) * P;
|
||||
|
||||
% 8) log Kalman state
|
||||
logs.K_gain(s) = K_gain;
|
||||
logs.x_est(s) = x_est;
|
||||
logs.P(s) = P;
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
233
Classes/04_DSP/Equalizer/FFE_MLSE.m
Normal file
233
Classes/04_DSP/Equalizer/FFE_MLSE.m
Normal file
@@ -0,0 +1,233 @@
|
||||
classdef FFE_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);
|
||||
|
||||
properties
|
||||
sps % usually 2
|
||||
order
|
||||
e
|
||||
e_tr
|
||||
error
|
||||
|
||||
len_tr
|
||||
mu_tr
|
||||
epochs_tr
|
||||
|
||||
dd_mode % 1 or 0 to set DD-mode on or off
|
||||
mu_dd %weight update in dd mode
|
||||
epochs_dd
|
||||
|
||||
constellation
|
||||
|
||||
L %viterbi memory length
|
||||
|
||||
alpha
|
||||
DIR
|
||||
DIR_flip
|
||||
trellis_states
|
||||
|
||||
traceback_depth
|
||||
end
|
||||
|
||||
methods
|
||||
function obj = FFE_MLSE(options)
|
||||
arguments(Input)
|
||||
|
||||
options.sps = 2;
|
||||
options.order = 15;
|
||||
|
||||
options.len_tr = 4096;
|
||||
options.mu_tr = 0;
|
||||
options.epochs_tr = 5;
|
||||
|
||||
options.dd_mode = 1;
|
||||
options.mu_dd = 1e-5;
|
||||
options.epochs_dd = 5;
|
||||
|
||||
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
|
||||
|
||||
function [X,X_viterbi] = process(obj, X, D)
|
||||
|
||||
% actual processing of the signal (steps 1. - 3.)
|
||||
% 1 normalize RMS
|
||||
X = X.normalize("mode","rms");
|
||||
|
||||
obj.constellation = unique(D.signal);
|
||||
|
||||
if length(X)/length(D) ~= obj.sps
|
||||
warning('Signal length does not fit to reference!');
|
||||
end
|
||||
|
||||
% Training Mode
|
||||
n = obj.len_tr;
|
||||
training = 1;
|
||||
obj.equalize(X.signal, D.signal,obj.mu_tr,obj.epochs_dd,n,training);
|
||||
obj.e_tr = obj.e;
|
||||
|
||||
% Decision Directed Mode
|
||||
n = X.length;
|
||||
training = 0;
|
||||
[y,y_vit]=obj.equalize(X.signal, D.signal,obj.mu_dd,obj.epochs_dd,n,training);
|
||||
|
||||
X_viterbi = X;
|
||||
|
||||
X.signal = y;
|
||||
X.fs = D.fs; %change sampling frequency of outgoing signal from fdac e.g. 2 sps to symbol spaced = fsym
|
||||
lbdesc = [num2str(obj.order),' tap FFE'];
|
||||
X = X.logbookentry(lbdesc); % append to logbook
|
||||
|
||||
X_viterbi.signal = y_vit;
|
||||
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)
|
||||
% ==============================================================
|
||||
% FFE + Whitening + Viterbi Equalizer (reference implementation)
|
||||
% ==============================================================
|
||||
|
||||
% --- Input padding and preallocation
|
||||
x = [zeros(floor(obj.order/2),1); x; zeros(obj.order,1)];
|
||||
N_ = N / obj.sps;
|
||||
y = zeros(N_,1);
|
||||
y_white = zeros(N_,1);
|
||||
|
||||
for epoch = 1:epochs
|
||||
|
||||
% ==============================================================
|
||||
% INITIALIZATION (only before final epoch and detection mode)
|
||||
% ==============================================================
|
||||
if epoch == epochs && ~training
|
||||
|
||||
% --- Parameters
|
||||
S = numel(unique(d));
|
||||
L = obj.L;
|
||||
nStates = S^L;
|
||||
nFeasible = S^(L-1)*S;
|
||||
|
||||
% --- Trellis setup
|
||||
obj.DIR = arburg(y-d, L);
|
||||
obj.DIR_flip = flip(obj.DIR);
|
||||
obj.trellis_states = reshape(unique(d),1,[]);
|
||||
|
||||
pre_comb_mat = repmat(obj.trellis_states, L, 1);
|
||||
pre_comb_cell = mat2cell(pre_comb_mat, ones(1,L), size(pre_comb_mat,2));
|
||||
combs = fliplr(combvec(pre_comb_cell{:}).');
|
||||
first_sym = combs(:,1);
|
||||
last_sym = combs(:,end);
|
||||
nStates = size(combs,1);
|
||||
|
||||
noise_free_received = inf(nStates,nStates);
|
||||
valid = false(nStates);
|
||||
|
||||
for from = 1:nStates
|
||||
for to = 1:nStates
|
||||
if all(combs(to,2:end) == combs(from,1:end-1))
|
||||
noise_free_received(to,from) = ...
|
||||
dot(combs(to,:), obj.DIR_flip(end:-1:2)) + last_sym(from)*obj.DIR_flip(1);
|
||||
valid(to,from) = true;
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
nf_vec = noise_free_received(valid);
|
||||
[valid_to, valid_from] = find(valid);
|
||||
from_per_to = arrayfun(@(to)find(valid(to,:)), 1:nStates, 'UniformOutput', false);
|
||||
|
||||
% --- Noise stats
|
||||
y_ideal = conv(d(:), obj.DIR(:), "same");
|
||||
sigma2 = mean(abs(y - y_ideal).^2);
|
||||
inv2s2 = 1/(2*sigma2);
|
||||
|
||||
% --- Vector initialization
|
||||
bm_vec = zeros(1,nFeasible);
|
||||
pm = zeros(nStates,1);
|
||||
pm_next = zeros(nStates,1);
|
||||
bm_fw = zeros(nStates,nStates,length(y));
|
||||
zi = zeros(max(numel(obj.DIR)-1,0),1);
|
||||
end
|
||||
|
||||
% ==============================================================
|
||||
% RUNTIME LOOP (FFE update + Viterbi detection in last epoch)
|
||||
% ==============================================================
|
||||
symbol = 0;
|
||||
for sample = 1:obj.sps:N
|
||||
symbol = symbol + 1;
|
||||
|
||||
% --- FFE output
|
||||
U = x(obj.order+sample-1:-1:sample);
|
||||
y(symbol,1) = obj.e.' * U;
|
||||
|
||||
% --- Decision
|
||||
if training
|
||||
d_hat(symbol,1) = d(symbol);
|
||||
else
|
||||
[~,symbol_idx] = min(abs(y(symbol) - obj.constellation));
|
||||
d_hat(symbol,1) = obj.constellation(symbol_idx);
|
||||
end
|
||||
|
||||
% --- LMS weight update
|
||||
err(symbol) = d_hat(symbol) - y(symbol);
|
||||
obj.e = obj.e + mu * (err(symbol) * U);
|
||||
|
||||
% --- Whitening + Viterbi (final epoch only)
|
||||
if epoch == epochs && ~training
|
||||
|
||||
[y_white(symbol), zi] = filter(obj.DIR,1,y(symbol), zi);
|
||||
|
||||
if symbol == 1
|
||||
pm = -inf(nStates,nStates);
|
||||
pm(:,1:nStates) = 0;
|
||||
else
|
||||
bm_vec = -(y_white(symbol) - nf_vec).^2 * inv2s2;
|
||||
bm_mat = -inf(nStates,nStates);
|
||||
bm_mat(valid) = bm_vec;
|
||||
|
||||
pm_new = pm + bm_mat;
|
||||
[pm_survive(:,symbol), pm_survivor_fw_idx(:,symbol)] = max(pm_new,[],2);
|
||||
pm = repmat(pm_survive(:,symbol).', nStates,1);
|
||||
end
|
||||
|
||||
% --- Traceback
|
||||
if mod(symbol,obj.traceback_depth) == 0
|
||||
[~,viterbi_path(symbol)] = max(pm_survive(:,symbol));
|
||||
for n = symbol:-1:symbol-obj.traceback_depth+2
|
||||
viterbi_path(n-1) = pm_survivor_fw_idx(viterbi_path(n),n);
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
% --- Final reconstruction
|
||||
if epoch == epochs && ~training
|
||||
y_vit = first_sym(viterbi_path);
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
355
Classes/04_DSP/Equalizer/ML_MLSE.m
Normal file
355
Classes/04_DSP/Equalizer/ML_MLSE.m
Normal file
@@ -0,0 +1,355 @@
|
||||
classdef ML_MLSE < 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 = []
|
||||
ce = ones(1,1)
|
||||
end
|
||||
|
||||
methods
|
||||
function obj = ML_MLSE(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);
|
||||
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
|
||||
|
||||
% ===================================================================
|
||||
% 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);
|
||||
try
|
||||
ref_bits=PAMmapper(obj.S,0).demap(y_ref(1:length(y)));
|
||||
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: %.2e\n',epoch,ber);
|
||||
obj.ber(epoch)=ber;
|
||||
catch
|
||||
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
|
||||
end
|
||||
end
|
||||
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
|
||||
Reference in New Issue
Block a user