📄 rscencoder.c
字号:
/* MATLAB's C-MEX turbo code RSC encoder
*
* Copyright Bagawan S. Nugroho, 2006
*********************************************/
# include "mex.h"
# include <stdlib.h>
void rscEncoder( double *x, double *y, double *G,
int L, int K, int T)
/* --------- The main subroutine ----------
* L, data length
* K, constraint length
* T, terminate the trellis
* T > 0 terminate
* T < 0 no termination */
{
/* maximum 8 state, # of memory = 7 */
int state[8] = {0,0,0,0,0,0,0,0};
int i, j, N1, N2;
div_t N;
state[0] = *x;
/* Systematic output */
for( j = 0; j < L; j++) {
*(y + 2*j) = *(x + j);
} /* for */
/* Non systematic output */
for( j = 0; j < L; j++) {
N1 = 0;
/* The upper matrix generator */
for( i = 1; i < K; i++) {
N = div( (N1 + state[i] * *(G+2*i)), 2);
N1 = N.rem;
} /* for */
N1 = N.rem;
/* The feedback */
N = div( (*(x+j) + N1), 2);
state[0] = N.rem;
N2 = 0;
/* The lower matrix generator */
for( i = 0; i < K; i++) {
N = div( (N2 + state[i] * *(G+2*i+1)), 2);
N2 = N.rem;
} /* for */
N2 = N.rem;
/* Update the state */
for( i = K - 1; i > 0; i--) {
state[i] = state[i-1];
} /* for */
/* Get the result */
*(y + 2*j + 1) = N2;
} /* for */
if ( T > 0) {
/* Trellis termination */
for( j = 0; j < K - 1; j++) {
N1 = 0;
/* The upper matrix generator */
for( i = 1; i < K; i++) {
N = div( (N1 + state[i] * *(G+2*i)), 2);
N1 = N.rem;
} /* for */
N1 = N.rem;
/* Systematic termination */
*(y + 2*L + 2*j) = N1;
/* The feedback */
state[0] = 0;
N2 = 0;
/* The lower matrix generator */
for( i = 0; i < K; i++) {
N = div( (N2 + state[i] * *(G+2*i+1)), 2);
N2 = N.rem;
} /* for */
N2 = N.rem;
/* Non systematic termination */
*(y + 2*L + 2*j + 1) = N2;
/* Update the state */
for( i = K - 1; i > 0; i--) {
state[i] = state[i-1];
} /* for */
} /* for */
} /* if */
}
/* ----------- Gateway function ---------- */
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[] )
/* input: x = prhs[1], input vector (1/0)
* G = prhs[0], generator matrix
* T = prhs[2], termination flag
* pos = terminated
* neg = unterminated
*
* output: y = plhs[0], output vector (0/1). */
{
double *x, *y, *G;
int T, inLength, mrows, ncols;
/* Create pointer to the input vector x */
x = mxGetPr( prhs[1]);
/* Create pointer to the input matrix G */
G = mxGetPr( prhs[0]);
/* Get the scalar input T */
T = mxGetScalar( prhs[2]);
/* Get the length of the vector input x */
inLength = mxGetN( prhs[1]);
/* Get the dimensions of the matrix generator G */
ncols = mxGetN( prhs[0]);
if( T > 0) {
/* Set the output pointer to the output vector */
plhs[0] = mxCreateDoubleMatrix( 1, 2*(inLength + ncols-1), mxREAL);
}
else {
plhs[0] = mxCreateDoubleMatrix( 1, 2 * inLength, mxREAL);
}
/* Create a pointer to a copy ot the output matrix */
y = mxGetPr( plhs[0]);
/* Call the main subroutine */
rscEncoder( x, y, G, inLength, ncols, T);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -