⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scrambler_dab.c

📁 通信系统中扰码的实现
💻 C
字号:
/*
 * sfuntmpl_basic.c: Basic 'C' template for a level 2 S-function.
 *
 *  -------------------------------------------------------------------------
 *  | See matlabroot/simulink/src/sfuntmpl_doc.c for a more detailed template |
 *  -------------------------------------------------------------------------
 *
 * Copyright 1990-2001 The MathWorks, Inc.
 * $Revision: 1.26 $
 */


/*
 * You must specify the S_FUNCTION_NAME as the name of your S-function
 * (i.e. replace sfuntmpl_basic with the name of your S-function).
 */

#define S_FUNCTION_NAME  scrambler_dab
#define S_FUNCTION_LEVEL 2

/*
 * Need to include simstruc.h for the definition of the SimStruct and
 * its associated macro definitions.
 */
#include "simstruc.h"
#include <math.h>



/* Error handling
 * --------------
 *
 * You should use the following technique to report errors encountered within
 * an S-function:
 *
 *       ssSetErrorStatus(S,"Error encountered due to ...");
 *       return;
 *
 * Note that the 2nd argument to ssSetErrorStatus must be persistent memory.
 * It cannot be a local variable. For example the following will cause
 * unpredictable errors:
 *
 *      mdlOutputs()
 *      {
 *         char msg[256];         {ILLEGAL: to fix use "static char msg[256];"}
 *         sprintf(msg,"Error due to %s", string);
 *         ssSetErrorStatus(S,msg);
 *         return;
 *      }
 *
 * See matlabroot/simulink/src/sfuntmpl_doc.c for more details.
 */

/*====================*
 * S-function methods *
 *====================*/

/* Function: mdlInitializeSizes ===============================================
 * Abstract:
 *    The sizes information is used by Simulink to determine the S-function
 *    block's characteristics (number of inputs, outputs, states, etc.).
 */
//-----------------------------------------------------
void deci2bin(int_T d, int_T size, int_T *b)
{
    int i;
 
    for(i = 0; i < size; i++)
        b[i] = 0;
 
    b[size - 1] = d & 0x01;
 
    for (i = size - 2; i >= 0; i--) {
        d = d >> 1;
        b[i] = d & 0x01;
    }
}
//-----------------------------------------------------
static void mdlInitializeSizes(SimStruct *S)
{
    /* See sfuntmpl_doc.c for more details on the macros below */

    ssSetNumSFcnParams(S, 1);  /* Number of expected parameters */
    if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
        /* Return if number of expected != number of actual parameters */
        return;
    }


    if (!ssSetNumInputPorts(S, 1)) return;
	ssSetInputPortFrameData(S,0,-1);
    //ssSetInputPortWidth(S, 0, DYNAMICALLY_SIZED);
	if(!ssSetInputPortMatrixDimensions(S,0,DYNAMICALLY_SIZED,1)) return;
    //ssSetInputPortWidth(S, 0, 1);
    /*
     * Set direct feedthrough flag (1=yes, 0=no).
     * A port has direct feedthrough if the input is used in either
     * the mdlOutputs or mdlGetTimeOfNextVarHit functions.
     * See matlabroot/simulink/src/sfuntmpl_directfeed.txt.
     */
    ssSetInputPortDirectFeedThrough(S, 0, 1);

    if (!ssSetNumOutputPorts(S, 1)) return;
	ssSetOutputPortFrameData(S,0,-1);
    //ssSetOutputPortWidth(S, 0, DYNAMICALLY_SIZED);
	if(!ssSetOutputPortMatrixDimensions(S,0,DYNAMICALLY_SIZED,1)) return;
    //ssSetOutputPortWidth(S, 0, 1);

    ssSetNumSampleTimes(S, 1);
    ssSetNumRWork(S, 0);
    ssSetNumIWork(S, 0);//for time counter
    ssSetNumPWork(S, 2);//1 pointer for dec2bin
    ssSetNumModes(S, 0);
    ssSetNumNonsampledZCs(S, 0);

    ssSetOptions(S, SS_OPTION_EXCEPTION_FREE_CODE); 
}



/* Function: mdlInitializeSampleTimes =========================================
 * Abstract:
 *    This function is used to specify the sample time(s) for your
 *    S-function. You must register the same number of sample times as
 *    specified in ssSetNumSampleTimes.
 */
static void mdlInitializeSampleTimes(SimStruct *S)
{
    ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
	//ssSetSampleTime(S, 0, 1);
    ssSetOffsetTime(S, 0, 0.0);

}



#define MDL_INITIALIZE_CONDITIONS   /* Change to #undef to remove function */
#if defined(MDL_INITIALIZE_CONDITIONS)
  /* Function: mdlInitializeConditions ========================================
   * Abstract:
   *    In this function, you should initialize the continuous and discrete
   *    states for your S-function block.  The initial states are placed
   *    in the state vector, ssGetContStates(S) or ssGetRealDiscStates(S).
   *    You can also perform any other initialization activities that your
   *    S-function may require. Note, this routine will be called at the
   *    start of simulation and if it is present in an enabled subsystem
   *    configured to reset states, it will be call when the enabled subsystem
   *    restarts execution to reset the states.
   */
  static void mdlInitializeConditions(SimStruct *S)
  {	
	  int_T *p_state;
	  int_T *g;
	  int_T i;
	  void **PWork=ssGetPWork(S);
	  int_T g_deci = *mxGetPr(ssGetSFcnParam(S,0));//PN generator in decimal
	  p_state= (int_T*)malloc(9 * sizeof(int_T) );
	  g= (int_T*)malloc(10 * sizeof(int_T) );
	  deci2bin(g_deci, 10 ,g);
	  /*for (i = 0; i < 10; i++)
		  printf("gg %d\n",*(g + i));*/
	  for (i = 0; i < 9; i++)
		  *(p_state + i) = 1;
	  PWork[0]=p_state;
	  PWork[1]=g;

  }
#endif /* MDL_INITIALIZE_CONDITIONS */



#undef MDL_START  /* Change to #undef to remove function */
#if defined(MDL_START) 
  /* Function: mdlStart =======================================================
   * Abstract:
   *    This function is called once at start of model execution. If you
   *    have states that should be initialized once, this is the place
   *    to do it.
   */
  static void mdlStart(SimStruct *S)
  {
  }
#endif /*  MDL_START */



/* Function: mdlOutputs =======================================================
 * Abstract:
 *    In this function, you compute the outputs of your S-function
 *    block. Generally outputs are placed in the output vector, ssGetY(S).
 */
static void mdlOutputs(SimStruct *S, int_T tid)
{
    int_T i,j;
	int_T *g;
	int_T prbs;
	//const real_T *u=ssGetInputPortSignal(S,0);
	InputRealPtrsType uPtrs=ssGetInputPortRealSignalPtrs(S,0);
	real_T *y=ssGetOutputPortRealSignal(S,0);
	int_T width=ssGetOutputPortWidth(S,0);
	//int_T in_width=ssGetInputPortWidth(S,0);
	void **PWork = ssGetPWork(S);

	int_T *p_state;
	p_state = ssGetPWorkValue(S,0);
	g = ssGetPWorkValue(S,1);
    // by yanglin
    for (i = 0; i < 9; i++)
		  *(p_state + i) = 1;
    ///
	/*for (i = 0; i < 9; i++)
		printf("ini stat %d\n",*(p_state + i));*/

	//printf("width %d\n",width);
	for (j = 0; j < width; j++){
		//printf("j= %d,",j);
		//-------calculating the current PRBS----------------
		/*for (i = 0; i < 9; i++)
			printf("  %d",*(p_bin + i));
		printf("\n");*/

		prbs = 0;
		/*prbs ^= *(p_bin + 4);
		prbs ^= *(p_bin + 8);*/
		for (i = 1; i < 10; i++){
			prbs ^= g[i] & *(p_state + i - 1);
		}
		//printf("\nPRBS %d\n",prbs);
		//printf("---------------------------------------\n");
		//------state shift for next input bit---------------
		for (i = 8; i > 0; i--)
			*(p_state + i) = *(p_state + i - 1);
		*p_state = prbs;
		/*for (i = 0; i < 9; i++)
			printf("j  %d stat %d\n",j,*(p_bin + i));*/
		//------scrambler output----------
		if (*uPtrs[j] == prbs)
			*y++ = 0;
		else
			*y++ = 1;
		//*y++ = 2.0*(*uPtrs[j]);
		//printf(" %d\n",(int_T)*u[j]);
	}
	PWork[0] = p_state;
}		




#undef MDL_UPDATE  /* Change to #undef to remove function */
#if defined(MDL_UPDATE)
  /* Function: mdlUpdate ======================================================
   * Abstract:
   *    This function is called once for every major integration time step.
   *    Discrete states are typically updated here, but this function is useful
   *    for performing any tasks that should only take place once per
   *    integration step.
   */
  static void mdlUpdate(SimStruct *S, int_T tid)
  {
  }
#endif /* MDL_UPDATE */



#undef MDL_DERIVATIVES  /* Change to #undef to remove function */
#if defined(MDL_DERIVATIVES)
  /* Function: mdlDerivatives =================================================
   * Abstract:
   *    In this function, you compute the S-function block's derivatives.
   *    The derivatives are placed in the derivative vector, ssGetdX(S).
   */
  static void mdlDerivatives(SimStruct *S)
  {
  }
#endif /* MDL_DERIVATIVES */



/* Function: mdlTerminate =====================================================
 * Abstract:
 *    In this function, you should perform any actions that are necessary
 *    at the termination of a simulation.  For example, if memory was
 *    allocated in mdlStart, this is the place to free it.
 */
static void mdlTerminate(SimStruct *S)
{
	if (ssGetPWorkValue(S,0)!=NULL)
		free(ssGetPWorkValue(S,0));
}


/*======================================================*
 * See sfuntmpl_doc.c for the optional S-function methods *
 *======================================================*/

/*=============================*
 * Required S-function trailer *
 *=============================*/

#ifdef  MATLAB_MEX_FILE    /* Is this file being compiled as a MEX-file? */
#include "simulink.c"      /* MEX-file interface mechanism */
#else
#include "cg_sfun.h"       /* Code generation registration function */
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -