% generate sim outage vs spat dependence
clear
Nsample = 1e7;
N = 3; g = 6;
lamTotal = 0.001;
Threshold = 2*ones(1,N);
P = 1;M = 64;
xVec = linspace(0,lamTotal,11);

for m = 1:length(xVec)
    m
    lamC = xVec(m);
    lamI = lamTotal - lamC;

    x = qammod(floor(M*rand([Nsample 1])),M);
    x = sqrt(P)*x./sqrt(var(x(:)));
    x = repmat(x,[1 N]);
    h = randn([Nsample N]) + 1i*randn([Nsample N]);
    s = x.*h;
    [Ir Ii]= gen_MultiAnt_HDist(N,g,lamC,lamI,0.3,'Rayleigh',1,1,1,Nsample);
    i_cplex  = Ir + 1i*Ii;
    I = sqrt(Ir.^2 + Ii.^2);
    
    %PDC Receiver
    SINR_Ant = (abs(s)./abs(i_cplex)).^2;
    for n = 1:N
        antOutPDC(:,n) = ( SINR_Ant(:,n) < Threshold(n) )*1;
    end
    outagePDC(m) = mean(prod(antOutPDC,2)==1 );
    
    %SC receiver
    antChoice = (abs(h) == repmat(max(abs(h)')',1,N));
    SINR_SC = sum(SINR_Ant.*antChoice,2);
    outageSC(m) = mean( (SINR_SC) < Threshold(1) )*1;
    
    %EGC Receiver
    r = sum(s.*repmat((1/N)*ones(1,N),Nsample,1),2);
    r_i = sum(i_cplex.*repmat((1/N)*ones(1,N),Nsample,1),2);
    SINR_EGC = (abs(r)./abs(r_i)).^2;
    outageEGC(m) = mean( (SINR_EGC) < Threshold(1) )*1;
        
    %MRC Receiver
    r = sum(s.*conj(h),2);
    r_i = sum(i_cplex.*conj(h),2);
    SINR_MRC = (abs(r)./abs(r_i)).^2;
    outageMRC(m) = mean( sum(SINR_MRC,2) < Threshold(1) )*1;
     
    %HL Receiver
    T = 1;
    y = s + i_cplex;
    w = (abs(y) - repmat(median(abs(y),2),1,N)) < T;
    r = sum(s.*conj(h).*w,2);
    r_i = sum(i_cplex.*conj(h).*w,2);
    SINR_HL = (abs(r)./abs(r_i)).^2;
    outageHL(m) = mean( (SINR_HL) < Threshold(1) )*1;
    
    %SL Receiver
    A = 3;
    w = (abs(y) - repmat(median(abs(y),2),1,N))*A ;
    r = sum(s.*conj(h).*exp(-abs(w)),2);
    r_i = sum(i_cplex.*conj(h).*exp(-abs(w)),2);
    SINR_SL = (abs(r)./abs(r_i)).^2;
    outageSL(m) = mean( (SINR_SL) < Threshold(1) )*1;
    
end

outageSim = [xVec' outageEGC' outageSC' outageMRC' outagePDC' outageHL' outageSL'];
save OutageVsSpatDep_Sim outageSim