Faster DP_fiber with GPU processing... Run test\gpu_cpu_comparison.m to see difference on your setup

This commit is contained in:
silas (home)
2026-02-01 13:53:20 +01:00
parent 67689bb70f
commit fba7cbdba2
15 changed files with 1281 additions and 217 deletions

219
test/bayesopt_ffe_tuning.m Normal file
View File

@@ -0,0 +1,219 @@
%% Bayesian Optimization for FFE Parameter Tuning
% This script uses bayesopt to find optimal mu_dd and mu_tr values
% that minimize BER for the FFE equalizer.
clear; clc;
%% Setup - Same as gpu_processing_dpfiber.m
s.wavelengthplan = calcWavelengthPlan(4, 400e9, 1310);
link_length = 10;
s.pmd = 0.1;
s.gamma = 0.0023;
s.M = 4;
fsym = 112e9;
fdac = 2*fsym;
fadc = 120000000000;
s.random_key = 1;
% Laser / Modulator
vbias_rel = 0.5;
u_pi = 4.6;
vbias = -vbias_rel*u_pi;
laser_linewidth = 0e6;
duob_mode = db_mode.no_db;
rcalpha = 0.05;
Pform = Pulseformer("fsym",fsym,"fdac",4*fsym,"pulse","rc","pulselength",16,"alpha",rcalpha);
s.chirpalpha = 0;
s.p_launch = 3;
s.p = "co";
N = numel(s.wavelengthplan);
switch s.p
case "co"
pol_rot = 100.*ones(1,N);
d_local = 0;
end
f_plan = physconst('lightspeed')./(s.wavelengthplan.*1e-9);
margin = 25e12;
f_span = (max(f_plan)+margin)-(min(f_plan)-margin);
f_nyq = f_span/2;
kover = 4;
upsample_required = f_nyq./(fdac*kover/2);
upsample_pow = 2^nextpow2(upsample_required);
s.f_opt = fdac*kover*upsample_pow;
s.f_opt_nyq = s.f_opt/2;
s.rop = -8; % Fixed ROP for optimization
%% Generate TX signals (run once)
fprintf('Generating TX signals...\n');
for l = 1:N
[Digi_sig,Symbols{l},Tx_bits{l}] = PAMsource( ...
"fsym",fsym,"M",s.M,"order",15,"useprbs",0, ...
"fs_out",fdac, ...
"applyclipping",0,"clipfactor",1.5, ...
"applypulseform",1,"pulseformer",Pform, ...
"randkey",s.random_key+l, ...
"mrds_code",0,"mrds_blocklength",512,"duobinary_mode",duob_mode ...
).process();
Lp_awg = Filter('filtdegree',3,"f_cutoff",56e9,"fs",fdac*kover, ...
"filterType",filtertypes.gaussian,"active",true);
El_sig = AWG("fdac",fdac,"f_cutoff",fsym,"lpf_active",1,"kover",kover, ...
"bit_resolution",6,"upsampling_method","samplehold","precomp_sinc_rolloff",0, ...
"H_lpf",Lp_awg,"dac_max",0.6,"dac_min",-0.6).process(Digi_sig);
El_sig = El_sig.normalize("mode","oneone");
scaling = 0.6*(u_pi/2-abs(vbias-u_pi/2));
El_sig = El_sig .* scaling;
Eml_out = EML("mode",eml_mode.im_cosinus,"power",3,"fsimu",El_sig.fs, ...
"lambda",s.wavelengthplan(l),"bias",vbias,"u_pi",u_pi, ...
"linewidth",laser_linewidth,"randomkey",s.random_key+l,"alpha",s.chirpalpha).process(El_sig);
signal_cell{l} = Polarization_Controller("mode","rot_power","desired_power",pol_rot(l)).process(Eml_out);
end
%% WDM mux + launch
Opt_sig_wdm = Optical_Multiplex("fs_in",fdac*kover,"fs_out",upsample_pow*fdac*kover, ...
"lambda_center",1310,"random_key",0,"filtype",1,"B",120e9).process(signal_cell);
Opt_sig_wdm = Amplifier("amp_mode","ideal_no_noise","gain_mode","output_power", ...
"amplification_db",s.p_launch+10*log10(N)).process(Opt_sig_wdm);
%% Fiber propagation
segment_length = 1;
nSegments = link_length/segment_length;
nSegments = round(nSegments);
zdw = 1310;
randomize_D = true;
Dvec = getDispersionVector(nSegments, d_local, zdw, randomize_D, s.random_key);
Opt_sig_wdm_fib = Opt_sig_wdm;
fprintf('Running fiber propagation...\n');
for seg = 1:nSegments
fprintf('Segment %d/%d\n', seg, nSegments);
Opt_sig_wdm_fib = DP_Fiber("L",segment_length,"D",Dvec(seg),"Dpmd",s.pmd,"Ds",0.07, ...
"beat_len",10,"corr_len",100,"dz",1,"manakov",0, ...
"gamma",s.gamma,"lambda",zdw,"n_waveplates",10,"SS_dphimax",0.01, ...
"SS_dzmax",50,"SS_dzmin",10,"X_alpha",0.3,"X_beta",0,"rng",1,"useGPU",true,"useSingle",true).process(Opt_sig_wdm_fib);
end
%% Pre-process to get Rx_sig (do demux once)
fprintf('Pre-processing receiver chain...\n');
l = 1; % Use channel 1 for optimization
Opt_sig_demux = Optical_Demultiplex("attenuation",0,"B",200e9,"filtype",1, ...
"fs_out",fdac*kover,"fs_in",fdac*kover*upsample_pow,"lambda_center",1310).process(Opt_sig_wdm_fib);
Opt_sig_rx = Amplifier("amp_mode","ideal_no_noise","gain_mode","output_power", ...
"amplification_db",s.rop).process(Opt_sig_demux{l});
PD_sig = Photodiode("fsimu",fdac*kover,"dark_current",2e-08,"responsivity",1,"temperature",20, ...
"nep",1.8e-11,"randomkey",s.random_key+l).process(Opt_sig_rx);
rx_bwl = 100e9;
PD_sig = Filter('filtdegree',4,"f_cutoff",rx_bwl,"fs",fdac*kover, ...
"filterType",filtertypes.butterworth,"active",true).process(PD_sig);
Lp_scpe = Filter('filtdegree',4,"f_cutoff",80e9,"fs",fadc,"filterType",filtertypes.butterworth,"active",true);
Scpe_sig = Scope("fsimu",fdac*kover,"fadc",fadc, ...
"delay",0,"fixed_delay",0,"filtertype",filtertypes.butterworth, ...
"samplingdelay",0,"rand_samplingdelay",0,"freq_offset",0,"samp_jitter",0, ...
"adcresolution",8,"quantbuffer",0.1,'block_dc',1,'lpf_active',0,'H_lpf',Lp_scpe).process(PD_sig);
Scpe_sig_2sps = Scpe_sig.resample("fs_out",2*fsym);
[~, Scpe_cell, ~, ~] = Scpe_sig_2sps.tsynch("reference", Symbols{l}, "fs_ref", fsym, "debug_plots", 0);
Rx_sig = Scpe_cell{1};
Rx_sig = Rx_sig.normalize("mode","rms");
fprintf('Receiver pre-processing complete. Ready for optimization.\n\n');
%% Define the objective function for bayesopt
function ber = ffe_objective(params, Rx_sig, Symbols_l, Tx_bits_l, M, duob_mode)
mu_dd = params.mu_dd;
mu_tr = params.mu_tr;
try
eq_ffe = FFE("epochs_tr", 5, "epochs_dd", 2, "len_tr", 2^13, ...
"mu_dd", mu_dd, "mu_tr", mu_tr, ...
"order", 50, "sps", 2, "decide", 0, ...
"adaption", adaption_method.nlms, "dd_mode", 1);
ffe_results = ffe(eq_ffe, M, Rx_sig, Symbols_l, Tx_bits_l, ...
"precode_mode", duob_mode, ...
'showAnalysis', 0, ...
"postFFE", [], ...
"eth_style_symbol_mapping", 0);
ber = ffe_results.metrics.BER;
if ber == 0
ber = 1e-10;
end
if ~isfinite(ber)
ber = 0.5;
end
fprintf(' mu_dd=%.4e, mu_tr=%.4e -> BER=%.4e\n', mu_dd, mu_tr, ber);
catch ME
fprintf(' mu_dd=%.4e, mu_tr=%.4e -> FAILED (%s)\n', mu_dd, mu_tr, ME.message);
ber = 0.5;
end
end
%% Define optimizable variables
mu_dd_var = optimizableVariable('mu_dd', [1e-5, 0.1], 'Transform', 'log');
mu_tr_var = optimizableVariable('mu_tr', [1e-5, 0.1], 'Transform', 'log');
%% Run Bayesian Optimization
fprintf('========== Starting Bayesian Optimization ==========\n');
fprintf('Optimizing mu_dd and mu_tr to minimize BER\n');
fprintf('Search range: mu_dd=[1e-5, 0.1], mu_tr=[1e-5, 0.1]\n\n');
objective_fn = @(params) ffe_objective(params, Rx_sig, Symbols{l}, Tx_bits{l}, s.M, duob_mode);
results = bayesopt(objective_fn, [mu_dd_var, mu_tr_var], ...
'MaxObjectiveEvaluations', 30, ...
'AcquisitionFunctionName', 'expected-improvement-plus', ...
'IsObjectiveDeterministic', false, ...
'ExplorationRatio', 0.5, ...
'Verbose', 1, ...
'PlotFcn', []);
%% Display Results
fprintf('\n========== FFE Optimization Complete ==========\n');
fprintf('Best FFE parameters found:\n');
fprintf(' mu_dd = %.6e\n', results.XAtMinObjective.mu_dd);
fprintf(' mu_tr = %.6e\n', results.XAtMinObjective.mu_tr);
fprintf(' BER = %.6e\n', results.MinObjective);
%% Verify with optimal parameters
fprintf('\nVerifying optimal FFE parameters...\n');
best_mu_dd = results.XAtMinObjective.mu_dd;
best_mu_tr = results.XAtMinObjective.mu_tr;
eq_ffe_best = FFE("epochs_tr", 5, "epochs_dd", 2, "len_tr", 2^13, ...
"mu_dd", best_mu_dd, "mu_tr", best_mu_tr, ...
"order", 50, "sps", 2, "decide", 0, ...
"adaption", adaption_method.nlms, "dd_mode", 1);
ffe_results_best = ffe(eq_ffe_best, s.M, Rx_sig, Symbols{l}, Tx_bits{l}, ...
"precode_mode", duob_mode, ...
'showAnalysis', 1, ...
"postFFE", [], ...
"eth_style_symbol_mapping", 0);
fprintf('\nFinal FFE BER with optimal parameters: %.6e\n', ffe_results_best.metrics.BER);