% plot_PeRayleighDiversity - plot symbol error rate for Rayleigh channels 

%% initialize
SNRdB = 0:0.1:12;
SNR = dB2lin(SNRdB);
maxL = 5;

Pe  = Q( sqrt( 2*SNR ) );               % AWGN
PeR = zeros(maxL,length(SNR));

for L=1:maxL
    mu = sqrt(SNR./(1+SNR));
    
    S = ones(size(SNR));
    for ll=1:L-1
       S = S + prod(L:L-1+ll)/prod(1:ll) * ((1+mu)/2).^ll; 
    end

    PeR(L,:) = ((1-mu)/2).^L .* S;
end

%% plot
semilogy(SNRdB, PeR, SNRdB, Pe)
xlabel( 'E_s/N_0 (dB)')
ylabel( 'Symbol Error Rate')
grid
axis([SNRdB(1) SNRdB(end) 1e-5 1])
legend('L=1','L=2','L=3','L=4','L=5','AWGN','Location','SouthWest')

%% Store name of script for automated figure generation
ScriptName = mfilename;
