📄 viterbi.m
字号:
%function [decoder_output,survivor_state,cumulated_metric]=viterbi(G,k,channel_output)
G=[1 0 0;1 0 1;1 1 1];
k=1;
channel_output=[1 1 0 1 1 0 1 1 0 1 1 1 0 1 0 1 0 1 1 0 0];
n=size(G,1);
if rem(size(G,2),k)~=0
error('size of G and k do not agree');
end
if rem(size(channel_output,2),n)~=0
error('shannel output not of the right size');
end
L=size(G,2)/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);
%对接收序列的前一部分进行解码,如给出的接收序列长度为21,则每三个进行分组后,为了保证最后两级能正确译码,这一部分程序进行到第五级。
for i=1:depth_of_trellis-L+1 %对每一级编码器,在网格图中仅搜索5级。
flag=zeros(1,number_of_states); %留存标志:flag=1,保留该支路。
if i<=L %由于前三级只有一条支路进入每个状态,不存在任何比较,故设置step,直至第四级开始第一次有两条支路进入每个状态,step=1.
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);%将输出矩阵中的元素转换为二进制形式。
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=1.
|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));%计算收敛时的状态数(第六级为0-4,第七级为0-2)。
for j=0:last_stop-1
branch_metric=0;
binary_output=deci2bin(output(j+1,1),n);
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-i+2)+1),depth_of_trellis-i+2);
end
decoder_output_metrix=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_metrix(:,i)=dec_output_bin(k:-1:1)';
end
decoder_output=reshape(decoder_output_metrix,1,k*(depth_of_trellis-L+1));
cumulated_metric=state_metric(1,1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -