📄 tuenc.c
字号:
/*
* function [EncOut] = TuEnc(Info, puncture, alpha, poly_g1, poly_g2)
*
* Inputs
* Info :information bit
* puncture:puncture flag
* alpha :interleaver index
*
* Output:
* EncOut :Turbo encoder output
*
* Created July 15, 2002
* Wang Dongming NCRL SEU
*/
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
#include<math.h>
#include "mex.h"
#define MAX_INTER_SIZE 32768
#define NU2_MAX 32
int SBG[NU2_MAX][2];
int SFG[NU2_MAX][2];
int return_gi (int g, int position)
{
return ( (g & (1 << (position)) ) != 0 );
}
int memory_length (int g)
{
int i;
for (i = 16; i >= 0; i--)
if (return_gi(g,i) != 0)
return i+1;
return 0;
}
int dot (int x, int y)
{
int i,l;
int result;
l = 32;
y=y << 1;
result = 0;
for (i=0; i<l; i++)
result = result + (return_gi(x,i) & return_gi(y,i));
result = result % 2;
return result;
}
int dot2 (int x, int y)
{
int i,l ;
int result;
l = 32;
result = 0;
for (i=0; i<l; i++)
result = result + (return_gi(x,i) & return_gi(y,i));
result = result % 2;
return result;
}
int Sbcp(int m,int j,int g1)
{
int nu;
nu = memory_length(g1) - 1;
return ((m >> 1) + (1 << (nu-1)) * ( (j + dot2(g1,m)) % 2 ));
}
int Sfcp(int m, int j, int g1)
{
int nu;
nu = memory_length(g1)-1;
return (m<<1)%(1<<nu) + ((j + dot(g1,m)) %2);
}
double encoder (int g1, int g2, double dk,int state_flag)
{
static int encoder_state = 0;
double yk1;
if (state_flag == 1)
{
return (double)encoder_state;
}
if(state_flag == 2)
{
encoder_state = 0;
return (double)encoder_state;
}
yk1 = (double)((dot(g2,encoder_state)+(((int)dk+dot(g1,encoder_state))%2) ) %2);
encoder_state=SFG[encoder_state][(int)dk];
return yk1;
}
void interleave(double * block, int N, double *alpha)
{
int j;
double interleaved_block[MAX_INTER_SIZE];
/* interleave to buffer */
for (j=0; j<N; j++)
interleaved_block[j] = block[(int)alpha[j]];
/* copy buffer to final location */
for (j=0;j<N;j++)
block[j]=interleaved_block[j];
}
void turbo_encoder(double *pinfo, int len, int puncture, double *encoder_out, double *alpha, int poly_g1, int poly_g2)
{
int i;
int nu, state_num;
double tail_bit;
double *enc_out1, *enc_out2, *ptmp_info, *info_tail;
nu = memory_length(poly_g1) - 1;
state_num = (1<<nu);
/* alloc memery for encoder out */
enc_out1 = (double *)malloc((len+nu)*sizeof(double));
enc_out2 = (double *)malloc((len+nu)*sizeof(double));
ptmp_info = (double *)malloc((len+nu)*sizeof(double));
info_tail = (double *)malloc((len+nu)*sizeof(double));
if ((enc_out1 == NULL)||(enc_out2 == NULL)||(ptmp_info == NULL)||(info_tail == NULL))
{
printf("not enough memery to alloc!\n");
exit(0);
}
/* Generate State Table */
for (i=0; i<(state_num); i++)
{
SBG[i][0] = Sbcp(i,0,poly_g1);
SBG[i][1] = Sbcp(i,1,poly_g1);
SFG[i][0] = Sfcp(i,0,poly_g1);
SFG[i][1] = Sfcp(i,1,poly_g1);
}
/* force the first encoder state to zero at first.*/
encoder(0,0,0,2);
for (i=0; i<len; i++)
{
enc_out1[i] = encoder(poly_g1, poly_g2,pinfo[i],0);
ptmp_info[i] = pinfo[i];
info_tail[i] = pinfo[i];
}
for (i=0; i<nu; i++)
{
tail_bit =(double) dot(poly_g1,(int)encoder(0,0,0,1));
enc_out1[len+i] = encoder(poly_g1, poly_g2,tail_bit,0);
ptmp_info[len+i] = tail_bit;
info_tail[len+i] = tail_bit;
}
interleave(ptmp_info,len+nu, alpha);
for (i=0;i<len+nu;i++)
{
enc_out2[i] = encoder(poly_g1, poly_g2,ptmp_info[i],0);
}
if (puncture > 0)
{
for (i=0;i<len+nu;i++)
{
encoder_out[3*i] = info_tail[i];
encoder_out[3*i+1] = enc_out1[i];
encoder_out[3*i+2] = enc_out2[i];
}
}
else
{
for (i=0;i<len+nu;i++)
{
encoder_out[2*i] = info_tail[i];
if (i%2)
{
encoder_out[2*i+1] = enc_out1[i];
}
else
{
encoder_out[2*i+1] = enc_out2[i];
}
}
}
free(enc_out1);
free(enc_out2);
free(ptmp_info);
free(info_tail);
}
/* The gateway routine. */
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
/* input parameters */
double *info, *alpha;
double puncture;
double poly_g1, poly_g2;
/* output parameters */
double *enc_out;
int len, enc_out_length;
/* Check for the proper number of arguments */
if ( nrhs != 5 )
mexErrMsgTxt("Incorrect number of inputs. three arguments");
if ( nlhs != 1)
mexErrMsgTxt("Incorrect number of outputs.one output");
/* Get the input parameters */
info = mxGetPr(prhs[0]);
puncture = mxGetScalar(prhs[1]);
alpha = mxGetPr(prhs[2]);
poly_g1 = mxGetScalar(prhs[3]);
poly_g2 = mxGetScalar(prhs[4]);
len = mxGetN(prhs[0]);
/* create a new array and set the output pointer to it */
if (puncture > 0)
{
enc_out_length = (len + memory_length((int)poly_g1) - 1)*3;
}
else
{
enc_out_length = (len + memory_length((int)poly_g1) - 1)*2;
}
plhs[0]= mxCreateDoubleMatrix(1, enc_out_length, mxREAL);
enc_out = mxGetPr(plhs[0]);
turbo_encoder(info, len, (int)puncture, enc_out, alpha, (int)poly_g1, (int)poly_g2);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -