
% datarate = 128e9;
M = 4;
laser_linewidth = 0;
kover = 32;
fsym = 170e9;%round(datarate*1e-9 / log2(M))*1e9;
fdac = 256e9;

% 1) PRBS Generation
O = 18; %order of prbs
N = 2^(O-1); %length of prbs
[~,seed] = prbs(O,1); %initialize first seed of prbs
bitpattern=[];

for i = 1:log2(M)
    [bitpattern(:,i),seed] = prbs(O,N,seed);
end
if M == 6
    bitpattern = reshape(bitpattern,[],1);
    bitpattern = bitpattern(1:end-mod(length(bitpattern),5));
end
bits = Informationsignal(bitpattern);

% 2) Digi modulation -> PAM-M signal
digimod_out = PAMmapper(M,0).map(bits);
digimod_out.fs = fsym;

% 3) Pulseform Raised Cosine
X = Pulseformer("fsym",fsym,"fdac",fdac,"pulse","rrc","pulselength",16,"rrcalpha",0.01).process(digimod_out);

% Implememt Precompensation

% Implement Precoding

% 4) AWG (lowpass, quantization, sample and hold)

LP_awg = Filter('filtdegree',4,"f_cutoff",75e9,"fs",fdac*kover,"filterType",filtertypes.gaussian);


AWG_=AWG("fdac",fdac,"dac_min",-1,"dac_max",1,"H_lpf",LP_awg,"kover",kover,"bit_resolution",16,"lpf_active",1,"normalize2dac",1,"upsampling_method","samplehold");
X = AWG_.process(X);

disp(['El. power: ',num2str(X.power),' dBm (into 50 Ohm)']);
disp(['El. RMS voltage: ',num2str(sqrt(mean(X.signal.^2))),' V']);
disp(['max voltage: ',num2str(max(X.signal)),' V']);


% 5) Lowpass behavior before laser
LP_modulator= Filter('filtdegree',4,"f_cutoff",70e9,"fs",fdac*kover,"filterType",filtertypes.butterworth);
X =  LP_modulator.process(X);

% 6) Laser; Modulation ->  OPTICAL DOMAIN
u_pi = 4;
vbias = 2;
extmodlaser = EML("mode",eml_mode.im_cosinus,"power",0,"fsimu",X.fs,"lambda",1290,"bias",vbias,"u_pi",u_pi,"linewidth",laser_linewidth,"randomkey",5);
[Opt,extmodlaser] = extmodlaser.process(X);

if 1
    f = figure(120);
    f.Name = 'bla';
    tiledlayout(2,4);

    nexttile
    rms_ = rms(X.signal);
    max_ = max(X.signal);
    min_ = min(X.signal);
    hold on
    plot(X.signal,'LineWidth',0.1);
    yline([max_, min_],'LineWidth',2,'LineStyle','--');
    yline([rms_, -rms_],'LineWidth',2,'LineStyle',':');
    ylim([-3 3]);
    title(['AWG output: ',num2str(X.power), 'dBm']);

    % Add text boxes for MIN, MAX, and RMS voltage
    text(0.5, min_-0.3, ['MIN: ', num2str(min_),' V'],'FontSize', 10, 'HorizontalAlignment', 'left');
    text(0.5, max_+0.3, ['MAX: ', num2str(max_),' V'],'FontSize', 10, 'HorizontalAlignment', 'left');
    text(0.5, rms_+0.22, ['RMS: ', num2str(rms_),' V'],'FontSize', 10, 'HorizontalAlignment', 'left');
    text(0.5, -rms_-0.22, ['RMS: ', num2str(rms_),' V'],'FontSize', 10, 'HorizontalAlignment', 'left');

    nexttile
    plot_eye(X.signal,X.fs,fsym);
    ylabel('Signal in V')

    nexttile
    hold on
    v_in_curve = [-u_pi*1.5/2:0.1:u_pi*1.5/2];
    field=sqrt(10^(extmodlaser.power/10-3));
    mzm_curve = ((field.*cos(pi/2*(real(v_in_curve)+vbias)/u_pi)).^2)*1e3;
    scatter(v_in_curve+vbias,mzm_curve,10,'o','filled','DisplayName','Modulator TF complete');
    scatter(X.signal(1:100000)+vbias,(abs(Opt.signal(1:100000)).^2)*1e3,0.1,'.','DisplayName','Modulator TF')
    scatter(min_+vbias,((field.*cos(pi/2*(real(min_)+vbias)/u_pi)).^2)*1e3,50,'x','LineWidth',2);
    scatter(max_+vbias,((field.*cos(pi/2*(real(max_)+vbias)/u_pi)).^2)*1e3,50,'x','LineWidth',2);
    xlim([-u_pi*1.5/2+vbias, u_pi*1.5/2+vbias]);
    ylim([min(mzm_curve),max(mzm_curve)]);
    xlabel('Input in V')
    ylabel('Output in mW')
    title("MZM input (v) to output (w)");

    nexttile
    plot_eye(abs(Opt.signal.^2).*1e3 ,Opt.fs,fsym);
    ylabel('Opt. Signal in mW')
    

    nexttile([1 2])
    spectrum_plot( Opt.signal,Opt.fs, 'bla');
end










