


% parfsym             = zeros(length(mudc_loop),length(allfiles));
% parsir              = zeros(length(mudc_loop),length(allfiles));
% ber_wo_reduction    = zeros(length(mudc_loop),length(allfiles));
% ber_w_dcremoval     = zeros(length(mudc_loop),length(allfiles));
% mudc                = zeros(length(mudc_loop),length(allfiles));
% blocklen            = zeros(length(mudc_loop),length(allfiles));

% parameters.mudc = [0, 0.0001,0.0005, 0.001,0.005, 0.01:0.01:0.1, 0.2:0.1:1];
% parameters.eq_parallelization_blocklength = [1];
% parameters.eq_updatelatency = [1];
% parameters.eq_avg_blocklength =[0];
block_loop = [1];

for j = 1:length(mudc_loop)

    mudc_current = mudc_loop(j);
    block_current = block_loop(1);

    parfor i = 1:length(allfiles)

        if 1

        if allfiles(i).bytes ~= 0
            current_filename = allfiles(i).name;
        else
            continue
        end

        else
% 
%         current_filename = 'pam4__loop_29_56Gbd_19092023_1546.mat';
%         current_filename = 'pam4__loop_30_92Gbd_19092023_1547.mat';
% 
         current_filename = 'pam4__loop_13_56Gbd_19092023_1512.mat';
         current_filename = 'pam4__loop_14_92Gbd_19092023_1513.mat';

        end

        % load dataset
        recorded_data = load([foldername,filesep, current_filename]);
        % get common variables of simulation loop

        %even if redundant, save stuff to results struct
        parsir(j,i)  = recorded_data.saveStructTemp.awg2scope_keysight_state.eigenlight_mpi - 3.3 + 7.2;
        parfsym(j,i) = recorded_data.saveStructTemp.common.f_sym;

        fdac = recorded_data.saveStructTemp.common.f_DAC;
        fadc = recorded_data.saveStructTemp.common.f_ADC;
        fsym = recorded_data.saveStructTemp.common.f_sym;

        % 0) build RX Signal

        y_rx = Electricalsignal(recorded_data.saveStructTemp.dp_tsynch_out');

        y_rx.fs = 2.*fsym;

        % 0) build tx reference Signal for eq training
        y_digimod = Electricalsignal(recorded_data.saveStructTemp.digi_mod_out');
        y_digimod.fs = fsym;

        rx_symbols          = y_rx.resample("fs_in",2.*fsym,"fs_out",fsym);
        rx_symbols          = rx_symbols.normalize("mode","rms").signal;
        correct_symbols     = recorded_data.saveStructTemp.digi_mod_out';

%         scatterleveldependent(rx_symbols,correct_symbols,parfsym(j,i));
%         title([num2str(fsym.*1e-9),' GBd; SIR: ',num2str(parsir(j,i)),' dB']);

%         if parfsym(j,i) == 56e9
%             deviation56(i) = var(( 1/500 .* movsum(y_rx.signal,[500/2,500/2])  ));
%             sir56(i) = simdata.saveStructTemp.awg2scope_keysight_state.eigenlight_mpi - 3.3 + 7.2;
%             std56(i,:) = calcleveldependentstd(rx_symbols,correct_symbols);
%         else
%             deviation92(i) = var(( 1/500 .* movsum(y_rx.signal,[500/2,500/2])  ));
%             sir92(i) = simdata.saveStructTemp.awg2scope_keysight_state.eigenlight_mpi - 3.3 + 7.2;
%             std92(i,:) = calcleveldependentstd(rx_symbols,correct_symbols);
%         end

        % 1) normlaize
        y_rx = y_rx.normalize("mode","rms");

        eq = EQ_silas("Ne",[25,0,0],"Nb",[2,0,0],"trainlength",4096,...
            "sps",2,...
            "mu_dc_dd",mudc_current,...
            "mu_dc_train",mudc_current,...
            "mu_ffe_train",0,...
            "mu_dfe_train",0.005,...
            "mu_ffe_dd",[0.0004 0.0004 0.0004],...
            "mu_dfe_dd",0.005,...
            "ddloops",3,...
            "trainloops",4,...
            "eq_parallelization_blocklength",block_current, ...
            "eq_updatelatency",3,...
            "eq_avg_blocklength",10000);

        [Eq_out] = eq.process(y_rx,y_digimod);

        % 2.2) MPI reduction blocks

        % 3) digital demodulation
        digimod = PAMmapper(2^recorded_data.saveStructTemp.common.M,0);

        d_hat = digimod.demap(Eq_out);

        d_    = digimod.demap(Electricalsignal(recorded_data.saveStructTemp.digi_mod_out'));
        d = recorded_data.saveStructTemp.prms_out;

%         scatterleveldependent(Eq_out.signal,correct_symbols,parfsym(j,i));
%         title([num2str(fsym.*1e-9),' GBd; SIR: ',num2str(parsir(j,i)),' dB']);

        % 4) BER calculation
        [totalbits,errors_bm,ber_dhat,loc]     = calc_ber(d_hat.signal(1:end,:)    ,d(1:end,:)',"skip",0,"returnErrorLocation",1);

        ber_nodctap = mean(recorded_data.saveStructTemp.prms_compare_state.BER  );

        disp(['fsym: ',num2str(parfsym(j,i).*1e-9),'GBd, SIR: ',num2str(parsir(j,i)),' ->> BER_new: ',sprintf('%2E',ber_dhat),' BER_old: ',sprintf('%2E',ber_nodctap)]);

        % 5) save to struct
        
        ber_wo_reduction(j,i) = ber_nodctap;
        ber_w_dcremoval(j,i) = ber_dhat;
        mudc(j,i) = eq.mu_dc_dd;
        blocklen(j,i) = eq.eq_parallelization_blocklength;

    end
end



rate = [92e9, 56e9];
figure(95)
col = cbrewer2('Paired',8);
for r = 1:length(rate)
    for  j = 1:length(mudc_loop)


        if mudc_loop(j) == 0
            lnsty = '--';
            
            mark = 'square';

        else
            lnsty = "-";
            
            mark = 'v';
        end

        if r == 1
            handlvis = 'on';
            colr = col(6,:);
        else
            handlvis = 'off';
            colr = col(8,:);
        end

        %plot curve of standard FFE+DFE
        if j == 1
            load(['C:\Users\Silas\Nextcloud4\Dokumente\02_Ablage_Office\OFC2024\',num2str(rate(r).*1e-9),'GBd_no_dcs.mat'],'b');
            plot(abs(b(1,:)),b(2,:),'DisplayName',[num2str(rate(r).*1e-9),' GBd; PAM4; mudc: ',0],'LineWidth',1,'Marker','o','MarkerSize',5,'LineStyle',lnsty,'HandleVisibility',handlvis,'Color',colr);
        end

        hold on
        b = [parsir(j,parfsym(j,:)==rate(r));ber_w_dcremoval(j,parfsym(j,:)==rate(r))];
        b=sort(b,2);
        plot(abs(b(1,:)),b(2,:),'DisplayName',[num2str(rate(r).*1e-9),' GBd; PAM4; mudc: ',num2str(mudc_loop(j))],'LineWidth',1,'Marker',mark,'MarkerSize',5,'LineStyle',lnsty,'HandleVisibility',handlvis,'Color',colr);

    end
end
set(gca, 'YScale', 'log');
yline(3.8e-3,'LineWidth',1, 'LineStyle','--','HandleVisibility','off');
xlim([15,35]);
xlabel('SIR in dB');
ylabel('BER');
annotation('textbox',...
    [0.328380952380952 0.477777777777781 0.197809523809524 0.106349206349207],...
    'String',{'3.8 $\cdot 10^{-3}$'},...
    'LineStyle','none',...
    'Interpreter','latex',...
    'FitBoxToText','off');

% legend('Standard EQ','Adaptive DC Subtraction')

figure(16)
hold on
plot(mudc_loop,ber_w_dcremoval(:,1));set(gca, 'YScale', 'log');set(gca, 'XScale', 'log');
xlim([0,1])
