%硬判决 Viterbi 译码
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%当输入rcvd卷积码编码序列后,经过biterbi译码,输出dec_op
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [dec_op]=viterbidec(rcvd)
%Concatenate two consecutive bits of recieved encoded sequence to
%构造标记
input=[];
for j=1:2:length(rcvd)
input=[ input (rcvd(j))* 2 + (rcvd(j+1))];
end
%数组初始化
op_table=[00 00 11; 01 11 00; 10 10 01; 11 01 10]; %输出数组
ns_table=[0 0 2; 1 0 2; 2 1 3; 3 1 3]; %下一状态数组
transition_table=[0 1 1 55; 0 0 1 1; 55 0 55 1; 55 0 55 1];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 数组更新部分
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
st_hist(1:4, 1:17)=55; %历史状态数组
aem=zeros(4, 17); %累计路径长度数组
ssq=zeros(1, 17); %构造连续状态数组
lim=length(input); %时钟循环数量
for (t=0:1:lim) %时钟循环
if(t==0)
st_hist(1,1)=0; %开始状态为00
else
temp_state=[];%存储中间状态的数组
temp_metric=[];%存储可能的状态的长度的数组
temp_parent=[];%存储上一状态的数组
for (i=1:1:4)
in=input(t);
if(st_hist(i, t)==55) %如果是无效状态,无变化
else
ns_a=ns_table(i, 2)+1; %下一个状态 state-1
ns_b=ns_table(i, 3)+1; %下一个状态 state-2
op_a=op_table(i, 2); %下一个输出 output-1
op_b=op_table(i, 3); %下一个输出 output-2
cs=i-1; %当前状态
M_a=hamm_dist(in, op_a); %对ns_a各个分支进行度量
M_b=hamm_dist(in, op_b); %对ns_b各个分支进行度量
indicator=0; %指示多余状态的标记
for k=1:1:length(temp_state) %检查下一个多余的状态
%如果下一个可能的状态 state-1是多余的
if(temp_state(1,k)==ns_a)
indicator=1;
%ADD-COMPARE-SELECT Operation
%em_c: 当前状态的度量长度
%em_r: 多余状态的度量
em_c=M_a + aem(i,t);
em_r=temp_metric(1,k) + aem(temp_parent(1, k)+1,t);
if( em_c< em_r)%比较连个路径的长度
st_hist(ns_a,t+1)=cs;%选择长度小的路径
temp_metric(1,k)=M_a;
temp_parent(1,k)=cs;
end
end
%如果下一个可能的状态state-2是多余的
if(temp_state(1,k)==ns_b)
indicator=1;
em_c=M_b + aem(i,t);
em_r=temp_metric(1,k) + aem(temp_parent(1, k)+1,t);
if( em_c < em_r)%比较两个分支长度
st_hist(ns_b,t+1)=cs;%选择长度小的路径
temp_metric(1,k)=M_b;
temp_parent(1,k)=cs;
end
end
end
%如果两可能的状态都非多余
if(indicator~=1)
%更新历史状态组
st_hist(ns_a,t+1)=cs;
st_hist(ns_b,t+1)=cs;
%更新中间矢量
temp_parent=[temp_parent cs cs];
temp_state=[temp_state ns_a ns_b];
temp_metric=[temp_metric M_a, M_b];
end
%打印中间变量
temp_parent;
temp_state;
temp_metric;
end
end
%更新所有路径的累计长度
%现在的瞬时时刻 't'
for h=1:1:length(temp_state)
xx1=temp_state(1, h);
xx2=temp_parent(1, h)+1;
aem(xx1, t+1)=temp_metric(1, h) + aem(xx2, t);
end
end
end %结束循环
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 路径反馈部分
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for(t=0:1:lim)
slm=min(aem(:, t+1));
slm_loc=find( aem(:, t+1)==slm );
sseq(t+1)=slm_loc(1)-1;
end
dec_op=[];
for p=1:1:length(sseq)-1
dec_op=[dec_op, transition_table((sseq(p)+1), (sseq(p+1)+1))];
end