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

📄 btcencode.c

📁 wimax802.16e中的BTC编码仿真程序
💻 C
字号:
/*
*********************************************************************************
* Copyright (c) National Mobile Communications Research Laboratory. 
* All rights reserved.
* 
* FILE NAME : BTCencode.c
* ABSTRUCT:
*	This file is the C file for BTC encoding.
*	
* AUTHOR:	Zhang Tao	2007-02-12
*
*********************************************************************************
*/

/*
*********************************************************************************
*                               INCLUDE FILES
*********************************************************************************
*/

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include "..\..\GlobalDef.h"
#include "..\..\GlobalVar.h"
#include "BTCstruct.h"
#include "BTCinit.h"
/*
*********************************************************************************
*                              Function Definition
*********************************************************************************
*/

/*
*********************************************************************************
* NAME:		Line_Blk_Encode  
* PURPOSE:	perform the liner block codes encoding.
*
* Input:	bit_in[]:  the information bits for encoding.
*			bit_len: the length of the information bits.
*				
* Output:	encode_out[]: the encoded bits.
*					
* AUTHOR: 	Zhang Tao	2007-02-12	
* 
*********************************************************************************
*/
void Line_Blk_Encode(int bit_in[],int bit_len,int encode_out[])
{
	int shift_reg[7] = {0};
	int check_len = 0;//the length of the check bits.
	int feedback = 0;//the feedback of the shift register.
	int index = 0;

	memcpy(encode_out,bit_in,bit_len*sizeof(int));

	switch(bit_len)
	{
	case 11: 
		check_len = 5; 
		for(index=0;index<bit_len;index++)
		{
			feedback = shift_reg[0] ^ bit_in[index];
			shift_reg[0] = shift_reg[1];
			shift_reg[1] = shift_reg[2];
			shift_reg[2] = feedback ^ shift_reg[3];
			shift_reg[3] = feedback;
			shift_reg[4] ^= bit_in[index];
		}
		for(index=0;index<4;index++)
		{
			shift_reg[4] ^= shift_reg[index];
		}
		memcpy((encode_out+bit_len),shift_reg,check_len*sizeof(int));
		break;
	case 26: 
		check_len = 6; 
		for(index=0;index<bit_len;index++)
		{
			feedback = shift_reg[0] ^ bit_in[index];
			shift_reg[0] = shift_reg[1];
			shift_reg[1] = shift_reg[2];
			shift_reg[2] = feedback ^ shift_reg[3];
			shift_reg[3] = shift_reg[4];
			shift_reg[4] = feedback;
			shift_reg[5] ^= bit_in[index];
		}
		for(index=0;index<5;index++)
		{
			shift_reg[5] ^= shift_reg[index];
		}
		memcpy((encode_out+bit_len),shift_reg,check_len*sizeof(int));
		break;
	case 57: 
		check_len = 7; 
		for(index=0;index<bit_len;index++)
		{
			feedback = shift_reg[0] ^ bit_in[index];
			shift_reg[0] = shift_reg[1];
			shift_reg[1] = shift_reg[2];
			shift_reg[2] = shift_reg[3];
			shift_reg[3] = shift_reg[4];
			shift_reg[4] = feedback ^ shift_reg[5];
			shift_reg[5] = feedback;
			shift_reg[6] ^= bit_in[index];
		}
		for(index=0;index<6;index++)
		{
			shift_reg[6] ^= shift_reg[index];
		}
		memcpy((encode_out+bit_len),shift_reg,check_len*sizeof(int));
		break;
	default:
		check_len = 1;
		for(index=0;index<bit_len;index++)
			shift_reg[0] ^= bit_in[index];
		encode_out[bit_len] = shift_reg[0];
	}
}

/*
*********************************************************************************
* NAME:		IntMatrixTrans
* PURPOSE:	perform the transposition of a integer matrix.
*
* Input:	data_in[]:  data input.
*			row : the row number of the matrix.
*			col : the column number of the matrix.
*				
* Output:	data_out[]: data output.
*					
* AUTHOR: 	Zhang Tao	2007-02-12	
* 
*********************************************************************************
*/
void IntMatrixTrans(int data_in[],int row,int col,int data_out[])
{
	int index1;
	int index2;
	int len1 = 0;
	int len2 = 0;

	for(index1=0;index1<row;index1++)
	{
		len2 = 0;
		for(index2=0;index2<col;index2++)
		{
			data_out[index1+len2] = data_in[len1+index2];
			len2 += row;
		}
		len1 += col;
	}
}
/*
*********************************************************************************
* NAME:		oneBTCencode
* PURPOSE:	one BTC block encoding without slot concatenation.
*
* Input:	data_in[]:  the information bits for encoding.
*				
* Output:	data_out[]: one block data output.
*			BTC :  the parameter for BTC.
*					
* AUTHOR: 	Zhang Tao	2007-02-12	
* 
*********************************************************************************
*/
void oneBTCencode(int data_in[],int data_out[],BTCstruct BTC)
{
	int *encode_seq; //the sequence contains message bits and zero bits.
	int *tmp_seq1; //middle sequence for row or column encoding.
	int *tmp_seq2; //middle sequence for row or column encoding.
	int index1,index2;
	int len;
	int i;

	encode_seq = (int *)malloc((BTC.kx * BTC.ky) * sizeof(int));

	//generate the encode sequence.
	memset(encode_seq,0,(BTC.kx * BTC.ky) * sizeof(int));

	//first row of the message.
	index1 = BTC.Iy * BTC.kx + BTC.Ix + BTC.B + BTC.Q;
	len = BTC.kx - BTC.Ix - BTC.B - BTC.Q;
	memcpy((encode_seq+index1),data_in,len*sizeof(int));
	index1 += len + BTC.Ix;
	index2 = len;

	//remaining rows of the message.
	len = BTC.kx - BTC.Ix;
	for(i=BTC.Iy+1;i<BTC.ky;i++)
	{
		memcpy((encode_seq+index1),(data_in+index2),len*sizeof(int));
		index1 += BTC.kx;
		index2 += len;
	}
	
	//row encoding.
	tmp_seq1 = (int *)malloc((BTC.nx * BTC.ny) * sizeof(int));
	index1 = BTC.kx * BTC.Iy;
	index2 = BTC.nx * BTC.Iy;
	memset(tmp_seq1,0,index2 * sizeof(int));	
	for(i=BTC.Iy;i<BTC.ky;i++)
	{
		Line_Blk_Encode((encode_seq+index1),BTC.kx,(tmp_seq1+index2));
		index1 += BTC.kx;
		index2 += BTC.nx;
	}

	//matrix transposition
	tmp_seq2 = (int *)malloc((BTC.nx * BTC.ny) * sizeof(int));
	IntMatrixTrans(tmp_seq1,BTC.ky,BTC.nx,tmp_seq2);

	//column encoding.
	index1 = BTC.ky * BTC.Ix;
	index2 = BTC.ny * BTC.Ix;
	memset(tmp_seq1,0,index2 * sizeof(int));	
	for(i=BTC.Ix;i<BTC.nx;i++)
	{
		Line_Blk_Encode((tmp_seq2+index1),BTC.ky,(tmp_seq1+index2));
		index1 += BTC.ky;
		index2 += BTC.ny;
	}
	
	//matrix transposition
	IntMatrixTrans(tmp_seq1,BTC.nx,BTC.ny,tmp_seq2);

	//output first row
	index1 = BTC.Iy * BTC.nx + BTC.Ix + BTC.B;
	len = BTC.nx - BTC.Ix - BTC.B;
	memcpy(data_out,(tmp_seq2+index1),len*sizeof(int));
	index1 += len + BTC.Ix;
	index2 = len;

	//output remaining rows
	len = BTC.nx - BTC.Ix;
	for(i=BTC.Iy+1;i<BTC.ny;i++)
	{
		memcpy((data_out+index2),(tmp_seq2+index1),len*sizeof(int));
		index1 += BTC.nx;
		index2 += len;
	}

	free(encode_seq);
	free(tmp_seq1);
	free(tmp_seq2);
}

/*
*********************************************************************************
* NAME:		BTCencode
* PURPOSE:	encoding and concatenation of slots.
*
* Input:	data_in[]:  the information data for encoding.
*			nslot: the slot number for the encoded and modulated data.
*			rep: the repetition factor.
*			fec: the struct for the FEC type.
*				
* Output:	data_out[]: the data output.
*					
* AUTHOR: 	Zhang Tao	2007-02-12	
* 
*********************************************************************************
*/
void BTCencode(int data_in[],int nslot,int rep,FEC_TYPE fec,int data_out[])
{
	int n_slot = 0;//n_slot=nslot/rep;
	int cnt_block = 0;
	int para_j = 0;//parameter dependent on the modulation and FEC rate
	int para_k = 0;//floor(n_slot/para_j).
	int para_m = 0;//n_slot modulo para_j.
	int *pdata_in = data_in;
	int *pdata_out = data_out;
	int slot_num = 0;
	BTCstruct BTC; //BTC parameter.
	int encoded_len;
	
	switch(fec.modID)
	{
		case 2:
			if(fec.rateID==1)
				para_j = 6;
			else
				para_j = 4;
			break;
		case 4:
			if(fec.rateID==1)
				para_j = 3;
			else
				para_j = 2;
			break;
		case 6:
			if(fec.rateID==1)
				para_j = 2;
			else if(fec.rateID==2)
				para_j = 1;
			else
				para_j = 1;
			break;
	}


	n_slot = nslot / rep;
	para_k = n_slot / para_j;
	para_m = n_slot % para_j;

	if(n_slot<=para_j)
	{
		encoded_len = n_slot * fec.modID * 48;
		BTCInit(encoded_len,fec.rateID,&BTC);
		oneBTCencode(pdata_in,pdata_out,BTC);
	}
	else if(n_slot>para_j && para_m==0)
	{
		encoded_len = para_j * fec.modID * 48;
		BTCInit(encoded_len,fec.rateID,&BTC);
		for(cnt_block=0;cnt_block<para_k;cnt_block++)
		{
			pdata_in = data_in + cnt_block * BTC.msg_len;
			oneBTCencode(pdata_in,pdata_out,BTC);
			pdata_out += encoded_len;
		}
	}
	else
	{
		encoded_len = para_j * fec.modID * 48;
		BTCInit(encoded_len,fec.rateID,&BTC);
		for(cnt_block=0;cnt_block<para_k-1;cnt_block++)
		{
			pdata_in = data_in + cnt_block * BTC.msg_len;
			oneBTCencode(pdata_in,pdata_out,BTC);
			pdata_out += encoded_len;
		}

		pdata_in += BTC.msg_len;
		slot_num = (para_m+para_j) / 2;
		encoded_len = slot_num * fec.modID * 48;
		BTCInit(encoded_len,fec.rateID,&BTC);
		oneBTCencode(pdata_in,pdata_out,BTC);
		pdata_out += encoded_len;

		pdata_in += BTC.msg_len;
		slot_num = n_slot - para_j*(para_k-1) - (para_m+para_j) / 2;
		encoded_len = slot_num * fec.modID * 48;
		BTCInit(encoded_len,fec.rateID,&BTC);
		oneBTCencode(pdata_in,pdata_out,BTC);		
	}

}




⌨️ 快捷键说明

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