
%straight outta workspace, show everything I want

cols = linspecer(8);
colpairs = cbrewer2('paired',8);

f=figure(20);
clf(f)
f.Name="Optspec";
%Spectrum of Optical Signal after superposition
subplot(3,2,1:2)
spectrum_plot(Combined_sig.normalize("mode","rms").signal',Combined_sig.fs,'Optspec',['Papr: ',num2str(Combined_sig.papr), '; CSPR: ',num2str(Combined_sig.cspr)]);
hold on
ylim([-100,0]);

%Phase Investigation
subplot(3,2,3)
hold on
phase_int = extmodlaser.phase(ceil(dly):end);
phase_main = delayseq(extmodlaser.phase,ceil(dly));
phase_main = phase_main(ceil(dly):end);
phase_diff = phase_int - phase_main;
t = (1:length(phase_int))' ./ extmodlaser.fsimu ;
plot(t*1e6,phase_int,'Color',colpairs(1,:),'DisplayName','Interferer Phase');
plot(t*1e6,phase_main,'Color',colpairs(2,:),'DisplayName','Signal Phase (delayed)');
plot(t*1e6,phase_diff,'Color',colpairs(4,:),'DisplayName','Delta Phase');
legend

subplot(3,2,4)
%Received Signal after Phdiode
yyaxis left
t = (1:Rx_sig.length)' ./ Rx_sig.fs;
scatter(t*1e6,Rx_sig.normalize("mode","oneone").signal.*max(unique(digimod_out.signal)),1,'.','MarkerEdgeColor',cols(6,:));
hold on
errors_t = errors./Rx_Bits.fs;
errors_sym = Rx_symboldecision.signal(errors);
scatter(errors_t*1e6,errors_sym,2,'x','LineWidth',1);
yline(PAMmapper(M,0).thresholds);

%Again: Phase Diff
yyaxis right
t = (1:length(phase_int))' ./ extmodlaser.fsimu ;
plot(t*1e6,phase_diff,'Color',colpairs(4,:),'DisplayName','Delta Phase');

title(['Lwidth:',num2str(laser_linewidth) ,'; SIR: ', num2str(sir) ,'dB; ROP: ',num2str(rop(i)),' dBm'])
xlabel('t in $\mu$s')
legend

%Equalized Signal 
subplot(3,2,5)
t = (1:EQ_sig.length)' ./ EQ_sig.fs;
for h = 1:size(levels,2)
    scatter(t*1e6,levels(:,h),1,'.');
    hold on
end
%yline(PAMmapper(M,0).thresholds);
title(['BER: ',sprintf('%.1e', BER(s,l,pnk,n,m,i))]);

%Histogram of EQzed Signal
subplot(3,2,6)
for h = 1:size(levels,2)
    std_dev = std(levels(:,h),'omitnan');
    mean_val = mean(levels(:,h),'omitnan');
    hold on
    histogram(levels(:,h),1000,"EdgeColor","none","Normalization","pdf");
    text(mean_val, 0.1, ['$\sigma^2:$ ',sprintf('%.2f', std_dev)], 'HorizontalAlignment', 'center');
end
hold on
xline(PAMmapper(M,0).thresholds);
xticks(sort([unique(digimod_out.signal);PAMmapper(M,0).thresholds']));
