hlayumi的个人空间 https://blog.eetop.cn/hslogic [收藏] [复制] [分享] [RSS]

空间首页 动态 记录 日志 相册 主题 分享 留言板 个人资料

日志

维特比译码算法

已有 989 次阅读| 2019-8-10 15:12 |系统分类:其他

%载自pudn,用到函数:bin2deci、deci2bin、metric、nxt_stat
%7/12/2008 ZHANGSONG

function [decoder_output,survivor_state,cumulated_metric]=viterbi(channel_output)
% [decoder_output,survivor_state,cumulated_metric]=viterbi(G,k,channel_output)
% 其中G是一个n行L*k列矩阵,它的每一行决定了从移位寄存器到输入码字的连接方式.
% survivor_state是一个矩阵,它显示了通过网格的最优路径,这个矩阵通过一个单独
% 的函数metric(x,y)给出,这个算法既能用于硬判决译码也能用于软判决译码。


k=2;
G=[0 0 1 0 1 0 0 1;0 0 0 0 0 0 0 1;1 0 0 0 0 0 0 1];
%k=1;
%G=[0 1 0 1 1 1;1 0 0 0 1 1;0 1 1 0 0 1;1 0 0 1 0 1];
n=size(G,1);
%检验G的维数 
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('channle 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 t=0:2^k-1
        [next_state,memory_contents]=nxt_stat(j,t,L,k);
        input(j+1,next_state+1)=t;
        branch_output=rem(memory_contents*G',2);
        nextstate(j+1,t+1)=next_state;
        output(j+1,t+1)=bin2deci(branch_output);
    end
end
input;
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);
[row_survivor col_survivor]=size(survivor_state);
%开始非尾信道输出的解码 
%i为段,j为每一阶段的状态,t为输入
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 t=0:2^k-1
            branch_metric=0;
            binary_output=deci2bin(output(j+1,t+1),n);
            for tt=1:n
                branch_metric=branch_metric+metric(channel_output_matrix(tt,i),binary_output(tt));
            end
            if ((state_metric(nextstate(j+1,t+1)+1,2)>state_metric(j+1,1)+branch_metric)|flag(nextstate(j+1,t+1)+1)==0)
                state_metric(nextstate(j+1,t+1)+1,2)=state_metric(j+1,1)+branch_metric;
                survivor_state(nextstate(j+1,t+1)+1,i+1)=j;
                flag(nextstate(j+1,t+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);
        for tt=1:n
            branch_metric=branch_metric+metric(channel_output_matrix(tt,i),binary_output(tt));
        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

%从最优路径产生解码输出 
%由段得到状态序列,再由状序列从input矩阵中得到该段的输出
state_sequence=zeros(1,depth_of_trellis+1);
size(state_sequence);
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
state_sequence;
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));
cumulated_metric=state_metric(1,1);

点赞

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 注册

  • 关注TA
  • 加好友
  • 联系TA
  • 0

    周排名
  • 0

    月排名
  • 0

    总排名
  • 0

    关注
  • 5

    粉丝
  • 0

    好友
  • 1

    获赞
  • 16

    评论
  • 5260

    访问数
关闭

站长推荐 上一条 /1 下一条

小黑屋| 关于我们| 联系我们| 在线咨询| 隐私声明| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 )

GMT+8, 2024-4-26 00:15 , Processed in 0.031965 second(s), 14 queries , Gzip On, Redis On.

eetop公众号 创芯大讲堂 创芯人才网
返回顶部