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

📄 v34trans.c

📁 Proakis《contemporarycommunication systems using matlab》matlab源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
 * V34trans   A SIMULINK example for v34 transmission
 *
 *  Syntax:  [sys, x0] = v34trans(t,x,u,flag,rate,ShMp)
 *
 * rate is a 12 element vector, which contains:
 * rate = [rate_s, rate_r,    rate_s_j, rate_s_p, rate_s_n, 
 *         rate_b, rate_q,    rate_K,   rate_M,   rate_M_exp,
 *         rate_L, rate_L_exp];
 * see V34param for the details of the rate parameter.
 *
 * ShMp is a four column matrix
 * ShMp(:, 1) contains g2
 * ShMp(:, 2) contains g4
 * ShMp(:, 3) contains g8
 * ShMp(:, 4) contains z8
 *
 * Wes Wang  Jan. 24, 1996
 * Copyright (c) 1996 by The MathWorks, Inc.
 * All Rights Reserved
 * $Revision: 1.3 $  $Date: 1996/04/10 20:20:38 $
 */
#define S_FUNCTION_NAME v34trans


#ifdef MATLAB_MEX_FILE
#include <stdio.h>    /* needed for declaration of sprintf */
#include "mex.h"      /* needed for declaration of mexErrMsgTxt */
#endif

#include "simstruc.h"

/* Defines for easy access of the input parameters */
#define NUM_ARGS   2
#define RATE     ssGetArg(S,0)
#define ShMp       ssGetArg(S,1)
                        
/*
 * mdlInitializeSizes - called to initialize the sizes array stored in
 *                      the SimStruct.  The sizes array defines the
 *                      characteristics (number of inputs, outputs,
 *                      states, etc.) of the S-Function.
 */

static void mdlInitializeSizes(S)
    SimStruct *S;
{
    int  rate_s = (int) mxGetPr(RATE)[0];
    int  rate_r = (int) mxGetPr(RATE)[1];
    int  rate_j = (int) mxGetPr(RATE)[2];
    int  rate_p = (int) mxGetPr(RATE)[3];
    int  rate_b = (int) mxGetPr(RATE)[5];
    int  rate_q = (int) mxGetPr(RATE)[6];
    int  rate_K = (int) mxGetPr(RATE)[7];
    int  rate_M = (int) mxGetPr(RATE)[8];

    /*
     * Set-up size information.
     */ 
/*    
#ifdef MATLAB_MEX_FILE
if (0) {
    char    err_msg[256];
    sprintf(err_msg, "Size initial, rate_s = %d; rate_r = %d; rate_j = %d; rate_p = %d;\n", rate_s, rate_r, rate_j, rate_p);
    mexPrintf(err_msg);
    sprintf(err_msg, "rate_b = %d; rate_q = %d; rate_K = %d; rate_M = %d;\n", rate_b, rate_q, rate_K, rate_M);
    mexPrintf(err_msg);
    sprintf(err_msg, "ShMp_N= %d, ShMp_M=%d;\n", mxGetN(ShMp), mxGetM(ShMp));
    mexPrintf(err_msg);    
}
#endif
*/
    if (ssGetNumArgs(S) == NUM_ARGS) {
      ssSetNumContStates(    S, 0);
      ssSetNumDiscStates(    S, 0);
      ssSetNumInputs(        S, rate_b+2);
      ssSetNumOutputs(       S, rate_q + 1 + 3);
      ssSetDirectFeedThrough(S, 1);
      ssSetNumInputArgs(     S, NUM_ARGS);
      ssSetNumSampleTimes(   S, 1);
      ssSetNumRWork(         S, 0);
      /* 1 last updata
       * 1 last trigger output
       * 1 current output counter
       * 23 scramble
       * 8 mijk number
       * scrambled b input data
       */
      ssSetNumIWork(         S, 1 + 1 + 1 + 23 + 8 + rate_b);
      ssSetNumPWork(         S, 0);
    } else {
#ifdef MATLAB_MEX_FILE
      char err_msg[256];
      sprintf(err_msg, "Wrong number of input arguments passed to S-function MEX-file.\n"
           "%d input arguments were passed in when expecting %d input arguments.\n", ssGetNumArgs(S) + 4, NUM_ARGS + 4);
      mexErrMsgTxt(err_msg);
#endif
    }
}

/*
 * mdlInitializeSampleTimes - initializes the array of sample times stored in
 *                            the SimStruct associated with this S-Function.
 */

static void mdlInitializeSampleTimes(S)
    SimStruct *S;
{
    /*
     * Note, blocks that are continuous in nature should have a single
     * sample time of 0.0.
     */

    ssSetSampleTimeEvent(S, 0, 0.0);
    ssSetOffsetTimeEvent(S, 0, 0.0);
}

/*
 * mdlInitializeConditions - initializes the states for the S-Function
 */

static void mdlInitializeConditions(x0, S)
    double *x0;
    SimStruct *S;
{
    int  rate_s = (int) mxGetPr(RATE)[0];
    int  rate_r = (int) mxGetPr(RATE)[1];
    int  rate_j = (int) mxGetPr(RATE)[2];
    int  rate_p = (int) mxGetPr(RATE)[3];
    int  rate_b = (int) mxGetPr(RATE)[5];
    int  rate_q = (int) mxGetPr(RATE)[6];
    int  rate_K = (int) mxGetPr(RATE)[7];
    int  rate_M = (int) mxGetPr(RATE)[8];
    
    int *last_updata = ssGetIWork(S);
    int *last_output = ssGetIWork(S) + 1;
    int *out_count   = ssGetIWork(S) + 2;
    int *scramble    = ssGetIWork(S) + 3;
    int *mijk        = ssGetIWork(S) + 3 + 23;
    int *data_scrm   = ssGetIWork(S) + 3 + 23 + 8;
    
    int i;
/*
#ifdef MATLAB_MEX_FILE
if (0) {
    char    err_msg[256];
    sprintf(err_msg, "Parame initial rate_s = %d; rate_r = %d; rate_j = %d; rate_p = %d;\n", rate_s, rate_r, rate_j, rate_p);
    mexPrintf(err_msg);
    sprintf(err_msg, "rate_b = %d; rate_q = %d; rate_K = %d; rate_M = %d;\n", rate_b, rate_q, rate_K, rate_M);
    mexPrintf(err_msg);
    sprintf(err_msg, "ShMp_N=%d , ShMp_M=%d;\n", mxGetN(ShMp), mxGetM(ShMp));
    mexPrintf(err_msg);    
}
#endif
*/
    *last_updata = 0;
    *last_output = 0;
    *out_count   = 0;
    
    for (i = 0; i < 23; i++)
        scramble[i] = 0;
    for (i = 0; i < 8; i++)
        mijk[i] = 0;
    for (i = 0; i < rate_b; i++)
        data_scrm[i] = 0;
}

/*
 * mdlOutputs - computes the outputs of the S-Function
 */

static void mdlOutputs(y, x, u, S, tid)
    double *y, *x, *u;
    SimStruct *S;
    int tid;
{
    int  rate_s = (int) mxGetPr(RATE)[0];
    int  rate_r = (int) mxGetPr(RATE)[1];
    int  rate_j = (int) mxGetPr(RATE)[2];
    int  rate_p = (int) mxGetPr(RATE)[3];
    int  rate_b = (int) mxGetPr(RATE)[5];
    int  rate_q = (int) mxGetPr(RATE)[6];
    int  rate_K = (int) mxGetPr(RATE)[7];
    int  rate_M = (int) mxGetPr(RATE)[8];
    
    int *last_updata = ssGetIWork(S);
    int *last_output = ssGetIWork(S) + 1;
    int *out_count   = ssGetIWork(S) + 2;
    int *scramble    = ssGetIWork(S) + 3;
    int *mijk        = ssGetIWork(S) + 3 + 23;
    int *data_scrm   = ssGetIWork(S) + 3 + 23 + 8;

    if ((u[rate_b] > 0) && (*last_updata == 0)) {
        /* begin the major updating work. */
        double  *g2 = mxGetPr(ShMp);
        double  *g4 = mxGetPr(ShMp) + 8 *(rate_M-1)+1;
        double  *g8 = mxGetPr(ShMp) + 16*(rate_M-1)+2;
        double  *z8 = mxGetPr(ShMp) + 24*(rate_M-1)+3;

        long R[8];
        long A, B, C, D, E, F, G, H, R_bef, R_now, V_bef, V_now;
        int  i, j;
#ifdef MATLAB_MEX_FILE
if (0) {
    char    err_msg[256];
    sprintf(err_msg, "Output rate_s = %d; rate_r = %d; rate_j = %d; rate_p = %d;\n", rate_s, rate_r, rate_j, rate_p);
    mexPrintf(err_msg);
    sprintf(err_msg, "rate_b = %d; rate_q = %d; rate_K = %d; rate_M = %d;\n", rate_b, rate_q, rate_K, rate_M);
    mexPrintf(err_msg);
    sprintf(err_msg, "ShMp_N=%d, ShMp_M=%d;\n", mxGetN(ShMp), mxGetM(ShMp));
    mexPrintf(err_msg);
    sprintf(err_msg, "\n\n\n");
    mexPrintf(err_msg);        
}
#endif
        /* define the maximum size vector */
      if (1) {
        /*scramble */
        for (i = 0; i < rate_b; i++) {
            data_scrm[i] = ((int)u[i] + scramble[17] + scramble[22]) % 2;
            for (j = 22; j > 0; j--)
                scramble[j] = scramble[j-1];
            scramble[0] = data_scrm[i];
		}
	  } else {
        for (i = 0; i < rate_b; i++)
            data_scrm[i] = (int)u[i];	    
	  }
		/*parser */
        /* data_scrm[0:K-1] Si1 -- SiK                  -- Six
         * data_scrm[K:K+2] I1i0, I2i0, I3i0            -- Ixij
         * data_scrm[K+3:K+3+q-1] Qi001, ..., Qi00q     -- Qijkx
         * data_scrm[K+3+q:K+3+2*q-1] Qi011, ..., Qi01q -- Qijkx
         * data_scrm[K+3+2*q:K+3+2*q+2] I1i1, I2i1, I3i1
         * data_scrm[K+6+2*q:K+6+3*q-1] Qi101, ..., Qi10q
         * data_scrm[K+6+3*q:K+6+4*q-1] Qi111, ..., Qi11q
         * data_scrm[K+6+4*q:K+6+4*q+2] I1i2, I2i2, I3i2         
         * data_scrm[K+9+4*q:K+9+5*q-1] Qi201, ..., Qi20q
         * data_scrm[K+9+5*q:K+9+6*q-1] Qi211, ..., Qi21q
         * data_scrm[K+9+6*q:K+9+6*q+2] I1i3, I2i3, I3i3
         * data_scrm[K+12+6*q:K+12+7*q-1] Qi301, ..., Qi30q
         * data_scrm[K+12+7*q:K+12+8*q-1] Qi311, ..., Qi31q
         */
        
        /* Shell Mapper */
        /* calculate A, B, C, D, E, F, G, H. */
        for (i = 0; i< 8; i++) {
           R[i] = 0;
        }
        A = 0; B = 0; C = 0; D = 0; E = 0; F = 0; G = 0;;
         
        /* A*/
        R[0] = data_scrm[rate_K - 1];
        i = rate_K - 2;
        while (i >= 0) {
            R[0] = R[0] * 2 + data_scrm[i];
            i--;
        }
        A = 0;
        while ((int)z8[A] <= R[0]) {
            A = A + 1;;
        }
        A = A - 1;
#ifdef MATLAB_MEX_FILE
if (0) {
    char    err_msg[256];

⌨️ 快捷键说明

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