⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 class.cpp

📁 一个很好的vitebi编译码程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//---------------------------------------------------------------------------
#include <stdlib.h>
#include <math.h>
#include "class.h"
#pragma hdrstop

//******************************************************************
//名称:        MCreatMatrix
//功能:        产生生成距阵
//******************************************************************
__fastcall MCreatMatrix::MCreatMatrix()
{
        Buffer=NULL;
        Buffer=new Byte[1];
        Row=0;
        Column=0;
}

__fastcall MCreatMatrix::~MCreatMatrix()
{
        if(Buffer!=NULL)
                delete Buffer;
}

void MCreatMatrix::Input(int n0,int k0,int m,int *Member)
{
        int i,j,k,g;
        int NewNum;
        Row=k0;
        Column=n0*(m+1);
        if(Buffer!=NULL)
                delete Buffer;
        Buffer=new Byte[Row*Column];
        for(i=0;i<k0;i++)
        {
                for(j=0;j<n0;j++)
                {
                        g=Member[i*n0+j];
                        for(k=0;k<m+1;k++)
                        {
                                NewNum=g&0x01;
                                Buffer[i*Column+j+(m-k)*n0]=(unsigned char )NewNum;
                                g=g>>1;
                        }
                }
        }
}

//******************************************************************
//名称:        MTransferState
//功能:        生成状态转移表
//******************************************************************
__fastcall MTransferState::MTransferState()
{
        Buffer=NULL;
        Buffer=new int[1];
        num=1;
        zh1=new Byte[1];
        zh2=new Byte[1];
        in=new int[1];
        out=new int[1];
        Zh1Num=1;
        Zh2Num=1;
        InNum=1;
        OutNum=1;
}
__fastcall MTransferState::~MTransferState()
{
        if(Buffer!=NULL)
                delete Buffer;
        Row=0;
        Column=0;
        if(zh1!=NULL)
                delete zh1;
        if(zh2!=NULL)
                delete zh2;
        if(in!=NULL)
                delete in;
        if(out!=NULL)
                delete out;
        Zh1Num=0;
        Zh2Num=0;
        InNum=0;
        OutNum=0;
}
void MTransferState::Input(int n0,int k0,int m,unsigned char *Matrix)
{
	int i,j,k,l,site,num2,d,out2;
	for(i=0,Row=1;i<m*k0;i++){//状态表行数
		Row=Row*2;
	}
	for(i=0,num2=1;i<k0;i++){//状态表列数
		num2=num2*2;
	}
        Column=num2*3;
	if(num!=Row*Column){   //申请地址
                delete Buffer;
                Buffer=NULL;
                Buffer=new int[num=Row*Column];
	}
	for(i=0;i<num;i++){
		Buffer[i]=1000;
	}
        if(OutNum!=n0)
        {
                delete out;
                out=new int[n0];
                OutNum=n0;
        }
        if(InNum!=k0)
        {
                delete in;
                in=new int[k0];
                InNum=k0;
        }
        if(Zh1Num!=(k0*m))
        {
                delete zh1;
                zh1=new Byte[k0*m];
                Zh1Num=k0*m;
        }
        if(Zh2Num!=(k0*m))
        {
                delete zh2;
                zh2=new Byte[k0*m];
                Zh2Num=k0*m;
        }
	for(i=0;i<Row;i++){//对每个状态循环
		for(k=m*k0-1,j=i;k>=0;k--){//将状态送入状态寄存器
			zh1[k]=(unsigned char)(j&1);
                        j=j>>1;
		}
		for(d=0;d<num2;d++){         //对各种输入循环
			for(k=k0-1,j=d;k>=0;k--){//将输入变为二进制
				in[k]=j&1;
				j=j>>1;
			}
			for(k=0;k<n0;k++){//清零
				out[k]=0;
			}
    		        for(k=0;k<k0;k++){           //对一个输入码字的每bit循环
        		        for(j=0;j<n0;j++){               //对一个输出码字的每bit循环
		    	         for(l=m-1;l>=0;l--){        //对一个移存器的每个单元循环
	    	    		        site=k*(m+1)*n0+(l+1)*n0+j;//g(k,n)在基本生成矩阵的位置
        		                out[j]=out[j]+zh1[k*m+l]*Matrix[site];
                                    }
                                    out[j]=out[j]+in[k]*Matrix[k*(m+1)*n0+j];
                                }
                        }
	               	for(k=0;k<n0;k++){
	      	        	out[k]=out[k]%2;
        	     	}
	            	for(k=n0-1,out2=0,l=1;k>=0;k--){//将输出转化成十进制
       	   	            if(out[k]!=0){
        	  		    out2=out2|l;
                            }
		            l=l<<1;
        	       	}
	            	for(k=0;k<k0;k++){
                                for(l=m-1;l>0;l--){       //更新移存器
                                        zh2[k*m+l]=zh1[k*m+l-1];
                                }
	        	        zh2[k*m]=(unsigned char)in[k];
                        }
        	        for(k=m*k0-1,l=1,site=0;k>=0;k--){//将移存器状态转化成十进制
                                if(zh2[k]!=0){
                                        site=site|l;
                                }
                                l=l<<1;
                        }
                        for(k=0;k<num2;k++){
	        		    if(Buffer[site*num2*3+k*3+2]==1000){
                                           Buffer[site*num2*3+k*3]=i;
		       	                   Buffer[site*num2*3+k*3+1]=out2;
                                           Buffer[site*num2*3+k*3+2]=d;
        				   break;
                                    }
                        }
                }
        }
out1:

	return ;
}


//******************************************************************
//名称:        MVitebiDecode
//功能:        通用维特比译码器
//******************************************************************
__fastcall MVitebiDecode::MVitebiDecode()
{
       Buffer=new unsigned char[1];
       BufBitLen=8;
       ByteBuffer=new Byte[1];
       ByteBufLen=1;
       PathStore=new unsigned char[1];
       Measurement=new int[1];
       NowStoreLength=0;
       NowStoreSite=0;
       Old=0;
       RemainBit=new Byte[1];
       RemainBitLen=0;
       State=new int[1];
       ByteCodeIn=new Byte[1];
}

__fastcall MVitebiDecode::~MVitebiDecode()
{
       if(Buffer!=NULL)
               delete Buffer;
       if(PathStore!=NULL)
               delete PathStore;
       if(Measurement!=NULL)
               delete Measurement;
       if(RemainBit!=NULL)
               delete RemainBit;
       if(ByteBuffer!=NULL)
               delete ByteBuffer;
       if(State!=NULL)
               delete State;
       if(ByteCodeIn!=NULL)
               delete ByteCodeIn;
}

void MVitebiDecode::Initial(int n,int k,int m,int *Member)
{
        int i;
        n0=n;
        k0=k;
        NowStoreLength=0;
        NowStoreSite=0;
        Old=0;

        CreatMatrix.Input(n,k,m,Member);
        TransferState.Input(n,k,m,CreatMatrix.Buffer);
        if(State!=NULL)
                delete State;
        State=new int[(TransferState.Row)*(TransferState.Column)];
        memcpy(State,TransferState.Buffer,sizeof(int)*TransferState.Row*TransferState.Column);
//        State=TransferState.Buffer;

	for(i=0,Row=1;i<m*k0;i++){//状态表行数
		Row=Row*2;
	}
	for(i=0,PathNum=1;i<k0;i++){//状态表列数
		PathNum=PathNum*2;
	}
        BitSite=PathNum/2;
        Column=PathNum*3;
        PathStoreLength=m*7;     //路径存储长度
        if(PathStore!=NULL)      //申请路径寄存器并清零
        {
                delete PathStore;
        }
        PathStore=new Byte[2*PathStoreLength*Row];
        memset(PathStore,0,2*PathStoreLength*Row);
        if(Measurement!=NULL)      //申请比特度量寄存器并清零
        {
                delete Measurement;
        }
        Measurement=new int[2*Row];
        memset(Measurement,0,sizeof(int)*2*Row);
        if(RemainBit!=NULL)
        {
                delete RemainBit;
        }
        RemainBit=new Byte[n0-1];
        RemainBitLen=0;
        if(ByteCodeIn!=NULL)
               delete ByteCodeIn;
        ByteCodeIn=new Byte[n0];
}

void MVitebiDecode::ByteInput(char *Data,int ByteLength)
{
        int i,j,k,b,l,Counter,ByteNum,RemainBitLen1;
        Byte CodeMeasurement,m;
        int Max,MaxMeasurement,MaxPath,MaxFrontState,POldStartSite,PNewStartSite,MOldStartSite,MNewStartSite;
        unsigned char CodeIn,Code;
        if(ByteLength==0)
        {
                if(ByteBuffer!=NULL)
                        delete ByteBuffer;
                ByteBuffer=NULL;
                ByteBufLen=0;
                return;
        }
        i=((ByteLength+RemainBitLen-PathStoreLength*n0+NowStoreLength*n0)/n0)*k0;//输出比特长度
        RemainBitLen1=(ByteLength+RemainBitLen)%n0;
        if(ByteBufLen!=i)
        {
                if(ByteBuffer!=NULL)
                        delete ByteBuffer;
                ByteBuffer=NULL;
                if(i>0){
                        ByteBufLen=i;
                        ByteBuffer=new Byte[ByteBufLen];
                        memset(ByteBuffer,0,ByteBufLen);
                }
                else{
                        ByteBufLen=0;
                }
        }
        else{
                if(ByteBufLen!=0)
                {
                        memset(ByteBuffer,0,ByteBufLen);
                }
        }
        Counter=0;
        for(i=-RemainBitLen;i<ByteLength-RemainBitLen1;i=i+n0)
        {
                New=(Old+1)%2;
                POldStartSite=Old*PathStoreLength*Row;//PathStore[]的起始位置
                PNewStartSite=New*PathStoreLength*Row;//PathStore[]的起始位置
                MOldStartSite=Old*Row;   // Measurement[]的起始位置
                MNewStartSite=New*Row;   //(此四变量是为了减少下面循环的运算量)
                MaxMeasurement=0;        //寻找最大比特度量的中间变量(所有状态)
//取编码数据
                if(i<0)
                {
                        for(j=0;j<RemainBitLen;j++)
                        {
                                ByteCodeIn[j]=RemainBit[j];
                        }
                        for(j=RemainBitLen;j<n0;j++)
                        {
                                ByteCodeIn[j]=Data[i+j];
                        }
                }
                else
                {
                        for(j=0;j<n0;j++)
                        {
                                ByteCodeIn[j]=Data[i+j];
                        }
                }
                for(j=0;j<Row;j++)    //对每个状态进行计算
                {
                        Max=0;                 //寻找最大比特度量路径(一个状态)的中间变量
                        for(k=0;k<PathNum;k++)//对进入一个状态的每条路径进行计算比特度量
                        {
                                Code=State[j*Column+k*3+1];     //取子码
                                CodeMeasurement=0;
                                for(l=0;l<n0;l++)
                                {
                                        if(ByteCodeIn[l]==((Code>>(n0-1-l))&0x01))
                                                CodeMeasurement=CodeMeasurement+1;
                                }
                 	        if((b=CodeMeasurement+Measurement[MOldStartSite+State[j*Column+k*3]])>Max)//求bit度量最大的前态,输出及其度量
                                {
                                        Max=b;
                                        MaxPath=k;      //最大比特度量路径(一个状态)
                                }
                        }
                        Measurement[MNewStartSite+j]=Max;//更新度量寄存器
                        if(Max>MaxMeasurement)          //Max为最大比特度量(一个状态)
                        {
                                MaxMeasurement=Max;     //MaxMeasurement为最大比特度量(所有状态)
                                MaxFrontState=State[j*Column+MaxPath*3]; //最大度量状态的前态(所有状态)
                        }
                        memcpy(&PathStore[PNewStartSite+j*PathStoreLength],&PathStore[POldStartSite+State[j*Column+MaxPath*3]*PathStoreLength],PathStoreLength);
                        PathStore[PNewStartSite+j*PathStoreLength+NowStoreSite]=State[j*Column+MaxPath*3+2];//最大比特度量的译码
                }                                               //NowStoreSite为PathStore[new]里进行更新的位置,同时也为PathStore[old]里进行输出的位置
        	if(MaxMeasurement>150000)               //防止bit度量溢出
                {
	             for(j=0;j<Row;j++)
	                    Measurement[MNewStartSite+j]=Measurement[MNewStartSite+j]-100000;
	        }
           	if(NowStoreLength==PathStoreLength)  //当路径存储器满时,输出结果
                {
                        b=POldStartSite+MaxFrontState*PathStoreLength+NowStoreSite;//在PathStore[Old]里应输出结果的位置
                        for(j=0,m=BitSite;j<k0;j++)
                        {
                                if((PathStore[b]&m)!=0)
                                        ByteBuffer[Counter]=1;
                                Counter++;
                                m=m>>1;
                        }
                }
                Old=New;
                NowStoreSite=(NowStoreSite+1)%PathStoreLength;    //更新
                if(NowStoreLength<PathStoreLength)              //存储记数加一
                {
	                NowStoreLength++;
                }
        }
//保留剩余数据
        RemainBitLen=(ByteLength+RemainBitLen)%n0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -