
clear

sir_loop = [-25:2:-13];
sir_loop = 0;
lw_loop = [1,2,3];

data = cell(length(sir_loop),length(lw_loop));

iterations=size(data);

parfor ix = 1:numel(data)

    [u1,u2] = ind2sub(iterations,ix);

    output = imddmodel(sir_loop(u1),lw_loop(u2));

    data{ix} = output;

end

for sir = 1:size(data,1)
    for lw = 1:size(data,2)
        ber_bm(sir,lw)   = data{sir,lw}.ber_bm;
        ber_dcsm(sir,lw) = data{sir,lw}.ber_dcsm;
        ber_lvsm(sir,lw) = data{sir,lw}.ber_lvsm;
        ber_lvlp(sir,lw) = data{sir,lw}.ber_lvlp;
    end
end

col = [    0.6510    0.8078    0.8902
    0.6980    0.8745    0.5412
    0.9922    0.7490    0.4353];
figure(22)
for l = 1:numel(lw_loop)
    hold on
    plot(sir_loop,ber_bm(:,l),'DisplayName',['DC mode:', num2str(lw_loop(l)),', Linewidth= 50 MHz'],'Marker','o','MarkerFaceColor',col(l,:),'Color',col(l,:),'LineWidth',2,'LineStyle','--');
end
set(gca,'yscale','log');
yline(3.8e-3,'LineWidth',2,'LineStyle','--','HandleVisibility','off');
set(gca,'xdir','reverse');



%% Plot
col = linspecer(8);
figure(1)
hold on
m = ["x","o","pentagram","hexagram","*","+"];
cnt = 1;
for d = 1%:size(data,2)
    for dc = 1:size(data,1)
        for lw = 1:size(data,3)
            ber_bm(dc,d,lw)   =  (data{dc,d,lw}.ber_bm);
            ber_dcsm(dc,d,lw) =  data{dc,d,lw}.ber_dcsm;
            ber_lvsm(dc,d,lw) =  data{dc,d,lw}.ber_lvsm;
            ber_lvlp(dc,d,lw) =  (data{dc,d,lw}.ber_lvlp);
        end
    end
    i = 1;
    comm_dn = [];% ['EQ DC Tap: ',num2str(dc_tap_loop(d)),' m'];
    title("Dependency on Laser Linewidth; B2B; Delay : 2*50m")
    plot(lw_loop*1e-6,mean(squeeze(ber_bm(1:end,d,:)),1),"LineWidth",1.2,"Marker",m(1),"MarkerSize",5,'Color',col(cnt,:),'DisplayName',[' ',comm_dn]);
    plot(lw_loop*1e-6,mean(squeeze(ber_dcsm(1:end,d,:)),1),"LineWidth",1,"Marker",m(1+4),"MarkerSize",5,'LineStyle','--','Color',col(cnt,:),'DisplayName',['DC smoothing ',comm_dn],'HandleVisibility','on');
    plot(lw_loop*1e-6,mean(squeeze(ber_lvsm(1:end,d,:)),1),"LineWidth",1.2,"Marker",m(1+1),"MarkerSize",5,'LineStyle',':','Color',col(cnt,:),'DisplayName',['Lvl Smoothing ',comm_dn],'HandleVisibility','on');
    plot(lw_loop*1e-6,mean(squeeze(ber_lvlp(1:end,d,:)),1),"LineWidth",1,"Marker",m(1+3),"MarkerSize",5,'LineStyle','-.','Color',col(cnt,:),'DisplayName',['Lvl Lowpass ',comm_dn],'HandleVisibility','on');
    set(gca,'yscale','log');
    set(gca,'xscale','log');
    %xticklabels([10 100 1000 10000]);
    grid minor
    yline(3.8e-3,'LineWidth',2,'LineStyle','--','HandleVisibility','off');
    ylim([1e-3 4e-2]);
    ylabel("BER");
    xlabel("Linewidth in MHz")
    legend
    cnt = cnt+1;
end


figure(1)
contour(lw_loop,sir_loop,thres_a0,16:0.4:21,'LineWidth',2,'FaceAlpha',0.3,'ShowText','on',"LabelFormat","%0.1f dB");
clim([16 21]);
ylabel("Bandwidth in Multiples of Linewidth");
xlabel("Linewidth in MHz");
% set(gca,'yscale','log');
grid minor
title("MPI removal - Optimization of Lowpass Filter Bandwidth")

figure(2)
contour(lw_loop,sir_loop([1:10,12:end]),thres_a0([1:10,12:end],:),16:0.3:21,'LineWidth',2,'FaceAlpha',0.3,'ShowText','on',"LabelFormat","%0.1f dB");
clim([16 21]);
ylabel("Window Length");
xlabel("Linewidth in MHz");
set(gca,'yscale','log');
grid minor
title("MPI removal - Optimization of Averaging Window Length")



%% Plot Winlen Contour
hdfec = 3.8e-3.*ones(size(sir_loop));

for dc = 1:size(data,2)
    for lw = 1:size(data,3)
        for s = 1:size(data,1)
            ber_lvlp(s,dc,lw)   =  data{s,dc,lw}.ber_dcsm;
            %             ber_dcsm(wl,lw,s)   =  data{wl,s,lw}.ber_dcsm;
        end

        a_bm = InterX([sir_loop;squeeze(ber_lvlp(:,dc,lw))'],[sir_loop;hdfec]);
        %         a_dcsm = InterX([sirloop;squeeze(ber_dcsm(wl,lw,:))'],[sirloop;hdfec]);
        try
            thres_a0(dc,lw) = -a_bm(1);
            %thres_a1(wl,lw) = -a_dcsm(1);
        catch
            thres_a0(dc,lw) = NaN;
            %thres_a1(wl,lw) = NaN;
        end

    end
end

figure(1)
contour(lw_loop,bw_loop,thres_a0,14:0.2:21,'LineWidth',1.5,'FaceAlpha',0.3,'ShowText','on',"LabelFormat","%0.1f dB");
a = flip(cbrewer2('seq','Spectral',32));
a = [a(1:12,:); a(22:end,:)];
colormap(a);
clim([16 21]);
ylabel("Window Length");
xlabel("Linewidth in MHz");
yticks(bw_loop);
yticklabels(bw_loop);
set(gca,'yscale','log');
set(gca,'xscale','log');
grid minor




%% Plot DC Tap Contour
hdfec = 3.8e-3.*ones(size(bw_loop));
thres_a0 = zeros(size(ber,1),size(ber,3));
thres_a1 = zeros(size(ber,1),size(ber,3));
for dc = 1:size(ber,1)
    for lw = 1:size(ber,3)
        a_lvsm = InterX([bw_loop;ber(dc,:,lw)],[bw_loop;hdfec]);
        thres_a0(dc,lw) = -a_lvsm(1);

        a1 = InterX([bw_loop;ber_a1(dc,:,lw)],[bw_loop;hdfec]);
        thres_a1(dc,lw) = -a1(1);
    end
end

figure(1)
subplot(2,1,1)
contour(lw_loop,sir_loop,thres_a0,16:0.5:21,'LineWidth',3,'FaceAlpha',0.3,'ShowText','on',"LabelFormat","%0.1f dB");
clim([16 21]);
ylabel("DC Tap");
xlabel("Linewidth in MHz");
set(gca,'yscale','log');
grid minor
subplot(2,1,2)
contour(lw_loop,sir_loop,thres_a1,16:0.5:21,'LineWidth',3,'FaceAlpha',0.3,'ShowText','on',"LabelFormat","%0.1f dB");
clim([16 21]);
ylabel("DC Tap");
xlabel("Linewidth in MHz");
set(gca,'yscale','log');
grid minor

%% Plot Curves of required SIR to see the minimum a bit better
col = flip(cbrewer2('seq','Spectral',16));
col = col([1:4, 10:end],:);
figure(2)
hold on
for lw = [size(ber,3):-2:2 2 1]
    plot(sir_loop,thres_a0(:,lw),'DisplayName',[' Linewidth: ',num2str(lw_loop(lw)*1e-6), ' MHz'],'Color',col(lw,:),'Marker','o','MarkerFaceColor',col(lw,:),'LineWidth',2);
    set(gca,'xscale','log');
end
xlabel("DC Tap Value");
ylabel("Required SIR to rech FEC in dB");

%%
col = linspecer(7);
figure(3)
cnt=1;
for lw = [1,2,11]

    for i = 1:length(sir_loop)-1

        subplot(1,3,cnt)
        hold on
        plot(-1.*bw_loop,ber(i,:,lw),"LineWidth",2,"Marker","o","MarkerSize",5,'Color',col(i,:),'DisplayName',['DC tap ',num2str(sir_loop(i))]);
        plot(-1.*bw_loop,ber_a1(i,:,lw),"LineWidth",2,"Marker","x","MarkerSize",5,'LineStyle','--','Color',col(i,:),'DisplayName',['A1. DC tap ',num2str(sir_loop(i))]);
        yline(3.8e-3,'LineWidth',2,'LineStyle','--','HandleVisibility','off');
        set(gca,'yscale','log');
        grid minor

        xlim([15,30]);
        ylim([1e-4,1e-2]);
        xlabel("SIR in dB");
        ylabel("BER");
        title(['BER for different SIR;',' Linewidth: ',num2str(lw_loop(lw)*1e-6), ' MHz'])
        text(25,4.2e-3,"FEC $3.8 e^{-3}$");
    end
    cnt = cnt+1;
end
legend

