% -----------------------------------------------------------------------
% Block coding performance comparison
% DRB 27-Feb-2007
% This script compares block codes by code rate versus information error rate
% -----------------------------------------------------------------------

% -----------------------------------------------------------------------
% USER PARAMETERS BELOW
% -----------------------------------------------------------------------
ebno_db = 6;
% -----------------------------------------------------------------------

ebno = 10.^(ebno_db/10);

% hamming codes
m = 3:6;
k = 2.^m-m-1;
n = 2.^m - 1;
t = 1*ones(size(n));
ier = zeros(1,length(n));
for i=1:length(n),
    ier(i) = blockcodecomputeier(n(i),k(i),t(i),ebno);
end
semilogy(k./n,ier,'ks');
hold on

% maximal length codes
m = 3:6;
k = m;
n = 2.^k - 1;
dmin = 2.^(k-1);
t = floor((dmin-1)/2);
ier = zeros(1,length(n));
for i=1:length(n),
    ier(i) = blockcodecomputeier(n(i),k(i),t(i),ebno);
end
plot(k./n,ier,'md');

% BCH codes
nkt = [7,4,1;15,11,1;15,7,2;15,5,3;31,26,1;31,21,2;31,16,3;31,11,5;31,6,7;63,57,1;63,51,2;63,45,3;63,39,4;63,36,5;63,30,6;63,24,7;63,18,10;63,16,11;63,10,13;63,7,15]; 	
k = nkt(:,2);
n = nkt(:,1);
t = nkt(:,3);
ier = zeros(1,length(n));
for i=1:length(n),
    ier(i) = blockcodecomputeier(n(i),k(i),t(i),ebno);
end
plot(k./n,ier,'bo');

% capacity line
pe = Q(sqrt(2*ebno));
C = 1+pe*log2(pe)+(1-pe)*log2(1-pe);
plot([C C],[min(ier) 0.5],'r--');

hold off
xlabel('rate (bits of information per symbol)')
ylabel('normalized error correction capability (errors per symbol)')
grid on
