Scattered stuff from Silas during Dissertation
This commit is contained in:
@@ -198,44 +198,65 @@ classdef Signal
|
||||
end
|
||||
|
||||
%% Add signals from one signal to another, the first object will sustain
|
||||
function Sum = plus(X,y)
|
||||
|
||||
if isa(y,'Signal')
|
||||
Sum = X;
|
||||
Sum.signal = X.signal + y.signal;
|
||||
elseif isnumeric(y)
|
||||
Sum = X;
|
||||
Sum.signal = X.signal + y;
|
||||
end
|
||||
|
||||
end
|
||||
function Sum = plus(X,y)
|
||||
|
||||
if isa(X,'Signal') && isa(y,'Signal')
|
||||
Sum = X;
|
||||
Sum.signal = X.signal + y.signal;
|
||||
elseif isa(X,'Signal') && isnumeric(y)
|
||||
Sum = X;
|
||||
Sum.signal = X.signal + y;
|
||||
elseif isnumeric(X) && isa(y,'Signal')
|
||||
Sum = y;
|
||||
Sum.signal = X + y.signal;
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
%% Add signals from one signal to another, the first object will sustain
|
||||
function Diff = minus(X,y)
|
||||
|
||||
if isa(y,'Signal')
|
||||
Diff = X;
|
||||
Diff.signal = X.signal - y.signal;
|
||||
elseif isnumeric(y)
|
||||
Diff = X;
|
||||
Diff.signal = X.signal - y;
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
function Product = times(X,y)
|
||||
|
||||
if isa(y,'Signal')
|
||||
Product = X;
|
||||
Product.signal = X.signal .* y.signal;
|
||||
elseif isnumeric(y)
|
||||
Product = X;
|
||||
Product.signal = X.signal .* y;
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
%% Display length
|
||||
function Diff = minus(X,y)
|
||||
|
||||
if isa(X,'Signal') && isa(y,'Signal')
|
||||
Diff = X;
|
||||
Diff.signal = X.signal - y.signal;
|
||||
elseif isa(X,'Signal') && isnumeric(y)
|
||||
Diff = X;
|
||||
Diff.signal = X.signal - y;
|
||||
elseif isnumeric(X) && isa(y,'Signal')
|
||||
Diff = y;
|
||||
Diff.signal = X - y.signal;
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
function Product = times(X,y)
|
||||
|
||||
if isa(X,'Signal') && isa(y,'Signal')
|
||||
Product = X;
|
||||
Product.signal = X.signal .* y.signal;
|
||||
elseif isa(X,'Signal') && isnumeric(y)
|
||||
Product = X;
|
||||
Product.signal = X.signal .* y;
|
||||
elseif isnumeric(X) && isa(y,'Signal')
|
||||
Product = y;
|
||||
Product.signal = X .* y.signal;
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
function Product = mtimes(X,y)
|
||||
|
||||
if (isa(X,'Signal') && isnumeric(y) && isscalar(y)) || ...
|
||||
(isnumeric(X) && isscalar(X) && isa(y,'Signal'))
|
||||
Product = times(X,y);
|
||||
else
|
||||
error('Signal:mtimes:UnsupportedOperands', ...
|
||||
'Use element-wise .* for Signal multiplication, or scalar * Signal for scaling.');
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
%% Display length
|
||||
function return_length = length(obj)
|
||||
%METHOD1 Summary of this method goes here
|
||||
% Detailed explanation goes here
|
||||
@@ -325,7 +346,8 @@ classdef Signal
|
||||
|
||||
else
|
||||
|
||||
obj.signal = resample(obj.signal,options.fs_out,options.fs_in,options.n,options.beta);
|
||||
[p, q] = rat(options.fs_out / options.fs_in);
|
||||
obj.signal = resample(obj.signal,p,q,options.n,options.beta);
|
||||
|
||||
desc = ['resample signal from ', num2str(options.fs_in*1e-9), ' GHz to ', num2str(options.fs_out*1e-9), ' GHz' ];
|
||||
|
||||
@@ -344,16 +366,17 @@ classdef Signal
|
||||
obj
|
||||
options.fignum = 2025
|
||||
options.displayname = "";
|
||||
options.color = [];
|
||||
options.linestyle = '-';
|
||||
options.normalizeToNyquist = 0;
|
||||
options.normalizeToSamplingRate = 0;
|
||||
options.addDCoffset = 0;
|
||||
options.normalizeToDC = 0;
|
||||
options.normalizeTo0dB = 0;
|
||||
options.show_onesided = false;
|
||||
options.max_num_lines = []; % Leave empty or omit to disable line rotation
|
||||
options.fft_length = [];
|
||||
options.color = [];
|
||||
options.linestyle = '-';
|
||||
options.HandleVisibility (1,1) string {mustBeMember(options.HandleVisibility, ["on","off"])} = "on";
|
||||
options.normalizeToNyquist = 0;
|
||||
options.normalizeToSamplingRate = 0;
|
||||
options.addDCoffset = 0;
|
||||
options.normalizeToDC = 0;
|
||||
options.normalizeTo0dB = 0;
|
||||
options.show_onesided = false;
|
||||
options.max_num_lines = []; % Leave empty or omit to disable line rotation
|
||||
options.fft_length = [];
|
||||
% --- NEW options ---
|
||||
options.useWavelengthAxis (1,1) logical = false % plot x-axis in wavelength
|
||||
options.lambda0_nm (1,1) double = 1310 % center wavelength [nm]
|
||||
@@ -364,23 +387,23 @@ classdef Signal
|
||||
end
|
||||
|
||||
|
||||
useSamplingRateAxis = options.normalizeToSamplingRate ~= 0;
|
||||
useRadPerSampleAxis = options.normalizeToNyquist ~= 0 && ~useSamplingRateAxis;
|
||||
|
||||
if ~useRadPerSampleAxis && ~useSamplingRateAxis
|
||||
[p_lin,f_Hz] = pwelch(obj.signal, hanning(options.fft_length), ...
|
||||
options.fft_length/2, options.fft_length, ...
|
||||
obj.fs, "centered", "power", "mean");
|
||||
f_GHz = f_Hz*1e-9; % keep frequency vector for frequency axis
|
||||
else
|
||||
[p_lin,f_rad] = pwelch(obj.signal, hanning(options.fft_length), ...
|
||||
options.fft_length/2, options.fft_length, ...
|
||||
"centered", "power", "mean");
|
||||
% In normalized modes, pwelch returns rad/sample centered on 0.
|
||||
% Divide by 2*pi for the f/fs axis where Nyquist is 0.5.
|
||||
end
|
||||
useSamplingRateAxis = options.normalizeToSamplingRate ~= 0;
|
||||
useRadPerSampleAxis = options.normalizeToNyquist ~= 0 && ~useSamplingRateAxis;
|
||||
|
||||
% p_lin = movmean(p_lin,4);
|
||||
if ~useRadPerSampleAxis && ~useSamplingRateAxis
|
||||
[p_lin,f_Hz] = pwelch(obj.signal, hanning(options.fft_length), ...
|
||||
options.fft_length/2, options.fft_length, ...
|
||||
obj.fs, "centered", "power", "mean");
|
||||
f_GHz = f_Hz*1e-9; % keep frequency vector for frequency axis
|
||||
else
|
||||
[p_lin,f_rad] = pwelch(obj.signal, hanning(options.fft_length), ...
|
||||
options.fft_length/2, options.fft_length, ...
|
||||
"centered", "power", "mean");
|
||||
% In normalized modes, pwelch returns rad/sample centered on 0.
|
||||
% Divide by 2*pi for the f/fs axis where Nyquist is 0.5.
|
||||
end
|
||||
|
||||
p_lin = movmean(p_lin,10);
|
||||
|
||||
if options.normalizeTo0dB
|
||||
p_lin = p_lin ./ max(p_lin);
|
||||
@@ -392,67 +415,67 @@ classdef Signal
|
||||
end
|
||||
|
||||
% --- If requested, build wavelength axis from frequency offset ---
|
||||
if options.useWavelengthAxis && ~useRadPerSampleAxis && ~useSamplingRateAxis
|
||||
c = physconst('LightSpeed'); % [m/s]
|
||||
lambda0_m = options.lambda0_nm*1e-9; % center wavelength [m]
|
||||
f_c = c / lambda0_m; % carrier frequency [Hz]
|
||||
if options.useWavelengthAxis && ~useRadPerSampleAxis && ~useSamplingRateAxis
|
||||
c = physconst('LightSpeed'); % [m/s]
|
||||
lambda0_m = options.lambda0_nm*1e-9; % center wavelength [m]
|
||||
f_c = c / lambda0_m; % carrier frequency [Hz]
|
||||
|
||||
% exact mapping
|
||||
f_abs = f_c + f_Hz; % absolute frequency [Hz]
|
||||
lambda_m = c ./ f_abs; % wavelength [m]
|
||||
lambda_nm = lambda_m * 1e9; % wavelength [nm]
|
||||
|
||||
% assign axis
|
||||
x_vec = lambda_nm(:);
|
||||
x_label = "Wavelength [nm]";
|
||||
dc_axis = f_Hz;
|
||||
|
||||
% Sort to ensure axis is ascending
|
||||
[x_vec, sortIdx] = sort(x_vec, 'ascend');
|
||||
p_dbm = p_dbm(sortIdx, :);
|
||||
dc_axis = dc_axis(sortIdx);
|
||||
else
|
||||
% Frequency or normalized axes
|
||||
if ~useRadPerSampleAxis && ~useSamplingRateAxis
|
||||
x_vec = f_GHz;
|
||||
x_label = "Frequency in GHz";
|
||||
dc_axis = f_GHz;
|
||||
elseif useSamplingRateAxis
|
||||
x_vec = f_rad ./ (2*pi);
|
||||
x_label = "Normalized Frequency f/fs";
|
||||
dc_axis = x_vec;
|
||||
else
|
||||
x_vec = f_rad; % normalized frequency in rad/sample
|
||||
x_label = "Normalized Frequency [rad/sample]";
|
||||
dc_axis = x_vec;
|
||||
end
|
||||
end
|
||||
|
||||
if options.show_onesided
|
||||
keep_idx = dc_axis >= 0;
|
||||
x_vec = x_vec(keep_idx);
|
||||
dc_axis = dc_axis(keep_idx);
|
||||
p_dbm = p_dbm(keep_idx, :);
|
||||
end
|
||||
|
||||
figure(options.fignum);
|
||||
ax = gca;
|
||||
% assign axis
|
||||
x_vec = lambda_nm(:);
|
||||
x_label = "Wavelength [nm]";
|
||||
dc_axis = f_Hz;
|
||||
|
||||
% Sort to ensure axis is ascending
|
||||
[x_vec, sortIdx] = sort(x_vec, 'ascend');
|
||||
p_dbm = p_dbm(sortIdx, :);
|
||||
dc_axis = dc_axis(sortIdx);
|
||||
else
|
||||
% Frequency or normalized axes
|
||||
if ~useRadPerSampleAxis && ~useSamplingRateAxis
|
||||
x_vec = f_GHz;
|
||||
x_label = "Frequency in GHz";
|
||||
dc_axis = f_GHz;
|
||||
elseif useSamplingRateAxis
|
||||
x_vec = f_rad ./ (2*pi);
|
||||
x_label = "Normalized Frequency f/fs";
|
||||
dc_axis = x_vec;
|
||||
else
|
||||
x_vec = f_rad; % normalized frequency in rad/sample
|
||||
x_label = "Normalized Frequency [rad/sample]";
|
||||
dc_axis = x_vec;
|
||||
end
|
||||
end
|
||||
|
||||
if options.show_onesided
|
||||
keep_idx = dc_axis >= 0;
|
||||
x_vec = x_vec(keep_idx);
|
||||
dc_axis = dc_axis(keep_idx);
|
||||
p_dbm = p_dbm(keep_idx, :);
|
||||
end
|
||||
|
||||
figure(options.fignum);
|
||||
ax = gca;
|
||||
hold on
|
||||
|
||||
p_dbm = p_dbm+options.addDCoffset;
|
||||
|
||||
if options.normalizeToDC
|
||||
[~,min_idx]=min(abs(dc_axis));
|
||||
pow_at_dc = p_dbm(min_idx);
|
||||
p_dbm = p_dbm-pow_at_dc;
|
||||
end
|
||||
p_dbm = p_dbm+options.addDCoffset;
|
||||
|
||||
if options.normalizeToDC
|
||||
[~,min_idx]=min(abs(dc_axis));
|
||||
pow_at_dc = p_dbm(min_idx);
|
||||
p_dbm = p_dbm-pow_at_dc;
|
||||
end
|
||||
% p_dbm = movmean(p_dbm,10);
|
||||
|
||||
for s = 1:min(size(p_dbm))
|
||||
if isempty(options.color)
|
||||
plot(x_vec, p_dbm(:,s), 'DisplayName', options.displayname, 'LineWidth', 1);
|
||||
plot(x_vec, p_dbm(:,s), 'DisplayName', options.displayname, 'LineWidth', 1, 'LineStyle', options.linestyle, 'HandleVisibility', options.HandleVisibility);
|
||||
else
|
||||
plot(x_vec, p_dbm(:,s), 'DisplayName', options.displayname, 'LineWidth', 1, 'Color', options.color,'LineStyle',options.linestyle);
|
||||
plot(x_vec, p_dbm(:,s), 'DisplayName', options.displayname, 'LineWidth', 1, 'Color', options.color,'LineStyle',options.linestyle, 'HandleVisibility', options.HandleVisibility);
|
||||
end
|
||||
end
|
||||
|
||||
@@ -468,27 +491,27 @@ classdef Signal
|
||||
% Axis labels and limits
|
||||
xlabel(x_label);
|
||||
|
||||
if options.useWavelengthAxis && ~useRadPerSampleAxis && ~useSamplingRateAxis
|
||||
xlim([min(x_vec) max(x_vec)]);
|
||||
else
|
||||
if ~useRadPerSampleAxis && ~useSamplingRateAxis
|
||||
% Keep your existing freq handling (you can fine-tune as needed)
|
||||
% xlim([-128 128]); % example for 256 GSa/s if desired
|
||||
xlim([min(x_vec) max(x_vec)]);
|
||||
elseif useSamplingRateAxis
|
||||
if options.show_onesided
|
||||
xlim([0, 0.5]);
|
||||
else
|
||||
xlim([-0.5, 0.5]);
|
||||
end
|
||||
else
|
||||
if options.show_onesided
|
||||
xlim([0, pi]);
|
||||
else
|
||||
xlim([-pi, pi]);
|
||||
end
|
||||
end
|
||||
end
|
||||
if options.useWavelengthAxis && ~useRadPerSampleAxis && ~useSamplingRateAxis
|
||||
xlim([min(x_vec) max(x_vec)]);
|
||||
else
|
||||
if ~useRadPerSampleAxis && ~useSamplingRateAxis
|
||||
% Keep your existing freq handling (you can fine-tune as needed)
|
||||
% xlim([-128 128]); % example for 256 GSa/s if desired
|
||||
xlim([min(x_vec) max(x_vec)]);
|
||||
elseif useSamplingRateAxis
|
||||
if options.show_onesided
|
||||
xlim([0, 0.5]);
|
||||
else
|
||||
xlim([-0.5, 0.5]);
|
||||
end
|
||||
else
|
||||
if options.show_onesided
|
||||
xlim([0, pi]);
|
||||
else
|
||||
xlim([-pi, pi]);
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
ylabel(ylab);
|
||||
|
||||
@@ -1007,15 +1030,47 @@ classdef Signal
|
||||
elseif mode == 1
|
||||
% generate eye diagram using histogram
|
||||
|
||||
maxA = max(sig(100:end-100))*1.3;
|
||||
minA = min(sig(100:end-100))*1.3;
|
||||
finite_eye = eye_mat(isfinite(eye_mat));
|
||||
if isempty(finite_eye)
|
||||
finite_eye = sig(isfinite(sig));
|
||||
end
|
||||
amp_min = min(finite_eye);
|
||||
amp_max = max(finite_eye);
|
||||
amp_center = (amp_max + amp_min) / 2;
|
||||
amp_span = amp_max - amp_min;
|
||||
if amp_span == 0
|
||||
amp_span = max(abs(amp_center),1);
|
||||
end
|
||||
amp_margin = 0.08 * amp_span;
|
||||
maxA = amp_center + amp_span/2 + amp_margin;
|
||||
minA = amp_center - amp_span/2 - amp_margin;
|
||||
if ~isa(obj,'Opticalsignal') && minA < 0 && maxA > 0
|
||||
targetStep = max(abs([minA maxA])) / 2;
|
||||
if targetStep > 0
|
||||
stepMagnitude = 10^floor(log10(targetStep));
|
||||
normalizedStep = targetStep / stepMagnitude;
|
||||
if normalizedStep <= 1
|
||||
tickStep = stepMagnitude;
|
||||
elseif normalizedStep <= 2
|
||||
tickStep = 2 * stepMagnitude;
|
||||
elseif normalizedStep <= 5
|
||||
tickStep = 5 * stepMagnitude;
|
||||
else
|
||||
tickStep = 10 * stepMagnitude;
|
||||
end
|
||||
axisLimit = 2 * tickStep;
|
||||
maxA = axisLimit;
|
||||
minA = -axisLimit;
|
||||
end
|
||||
end
|
||||
|
||||
% maxA = 0.12;
|
||||
% minA = -0.08;
|
||||
|
||||
difference= maxA-minA;
|
||||
|
||||
data_ind_y=round((eye_mat-minA)/difference*(histpoints-1)) +1;
|
||||
data_ind_y=round((eye_mat-minA)/difference*(histpoints-1)) +1;
|
||||
data_ind_y = min(max(data_ind_y,1),histpoints);
|
||||
|
||||
for n=1:size(data_ind_y,1)
|
||||
nn=histcounts(data_ind_y(n,:),1:histpoints+1);
|
||||
@@ -1041,22 +1096,22 @@ classdef Signal
|
||||
if isa(obj,'Opticalsignal')
|
||||
title(['Optical Eye ',options.displayname])
|
||||
ylabel("Power in mW");
|
||||
y_tickstring = string(linspace(maxA.*1e3,minA.*1e3,6));
|
||||
min_ = min(abs(obj.signal(100:end-100)).^2);
|
||||
max_ = abs(max(obj.signal(100:end-100)).^2);
|
||||
elseif isa(obj,'Electricalsignal')
|
||||
title(['Electrical Eye ',options.displayname])
|
||||
ylabel("Voltage in V");
|
||||
y_tickstring = string(linspace(maxA,minA,6));
|
||||
min_ = min(obj.signal(100:end-100));
|
||||
max_ = abs(max(obj.signal(100:end-100)));
|
||||
else
|
||||
title(['Digital Eye ',options.displayname])
|
||||
ylabel("Digital Signal Amplitude");
|
||||
y_tickstring = string(linspace(maxA,minA,6));
|
||||
min_ = min(obj.signal(100:end-100));
|
||||
max_ = abs(max(obj.signal(100:end-100)));
|
||||
end
|
||||
yTickValues = linspace(maxA.*1e3,minA.*1e3,5);
|
||||
min_ = min(abs(obj.signal(100:end-100)).^2);
|
||||
max_ = abs(max(obj.signal(100:end-100)).^2);
|
||||
elseif isa(obj,'Electricalsignal')
|
||||
title(['Electrical Eye ',options.displayname])
|
||||
ylabel("Voltage in V");
|
||||
yTickValues = linspace(maxA,minA,5);
|
||||
min_ = min(obj.signal(100:end-100));
|
||||
max_ = abs(max(obj.signal(100:end-100)));
|
||||
else
|
||||
title(['Digital Eye ',options.displayname])
|
||||
ylabel("Digital Signal Amplitude");
|
||||
yTickValues = linspace(maxA,minA,5);
|
||||
min_ = min(obj.signal(100:end-100));
|
||||
max_ = abs(max(obj.signal(100:end-100)));
|
||||
end
|
||||
xlabel('Time in ps')
|
||||
|
||||
|
||||
@@ -1094,7 +1149,14 @@ classdef Signal
|
||||
hist_interest_smoth = smooth(hist_interest,20);
|
||||
a = scatter(hist_interest_smoth+posxall,1:length(hist_interest_smoth),4,'.','MarkerEdgeColor','red');
|
||||
|
||||
[pk,loc] = findpeaks(hist_interest_smoth,"MinPeakDistance",10,"NPeaks",M,"MinPeakHeight",30,"MinPeakProminence",10);
|
||||
minPeakDistance = max(10, floor(histpoints / (2*M)));
|
||||
minPeakProminence = max(3, 0.05 * max(hist_interest_smoth));
|
||||
[pk,loc] = findpeaks(hist_interest_smoth, ...
|
||||
"MinPeakDistance",minPeakDistance, ...
|
||||
"NPeaks",M, ...
|
||||
"MinPeakProminence",minPeakProminence, ...
|
||||
"SortStr","descend");
|
||||
loc = sort(loc);
|
||||
|
||||
scatter(posxall,loc,'red','Marker','x','LineWidth',2);
|
||||
|
||||
@@ -1177,13 +1239,14 @@ classdef Signal
|
||||
end
|
||||
|
||||
|
||||
yticks(linspace(0,histpoints,6));
|
||||
y_tickstring = sprintfc('%.2f', y_tickstring);
|
||||
yticklabels(y_tickstring);
|
||||
|
||||
xticks(linspace(0,histpoints_horizontal,6))
|
||||
x_tickstring = sprintfc('%.2f', linspace(0, 2/fsym, 8) .* 1e12);
|
||||
xticklabels(x_tickstring);
|
||||
yTickPositions = linspace(1,histpoints,numel(yTickValues));
|
||||
yticks(yTickPositions);
|
||||
yticklabels(sprintfc('%.2f', yTickValues));
|
||||
|
||||
xTickValues = linspace(0, 2/fsym, 6) .* 1e12;
|
||||
xticks(linspace(1,histpoints_horizontal,numel(xTickValues)))
|
||||
x_tickstring = sprintfc('%.2f', xTickValues);
|
||||
xticklabels(x_tickstring);
|
||||
|
||||
%
|
||||
end
|
||||
|
||||
@@ -254,7 +254,7 @@ classdef ChannelFreqResp < handle
|
||||
xlim([0.2 .5*max(obj.faxis)*1e-9]); grid on;
|
||||
|
||||
%%% plot for publication
|
||||
figure(101);
|
||||
figure(1996);
|
||||
hold all;
|
||||
box on;
|
||||
title('Magnitude Freq. Response');
|
||||
|
||||
394
Classes/04_DSP/Equalizer/Copy_of_VNLE.m
Normal file
394
Classes/04_DSP/Equalizer/Copy_of_VNLE.m
Normal file
@@ -0,0 +1,394 @@
|
||||
classdef Copy_of_VNLE < handle
|
||||
% Implementation of plain and simple FFE.
|
||||
% 1) Training mode (stable performance when you use NLMS)
|
||||
% 2) Decision directed mode
|
||||
|
||||
% Eq = VNLE("epochs_tr",5,"epochs_dd",5,"len_tr",4096*2,"mu_dd",[0.0004 0.0005 0.0006],"mu_tr",0,"order",[25,2,2],"sps",2,"decide",1);
|
||||
% Somehow it is not possible to use only 1 nonlinear order
|
||||
|
||||
properties
|
||||
sps % usually 2
|
||||
order
|
||||
e
|
||||
e_dc
|
||||
error
|
||||
|
||||
len_tr
|
||||
mu_tr
|
||||
epochs_tr
|
||||
|
||||
mu_dd
|
||||
epochs_dd
|
||||
mu_dc
|
||||
|
||||
constellation
|
||||
|
||||
decide
|
||||
save_debug = 0;
|
||||
debug_struct
|
||||
|
||||
optmize_mus = 0;
|
||||
mu_optimization
|
||||
mu_optimization_iter = 0;
|
||||
|
||||
x_norm
|
||||
ce
|
||||
ie2
|
||||
ie3
|
||||
end
|
||||
|
||||
methods
|
||||
function obj = Copy_of_VNLE(options)
|
||||
arguments(Input)
|
||||
|
||||
options.sps = 2;
|
||||
options.order = [15,2,2];
|
||||
|
||||
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;
|
||||
|
||||
options.decide = false;
|
||||
options.save_debug = 0;
|
||||
options.optmize_mus = 0;
|
||||
|
||||
end
|
||||
|
||||
fn = fieldnames(options);
|
||||
for n = 1:numel(fn)
|
||||
obj.(fn{n}) = options.(fn{n});
|
||||
end
|
||||
|
||||
|
||||
obj.error = 0;
|
||||
obj.e_dc = 0;
|
||||
|
||||
end
|
||||
|
||||
function [X,N] = 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);
|
||||
obj.x_norm = obj.calcPowerNormalization(X.signal);
|
||||
obj.ce = obj.calcVNLEMemoryLength(obj.order);
|
||||
[obj.ie2,obj.ie3] = obj.calcIndiceVectors(obj.order);
|
||||
|
||||
obj.e = zeros( sum(obj.ce) ,1);
|
||||
obj.e_dc = 0;
|
||||
|
||||
if obj.optmize_mus
|
||||
obj.optimizeMus(X.signal,D.signal);
|
||||
obj.e = zeros(sum(obj.ce),1);
|
||||
obj.e_dc = 0;
|
||||
end
|
||||
|
||||
% Training Mode
|
||||
training = 1;
|
||||
showviz = 0;
|
||||
obj.equalize(X.signal, D.signal,obj.mu_tr,obj.epochs_tr,obj.len_tr,training,showviz);
|
||||
|
||||
% Decision Directed Mode
|
||||
N = X.length;
|
||||
training = 0;
|
||||
showviz = 0;
|
||||
[signal,decision]=obj.equalize(X.signal, D.signal,obj.mu_dd,obj.epochs_dd,N,training,showviz);
|
||||
|
||||
% 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
|
||||
|
||||
N = X;
|
||||
N = X - D;
|
||||
|
||||
|
||||
end
|
||||
|
||||
function [y,d_hat] = equalize(obj,x,d,mu,epochs,N,training,showviz)
|
||||
|
||||
arguments
|
||||
obj
|
||||
x
|
||||
d
|
||||
mu
|
||||
epochs
|
||||
N
|
||||
training
|
||||
showviz
|
||||
end
|
||||
|
||||
if all(mu == mu(1))
|
||||
% mu = mu(1);
|
||||
mu = diag(ones(1,sum(obj.ce))*mu(1));
|
||||
else
|
||||
mu = diag([ones(1,obj.ce(1))*mu(1) ...
|
||||
ones(1,obj.ce(2))*mu(2) ...
|
||||
ones(1,obj.ce(3))*mu(3) ]);
|
||||
end
|
||||
|
||||
x = [zeros(floor(obj.order(1)/2),1); x; zeros(obj.order(1),1)];
|
||||
|
||||
if showviz
|
||||
f = figure(111);
|
||||
subplot(2,2,1:2);
|
||||
hold on
|
||||
a = scatter(1:numel(x),x,1,'.');
|
||||
a2 = scatter(1,1,1,'.');
|
||||
a3 = scatter(1,1,2,'.');
|
||||
a4 = xline(1);
|
||||
ylim([-3 3])
|
||||
xlim([0 length(x)]);
|
||||
subplot(2,2,3:4)
|
||||
c = stem(obj.e);
|
||||
ylim([-1 1])
|
||||
drawnow
|
||||
end
|
||||
|
||||
for epoch = 1 : epochs
|
||||
symbol = 0;
|
||||
for sample = 1 : obj.sps : N
|
||||
|
||||
symbol = symbol+1;
|
||||
|
||||
|
||||
% x_in = x(obj.order(1)+sample+(obj.sps-1):-1:sample+obj.sps);
|
||||
x_in = x(obj.order(1)+sample-1:-1:sample);
|
||||
x_in = obj.calcVNLENonlinVecs(x_in,obj.ie2,obj.ie3,obj.order,obj.x_norm);
|
||||
|
||||
y(symbol,1) = obj.e_dc + obj.e.' * x_in; % Calculating output of LMS __ * |
|
||||
|
||||
if training
|
||||
err = y(symbol) - d(symbol); % Instantaneous error
|
||||
else
|
||||
[~,symbol_idx] = min(abs(y(symbol) - obj.constellation)); % decision for closest constellation point
|
||||
d_hat(symbol,1) = obj.constellation(symbol_idx);
|
||||
err = y(symbol) - d_hat(symbol); % Instantaneous error
|
||||
end
|
||||
|
||||
if ~all(mu==0,'all') %mu has not only zeros
|
||||
obj.e = obj.e - ( (mu * x_in) * err ) ; % Weight update rule of LMS
|
||||
else
|
||||
normalizationfactor = (x_in.' * x_in);
|
||||
obj.e = obj.e - err * x_in / normalizationfactor; % Weight update rule of NLMS
|
||||
end
|
||||
if obj.mu_dc ~= 0
|
||||
obj.e_dc = obj.e_dc - obj.mu_dc * err;
|
||||
end
|
||||
|
||||
if mod(sample,100) == 1 && showviz
|
||||
a2.XData = 1:2*numel(y);
|
||||
a2.YData = repelem(y, 2);
|
||||
a3.XData = 1:2*numel(d_hat);
|
||||
a3.YData = repelem(d_hat, 2);
|
||||
a4.Value = sample;
|
||||
% b.YData = x(symbol:symbol+500);
|
||||
c.YData = obj.e;
|
||||
drawnow;
|
||||
end
|
||||
|
||||
obj.error(epoch,symbol) = err * err'; % Instantaneous square error
|
||||
if obj.save_debug
|
||||
obj.debug_struct.error(epoch,symbol) = err * err';
|
||||
if training
|
||||
obj.debug_struct.error_tr(epoch,symbol) = err * err';
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
function optimizeMus(obj,x,d)
|
||||
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")];
|
||||
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, ...
|
||||
"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;
|
||||
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);
|
||||
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);
|
||||
end
|
||||
end
|
||||
|
||||
function objective = muObjective(obj,params,x,d)
|
||||
old_debug = obj.save_debug;
|
||||
old_mu_dc = obj.mu_dc;
|
||||
obj.save_debug = 1;
|
||||
optimize_mu_dc = ismember("mu_dc",string(params.Properties.VariableNames));
|
||||
if optimize_mu_dc
|
||||
obj.mu_dc = params.mu_dc;
|
||||
end
|
||||
|
||||
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);
|
||||
|
||||
objective = mean(obj.debug_struct.error(end,:),"omitnan");
|
||||
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);
|
||||
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);
|
||||
end
|
||||
obj.save_debug = old_debug;
|
||||
obj.mu_dc = old_mu_dc;
|
||||
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)
|
||||
l1=length(x_in_block);
|
||||
l2=length(I_2);
|
||||
l3=length(I_3);
|
||||
final_length = l1+l2+l3;
|
||||
|
||||
x_in_vnle_format = zeros(final_length,1);
|
||||
|
||||
idx = l1;
|
||||
x_in_vnle_format(1:idx) = x_in_block;
|
||||
|
||||
if N_(2) > 0
|
||||
delta_2 = round((N_(1)-N_(2)) / 2);
|
||||
input_vec_se = x_in_block(delta_2:end) / norm_(2); %TODO normalization step
|
||||
|
||||
% Extract columns from I_2
|
||||
col1 = input_vec_se(I_2(:,1));
|
||||
col2 = input_vec_se(I_2(:,2));
|
||||
|
||||
x2 = col1 .* col2;
|
||||
x_in_vnle_format(idx+1:idx+l2) = x2;
|
||||
end
|
||||
|
||||
if N_(3) > 0
|
||||
delta_3 = round((N_(1)-N_(3))/2);
|
||||
input_vec_th = x_in_block(delta_3:end) / norm_(3);
|
||||
|
||||
% Extract columns from I_3
|
||||
col1 = input_vec_th(I_3(:,1));
|
||||
col2 = input_vec_th(I_3(:,2));
|
||||
col3 = input_vec_th(I_3(:,3));
|
||||
|
||||
% Perform matrix multiplication
|
||||
x3 = col1 .* col2 .* col3;
|
||||
|
||||
idx = idx+l2;
|
||||
x_in_vnle_format(idx+1:idx+l3) = x3;
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
%% Functions needed for Preparation
|
||||
function [C] = calcVNLEMemoryLength(~,N)
|
||||
|
||||
%calculates the memory length of VNLE
|
||||
C = zeros(size(N));
|
||||
|
||||
for o = 1:numel(N)
|
||||
switch o
|
||||
case 1
|
||||
C(o) = N(o);
|
||||
case 2
|
||||
C(o) = N(o)*(N(o)+1) / 2;
|
||||
case 3
|
||||
C(o) = N(o)*(N(o)+1)*(N(o)+2) / 6;
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
function [indvec2nd, indvec3rd] = calcIndiceVectors(~,N)
|
||||
|
||||
% Init vectors of 2nd and 3rd order coefficient indices ->
|
||||
% yield combination with
|
||||
indvec2nd=[];
|
||||
indvec3rd=[];
|
||||
for o = 2:numel(N)
|
||||
n = N(o);
|
||||
v = 1:n; % Ursprünglicher Vektor
|
||||
row = 1;
|
||||
|
||||
% Schleifen zur Generierung des Indize Vektors
|
||||
switch o
|
||||
|
||||
case 2
|
||||
|
||||
indvec2nd = zeros(n*(n+1)/2, o);
|
||||
for i = 1:n
|
||||
for j = i:n
|
||||
indvec2nd(row, :) = [v(i) v(j)];
|
||||
row = row + 1;
|
||||
end
|
||||
end
|
||||
|
||||
case 3
|
||||
|
||||
indvec3rd = zeros(n*(n+1)*(n+2)/6, 3);
|
||||
for i = 1:n
|
||||
for j = i:n
|
||||
for k = j:n
|
||||
indvec3rd(row, :) = [v(i) v(j) v(k)];
|
||||
row = row + 1;
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
function powerNorm = calcPowerNormalization(~,v)
|
||||
|
||||
powerNorm(1) = sqrt(mean(abs(v ).^2));
|
||||
powerNorm(2) = sqrt(mean(abs(v.^2).^2));
|
||||
powerNorm(3) = sqrt(mean(abs(v.^3).^2));
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
@@ -9,6 +9,12 @@ classdef FFE < handle
|
||||
|
||||
% 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);
|
||||
|
||||
% eq_ = FFE("epochs_tr",5,"epochs_dd",5,"len_tr",len_tr, ...
|
||||
% "mu_dd",1e-1,"mu_tr",0.4,"order",50, ...
|
||||
% "sps",2,"decide",0,"optmize_mus",1,"dd_mode",1, ...
|
||||
% "adaption_technique","nlms","mu_dc",1.021e-05);
|
||||
|
||||
|
||||
properties
|
||||
sps % usually 2
|
||||
order
|
||||
|
||||
@@ -1,245 +0,0 @@
|
||||
classdef FFE_Kalman < 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
|
||||
|
||||
constellation
|
||||
|
||||
decide
|
||||
end
|
||||
|
||||
methods
|
||||
function obj = FFE_Kalman(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.decide = false;
|
||||
|
||||
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,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;
|
||||
showviz = 0;
|
||||
obj.equalize(X.signal, D.signal,obj.mu_tr,obj.epochs_tr,obj.len_tr,training,showviz);
|
||||
|
||||
% Decision Directed Mode
|
||||
n = X.length;
|
||||
training = 0;
|
||||
showviz = 0;
|
||||
[signal,decision]=obj.equalize(X.signal, D.signal,obj.mu_dd,obj.epochs_dd,n,training,showviz);
|
||||
|
||||
% 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;
|
||||
Noi = X - D;
|
||||
|
||||
|
||||
end
|
||||
|
||||
function [y,d_hat] = equalize(obj,x,d,mio,epochs,N,training,showviz)
|
||||
|
||||
arguments
|
||||
obj
|
||||
x
|
||||
d
|
||||
mio
|
||||
epochs
|
||||
N
|
||||
training
|
||||
showviz
|
||||
end
|
||||
|
||||
x = [zeros(floor(obj.order/2),1); x; zeros(obj.order,1)];
|
||||
|
||||
for epoch = 1 : epochs
|
||||
|
||||
|
||||
|
||||
symbol = 0;
|
||||
% Initialization of Kalman filter variables
|
||||
A = 1; % State transition matrix
|
||||
H = 1; % Observation matrix
|
||||
Q = 1e-4; % Process noise covariance
|
||||
R = 1e-1; % Measurement noise covariance
|
||||
P = 1; % Initial error covariance
|
||||
mpi_est = 0; % Initial estimate for MPI noise
|
||||
K = 0; % Kalman gain
|
||||
subtract_mpi_est = 1;
|
||||
|
||||
for sample = 1 : obj.sps : N
|
||||
symbol = symbol + 1;
|
||||
|
||||
% Get the current input sample and the equalizer output
|
||||
U = x(obj.order + sample - 1 : -1 : sample);
|
||||
|
||||
if subtract_mpi_est || ~training
|
||||
y(symbol,1) = (obj.e.' * U) - mpi_est .* 1 ; % Subtract MPI estimate
|
||||
else
|
||||
y(symbol,1) = obj.e.' * U;
|
||||
end
|
||||
|
||||
% Decision and error calculation
|
||||
if training
|
||||
d_hat(symbol,1) = d(symbol);
|
||||
else
|
||||
[~, symbol_idx] = min(abs(y(symbol) - obj.constellation)); % Closest constellation point
|
||||
d_hat(symbol,1) = obj.constellation(symbol_idx);
|
||||
end
|
||||
|
||||
err(symbol) = y(symbol) - d_hat(symbol); % Instantaneous residual error
|
||||
|
||||
true_err(symbol) = y(symbol) - d(symbol);
|
||||
|
||||
% Kalman filter update to track the MPI noise
|
||||
% Prediction step
|
||||
P = A * P * A' + Q; % Update error covariance
|
||||
K = P * H' / (H * P * H' + R); % Kalman gain
|
||||
|
||||
% Update step
|
||||
mpi_est_new = mpi_est + K * (err(symbol) - H * mpi_est); % MPI noise estimation
|
||||
alpha=0;
|
||||
mpi_est = alpha * mpi_est + (1 - alpha) * mpi_est_new;
|
||||
|
||||
P = (1 - K * H) * P; % Update error covariance
|
||||
|
||||
% Subtract MPI noise from the signal
|
||||
if subtract_mpi_est || ~training
|
||||
y(symbol) = y(symbol);% - mpi_est;
|
||||
else
|
||||
|
||||
end
|
||||
|
||||
% Equalizer weight update (LMS or NLMS)
|
||||
if mio ~= 0
|
||||
obj.e = obj.e - (mio * err(symbol) * U); % LMS weight update
|
||||
else
|
||||
normalizationfactor = (U.' * U);
|
||||
obj.e = obj.e - err(symbol) * U / normalizationfactor; % NLMS weight update
|
||||
end
|
||||
|
||||
% Store MPI estimate for visualization
|
||||
mpi_estimates(symbol) = mpi_est;
|
||||
|
||||
Kgain(symbol) = K;
|
||||
P_(symbol) = P;
|
||||
H_(symbol) = H;
|
||||
|
||||
|
||||
end %symbols
|
||||
end %epoch
|
||||
|
||||
if ~training
|
||||
|
||||
if 1
|
||||
% figure;
|
||||
% subplot(2,2,1)
|
||||
% hold on
|
||||
% scatter(1:numel(y),y,1,'.');
|
||||
% plot(1:numel(mpi_estimates), mpi_estimates);
|
||||
% subplot(2,2,3)
|
||||
% plot(1:numel(true_err), true_err);
|
||||
% subplot(2,2,2)
|
||||
% scatter(1:numel(y),y'-mpi_estimates,1,'.');
|
||||
% xlabel('Sample Index');
|
||||
% ylabel('MPI Noise Estimate');
|
||||
% title('MPI Noise Estimation Over Time (Kalman Filter)');
|
||||
% grid on;
|
||||
|
||||
figure(111);
|
||||
|
||||
subplot(3,1,1)
|
||||
hold on
|
||||
cla
|
||||
scatter(1:numel(y),y,1,'.');
|
||||
plot(1:numel(mpi_estimates), mpi_estimates,'DisplayName','mpi est');
|
||||
ylim([-2,2]);
|
||||
|
||||
subplot(3,1,2)
|
||||
cla
|
||||
plot(1:numel(true_err), true_err,'DisplayName','true err');
|
||||
ylim([-1,1]);
|
||||
|
||||
subplot(3,1,3)
|
||||
plot(1:numel(true_err), true_err-mpi_estimates,'DisplayName',['diff rms: ',num2str(rms(true_err-mpi_estimates))]);
|
||||
ylim([-1,1]);
|
||||
legend
|
||||
|
||||
figure(333)
|
||||
hold on
|
||||
von = 5000;
|
||||
bis = 15000;
|
||||
plot(von:bis,true_err(von:bis),'DisplayName','Error')
|
||||
plot(von:bis,movmean(true_err(von:bis), [30,30]),'DisplayName','Error')
|
||||
plot(von:bis,mpi_estimates(von:bis),'DisplayName','Est','LineWidth',2);
|
||||
legend
|
||||
|
||||
figure(222)
|
||||
hold on
|
||||
crr = xcorr(mpi_estimates,true_err,'normalized');
|
||||
plot(crr);
|
||||
|
||||
end
|
||||
|
||||
% y = y-mpi_estimates';
|
||||
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,197 +0,0 @@
|
||||
classdef FFE_Kalman_Feedback < 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
|
||||
|
||||
constellation
|
||||
|
||||
decide
|
||||
end
|
||||
|
||||
methods
|
||||
function obj = FFE_Kalman_Feedback(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.decide = false;
|
||||
|
||||
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,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;
|
||||
showviz = 0;
|
||||
obj.equalize(X.signal, D.signal,obj.mu_tr,obj.epochs_tr,obj.len_tr,training);
|
||||
|
||||
% Decision Directed Mode
|
||||
n = X.length;
|
||||
training = 0;
|
||||
showviz = 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;
|
||||
Noi = X - D;
|
||||
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
function [y, d_hat] = equalize(obj, x, d, mio, epochs, N, training)
|
||||
|
||||
arguments
|
||||
obj
|
||||
x
|
||||
d
|
||||
mio
|
||||
epochs
|
||||
N
|
||||
training
|
||||
end
|
||||
|
||||
% Initialize Kalman filter variables
|
||||
A = 0.7; % State transition matrix
|
||||
H = 1; % Observation matrix
|
||||
Q = 1e-2; % Process noise covariance
|
||||
R = 1e-3; % Measurement noise covariance
|
||||
P = 1; % Initial error covariance
|
||||
mpi_est = 0; % Initial estimate for MPI noise
|
||||
K = 0; % Kalman gain
|
||||
|
||||
% Error buffers for parallelized structure
|
||||
parallel_depth = 100; % Depending on your parallelization depth
|
||||
err_buffer = zeros(parallel_depth, 1); % Store collected errors
|
||||
mpi_est_buffer = zeros(parallel_depth, 1); % Store MPI estimates
|
||||
|
||||
x = [zeros(floor(obj.order/2), 1); x; zeros(obj.order, 1)];
|
||||
|
||||
for epoch = 1 : epochs
|
||||
symbol = 0;
|
||||
|
||||
for sample = 1 : obj.sps : N
|
||||
symbol = symbol + 1;
|
||||
|
||||
U = x(obj.order + sample - 1 : -1 : sample); % Input window for FFE
|
||||
|
||||
% Subtract the estimated MPI noise from the input signal before FFE
|
||||
x_mpi_reduced = U;
|
||||
y(symbol, 1) = obj.e.' * x_mpi_reduced; % Output of FFE
|
||||
|
||||
y(symbol, 1) = y(symbol, 1) - mpi_est;
|
||||
|
||||
% Decision and error calculation
|
||||
if training
|
||||
[~, symbol_idx] = min(abs(d(symbol) - obj.constellation)); % Closest constellation point
|
||||
d_hat(symbol, 1) = d(symbol);
|
||||
else
|
||||
[~, symbol_idx] = min(abs(y(symbol) - obj.constellation)); % Closest constellation point
|
||||
d_hat(symbol, 1) = obj.constellation(symbol_idx);
|
||||
end
|
||||
|
||||
% Calculate the residual error
|
||||
err(symbol) = y(symbol) - d_hat(symbol); % Instantaneous residual error
|
||||
|
||||
% Collect error for feedback after 'parallel_depth' samples
|
||||
err_buffer(mod(symbol, parallel_depth) + 1) = err(symbol);
|
||||
|
||||
% Update Kalman filter based on the accumulated errors every parallel_depth samples
|
||||
if mod(symbol, parallel_depth) == 0
|
||||
% Compute the mean error over the last 'parallel_depth' samples
|
||||
avg_error = mean(err_buffer);
|
||||
|
||||
% Prediction step (Kalman filter)
|
||||
P = A * P * A' + Q; % Update the error covariance
|
||||
K = P * H' / (H * P * H' + R); % Compute Kalman gain
|
||||
|
||||
% Update MPI noise estimate
|
||||
mpi_est = mpi_est + K * (avg_error - H * mpi_est); % Update based on averaged error
|
||||
P = (1 - K * H) * P; % Update error covariance
|
||||
|
||||
end
|
||||
|
||||
% Store MPI estimate for visualization or debugging
|
||||
k_buffer(symbol) = K;
|
||||
mpi_est_buffer(symbol) = mpi_est;
|
||||
|
||||
% FFE weight update using NLMS
|
||||
if mio ~= 0
|
||||
obj.e = obj.e - (mio * err(symbol) * U); % LMS weight update
|
||||
else
|
||||
normalizationfactor = (U.' * U);
|
||||
obj.e = obj.e - err(symbol) * U / normalizationfactor; % NLMS weight update
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
if ~training
|
||||
|
||||
figure(111);
|
||||
|
||||
subplot(3,1,1)
|
||||
hold on
|
||||
cla
|
||||
scatter(1:numel(y),y,1,'.');
|
||||
plot(1:numel(mpi_est_buffer), mpi_est_buffer,'DisplayName','mpi est');
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
@@ -1,499 +1,3 @@
|
||||
% classdef ML_MLSE < handle
|
||||
% % 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
|
||||
% 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
|
||||
%
|
||||
% adaptive_mu
|
||||
%
|
||||
% constellation
|
||||
%
|
||||
% L %viterbi memory length
|
||||
%
|
||||
% alpha
|
||||
% DIR
|
||||
% DIR_flip
|
||||
% trellis_states
|
||||
%
|
||||
% traceback_depth
|
||||
%
|
||||
% % --- Added internal class variables used later ---
|
||||
% S
|
||||
% Nf
|
||||
% delta
|
||||
% nStates
|
||||
% nFeasible
|
||||
% combs
|
||||
% first_sym
|
||||
% last_sym
|
||||
% valid
|
||||
% valid_to_idx
|
||||
% valid_from_idx
|
||||
% 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
|
||||
% function obj = ML_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.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
|
||||
%
|
||||
% function [X,X_viterbi] = process(obj, X, D)
|
||||
%
|
||||
% % actual processing of the signal (steps 1. - 3.)
|
||||
% % 1 normalize RMS
|
||||
% X = X.normalize("mode","rms");
|
||||
%
|
||||
% % 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(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(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{:}).'); % 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);
|
||||
% 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);
|
||||
%
|
||||
% % --- Allocate vectors and weights
|
||||
% % !! IF SHAPE FIT, then we already have smth there an we want
|
||||
% % to start with the existing fitler-set
|
||||
% 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
|
||||
%
|
||||
% % --- 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
|
||||
% % ==============================================================
|
||||
%
|
||||
% % Training Mode
|
||||
% n = obj.len_tr;
|
||||
% training = 1;
|
||||
% obj.equalize(X.signal, D.signal,obj.mu_tr,obj.epochs_tr,n,training);
|
||||
% obj.e_tr = obj.e;
|
||||
%
|
||||
% % ==============================================================
|
||||
% % DD-Mode / Fixed Mode
|
||||
% % ==============================================================
|
||||
%
|
||||
% % 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_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);
|
||||
%
|
||||
% % number of symbol steps in this block
|
||||
% nSymbols = ceil(N/obj.sps);
|
||||
%
|
||||
% for epoch = 1:epochs
|
||||
%
|
||||
% % 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(nSymbols, obj.nStates, 'uint32');
|
||||
% pm_sto = nan(obj.nStates, nSymbols,'like',pm);
|
||||
% CE_accum = 0;
|
||||
%
|
||||
%
|
||||
% %%% 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
|
||||
% start_sample = 1;%obj.len_tr;
|
||||
% 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 = 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;
|
||||
% 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)]; % Nf×1
|
||||
% yk = [yk;1];
|
||||
%
|
||||
% % --- 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: 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 1 %training
|
||||
% % --- allocate storage once
|
||||
% if epoch == 1 && symbol == 1
|
||||
% obj.true_to_state_idx = ones(ceil(N/obj.sps),1,'uint32');
|
||||
% end
|
||||
%
|
||||
% % --- 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);
|
||||
% 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)
|
||||
% 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
|
||||
%
|
||||
% % 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
|
||||
% 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 - mu_eff .* 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;
|
||||
% [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
|
||||
%
|
||||
% % --- 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
|
||||
% viterbi_path(n-1) = pred(n, viterbi_path(n));
|
||||
% end
|
||||
%
|
||||
% y_ref = d(start_symbol:end);
|
||||
% y = obj.first_sym(viterbi_path);
|
||||
%
|
||||
% 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)));
|
||||
%
|
||||
% 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
|
||||
%
|
||||
% obj.ce(epoch) = CE_accum./symbol;
|
||||
%
|
||||
% 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
|
||||
% 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
|
||||
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
classdef ML_MLSE < handle
|
||||
% ---------------------------------------------------------------------
|
||||
% W. Lanneer and Y. Lefevre,
|
||||
@@ -661,8 +165,8 @@ classdef ML_MLSE < handle
|
||||
% EQUALIZE
|
||||
% ==============================================================
|
||||
function [y,y_ref] = equalize(obj,x,d,mu,epochs,N,training)
|
||||
debug = 1;
|
||||
showPlots = 1;
|
||||
debug = 0;
|
||||
showPlots = 0;
|
||||
y = zeros(N,1);
|
||||
nSymbols = ceil(N/obj.sps);
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ classdef VNLE < handle
|
||||
sps % usually 2
|
||||
order
|
||||
e
|
||||
e_dc
|
||||
error
|
||||
|
||||
len_tr
|
||||
@@ -18,10 +19,17 @@ classdef VNLE < handle
|
||||
|
||||
mu_dd
|
||||
epochs_dd
|
||||
mu_dc
|
||||
|
||||
constellation
|
||||
|
||||
decide
|
||||
save_debug = 0;
|
||||
debug_struct
|
||||
|
||||
optmize_mus = 0;
|
||||
mu_optimization
|
||||
mu_optimization_iter = 0;
|
||||
|
||||
x_norm
|
||||
ce
|
||||
@@ -42,8 +50,11 @@ classdef VNLE < handle
|
||||
|
||||
options.mu_dd = 1e-5;
|
||||
options.epochs_dd = 5;
|
||||
options.mu_dc = 0;
|
||||
|
||||
options.decide = false;
|
||||
options.save_debug = 0;
|
||||
options.optmize_mus = 0;
|
||||
|
||||
end
|
||||
|
||||
@@ -54,6 +65,7 @@ classdef VNLE < handle
|
||||
|
||||
|
||||
obj.error = 0;
|
||||
obj.e_dc = 0;
|
||||
|
||||
end
|
||||
|
||||
@@ -69,6 +81,13 @@ classdef VNLE < handle
|
||||
[obj.ie2,obj.ie3] = obj.calcIndiceVectors(obj.order);
|
||||
|
||||
obj.e = zeros( sum(obj.ce) ,1);
|
||||
obj.e_dc = 0;
|
||||
|
||||
if obj.optmize_mus
|
||||
obj.optimizeMus(X.signal,D.signal);
|
||||
obj.e = zeros(sum(obj.ce),1);
|
||||
obj.e_dc = 0;
|
||||
end
|
||||
|
||||
% Training Mode
|
||||
training = 1;
|
||||
@@ -148,7 +167,7 @@ classdef VNLE < handle
|
||||
x_in = x(obj.order(1)+sample-1:-1:sample);
|
||||
x_in = obj.calcVNLENonlinVecs(x_in,obj.ie2,obj.ie3,obj.order,obj.x_norm);
|
||||
|
||||
y(symbol,1) = obj.e.' * x_in; % Calculating output of LMS __ * |
|
||||
y(symbol,1) = obj.e_dc + obj.e.' * x_in; % Calculating output of LMS __ * |
|
||||
|
||||
if training
|
||||
err = y(symbol) - d(symbol); % Instantaneous error
|
||||
@@ -164,6 +183,9 @@ classdef VNLE < handle
|
||||
normalizationfactor = (x_in.' * x_in);
|
||||
obj.e = obj.e - err * x_in / normalizationfactor; % Weight update rule of NLMS
|
||||
end
|
||||
if obj.mu_dc ~= 0
|
||||
obj.e_dc = obj.e_dc - obj.mu_dc * err;
|
||||
end
|
||||
|
||||
if mod(sample,100) == 1 && showviz
|
||||
a2.XData = 1:2*numel(y);
|
||||
@@ -177,12 +199,85 @@ classdef VNLE < handle
|
||||
end
|
||||
|
||||
obj.error(epoch,symbol) = err * err'; % Instantaneous square error
|
||||
if obj.save_debug
|
||||
obj.debug_struct.error(epoch,symbol) = err * err';
|
||||
if training
|
||||
obj.debug_struct.error_tr(epoch,symbol) = err * err';
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
function optimizeMus(obj,x,d)
|
||||
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")];
|
||||
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, ...
|
||||
"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;
|
||||
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);
|
||||
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);
|
||||
end
|
||||
end
|
||||
|
||||
function objective = muObjective(obj,params,x,d)
|
||||
old_debug = obj.save_debug;
|
||||
old_mu_dc = obj.mu_dc;
|
||||
obj.save_debug = 1;
|
||||
optimize_mu_dc = ismember("mu_dc",string(params.Properties.VariableNames));
|
||||
if optimize_mu_dc
|
||||
obj.mu_dc = params.mu_dc;
|
||||
end
|
||||
|
||||
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);
|
||||
|
||||
objective = mean(obj.debug_struct.error(end,:),"omitnan");
|
||||
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);
|
||||
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);
|
||||
end
|
||||
obj.save_debug = old_debug;
|
||||
obj.mu_dc = old_mu_dc;
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user