%logmap译码算法,zhenshu为所选帧包含的bit数
function [l_all,waixinxi]=logmapsuanfa(shuru,g,xianyan,index)
zhenshu=length(shuru)/2;
[n,k]=size(g);
m=k-1;
state=2^m;
a=zeros(zhenshu+1,state);
b=zeros(zhenshu+1,state);
[next_out,next_state,last_out,last_state] = wanggersc(g);
jidazhi=1e10;
a(1,1)=0;
a(1,2:state)=-jidazhi*ones(1,state-1);
if (index==1)
b(zhenshu,1)=0;
b(zhenshu,2:state)=-jidazhi*ones(1,state-1);
else
b(zhenshu,1:state)=zeros(1,state);
end
%计算a
for i=2:zhenshu+1
for j=1:state
qiao=-jidazhi*ones(1,state);
qiao(last_state(j,1))=-log(1+exp(xianyan(i-1)))-shuru(2*i-3)+shuru(2*i-2)*last_out(j,2);
qiao(last_state(j,2))=xianyan(i-1)-log(1+exp(xianyan(i-1)))+shuru(2*i-3)+shuru(2*i-2)*last_out(j,4);
if (sum(exp(qiao+a(i-1,:)))<1e-300)
a(i,j)=-jidazhi;
else
a(i,j)=log(sum(exp(qiao+a(i-1,:))));
end
end
temp(i)=max(a(i,:));
a(i,:)=a(i,:)-temp(i);
end
%计算b
for i=zhenshu-1:-1:1
for j=1:state
qiao=-jidazhi*ones(1,state);
qiao(next_state(j,1))=-log(1+exp(xianyan(i+1)))-shuru(2*i+1)+shuru(2*i+2)*next_out(j,2);
qiao(next_state(j,2))=xianyan(i+1)-log(1+exp(xianyan(i+1)))+shuru(2*i+1)+shuru(2*i+2)*next_out(j,4);
if (sum(exp(qiao+b(i+1,:)))<1e-300)
b(i,j)=-jidazhi;
else
b(i,j)=log(sum(exp(qiao+b(i+1,:))));
end
end
temp(i)=max(b(i,:));
b(i,:)=b(i,:)-temp(i);
end
%计算l_all
for i=1:zhenshu
for j=1:state
qiao0=-log(1+exp(xianyan(i)))-shuru(2*i-1)+shuru(2*i)*last_out(j,2);
qiao1=xianyan(i)-log(1+exp(xianyan(i)))+shuru(2*i-1)+shuru(2*i)*last_out(j,4);
temp0(j)=exp(qiao0+a(i,last_state(j,1))+b(i,j));
temp1(j)=exp(qiao1+a(i,last_state(j,2))+b(i,j));
end
l_all(i)=log(sum(temp1))-log(sum(temp0));
end
for i=1:zhenshu
waixinxi(i)=2*shuru(2*i-1);
waixinxi(i)=l_all(i)-waixinxi(i)-xianyan(i);
end