function [fratio]=fixG_fun(Gmatrix,dis)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load t_en.mat;
load k;
%-------------------------------------------------------------------------%
error_number=3; %错误个数
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%再做一个外层循环;统计成功与失败次数;
statistic_number=100;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
throw=0;loop=1;
f=0;
s=0; %失败(f)和成功(s)次数的统计
while ((loop<=statistic_number)&&(throw==0))
%---------------------------------编码-------------------------------------%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%original message%
msg=randperm(K)>(K/2);
%编码后的信息分组
en_msg=mod((msg*Gmatrix),2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%-------------------------------模拟加错-----------------------------------%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
error_msg=((randperm(total_encode))<=error_number);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%-------------------------------解码准备-----------------------------------%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
de_msg=ones(1,K)*(-1);
copymat=sparse(Gmatrix); %^^^^
%复制一个生成矩阵用来解码;
decode_count=0;
%解码节点的个数
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%--------------------------------加错--------------------------------------%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
receive_msg=en_msg;
for q=1:length(error_msg)
if error_msg(q)==1
receive_msg(q)=-1;
end
end
%-------------------------------解码---------------------------------------%
%------------------------将错误的接收比特去掉-------------------------------%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for r=1:length(receive_msg)
if receive_msg(r)==-1
copymat(:,r)=zeros(K,1);
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fail=1;
%----------------%
%终止循环用:
flag=0;
% throw=0;
%----------------%
%copymat=sparse(Gmatrix);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
while (flag)==0
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:total_encode
cloc=(find(copymat(:,i)));
if length(cloc)==1 %cloc标记度为1的节点
if de_msg(cloc)==-1 %未赋过值
fail=1;
de_msg(cloc)=receive_msg(i); %赋值
decode_count=decode_count+1; %解码个数+1
if decode_count==K %全部解码
if de_msg==msg;
s=s+1; %统计成功次数
flag=1;%throw=1;
break;
end
end
if flag==0
copymat(cloc,i)=0; %矩阵中的元素标记为0
sloc=(find(copymat(cloc,:))); %标记G矩阵中与信息比特s相连的校验比特c
scount=length(sloc);
for j=1:scount
receive_msg(sloc(j))=xor(receive_msg(sloc(j)),de_msg(cloc));
%信息节点值加到编码节点
copymat(cloc,sloc(j))=0;
end
end
end
else fail=fail+1;
% if (flag==0)&&(fail>1.2*total_encode)
if (flag==0)&&(fail>total_encode)
f=f+1;%统计失败次数;
if f==10
disp('throw');
throw=1;
end
flag=1;
end
end
end
end
loop=loop+1;
end
% 显示部分控制:
if throw==0
show=[s,f];
if dis==1 %是否显示每次寻找结果
if s*f~=0
string1=['解码成功',num2str((s/(s+f))*100),'%'];
string2=['解码失败',num2str((f/(s+f))*100),'%'];
pie3(show,[1 0],{string1,string2});
elseif s==0
disp('100%失败!');
elseif f==0
disp('100%成功!');
end
end
fratio=f/statistic_number;
else
fratio=1;
end