function output =  imddmodel(sir,dcmode)

for realiz = 1:3

    rng(realiz);

    %% Set Simulation Variables
    delay   = 0;10+(10*realiz); %mpi delay in meter

    fiblen = 0; %main link in km

    laser_linewidth =0e6;

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

    % Modulation
    M = 4; %PAM-M
    bitpattern = zeros(N,log2(M));

    % Symbol Rate
    fsym = 112e9;

    % DAC Rate
    fdac = 120e9;

    % Simulation oversampling rate "k";
    kover = 16;

    % ADC Rate
    fadc = 256e9;

    % Simulation frequency in "analog domain"
    fsimu = kover * fdac ;



    %% B) CONSTRUCT ALL CLASSES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    digimod     = PAMmapper(M,0);

    pulseform   = Pulseformer("pulseform","rrc","fdac",fdac,"fsym",fsym,"pulselength",32,"rrcalpha",0.027);

    awg         = AWG('fdac',fdac,'kover',kover,'lpf_active',1,'f_cutoff',56e9,'lpf_type',filtertypes.gaussian,'bit_resolution',5.5);

    lp_laser    = Filter('filtdegree',1,"f_cutoff",50e9,"fsamp",fdac*kover,"filterType",filtertypes.bessel_inp);

    u_pi = 3.5;
    vbias = (0.5*u_pi)-u_pi;
    extmodlaser = EML("mode",eml_mode.im_cosinus,"power",5,"fsimu",fsimu,"lambda",1550,"bias",vbias,"u_pi",u_pi,"linewidth",laser_linewidth);

    fib         = Fiber("fsimu",fdac*kover,"fiber_length",fiblen,"alpha",0.2,"D",16,"lambda0",thz2nm(193.1),"gamma",0);

    reflectionpoint  = Amplifier("amp_mode","ideal_no_noise","gain_mode","gain","amplification_db",sir);
    reflectionprop   = Fiber("fsimu",fdac*kover,"fiber_length",delay/1000,"alpha",0.2,"D",16,"lambda0",1550,"gamma",0);

    opticatten  = Amplifier("amp_mode","ideal_no_noise","gain_mode","output_power","amplification_db",-8);

    phdiode     = Photodiode("fsimu",fdac*kover,"dark_current",2e-08,"responsivity",1,"temperature",20);

    lp_diode    = Filter('filtdegree',1,"f_cutoff",50e9,"fsamp",fdac*kover,"filterType",filtertypes.bessel_inp);

    scp         = Scope("fsimu",fdac*kover,"fadc",fadc,...
        "delay",0,"fixed_delay",0,"lpf_bw",113e9,"filtertype",filtertypes.butterworth,...
        "samplingdelay",0,"rand_samplingdelay",0,"freq_offset",0,"samp_jitter",0,...
        "adcresolution",6,"quantbuffer",0.1,'block_dc',1);

    eq          = EQ("K",2,"plottrain",0,"plotfinal",0,...
        "training_length",4096,"training_loops",2,...
        "Ne",[50,5,3],"Nb",[3,2,2],...
        "DCmu",0.05,"DDmu",[0.0004 0.0005 0.0006 0.0007 ],"DFEmu",0.005,"FFEmu",0.005,...
        "dd_loops",2,"epsilon",[10 100 1000 ],"M",2,...
        "thres",[0.005 0.004 0.0005 ],"l1act",0,"delay",0,"rho",0.0005,"ideal_dfe",0,"DB_aim",0);

    eq2 = EQ_silas("Ne",[20,0,0],"Nb",[3,0,0],"trainlength",4096,"mu_dc_dd",0.005,"mu_dc_train",0.05,...
        "mu_ffe_train",0.005,"mu_combined_dd",[0.0004 0.0006 0.0003 0.005],"ddloops",3,"dcmode",dcmode);


    %% C) PROCESS TX %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    % 1) PRBS Generation
    for i = 1:log2(M)
        [bitpattern(:,i),seed] = prbs(O,N,seed);
    end

    % 2 ) Build Inf. signal class
    bits = Informationsignal(bitpattern);

    % 3) Digi modulation -> PAM-M signal
    digimod_out = digimod.map(bits);

    % 4) Pulse shaping -> racos
    X = pulseform.process(digimod_out);

    % 5) AWG (lowpass, quantization, sample and hold)
    X = awg.process(X);

    % 6) Lowpass behavior of laser and hf-cable? why twice?
    X = lp_laser.process(X);
    X = lp_laser.process(X);

    % 7) Normalize signal
    X = X.normalize("mode","oneone");
    X.signal = X.signal .* 1.3800;



    %% D) PROCESS OPTICAL CHANNEL %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    % 1) Laser; Modulation ->  OPTICAL DOMAIN
    [X,extmodlaser] = extmodlaser.process(X);

    % 2) Main fiber propagation
    X = fib.process(X);

    if delay ~= 0

    % 3) Reflection
    % Reflection is just an attenuation
    R = reflectionpoint.process(X);

    % Propagate back and forth (actual fiber propagation)
    R = reflectionprop.process(R);

    % Delay the reflected signal
    [R,n] = R.delay("delay_meter",delay);

    % Add together
    X.signal = X.signal(n:end);
    R.signal = R.signal(n:end);

    %disp(['SIR ',num2str(10*log10(X.power/R.power))]);

    X = X+R;

    % 4) Equalize
    % cut reference signal to correct length (nessecary due to MPI delay)
    digimod_out.signal = digimod_out.signal(round(n * fsym/fsimu) : end,:);
    bitpattern = bitpattern(round(n * fsym/fsimu):end,:);
    end
    
    % 4) Attenuation
    X = opticatten.process(X);
    % X  = edfaamp.process(X);

    % 5) Photo Diode -> ELECTRICAL DOMAIN
    X = phdiode.process(X);
    X = lp_diode.process(X);


    %% E) PROCESS RX %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    % 1) Oscilloscope (Sampling to f_adc; Quantization; Bandwidth Limitation)
    X = scp.process(X);

    % 2) Resample to 2x symbol rate
    X = X.resample("fs_out",2*fsym,"fs_in",fadc);

    % 3) Normalize
    Eq_in = X.normalize("mode","rms");



    % MPI reduction DC removal BEFORE EQ
    wl = 3000; % symbols
    Eq_in.signal = Eq_in.signal - 1/wl .* movsum( Eq_in.signal,[wl/2,wl/2]);

    % Equalize Signal
    [Eq_out]             = eq2.process(Eq_in,digimod_out);

    


    %% A1: MPI reduction DC removal
    wl = 1000; % symbols
    yk_dcsm = Eq_out;
    yk_dcsm.signal = Eq_out.signal - 1/wl .* movsum( Eq_out.signal,[wl/2,wl/2]);

    %% A2: MPI reduction Level wise error removal
    yk_lvsm = Eq_out;
    yk_lvlp = Eq_out;
    pre_decision_level_uni  = digimod.decide_pamlevel(Eq_out);
    pre_decision_level_bi   = ( pre_decision_level_uni*2-3 ) .* 1/sqrt(5);

    e = Eq_out.signal - pre_decision_level_bi;

    lp_mpi    = Filter('filtdegree',1,"f_cutoff",2e6,"fsamp",fsym,"filterType",filtertypes.bessel_inp);

    filtered = lp_mpi.process(e);

    wl = 30; % symbols
    smoothed = ( 1/wl .* movsum(e,[wl/2,wl/2])  );

    % remove interference
    for level = 0:3
        yk_lvsm.signal(pre_decision_level_uni==level) = yk_lvsm.signal(pre_decision_level_uni==level) - smoothed(pre_decision_level_uni==level);
        yk_lvlp.signal(pre_decision_level_uni==level) = yk_lvlp.signal(pre_decision_level_uni==level) - filtered(pre_decision_level_uni==level);
    end

    %% PROCESS RX %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    % 1) Digi Demod
    d_bm = digimod.demap(Eq_out);
    d_dcsm = digimod.demap(yk_dcsm);
    d_lvsm = digimod.demap(yk_lvsm);
    d_lvlp = digimod.demap(yk_lvlp);

    % 2) BER

    dbit =  length(d_bm.signal)-length(bitpattern);

    [~,errors_bm,ber_bm(realiz),loc] = calc_ber(d_bm.signal(1:end-dbit,:)    ,bitpattern(1:end,:),"skip",0,"returnErrorLocation",1);
    [~,errors_dcsm,ber_dcsm(realiz)] = calc_ber(d_dcsm.signal(1:end-dbit,:)  ,bitpattern(1:end,:),"skip",0,"returnErrorLocation",0);
    [~,errors_lvsm,ber_lvsm(realiz)] = calc_ber(d_lvsm.signal(1:end-dbit,:)  ,bitpattern(1:end,:),"skip",0,"returnErrorLocation",0);
    [~,errors_lvlp,ber_lvlp(realiz)] = calc_ber(d_lvlp.signal(1:end-dbit,:)  ,bitpattern(1:end,:),"skip",0,"returnErrorLocation",0);


end

output.ber_bm   = mean(ber_bm);
output.ber_dcsm = mean(ber_dcsm);
output.ber_lvsm = mean(ber_lvsm);
output.ber_lvlp = mean(ber_lvlp);


end