您好,欢迎访问三七文档
这是我搜集整理的一个关于卷积编码和Viterbi译码的Matlab程序,现在把它们放在这里,希望对需要的人有些帮助。卷积编码程序:function[output,len_tal]=cnv_encd(secrettext,encodetext)g=[00100100;00000001;10000001;01001101];k0=1;%读入文本文件并计算文件长度frr=fopen(secrettext,'r');[msg,len]=fread(frr,'ubit1');msg=msg';%checktoseeifextrazeropaddingisnecessaryifrem(length(msg),k0)0msg=[msg,zeros(size(1:k0-rem(length(msg),k0)))];endn=length(msg)/k0;%把输入比特按k0分组,n为所得的组数。%checkthesizeofmatrixgifrem(size(g,2),k0)0error('Error,gisnotoftherightsize.');end%determineLandn0L=size(g,2)/k0;n0=size(g,1);%addextrazeros,以保证编码器是从全0开始,并回到全0状态。u=[zeros(size(1:(L-1)*k0)),msg,zeros(size(1:(L-1)*k0))];%generateuu,amatrixwhosecolumnsarethecontentsofconv.encoderat%variousclockcycles.u1=u(L*k0:-1:1);fori=1:n+L-2u1=[u1,u((i+L)*k0:-1:i*k0+1)];enduu=reshape(u1,L*k0,n+L-1);%determinetheoutputoutput=reshape(rem(g*uu,2),1,n0*(L+n-1));len_tal=n0*(L+n-1);%writetheoutputtotheencodetextresult=fopen(encodetext,'w');fori=1:n0*(L+n-1)fwrite(result,output(i),'bit1');endfclose(result);Viterbi译码程序:function[decoder_output,survivor_state,cumulated_metric]=viterbi(channel_output,decodetext)ticG=[00100100;00000001;10000001;01001101];k=1;frr=fopen(channel_output,'r');[msg,len]=fread(frr,'ubit1');channel_output=msg';n=size(G,1);%checkthesizesifrem(size(G,2),k)~=0error('channel_outputnotoftherightsize');endL=size(G,2)/k;number_of_states=2^((L-1)*k);%generatestatetransitionmatrix,outputmatrix,andinputmatrixforj=0:number_of_states-1fori=0:2^k-1[next_state,memory_contents]=nxt_stat(j,i,L,k);input(j+1,next_state+1)=i;branch_output=rem(memory_contents*G',2);nextstate(j+1,i+1)=next_state;output(j+1,i+1)=bin2deci(branch_output);endend%addtheextrazero,ensurethelengthofchannel_outputisintegral%timeston.ifrem(len,n)0channel_output=[channel_output,zeros(size(n-rem(len,n):-1:1))];endstate_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);%startdecodingofnon-tailchanneloutputsfori=1:depth_of_trellis-L+1flag=zeros(1,number_of_states);ifi=Lstep=2^((L-i)*k);elsestep=1;endforj=0:step:number_of_states-1forl=0:2^k-1branch_metric=0;binary_output=deci2bin(output(j+1,l+1),n);forr=1:nbranch_metric=branch_metric+metric(channel_output_matrix(r,i),binary_output(r));endif((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;suvivor_state(nextstate(j+1,l+1)+1,i+1)=j;flag(nextstate(j+1,l+1)+1)=1;endendendstate_metric=state_metric(:,2:-1:1);end%startdecodingofthetailchannel-outputsfori=depth_of_trellis-L+2:depth_of_trellisflag=zeros(1,number_of_states);last_stop=number_of_states/(2^((i-depth_of_trellis+L-2)*k));forj=0:last_stop-1branch_metric=0;binary_output=deci2bin(output(j+1,1),n);forr=1:nbranch_metric=branch_metric+metric(channel_output_matrix(r,i),binary_output(r));endif((state_metric(nextstate(j+1,l+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;suvivor_state(nextstate(j+1,1)+1,i+1)=j;flag(nextstate(j+1,1)+1)=1;endendstate_metric=state_metric(:,2:-1:1);end%generatethedecodeoutputfromtheoptimalpathstate_sequence=zeros(1,depth_of_trellis+1);state_sequence(1,depth_of_trellis)=survivor_state(1,depth_of_trellis+1);fori=1:depth_of_trellisstate_sequence(1,depth_of_trellis-i+1)=suvivor_state((state_sequence(1,depth_of_trellis+2-i)...+1),depth_of_trellis-i+2);enddecoder_output_matrix=zeros(k,depth_of_trellis-L+1);fori=1:depth_of_trellis-L+1dec_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)';enddecoder_output=reshape(decoder_output_matrix,1,k*(depth_of_trellis-L+1));cumulated_metric=state_metric(1,1);%writetheoutputtotheencodetextresult=fopen(decodetext,'w');fori=1:k*(depth_of_trellis-L+1)fwrite(result,decoder_output(i),'bit1');endfclose(result);toc%*************************************************************************%functiondistance=metric(x,y)ifx==ydistance=0;elsedistance=1;end%************************************************************************%function[next_state,memory_contents]=nxt_stat(current_state,input,L,k)binary_state=deci2bin(current_state,k*(L-1));binary_input=deci2bin(input,k);next_state_binary=[binary_input,binary_state(1:(L-2)*k)];next_state=bin2deci(next_state_binary);memory_contents=[binary_input,binary_state];%************************************************************************%functiony=bin2deci(x)l=length(x);y=(l-1:-1:0);y=2.^y;y=x*y';%************************************************************************%functiony=deci2bin(x,L)y=zeros(1,L);i=1;whilex=0&i=Ly(i)=rem(x,2);x=(x-y(i))/2;i=i+1;end
本文标题:卷积码编译码程序
链接地址:https://www.777doc.com/doc-5153603 .html