0

这可能是一个简单的问题。但我找不到解决这个问题的方法。我正在使用 s 功能块将波形信号输入到我的算法中。正在从大约 2000 点的文件中读取波形信号

首先,我从等于 50 的模拟时间开始。然后它只读取 50 分。我检查了“tout”变量。它是 0,1,2,....50

然后我将模拟时间增加到 100。仍然结果相同。只读取 50 点。但是 tout 是 0,2,4,6..50

我尝试了多达 10000 个。无论我做什么,它只读取 50 个值,时间步长为 0,200,400,600 等。这是我的 s-function 或 simulink 设置的问题吗?这是c s-function文件

    /* Give S-function a name */
#define S_FUNCTION_NAME  Readip
#define S_FUNCTION_LEVEL 2

/* Include SimStruct definition and file I/O functions */
#include "simstruc.h"
#include <stdio.h>
#include <stdlib.h>

static FILE* file2;
/* Called at the beginning of the simulation */
    static void mdlInitializeSizes(SimStruct *S)

{

        ssSetNumSFcnParams(S, 0); 


   if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
        return;
    }
    if (!ssSetNumOutputPorts(S, 3)) return;
    ssSetNumContStates(S, 0);
    ssSetNumDiscStates(S, 0);
    ssSetOutputPortWidth(S, 0, 1);
    ssSetOutputPortDataType(S,0,DYNAMICALLY_SIZED);
    ssSetOutputPortOptimOpts(S, 0, SS_REUSABLE_AND_LOCAL);
    ssSetOutputPortWidth(S, 1, 1);
    ssSetOutputPortDataType(S,1,DYNAMICALLY_SIZED);
    ssSetOutputPortOptimOpts(S, 1, SS_REUSABLE_AND_LOCAL);
    ssSetOutputPortWidth(S, 2, 1);
    ssSetOutputPortDataType(S,2,DYNAMICALLY_SIZED);
    ssSetOutputPortOptimOpts(S, 2, SS_REUSABLE_AND_LOCAL);


       ssSetNumPWork(S,1);
        ssSetNumSampleTimes(S, 1);
    }


    /* Set sample times for the block */
    static void mdlInitializeSampleTimes(SimStruct *S)
    {
        ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
        ssSetOffsetTime(S, 0, 0.0);
    }



        #define 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 shou

ld be initialized once, this is the place
   *    to do it.
   */
  static void mdlStart(SimStruct *S)
  {
      /*at start of model execution, open the file and store the pointer
       *in the pwork vector */
      void** pwork = ssGetPWork(S);
      FILE *datafile;

      datafile = fopen("table.data","r");
      pwork[0] =  datafile;

      }
    #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)
{
    //get pointer to the block's output signal
    real_T       *y1 = ssGetOutputPortSignal(S,0);
    real_T       *y2 = ssGetOutputPortSignal(S,1);
    real_T       *y3 = ssGetOutputPortSignal(S,2);
    char a[10];
    char b[10];
    char c[10];
    /*get pointer to array of pointers, where the first element is the address
     *of the open file */
    void** pwork = ssGetPWork(S);

/*read a floating point number and then the comma delimiter
 *store the result in y*/

fscanf(pwork[0],"%s    %s    %s",&a,&b,&c);
*y1=atof(a);
*y2=atof(b);
    *y3=atof(c);

}



/* 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)
{
    //close the file
    void** pwork = ssGetPWork(S);
      FILE *datafile;

      datafile = pwork[0];
      fclose(datafile);

}



/*=============================*
 * 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
4

2 回答 2

3

这似乎只是一个设置问题。如果您没有为求解器指定步长并且您的模块没有指示采样时间,Simulink 将选择默认的Simulation Time / 50. 只需打开“模型配置参数”对话框并单击左侧窗格中的求解器。如果您使用的是固定步长求解器,则可以显式设置步长。如果您使用可变步长求解器,则可以指定最大/最小步长。

此外,如果您正在寻找专门为您的 S-Function 指定离散采样周期,那么您可能需要查看此链接以确保您mdlInitializeSampleTimes按照需要实施。

于 2012-10-10T11:19:54.100 回答
1

您需要使用固定步长离散求解器。将定期采样时间约束设置为“无约束”输入固定步长和模拟停止时间,以便获得 2000 步。

例如 1s/step 和 2000s 的停止时间,或 0.1s/step 和 200s 的停止时间。

于 2012-10-26T14:44:14.263 回答