function [mybit,decoder_output,BER,channel_output,cumulated_metric,survivor_state,y]=viterbi_ber
%changdu=input('Please input the length of the code sequence\n\n');
biaozhi=input('please choose the restrict length of the encoder\n3:restrict length=3\n7:restrict length=7\n');
save mybiaozhi biaozhi
%mybit=fix(rand(1,changdu)+0.5); % 产生随机信息
%pause
mybit=[fix(rand(1,84)) 0 1 1 1 1 1 1 1 1 1 1 1 fix(rand(1,104)) 0 1 0 0 fix(rand(1,12))];
% 卷积编码
if biaozhi==3
G=[1 1 1;1 0 1]; % (2,1,3)生成矩阵
elseif biaozhi==7
G=[1 1 1 1 0 0 1;1 0 1 1 0 1 1]; % (2,1,7)生成矩阵
end
k=1;
right_channel_output=cnv_encd(mybit);
%pause
lgth=length(right_channel_output);
%xunhuan=input('please input the number of the cycle\n');
%BER=zeros(1,xunhuan);
%cumulated_metric_table=zeros(1,xunhuan);
%decoder_output_table=zeros(xunhuan,lgth/2-biaozhi+1);
%channel_output_table=zeros(xunhuan,lgth);
% 模拟信道误码
%for z=1:xunhuan
channel=zeros(1,lgth);
%p=input('please input the number of the erroe bit\n');
%for z=1:2
%if z==1
%channel_output=right_channel_output;
%end
%if z==2
%for i=1:444
%channel(i)=1;
%end
%信道误码率=1/z,即每循环一次,就有一个信道误码率p
%for yz=18:32
% channel(yz)=1;
%end
%end
%channel_output=mod(right_channel_output+channel,2)
%end
%channel_output=input('please input the erroe code sequence\n');
%channel_output_table(z,:)=channel_output; % 数据转移
channel_output=right_channel_output;
n=size(G,1); % n=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('channel output not of the right size') % 经过信道后,进入译码器前,卷积编码的个数合理
end
L=size(G,2)/k; % 约束长度=寄存器个数+1
number_of_states=2^((L-1)*k); % 状态数
% 产生状态转移矩阵、输出矩阵和输入矩阵
for j=0:number_of_states-1 % 对(2,1,3) j=0,1,2,3
for l=0:2^k-1 % l=0,1
[next_state,memory_contents]=nxt_stat(j,l,L,k);
myinput(j+1,next_state+1)=l;
branch_output=rem(memory_contents*G',2); % 状态图上根据寄存器状态和input作出的编码
nextstate(j+1,l+1)=next_state; % 生成矩阵,含有十进制表示的下一状态
output(j+1,l+1)=bin2deci(branch_output); % 生成矩阵,含有十进制表示的卷积编码
end
end
state_metric=zeros(number_of_states,2); % 4行,2列 (初始化)
depth_of_trellis=length(channel_output)/n; % 需要译码的个数 设为6
channel_output_matrix=reshape(channel_output,n,depth_of_trellis); % 将经过信道的编码序列转换为以需要译码的个数为行数,以n为列数的矩阵
survivor_state=zeros(number_of_states,depth_of_trellis+1); % 4行,7列 (初始化)
% 开始无尾信道输出解码
for i=1:depth_of_trellis-L+1 % i=1,2,3,4
flag=zeros(1,number_of_states); % 1行,4列 (初始化)
if i<=L
step=2^((L-i)*k); % i=1,2,3 step= 4,2,1
else
step=1; % i=4 step=1
end
for j=0:step:number_of_states-1 % i=1 j=0;i=2 j=0, 2;i=3,4 j=0,1,2,3
for l=0:2^k-1 % l=0,1
branch_metric=0; % 初始化
binary_output=deci2bin(output(j+1,l+1),n); % 将此时刻十进制的编码结果用二进制表示
for ll=1:n % ll=1,2
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);
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=myinput(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));
%decoder_output_table(z,:)=decoder_output;
%decoder_output_table(z,:)=decoder_output; % 数据转移
cumulated_metric=state_metric(1,1);
%cumulated_metric_table(z)=cumulated_metric; % 数据转移
BER=Bit_Error_Rate(mybit,decoder_output);
%channel_BER(z)=Bit_Error_Rate(right_channel_output,channel_output);
y=mod((decoder_output+mybit),2);
%end
%----------------------------------------subfunction Bit_Error_Rate-------------------------------------------------
function y=Bit_Error_Rate(a,b)
y=nnz(mod(a+b,2))/length(a);
%-------------------------------------------subfunction cnv_encd----------------------------------------------------
function the_output1=cnv_encd(mybit) % the_output1为完全正确译码,编码输出结果多出2*移位寄存器个数; the_output2为不完全译码,译码结果与编码之前的原始信息
hndiviterbi.zip_viterbi
版权申诉
125 浏览量
2022-07-15
11:46:01
上传
评论
收藏 18KB ZIP 举报
朱moyimi
- 粉丝: 65
- 资源: 1万+
最新资源
- tomcat安装及配置教程.md
- 51单片机贴片心形灯原理图PCB程序 亚克力外壳.dwg资料
- 户型为单家独院式图纸编号D068-三层-20.60&13.40米-施工图.dwg
- 农村小别墅图纸编号D067-三层-10.00&11.50米- 施工图.dwg
- 三层独栋别墅图纸编号D066-三层-12.00&12.00米- 施工图.dwg
- 农村小别墅图纸编号D065-三层-14.40&18.55米-施工图.dwg
- 基于YOLOv8检测高铁吊弦缺陷实现的系统的Python源码+文档说明+训练源文件+模型.zip
- 三层农村小别墅图纸编号D064-三层-13.80&22.20米-施工图.dwg
- 三层别墅图纸编号D063-三层-13.57&17.40米- 施工图.dwg
- STC IAP15F2K61S2单片机i2c 接口PCF8591-ADC实验+DAC实验 KEIL例程源码+开发板硬件原理图
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈