Matlab卷积编码器维特比译码-通信专业课程设计

来源:互联网 发布:笔记本电脑必要软件 编辑:程序博客网 时间:2024/05/02 04:28

首先是演示代码,首先输入输入信号,用m序列加扰(演示里只用16位长度的m序列),再1/3卷积编码,接收端先维特比解码,再解扰,得到原始信号

Demo.m

function demoinput=[1 0 1 1 0 1 0 0 0 1 1];%输入信号subplot(2,3,1);drawSig(input);input_r=scramble(input);%加扰subplot(2,3,2);drawSig(input_r);Y=coder(input_r);%进行卷积编码subplot(2,3,3);drawSig(Y);O=decoder(Y);%维特比解码subplot(2,3,4);drawSig(O);Res=scramble(O);%解扰subplot(2,3,5);drawSig(Res);end
得到图片



mxulie.m

%产生16位m序列function Y=mxulie()F(16)=0;a3=1;a2=0;a1=0;a0=0;for i=1:16    F(i)=a0;    a0=a1;    a1=a2;    a2=a3;    a3=mod((a3+a0),2);endY=F;end

coder.m

%1/3,3卷积编码function Y = coder( X )len=length(X);T=zeros(1,len+3,'int8');F=zeros(1,len*3,'int8');for i=4:len+3    T(i)=X(i-3);endfor i=1:len    F(3*(i-1)+1)=T(i+3);    F(3*(i-1)+2)=xor(T(i+3),T(i+1));    F(3*(i-1)+3)=xor(xor(T(i+3),T(i+2)),T(i+1));endY=F;end

decoder.m

%1/3,3维特比卷积解码function Y = decoder( X )len=length(X);Path=zeros(4,len/3,'int8');%4条幸存路径Path_t=zeros(4,len/3,'int8');Da=0;Db=0;Dc=0;Dd=0;%抵达a,b,c,d总汉明距离Pa=1;Pb=2;Pc=3;Pd=4;%a,b,c,d路径指针T(9)=0;for i=1:9    T(i)=X(i);end%atp1=dist(T,[0 0 0 0 0 0 0 0 0],9);tp2=dist(T,[1 1 1 0 0 1 0 1 1],9);if(tp1<tp2)    Da=tp1;    Path(1,1)=0;Path(1,2)=0;Path(1,3)=0;else    Da=tp2;    Path(1,1)=1;Path(1,2)=0;Path(1,3)=0;end%btp1=dist(T,[0 0 0 0 0 0 1 1 1],9);tp2=dist(T,[1 1 1 0 0 1 1 0 0],9);if(tp1<tp2)    Db=tp1;    Path(2,1)=0;Path(2,2)=0;Path(2,3)=1;else    Db=tp2;    Path(2,1)=1;Path(2,2)=0;Path(2,3)=1;end%ctp1=dist(T,[0 0 0 1 1 1 0 0 1],9);tp2=dist(T,[1 1 1 1 1 0 0 1 0],9);if(tp1<tp2)    Dc=tp1;    Path(3,1)=0;Path(3,2)=1;Path(3,3)=0;else    Dc=tp2;    Path(3,1)=1;Path(3,2)=1;Path(3,3)=0;end%dtp1=dist(T,[0 0 0 1 1 1 1 1 0],9);tp2=dist(T,[1 1 1 1 1 0 1 0 1],9);if(tp1<tp2)    Dd=tp1;    Path(4,1)=0;Path(4,2)=1;Path(4,3)=1;else    Dd=tp2;    Path(4,1)=1;Path(4,2)=1;Path(4,3)=1;end%迭代Dat=0;Dbt=0;Dct=0;Ddt=0;%交换缓冲fga=0;fgb=0;fgc=0;fgd=0;%路径标志rmSz=int32((len-9)/3);for i=1:rmSz    T(1)=X(9+(i-1)*3+1);    T(2)=X(9+(i-1)*3+2);    T(3)=X(9+(i-1)*3+3);    %a    tp1=dist(T,[0 0 0],3)+Da;    tp2=dist(T,[0 1 1],3)+Dc;    if(tp1<tp2)        Dat=tp1;        fga=0;    else        Dat=tp2;        fga=1;    end    %b    tp1=dist(T,[1 1 1],3)+Da;    tp2=dist(T,[1 0 0],3)+Dc;    if(tp1<tp2)        Dbt=tp1;        fgb=0;    else        Dbt=tp2;        fgb=1;    end    %c    tp1=dist(T,[0 0 1],3)+Db;    tp2=dist(T,[0 1 0],3)+Dd;    if(tp1<tp2)        Dct=tp1;        fgc=0;    else        Dct=tp2;        fgc=1;    end    %d    tp1=dist(T,[1 1 0],3)+Db;    tp2=dist(T,[1 0 1],3)+Dd;    if(tp1<tp2)        Ddt=tp1;        fgd=0;    else        Ddt=tp2;        fgd=1;    end    %更新幸存路径    %a    if(fga==0)        Path_t(Pa,:)=Path(Pa,:);        Path_t(Pa,3+i)=0;        Da=Dat;    else        Path_t(Pa,:)=Path(Pc,:);        Path_t(Pa,3+i)=0;        Da=Dat;    end    %b    if(fgb==0)        Path_t(Pb,:)=Path(Pa,:);        Path_t(Pb,3+i)=1;        Db=Dbt;    else        Path_t(Pb,:)=Path(Pc,:);        Path_t(Pb,3+i)=1;        Db=Dbt;    end    %c    if(fgc==0)        Path_t(Pc,:)=Path(Pb,:);        Path_t(Pc,3+i)=0;        Dc=Dct;    else        Path_t(Pc,:)=Path(Pd,:);        Path_t(Pc,3+i)=0;        Dc=Dct;    end    %d    if(fgd==0)        Path_t(Pd,:)=Path(Pb,:);        Path_t(Pd,3+i)=1;        Dd=Ddt;    else        Path_t(Pd,:)=Path(Pd,:);        Path_t(Pd,3+i)=1;        Dd=Ddt;    end    %    Path(Pa,:)=Path_t(Pa,:);    Path(Pb,:)=Path_t(Pb,:);    Path(Pc,:)=Path_t(Pc,:);    Path(Pd,:)=Path_t(Pd,:);endk=min([Da Db Dc Dd]);if(k==Da)    Y=Path(Pa,:);endif(k==Db)    Y=Path(Pb,:);endif(k==Dc)    Y=Path(Pc,:);endif(k==Dd)    Y=Path(Pd,:);endend

dist.m

%汉明距离function d = dist(X1,X2,n)sum=0;for i=1:n    if X1(i)~=X2(i)        sum=sum+1;    endendd=sum;end

scramble.m

%加扰function Y = scramble( X )n=length(X);M=mxulie();F(n)=0;for i=1:n;   F(i)=xor(X(i),M(i));endY=F;end

drawSig.m

%用于绘制信号图function drawSig(X)n=length(X);G(n*100)=0;for i=1:n    if X(i)==0        for j=1:100            G((i-1)*100+j)=0;        end    else        for j=1:100            G((i-1)*100+j)=1;        end    endendplot(G);axis([0 n*100 -2 2]);end

0 0