📄 viterbi.m
字号:
% This is the viterbi() function. It is used for decoding
% the trellis code which generated by cnv_encd()
% 采用Viterbi译码算法!
function [decoder_output,survivor_state,cumulates_metric]=viterbi(G,k,channel_output)
% 检查G的大小,并确定G是否与k匹配,若是,给出约束长度L,
n=size(G,1);
if rem(size(G,2),k)~=0
error('size of G and k do not agree')
end
L=size(G,2)/k;
% 确定状态数:2^((L-1)*k)
number_of_states=2^((L-1)*k);
% 生成状态转移矩阵、输出矩阵和输入矩阵
for j=0:number_of_states-1
for l=0:2^k-1
[next_state,memory_contents]=nxt_stat(j,l,L,k);
input(j+1,next_state+1)=l;
branch_output=rem(memory_contents*G',2);
nextstate(j+1,l+1)=next_state; % 状态转移矩阵
output(j+1,l+1)=bin2deci(branch_output); % 输出矩阵
end
end
state_metric=zeros(number_of_states,2);
depth_of_trellis=length(channel_output)/n;
channel_output_matrix=reshape(channel_output,n,depth_of_trellis); % 输入矩阵
survivor_state=zeros(number_of_states,depth_of_trellis+1);
% 开始无尾信道输出编码
for i=1:depth_of_trellis-L+1
flag=zeros(1,number_of_states);
if i<=L
step=2^((L-i)*k);
else
step=1;
end
for j=0:step:number_of_states-1
for l=0:2^k-1
branch_metric=0;
binary_output=deci2bin(output(j+1,l+1),n);
% 调用函数metric()计算Hamming距离
for ll=1:n
branch_metric=branch_metric+metric(channel_output_matrix(ll,i),...
binary_output(ll));
end
if((state_metric(nextstate(j+1,l+1)+1,2)>state_metric(j+1,1)+...
branch_metric)|flag(nextstate(j+1,l+1)+1)==0)
state_metric(nextstate(j+1,l+1)+1,2)=state_metric(j+1,1)+branch_metric;
survivor_state(nextstate(j+1,l+1)+1,i+1)=j;
flag(nextstate(j+1,l+1)+1)=1;
end
end
end
state_metric=state_metric(:,2:-1:1);
end
% 开始尾部信道输出编码
for i=depth_of_trellis-L+2:depth_of_trellis
flag=zeros(1,number_of_states);
last_stop=number_of_states/(2^((i-depth_of_trellis+L-2)*k));
for j=0:last_stop-1
branch_metric=0;
binary_output=deci2bin(output(j+1,1),n);
% 调用函数metric()计算Hamming距离
for ll=1:n
branch_metric=branch_metric+metric(channel_output_matrix(ll,i),binary_output(ll));
end
if((state_metric(nextstate(j+1,1)+1,2)>state_metric(j+1,1)+...
branch_metric)|flag(nextstate(j+1,1)+1)==0)
state_metric(nextstate(j+1,1)+1,2)=state_metric(j+1,1)+branch_metric;
survivor_state(nextstate(j+1,1)+1,i+1)=j;
flag(nextstate(j+1,1)+1)=1;
end
end
state_metric=state_metric(:,2:-1:1);
end
% 从最佳路径中得到解码输出
state_sequence=zeros(1,depth_of_trellis+1);
state_sequence(1,depth_of_trellis)=survivor_state(1,depth_of_trellis+1);
% 从幸存的状态矩阵中找出幸存路径
for i=1:depth_of_trellis
state_sequence(1,depth_of_trellis-i+1)=survivor_state((state_sequence...
(1,depth_of_trellis+2-i)+1),depth_of_trellis-i+2);
end
decoder_output_matrix=zeros(k,depth_of_trellis-L+1);
% 依幸存路径计算译码输出
for i=1:depth_of_trellis-L+1
dec_output_deci=input(state_sequence(1,i)+1,state_sequence(1,i+1)+1);
dec_output_bin=deci2bin(dec_output_deci,k);
decoder_output_matrix(:,i)=dec_output_bin(k:-1:1);
end
decoder_output=reshape(decoder_output_matrix,1,k*(depth_of_trellis-L+1));
cumulates_metric=state_metric(1,1);
% 参考输入验证:
% g=[1 0 1;1 1 1];
% k0=1;
% msg_in=[0 1 1 0 1 1 1 1 0 1 0 0 0 1];
% viterbi(g,k0,msg_in)
% de_out=[1 1 0 0 0]
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -