r1=3;
r2=4;
%
[H1,G1,n1,k1]=hammgen(r1);
[H2,G2,n2,k2]=hammgen(r2);
s1=sprintf("Hamming code (%d,%d)",n1,k1);
s2=sprintf("Hamming code (%d,%d)",n2,k2);
%
p0=[5e-4 1e-3 5e-3 1e-2 5e-2 1e-1];
stat=zeros(2,6);
%
msg1=randi([0 1],1e5,k1);
msg2=randi([0 1],1e5,k2);
%
menc1=encode(msg1,n1,k1,"hamming");
menc2=encode(msg2,n2,k2,"hamming");
%
for i=1:1:6
  mrec1=bsc(menc1,p0(i));
  mdec1=decode(mrec1,n1,k1,"hamming");
  [num,rate]=biterr(msg1,mdec1);
  stat(1,i)=rate;
  mrec2=bsc(menc2,p0(i));
  mdec2=decode(mrec2,n2,k2,"hamming");
  [num,rate]=biterr(msg2,mdec2);
  stat(2,i)=rate;
end
%
format long;
%
stat
%
mfig=figure;
L1=loglog(p0,stat(1,:));
set(L1,"LineWidth",3,"Color","k");
hold on;
L2=loglog(p0,stat(2,:));
set(L2,"LineWidth",3,"Color","b");
hold on;
title(sprintf("Hamming codes (%d,%d) and (%d,%d) in BSC channel",n1,k1,n2,k2));
xlabel("BER in BSC channel, p0");
ylabel("Error rate after decoding");
legend(s1,s2,3);
legend("show");
grid on;
print(mfig,'-dpng',sprintf("ham-%d-%d_ham-%d-%d_bsc_err-rate",n1,k1,n2,k2));