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

📄 encoding.cpp

📁 这个是数据打孔重传程序的源代码
💻 CPP
字号:
/**********************************************/
/* LDPC Encoder
/* int * EncodingF(struct BasicParaS * ctrl, int * input)
/*      Written by: Ouyang Ziyue,
/*            Date: Dec 21st, 2007,
/*        Function: It encodes the input info bits according to the type of LDPC code selected ahead.
/* Input parameter:
/*        The length of N is the length of a code word,
/*        The input includes all the input info bits
/*		  The numRetrans is needed.
/* Output parameter:
/*        An INT set which includes the codeword is outputed.
/* Note:
/*        ctrl should be built before this function is called.
/**********************************************/

#include "parameter_sets.h"


int * EncodingF(struct BasicParaS * ctrl, int * input, int numRetrans)
{
	//////////////////////////////////////////////////////////////////////////
	//Declaration
	int i,j,k,h,m,n;
	int * temp1 = new int[ctrl->zfactor];
	int * inputin = new int[ctrl->numInBits];
	int * encodeout = new int[ctrl->numOutBits];
	int * encodedout ;
    int prefix = ctrl->numInBits - ctrl->codeK;
	if (numRetrans==0)
		encodedout = new int[ctrl->codeN];	//初始发射所有数据块
	else
		encodedout = new int[ctrl->codeM];	//重发时仅发送校验块

	//////////////////////////////////////////////////////////////////////////
	//Initializing
	// Initialize temp1 of the uniform matrix 
	for (k=0; k<ctrl->zfactor; k++)
		*(temp1+k) = 0;

	// Initialize the input of the encoder
	for (i=0; i<prefix; i++)
		*(inputin+i) = 0;
	for (i=0; i<ctrl->codeK; i++)
		*(inputin+i+prefix) = *(input+i);
	
#ifdef DP1					//用第一种方式打孔
	if (numRetrans != 0)
	{
		k=ctrl->codeK/ctrl->maxRetrans;
		for (i=1; i<=ctrl->maxRetrans; i++)
		{
			if(i == numRetrans)
				continue;
			for (j=0; j<k; j++)
				*(inputin + (i-1)*k + j) = 0;
		}
	}

#endif

#ifdef DP2			//用第二种方式打孔
	if (numRetrans != 0)
	{
		int * inputin2 = new int[ctrl->numInBits];
		for (i=0; i<ctrl->numInBits; i++)
			*(inputin2+i) = 0;

		k=ctrl->codeK/ctrl->maxRetrans;
		for (i=0; i<k; i++)
			*(inputin2 + (ctrl->maxRetrans-numRetrans)*k + i) = * (inputin+ (ctrl->maxRetrans-numRetrans)*k + i);
		for (i=0; i<ctrl->numInBits; i++)
			*(inputin+i)=*(inputin2+i);
		delete [] inputin2;
	}
#endif

#ifdef DEBUG
	printf("the input data is...\n");
	for (i=0; i<ctrl->numInBits/24; i++) 
	{
		for (j=0; j<24; j++)
		{
			printf("%2d", inputin[i*24+j]);
		}
		printf("\n");
	}


#endif

	//Initialize the output of the encoder
	for (i=0; i<ctrl->numInBits; i++)
	{
		*(encodeout+i) = *(inputin+i);
	}
	
	//////////////////////////////////////////////////////////////////////////
	//Encoding
	//The first step 
	for (i=1; i<ctrl->numRows-1; i++)
	{
		if (ctrl->expandedH[i][ctrl->numCols-ctrl->numRows]>=0 )
		{
				n=ctrl->expandedH[i][ctrl->numCols-ctrl->numRows];
				break;
		}
	}
	
	for (j=0; j<ctrl->numCols-ctrl->numRows; j++)
	{
		for (i=0; i<ctrl->numRows; i++)
		{
			if (ctrl->expandedH[i][j]>=0)
			{
				h = ctrl->expandedH[i][j];
				for (k=0; k<ctrl->zfactor; k++)
				{
					m = k + n;
					m %= ctrl->zfactor;
					*(temp1+m) += * (inputin + j*ctrl->zfactor + h);
					*(temp1+m) %= 2;
					h++;
					h %= ctrl->zfactor;
				}
			}
		}
	}
	for (k=0; k<ctrl->zfactor; k++)
	{
		*(encodeout+ctrl->numInBits +k) = *(temp1+k);
		*(temp1+k) = 0;
	}

	//The second step
	for (i=1; i<ctrl->numRows; i++)
	{
		for (j=0; j<ctrl->numCols-ctrl->numRows+i; j++ )
		{
			if (ctrl->expandedH[i-1][j] >= 0)
			{
				h = ctrl->expandedH[i-1][j];
				for (k=0; k<ctrl->zfactor; k++) 
				{
					*(temp1+k) += *(encodeout+j*ctrl->zfactor+h);
					*(temp1+k) %= 2;
					h++;
					h %= ctrl->zfactor;
				}
			}
		}
		for (k=0; k<ctrl->zfactor; k++)
		{
			*(encodeout+ctrl->numInBits+ctrl->zfactor*i+k) = *(temp1+k);
			*(temp1+k) = 0;
		}
	}
	
#ifdef DEBUG
	//Check whether the encoded output(encodeout) is correct
	int * temp = new int[ctrl->numChk];
	for (i=0; i<ctrl->numChk; i++)
	{
		*(temp+i) = 8;
	}
	for (i=0; i<ctrl->numRows; i++)
	{
		for (j=0; j<ctrl->numCols; j++)
		{
			if (ctrl->expandedH[i][j]>=0)
			{
				h = ctrl->expandedH[i][j];
				for (k=0; k<ctrl->zfactor; k++)
				{
					* (temp + i*ctrl->zfactor +k) += * (encodeout + j*ctrl->zfactor +h);
					* (temp + i*ctrl->zfactor +k) %= 2;
					h++;
					h %= ctrl->zfactor;
				}
			}
		}
	}
	printf("Check the encoded result...\n");
	k = ctrl->numChk/24;
	h = ctrl->numChk%24;
	for (i=0; i<k; i++)
	{
		for (j=0; j<24; j++)
		{
			printf("%2d", *(temp+i*24+j));
		}
		printf("\n");
	}
	for (i=0; i<h; i++) 
	{
		printf("%2d", *(temp+k*24+i));
	}
	printf("\n");

	delete []temp;
	printf("The check bits before punctured are ..\n");
	k = ctrl->numChk/24;
	h = ctrl->numChk%24;
	for (i=0; i<k; i++) 
	{
		for (j=0; j<24; j++)
		{
			printf("%2d", encodeout[ctrl->numInBits+i*24+j]);
		}
		printf("\n");
	}
	for (i=0; i<h; i++) 
	{
		printf("%2d", encodeout[ctrl->numInBits+k*24+i]);
	}
	if (h)
	{
		printf("\n");
	}

#endif

	//the third step
	if (numRetrans==0)
		for (i=0; i<ctrl->codeN; i++)
			*(encodedout + i) = * (encodeout + i);
	else
		for(i=0; i<ctrl->codeM; i++)
			*(encodedout + i) = * (encodeout + ctrl->codeK + i);


	//////////////////////////////////////////////////////////////////////////
	//DEBUG
//#ifdef DEBUG
//	printf("The check bits are ..\n");
//	k = ctrl->codeM/24;
//	h = ctrl->codeM%24;
//	for (i=0; i<k; i++) 
//	{
//		for (j=0; j<24; j++)
//		{
//			printf("%2d", *(encodedout+ctrl->codeK+i*24+j));
//		}
//		printf("\n");
//	}
//	for (i=0; i<h; i++) 
//	{
//		printf("%2d", *(encodedout+ctrl->codeK+k*24+i));
//	}
//	if (h)
//	{
//		printf("\n");
//	}
//#endif

	delete []encodeout;
	delete []temp1;
	delete []inputin;

	return encodedout;


}

⌨️ 快捷键说明

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