n1=12;
n2=15;
k=7;
%
s1=sprintf("Cyclic code (%d,%d)",n1,k);
s2=sprintf("Cyclic code (%d,%d)",n2,k);
s3=sprintf("Without coding");
%
p0=[1e-3 5e-3 1e-2 5e-2 1e-1];
stat=zeros(3,5);
%
msg=randi([0 1],1e5,k);
%
menc1=encode(msg,n1,k,"cyclic");
menc2=encode(msg,n2,k,"cyclic");
%
for i=1:1:5
  mrec1=bsc(menc1,p0(i));
  mdec1=decode(mrec1,n1,k,"cyclic");
  [num,rate]=biterr(msg,mdec1);
  stat(1,i)=rate;
  mrec2=bsc(menc2,p0(i));
  mdec2=decode(mrec2,n2,k,"cyclic");
  [num,rate]=biterr(msg,mdec2);
  stat(2,i)=rate;
  mrec3=bsc(msg,p0(i));
  [num,rate]=biterr(msg,mrec3);
  stat(3,i)=rate;
end
%
format long;
%
stat
%
mfig=figure;
L3=loglog(p0,stat(3,:));
set(L3,"LineWidth",3,"Color","r");
hold on;
L1=loglog(p0,stat(1,:));
set(L1,"LineWidth",1,"Color","k");
hold on;
L2=loglog(p0,stat(2,:));
set(L2,"LineWidth",3,"Color","b");
hold on;
title(sprintf("Cyclic codes (%d,%d) and (%d,%d) in BSC channel",n1,k,n2,k));
xlabel("BER in BSC channel, p0");
ylabel("Error rate after decoding");
legend(s3,s1,s2,0);
legend("show");
grid on;
print(mfig,'-dpng',sprintf("cycl-%d-%d_cycl-%d-%d_bsc_err-rate",n1,k,n2,k));
