% Plot BER versus Eb/No in dB for Hamming(7,4) and 3 times repetition code
%
% Rich Kozick, October 2001

% Hamming code parameters:
k = 4;    % Number of message bits per block
n = 7;    % Number of codeword bits per block

pfine = logspace(-4, 0, 200);
zi = find(pfine <= 0.5);
pfine = pfine(zi);
PeU = 1 - (1-pfine).^7 - 7*(pfine).*(1-pfine).^6;
PeL = PeU/k;

PeRep3 = pfine.^3 + 3*(pfine.^2).*(1-pfine);

figure(1)
loglog(pfine, PeU, 'r-', pfine, PeL, 'b-', ...
pfine, pfine, 'k-', pfine, PeRep3, 'g--')
legend('HAMMING (7,4) U.B.', 'HAMMING (7,4) L.B.', 'UNCODED', ...
'3 \times REPETITION CODE')
xlabel('ERROR PROBABILITY FOR BSC (p)')
ylabel('BER')
title('BER VERSUS p FOR BSC')
axis([1e-4 0.5 1e-7 1])

% Now make the same plot versus Eb/N0 for BPSK signals

EbN0dB = -20:30;
EbN0 = 10.^(EbN0dB/10);
p = Qf(sqrt(2*EbN0));
PeUe = 1 - (1-p).^7 - 7*(p).*(1-p).^6;
PeLe = PeUe/k;

PeRep3e = p.^3 + 3*(p.^2).*(1-p);

figure(2)
semilogy(EbN0dB, PeUe, 'r-', EbN0dB, PeLe, 'b-', ...
EbN0dB, p, 'k-', EbN0dB, PeRep3e, 'k--')
legend('HAMMING (7,4) U.B.', 'HAMMING (7,4) L.B.', 'UNCODED', ...
'3 \times REPETITION CODE')
xlabel('E_b / N_0  (dB)')
ylabel('BER')
title('BER FOR BPSK IN AWGN')
axis([-6 12 1e-6 .5])
grid
