% Figure 3.26: Outage probability of the Alamouti STBC for a 2x1 MISO and
% 2x2 MIMO system and an i.i.d. Rayleigh slow fading channel model.
% Errata: in book the curves are for a 2x4 MIMO system and not a 2x2 MIMO
% system
% Elisabeth de Carvalho, April 2012


clear all

% number of channel realizations. This number needs to be increased when
% estimating a small probability
It = 200000;

% SNR range in dB
SNRdBvalues = [0:2:30];

% transmission rate: 4 bits per tranmission
R=4;


SNRidx = 0;
for SNRdB=SNRdBvalues
    SNRdB
    SNRidx = SNRidx + 1;
    SNR=10^(SNRdB/10);

    
    % initialize variables: number of channel realizations where R exceeds
    % the maximal achievable rate for Alamouti 2x1 and 2x1    
    N1=0;
    N2=0;
    
    
    for kk=1:It
        
        % generate channel realization
        H1 = (randn(1,2) + j*randn(1,2))/sqrt(2);  % 2 tx and 1 rx antenns
        H2 = (randn(2)   + j*randn(2))/sqrt(2);    % 2 tx and 2 rx antenns
        
        % Alamouti 2x1
        SNR1 = norm(H1)^2*SNR/2;        % SNR
        C1   = (log2(1 + SNR1));        % Max achievable rate
        N1   = N1 + .5*(1-sign(C1-R));  % number of channel realizations where R exceeds C1
        
        % Alamouti 2x2
        SNR2 = ( norm(H2(1,:))^2 + norm(H2(2,:))^2 )*SNR/2;  % SNR of Alamouti 2x1
        C2   = (log2(1 + SNR2));        % Max achievable rate
        N2   = N2 + .5*(1-sign(C2-R));  % number of channel realizations where R exceeds C2
        
        
    end
    
    % Outage probabilities
    P1(SNRidx) = N1/It;   % Alamouti 2x1
    P2(SNRidx) = N2/It;   % Alamouti 2x2
    
end


figure(1)
semilogy(SNRdBvalues, P1,'k-.','linewidth',1)
hold on
semilogy(SNRdBvalues, P2,'k','linewidth',1)
grid
title('Alamouti STBC - Outage probability for 2x1 MISO and 2x2 MIMO')
xlabel('SNR \rho (dB)')
ylabel('Outage probability')
legend('Alamouti STBC 2x1','Alamouti STBC 2x2',3)
axis([0 30 10e-8 1])
grid
hold off
grid
