📄 convencoder.c
字号:
/* MATLAB's C-MEX convolutional code encoder
*
* Copyright Bagawan S. Nugroho, 2006
*********************************************/
#include "mex.h"
#include <stdlib.h>
void shiftReg( short *out, double *g, double *x, int inLen, int ioR, short R, short K)
/* Convolve the input and tail vector according to generator matrix g */
{
short NN;
int i, j;
div_t N;
/* Number of state's bits = constraint lengths K */
short state[9] = {0,0,0,0,0,0,0,0,0};
/* Convolve the input bits */
for( i = 0; i < inLen; i++) {
NN = 0;
/* Data in */
*state = *(x + i);
for( j = 0; j < K; j++) {
N = div( (NN + *(state + j)**(g + j*R + ioR)), 2);
NN = N.rem;
} /* for j */
/* Update the output vector */
*(out + i*R + ioR) = NN;
/* Update state */
for( j = K - 1; j > 0; j--) {
*(state + j) = *(state + j - 1);
} /* for j */
} /* for i */
/* Convolve the zero bits tail */
for( i = inLen; i < inLen + K - 1; i++) {
NN = 0;
/* Zeros tail */
*state = 0;
for( j = 0; j < K; j++) {
N = div( (NN + *(state + j)**(g + j*R + ioR)), 2);
NN = N.rem;
} /* for j */
/* Update the output vector */
*(out + i*R + ioR) = NN;
/* Update state */
for( j = K - 1; j > 0; j--) {
*(state + j) = *(state + j - 1);
} /* for j */
} /* for i */
}
/* ----------- The main function ----------- */
void convEncoder( double *x, double *y, double *G, int inLen, short R, short K)
/* x, input vector
* G, generator matrix
* inLen, data length
* R, code rate (1/R)
* K, constraint length
*
* y, output vector. */
{
short out[10240];
int i;
for( i = 0; i < R; i++) {
shiftReg( out, G, x, inLen, i, R, K);
} /* for i */
/* Return the vector's result to the gateway output */
for( i = 0; i < R*(inLen + K - 1); i++) {
*(y + i) = (double) *(out + i);
} /* for i */
}
/* ---------- The gateway function ---------- */
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[] )
/* input: G = prhs[0], generator matrix
* x = prhs[1], input vector (1/0)
*
*
* output: y = plhs[0], output vector (0/1). */
{
double *x, *y, *G;
short row, col;
int inLen;
/* Create pointer to the input matrix G */
G = mxGetPr( prhs[0]);
/* Create pointer to the input vector x */
x = mxGetPr( prhs[1]);
/* Get the dimensions of the matrix generator G */
row = mxGetM( prhs[0]);
col = mxGetN( prhs[0]);
/* Get the length of the vector input x */
inLen = mxGetN( prhs[1]);
/* Set the output pointer to the output vector */
plhs[0] = mxCreateDoubleMatrix( 1, row*(inLen + col - 1), mxREAL);
/* Create a pointer to a copy ot the output vector y */
y = mxGetPr( plhs[0]);
/* Call the main subroutine */
convEncoder( x, y, G, inLen, row, col);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -