📄 btcdecode.c
字号:
/*
*********************************************************************************
* Copyright (c) National Mobile Communications Research Laboratory.
* All rights reserved.
*
* FILE NAME : BTCdecode.c
* ABSTRUCT:
* This file is the C file for BTC decoding.
*
* AUTHOR: Zhang Tao 2007-02-13
*
*********************************************************************************
*/
/*
*********************************************************************************
* INCLUDE FILES
*********************************************************************************
*/
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include "..\..\GlobalDef.h"
#include "..\..\GlobalVar.h"
#include "BTCstruct.h"
#include "BTCinit.h"
#include "chase.h"
/*
*********************************************************************************
* Function Definition
*********************************************************************************
*/
/*
*********************************************************************************
* NAME: DBMatrixTrans
* PURPOSE: perform the transposition of a double style 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 DBMatrixTrans(double data_in[],int row,int col,double 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: GenZeroSoft
* PURPOSE: obtain the soft value of the zero bit.
*
* Input: rowsoft[]: soft value of the sequence in row order.
* BTC : the parameter for BTC.
*
* Output:
*
* AUTHOR: Zhang Tao 2007-02-13
*
*********************************************************************************
*/
void GenZeroSoft(double rowsoft[],BTCstruct BTC)
{
double softvalue = 2.0;
int index;
int i;
int j;
index = BTC.nx * BTC.Iy + BTC.Ix + BTC.B + BTC.Q;
for(i=0;i<index;i++)
{
rowsoft[i] = softvalue;
}
index = BTC.nx * (BTC.Iy + 1);
for(j=BTC.Iy+1;j<BTC.ny;j++)
{
for(i=0;i<BTC.Ix;i++)
{
rowsoft[index+i] = softvalue;
}
index += BTC.nx;
}
}
/*
*********************************************************************************
* NAME: oneBTCdecode
* PURPOSE: one BTC block decoding without slot concatenation.
*
* Input: code_in[]: the code for decoding.
* BTC : the parameter for BTC.
*
* Output: data_out[]: one block data output.
*
* AUTHOR: Zhang Tao 2007-02-13
*
*********************************************************************************
*/
void oneBTCdecode(double code_in[],int data_out[],BTCstruct BTC)
{
int matrix_len = BTC.nx * BTC.ny; //the whole length of one BTC block.
double *matrix_seq; //the whole sequence with received code and zero bits.
double zero_soft = 1.0; //the soft value for the zero bits.
int *mid_seq; //the middle decoded code vector.
double *rowsoft; //extrinsic information from row decoding.
double *colsoft; //extrinsic information from column decoding.
double *reliable;//the sequence for the reliable value.
double *matrix_seq_trans; //the transposition of the matrix_seq.
double alfa1[4] = {0.0,0.3,0.7,1.0};
double alfa2[4] = {0.2,0.5,0.9,1.0};
double beta1[4] = {0.2,0.6,1.0,1.0};
double beta2[4] = {0.4,0.8,1.0,1.0};
int iterate = 0; //the counter for the iteration number.
int index1,index2;
int len;
int i,j;
//generate the matrix_seq
matrix_seq = (double *)malloc(matrix_len * sizeof(double));
index1 = BTC.nx * BTC.Iy + BTC.Ix + BTC.B + BTC.Q;
index2 = BTC.Q;
for(i=0;i<index1;i++)
{
matrix_seq[i] = zero_soft;
}
//first row
len = BTC.nx - (BTC.Ix + BTC.B + BTC.Q);
memcpy(matrix_seq+index1,code_in+index2,len*sizeof(double));
//remaining rows
index1 += len;
index2 += len;
len = BTC.nx - BTC.Ix;
for(i=BTC.Iy+1;i<BTC.ny;i++)
{
for(j=0;j<BTC.Ix;j++)
{
matrix_seq[index1+j] = zero_soft;
}
index1 += BTC.Ix;
memcpy((matrix_seq+index1),(code_in+index2),len*sizeof(double));
index1 += len;
index2 += len;
}
//initialization
mid_seq = (int *)malloc(matrix_len * sizeof(int));
rowsoft = (double *)malloc(matrix_len * sizeof(double));
colsoft = (double *)malloc(matrix_len * sizeof(double));
memset(colsoft, 0, matrix_len * sizeof(double));
reliable = (double *)malloc(matrix_len * sizeof(double));
matrix_seq_trans = (double *)malloc(matrix_len * sizeof(double));
DBMatrixTrans(matrix_seq,BTC.ny,BTC.nx,matrix_seq_trans);
//iterative decoding
while(iterate<4)
{
DBMatrixTrans(colsoft,BTC.nx,BTC.ny,rowsoft);
GenZeroSoft(rowsoft,BTC);
//generate reliable value sequence.
for(i=0;i<matrix_len;i++)
{
reliable[i] = matrix_seq[i] + alfa1[iterate] * rowsoft[i];
}
//row decoding
index1 = BTC.nx * BTC.Iy;
for(i=BTC.Iy;i<BTC.ny;i++)
{
Chase((reliable+index1),0,beta1[iterate],BTC,(mid_seq+index1),(rowsoft+index1));
index1 += BTC.nx;
}
GenZeroSoft(rowsoft,BTC);
DBMatrixTrans(rowsoft,BTC.ny,BTC.nx,colsoft);
//generate reliable value sequence.
for(i=0;i<matrix_len;i++)
{
reliable[i] = matrix_seq_trans[i] + alfa2[iterate] * colsoft[i];
}
//column decoding
index1 = BTC.ny * BTC.Ix;
for(i=BTC.Ix;i<BTC.nx;i++)
{
Chase((reliable+index1),1,beta2[iterate],BTC,(mid_seq+index1),(colsoft+index1));
index1 += BTC.ny;
}
iterate++;
}
//output
index1 = BTC.ny * (BTC.Ix + BTC.B + BTC.Q) + BTC.Iy;
len = BTC.kx - (BTC.Ix + BTC.B + BTC.Q);
for(i=0;i<len;i++)
{
data_out[i] = mid_seq[index1];
index1 += BTC.ny;
}
index2 = len;
len = BTC.kx - BTC.Ix;
for(j=BTC.Iy+1;j<BTC.ky;j++)
{
index1 = BTC.ny * BTC.Ix + j;
for(i=0;i<len;i++)
{
data_out[index2+i] = mid_seq[index1];
index1 += BTC.ny;
}
index2 += len;
}
free(matrix_seq);
free(mid_seq);
free(rowsoft);
free(colsoft);
free(reliable);
free(matrix_seq_trans);
}
/*
*********************************************************************************
* NAME: BTCdecode
* PURPOSE: decoding BTC with concatenation of slots.
*
* Input: data_in[]: the received data for decoding.
* 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-13
*
*********************************************************************************
*/
void BTCdecode(double 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.
double *pdata_in = data_in;
int *pdata_out = data_out;
BTCstruct BTC; //BTC parameter.
int slot_num = 0;
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);
oneBTCdecode(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 * encoded_len;
oneBTCdecode(pdata_in,pdata_out,BTC);
pdata_out += BTC.msg_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 * encoded_len;
oneBTCdecode(pdata_in,pdata_out,BTC);
pdata_out += BTC.msg_len;
}
pdata_in += encoded_len;
slot_num = (para_m+para_j) / 2;
encoded_len = slot_num * 48 * fec.modID;
BTCInit(encoded_len,fec.rateID,&BTC);
oneBTCdecode(pdata_in,pdata_out,BTC);
pdata_out += BTC.msg_len;
pdata_in += encoded_len;
slot_num = n_slot - para_j*(para_k-1) - (para_m+para_j) / 2;
encoded_len = slot_num * 48 * fec.modID;
BTCInit(encoded_len,fec.rateID,&BTC);
oneBTCdecode(pdata_in,pdata_out,BTC);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -