www.pudn.com > communicationmatlab.rar > SIMBCHDC.C
/*============================================================================ * Syntax:[sys, x0, str, ts] = simbchdc(t, x, u, flag, n, k, t_err, tp, dim) *SIMBCHDC SIMULINK S-function for BCH decode. * The m-dile is designed to be used in a SIMULINK S-Function block. * This function will decode the cyclic code using BCH decod method. * Parameters: n -- length of code word. * k -- length of message. * t -- error-correction capability. * tp-- list of all GF(2^M) elements. n=2^M-1. * * The output has length n+1, the last digit is the error signal err. * A non-negative err indicates number of errors have been corrected. * A nagative err indicates that more than t error have been found in * in the code, the routine cannot correct such error. * *============================================================================ * Original designed by Wes Wang, * Jun Wu, The Mathworks, Inc. * Feb-07, 1996 * * Copyright (c) 1996 by The MAthWorks, Inc. * All Rights Reserved * $Revision: 1.1 $ $Date: 1996/04/01 19:04:17 $ *============================================================================ */ #define S_FUNCTION_NAME simbchdc /* need to include simstruc.h for the definition of the SimStruct and * its associated macro definitions. */ #include#include "simstruc.h" #include "gflib.c" #ifdef MATLAB_MEX_FILE #include "mex.h" /* needed for declaration of mexErrMsgTxt */ #endif #define NUM_ARGS 5 /* five additional input arguments */ #define N ssGetArg(S,0) #define K ssGetArg(S,1) #define T_ERR ssGetArg(S,2) #define P ssGetArg(S,3) #define DIM ssGetArg(S,4) #define Prim 2 static void mdlInitializeSizes(S) SimStruct *S; { int i, np; int n = (int)mxGetPr(N)[0]; int k = (int)mxGetPr(K)[0]; int t_err = (int)mxGetPr(T_ERR)[0]; int dim = (int)mxGetPr(DIM)[0]; np = 1; for(i=0; i < dim; i++) np = np*2; if ( np-1 != n ){ #ifdef MATLAB_MEX_FILE mexErrMsgTxt("BCH decode has illegal code word length"); #endif } ssSetNumContStates( S, 0); /* no continuous states */ ssSetNumDiscStates( S, 0); /* no discrete states */ ssSetNumOutputs( S, k+1); /* number of output, code word length plus one*/ ssSetNumInputs( S, n+1); /* number of inputs, message length*/ ssSetDirectFeedThrough( S, 1); /* direct feedthrough flag */ ssSetNumSampleTimes( S, 1); /* number of sample times */ ssSetNumInputArgs( S, NUM_ARGS); /* number of input arguments*/ ssSetNumRWork( S, 0); ssSetNumIWork( S, 6*n+(3*n+2)*dim+t_err*t_err+15*t_err+19); /* n -------------- *code * (n+1)*dim ------ *pp * n -------------- *ccode * 1 -------------- *err * (2*n+1)*(dim+2) * +t_err*t_err+15*t_err+16 --- Iwork for bchcore() */ ssSetNumPWork( S, 0); } /* * mdlInitializeConditions - initialize the states * Initialize the states, Integers and real-numbers */ static void mdlInitializeConditions(x0, S) double *x0; SimStruct *S; { int i; int n, k, dim, t_err; int *code, *pp, *ccode, *Iwork, *err; double *p; p = mxGetPr(P); n = (int)mxGetPr(N)[0]; k = (int)mxGetPr(K)[0]; dim = (int)mxGetPr(DIM)[0]; t_err = (int)mxGetPr(T_ERR)[0]; code = ssGetIWork(S); pp = ssGetIWork(S)+n; ccode = ssGetIWork(S)+n+(n+1)*dim; err = ssGetIWork(S)+2*n+(n+1)*dim; Iwork = ssGetIWork(S)+2*n+(n+1)*dim+1; for(i=0; i<(n+1)*dim; i++) pp[i] = (int)p[i]; } /* * mdlInitializeSampleTimes - initialize the sample times array * * This function is used to specify the sample time(s) for your S-function. * If your S-function is continuous, you must specify a sample time of 0.0. * Sample times must be registered in ascending order. */ static void mdlInitializeSampleTimes(S) SimStruct *S; { ssSetSampleTimeEvent(S, 0, -1.0); ssSetOffsetTimeEvent(S, 0, 0.0); } /*if (flag == 3) * % refresh the state only when there is a trigger signal * len_u = length(u); * if u(len_u) * % output the decode result. * % main calculation * [sys, err] = bchcore(u(1:len_u-1), n, dim, k, t_err, tp); * % output is the combination of message and error information. * sys = [sys(:); err]; * elseif (t <= 0) * % if there is no trigger, no calculation. * sys = zeros(k+1, 1); * end; */ /* * mdlOutputs - compute the outputs * * In this function, you compute the outputs of your S-function * block. The outputs are placed in the y variable. */ static void mdlOutputs(y, x, u, S, tid) double *y, *x, *u; SimStruct *S; int tid; { int i; int n, k, dim, t_err; int *code, *pp, *ccode, *Iwork, *err; double t; double *p; t = ssGetT(S); p = mxGetPr(P); n = (int)mxGetPr(N)[0]; k = (int)mxGetPr(K)[0]; dim = (int)mxGetPr(DIM)[0]; t_err = (int)mxGetPr(T_ERR)[0]; code = ssGetIWork(S); pp = ssGetIWork(S)+n; ccode = ssGetIWork(S)+n+(n+1)*dim; err = ssGetIWork(S)+2*n+(n+1)*dim; Iwork = ssGetIWork(S)+2*n+(n+1)*dim+1; for(i=0; i<(n+1)*dim; i++) pp[i] = (int)p[i]; for(i=0; i