📄 ldpcdecoderandencoder.m
字号:
%encode
load GAfinal
code=rand(1,3048)>0.5;%随机产生一个信号源码字,这个code还是逻辑结果,不是数
code=code+0; %为了变成数,把code加零,对code无影响
coded=zeros(1,7493);%为coded分配空间
for n=1:4445
k=zeros(1,3048)-1;
gcolumn=GAfinal(1:3048,n);
onescount=1;
for m=1:3048
if gcolumn(m)==1
k(onescount)=code(m);
onescount=onescount+1;
end
end
for x=onescount-1:-1:2
k(x-1)=xor(k(x),k(x-1));
end
coded(n)=k(1);
end
coded(1,4446:7493)=code;
%randomcoded=coded;
%for n=1:106
%randomcoded(n*70)=~randomcoded(n*70)+0;
%end
receivecode=zeros(1,7493);
for k=1:7493
if coded(k)==0
receivecode(k)=1;
else
receivecode(k)=-1;
end
end
%decode
load HAmatrix
nzmax=nnz(H);
ir=zeros(1,nzmax);
jc=zeros(1,7494);
[i,j]=find(H);
for a=1:nzmax
ir(a)=i(a);
end
k=0;
for n=1:7493
for m=1:4445
if H(m,n)==1
k=k+1;
end
end
jc(n+1)=k;
end
yi=coded;
LLR_intrinsic=-2.*yi;
LLR_extrinsic = zeros(1,nzmax);
check_LLR = zeros(1,nzmax);
check_LLR_mag = zeros(1,nzmax);
LLR_overall = zeros(1,n);
decoded=zeros(1,n);
%initialize variable-to-check messages
for k=1:n
if jc(k)==0
jc(k)=jc(k)+1;
for l=jc(k):jc(k+1)
LLR_extrinsic(l) = LLR_intrinsic(k);
end
else
for l=jc(k)+1:jc(k+1)
LLR_extrinsic(l) = LLR_intrinsic(k);
end
end
end
for iteration=1:100
%Step 1: compute check-to-variable messages
for k=1:nzmax
if LLR_extrinsic(k)<0
check_LLR(k) = -1;
else
check_LLR(k) = 1;
end
if LLR_extrinsic(k)<0
check_LLR_mag(k)= -LLR_extrinsic(k);
else
check_LLR_mag(k)=LLR_extrinsic(k);
end
end
rowTotal = ones(1,m);
for k=1: nzmax
rowTotal(ir(k))=rowTotal(ir(k))* check_LLR(k);
end
for k=1:nzmax
check_LLR(k) = check_LLR(k) * rowTotal(ir(k));
end
%sign of check-to-variable messages
for k=1:nzmax
check_LLR_mag(k) = -log( tanh(max(check_LLR_mag(k),0.000000001)/2 ) );
end
for k=1:m
rowTotal(k) = 0;
end
for k=1:nzmax
rowTotal(ir(k))=rowTotal(ir(k)) + check_LLR_mag(k);
end
for k=1:nzmax
check_LLR_mag(k) = -log( tanh(max( rowTotal(ir(k))- check_LLR_mag(k),0.000000001)/2 ) );
end
%magnitude of check-to-variable messages
for k=1:nzmax
check_LLR(k) = check_LLR(k) .* check_LLR_mag(k);
end
%check-to-variable messages
%Step 2: compute variable-to-check messages
for k=1:n
LLR_overall(k) = LLR_intrinsic(k);
if jc(k)==0
jc(k)=jc(k)+1;
for l=jc(k):jc(k+1)
LLR_overall(k)=LLR_overall(k) + check_LLR(l);
end
else
for l=jc(k)+1:jc(k+1)
LLR_overall(k)=LLR_overall(k) + check_LLR(l);
end
end
end
for k=1:n
if jc(k)==0
jc(k)=jc(k)+1;
for l=jc(k):jc(k+1)
LLR_extrinsic(l) = LLR_overall(k) - check_LLR(l);
end
else
for l=jc(k)+1:jc(k+1)
LLR_extrinsic(l) = LLR_overall(k) - check_LLR(l);
end
end
end
%variable-to-check messages
decoded=zeros(1,n);
for k=1:n
if LLR_overall(k)<0
decoded(k)=1;
else
decoded(k)=0;
end
end
checkrowtotal=zeros(1,4445);
for k=1:n
if jc(k)==0
jc(k)=jc(k)+1;
for l=jc(k):jc(k+1)
checkrowtotal(ir(l))=checkrowtotal(ir(l))+decoded(k);
end
else
for l=jc(k)+1:jc(k+1)
checkrowtotal(ir(l))=checkrowtotal(ir(l))+decoded(k);
end
end
end
for k=1:m
if( mod(checkrowtotal(k),2)~=0)
break;
end
end
if k==m
break;
end
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -