% -----------------------------------------------------------------------
% Block coding performance calculator
% DRB 27-Feb-2007
% This script compares the rformance of coded binary digital
% communications with uncoded binary digital communications.
% -----------------------------------------------------------------------

% -----------------------------------------------------------------------
% USER PARAMETERS BELOW
% -----------------------------------------------------------------------
ebnodb_test=0:0.1:12;               % r-bit SNR values to look at (dB)
k=4;                               % (n,k) code parameters
n=7;                              % (n,k) code parameters
t=1;                                % error correction capability of the code
% -----------------------------------------------------------------------

r=k/n;                              % code rate

ebno_test=10.^(ebnodb_test/10);     % convert from dB to linear

ier=zeros(2,length(ebno_test));      % allocate space for information error rate
PB=zeros(2,length(ebno_test));      % allocate space for block error rate
pe=zeros(1,length(ebno_test));

i1=0;
for ebno=ebno_test,                 % MAIN LOOP
    i1=i1+1;
    
    % uncoded BPSK system
    ier(1,i1)=Q(sqrt(2*ebno));       % information error rate for uncoded
    PB(1,i1)=1-(1-ier(1,i1))^k;      % block error rate for uncoded
    
    % coded BPSK systemp
    pe(i1)=Q(sqrt(r*2*ebno));       % error probability of each bit in codeword
    PB(2,i1)=1;
    for i=0:t,                      % loop to compute block error rate
        PB(2,i1)=PB(2,i1)-nchoosek(n,i)*pe(i1)^i*(1-pe(i1))^(n-i);
    end
    ier(2,i1)=PB(2,i1)/k;            % approximation to compute information error rate
end

% PLOTTING CODE
subplot(1,2,1);
semilogy(ebnodb_test,PB,'Linewidth',2);       % block error
legend('uncoded','block code (after decoding)');
xlabel('uncoded Eb/N0 (dB)')
ylabel('block error rate')
grid on
axis square
title('block error rate')

subplot(1,2,2)
semilogy(ebnodb_test,ier,ebnodb_test,pe,'Linewidth',2);       % information error
legend('uncoded','block code (after decoding)','error prob of each bit in codeword');
xlabel('uncoded Eb/N0 (dB)')
ylabel('information error rate')
grid on
axis square
title('information error rate');
