永发信息网

求一段维特比硬判决译码的matlab代码。

答案:1  悬赏:70  手机版
解决时间 2021-11-23 15:57
  • 提问者网友:心如荒岛囚我终老
  • 2021-11-23 12:15
求一段维特比硬判决译码的matlab代码。
最佳答案
  • 五星知识达人网友:旧脸谱
  • 2021-11-23 12:43
%一个是硬判决,一个是软判决的函数,各自存了,然后再调用,注意是两个函数,别弄混了

function decoder_output=viterbi_hard(y,L)

global G;
n=size(G,1);
K=size(G,2);
number_of_states=2^(K-1);
%------------------------------------------------
%-------------生成各分支的输出--------------------
%------------------------------------------------
for j=0:number_of_states-1
for t=0:1
[next_state,memory_contents]=next_state_fun(j,t,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
%------------------------------------------------
metric_of_states=zeros(1,number_of_states); %各状态的度量metric
metric_of_states_c=zeros(number_of_states,2); %各状态两个输入的度量
length_seq=length(y)/n; %符号个数
decoder_output=zeros(1,length_seq-K+1); %解码输出
channel_output_matrix=reshape(y,n,length_seq); %将解调输出的比特按符号排列
survivor_state=zeros(number_of_states,length_seq+1); %留存路径
input_of_state=zeros(number_of_states,length_seq+1,2); %汇聚到各状态的分支对应的输入
state_sequence=zeros(1,length_seq+1);
count=zeros(1,number_of_states);
for i=1:length_seq-K+1
%------------------------------------------------
for j=0:number_of_states-1
for t=0:1
binary_output=deci2bin(output(j+1,t+1),n); %将各分支的输出转换为2进制
branch_metric=Hamming_dis(channel_output_matrix(:,i)',binary_output); %计算分支度量
count(nextstate(j+1,t+1)+1)=count(nextstate(j+1,t+1)+1)+1;
metric_of_states_c(nextstate(j+1,t+1)+1,count(nextstate(j+1,t+1)+1))=metric_of_states(j+1)+branch_metric; %计算累积度量(加)
input_of_state(nextstate(j+1,t+1)+1,:,count(nextstate(j+1,t+1)+1))=survivor_state(j+1,:); %该分支所在路径的对应的输入
input_of_state(nextstate(j+1,t+1)+1,i,count(nextstate(j+1,t+1)+1))=t;
end;
end;
%----------------比较汇聚到同一状态的两条路径,选取距离较小的-----------------
for j=0:number_of_states-1
if metric_of_states_c(j+1,1)>=metric_of_states_c(j+1,2)
metric_of_states(j+1)=metric_of_states_c(j+1,2);
survivor_state(j+1,:)=input_of_state(j+1,:,2);
else
metric_of_states(j+1)=metric_of_states_c(j+1,1);
survivor_state(j+1,:)=input_of_state(j+1,:,1);
end;
end;
count=zeros(1,number_of_states);
%--------------------------截短输出------------------------------------
if i>L
[min_metric,location]=min(metric_of_states);
decoder_output(i-L)=survivor_state(location,i-L);
end;
end
%---------------------最后L个比特译码输出--------------------------------
[min_metric,location]=min(metric_of_states);
decoder_output(length_seq-K+1-L+1:length_seq-K+1)=survivor_state(location,length_seq-K+1-L+1:length_seq-K+1);
========================华丽的分割线=================================

%以上为硬判决,一下为软判决

function decoder_output=viterbi_soft(y,L)
global G;
n=size(G,1);
K=size(G,2);
number_of_states=2^(K-1);
for j=0:number_of_states-1
for t=0:1
[next_state,memory_contents]=next_state_fun(j,t,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
metric_of_states=zeros(1,number_of_states);
metric_of_states_c=zeros(number_of_states,2);
length_seq=length(y)/n;
decoder_output=zeros(1,length_seq-K+1);
channel_output_matrix=reshape(y,n,length_seq);
survivor_state=zeros(number_of_states,length_seq+1);
input_of_state=zeros(number_of_states,length_seq+1,2);
state_sequence=zeros(1,length_seq+1);
count=zeros(1,number_of_states);
for i=1:length_seq-K+1
flag=zeros(1,number_of_states);
for j=0:number_of_states-1
for t=0:1
binary_output=deci2bin(output(j+1,t+1),n);
branch_metric=cor_dis(channel_output_matrix(:,i)',binary_output);
count(nextstate(j+1,t+1)+1)=count(nextstate(j+1,t+1)+1)+1;
metric_of_states_c(nextstate(j+1,t+1)+1,count(nextstate(j+1,t+1)+1))=metric_of_states(j+1)+branch_metric;
input_of_state(nextstate(j+1,t+1)+1,:,count(nextstate(j+1,t+1)+1))=survivor_state(j+1,:);
input_of_state(nextstate(j+1,t+1)+1,i,count(nextstate(j+1,t+1)+1))=t;
end;
end;
for j=0:number_of_states-1
if metric_of_states_c(j+1,1)<=metric_of_states_c(j+1,2)
metric_of_states(j+1)=metric_of_states_c(j+1,2);
survivor_state(j+1,:)=input_of_state(j+1,:,2);
else
metric_of_states(j+1)=metric_of_states_c(j+1,1);
survivor_state(j+1,:)=input_of_state(j+1,:,1);
end;
end;
count=zeros(1,number_of_states);
if i>L
[max_metric,location]=max(metric_of_states);
decoder_output(i-L)=survivor_state(location,i-L);
end;
end

[max_metric,location]=max(metric_of_states);
decoder_output(length_seq-K+1-L+1:length_seq-K+1)=survivor_state(location,length_seq-K+1-L+1:length_seq-K+1);追问谢谢你的代码,可不可以给我一个主程序,主要功能是维特比译码器。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯