function [ C_decode ] = Jiaoyanzi_decode( n,k,R,gx )
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
% n 码字长度,k为信息为长度,R为接收码字序列,gx为生成多项式序列
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%校验子译码%
g=zeros(1,n-k+1);
for i=1:n-k+1 %倒序的生成多项式 对应的多项式为:1+x+x3 生成多项式序列后面为高位
g(i)=gx(n-k+1+1-i);
end
R1=zeros(1,n);
for i=1:n
R1(i)=R(n+1-i); %将循环码cm进行正序,因为我们习惯码字序列高位在前,低位在后。
end
p=2;
[q,s]=gfdeconv(R1,g,2); %gfdeconv函数输入和输出的多项式的对应的序列后面为高位。 移位后的m(x)与g(x)做模2除法得到商q和余式r(x)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[s1,s2]=size(s);
if s2==3 %将S校验子序列转化为十进制数
SS=4*s(3)+2*s(2)+s(1);
elseif s2==2
SS=2*s(2)+s(1);
elseif s2==1
SS=s(1);
end
if SS==0
EE=[0,0,0,0,0,0,0];
elseif SS==1
EE=[0,0,0,0,0,0,1];
elseif SS==2
EE=[0,0,0,0,0,1,0];
elseif SS==4
EE=[0,0,0,0,1,0,0];
elseif SS==3
EE=[0,0,0,1,0,0,0];
elseif SS==6
EE=[0,0,1,0,0,0,0];
elseif SS==7
EE=[0,1,0,0,0,0,0];
elseif SS==5
EE=[1,0,0,0,0,0,0];
end
map=find(EE==1); %确定接收码字错误的位置
R(map)=~R(map); %错误码字译码
C_decode=R;
end