How to execute multiple mathematical operations in S-Function using C
Show older comments
I am trying to process the input signal. For this case the S-Function block has two input ports(U and V) and one output port (R). The output is mathematically modeled as R=(U*cos(Omega*i))-(V*sin(Omega*i)). But, while I am trying to implement the operation in S-Function block, the output takes only values of U, and no processing takes place.
The C code for the same is given below:
#define S_FUNCTION_NAME reconstruction
#define S_FUNCTION_LEVEL 2
#include<stdio.h>
#include "simstruc.h"
#include<math.h>
#include<stdlib.h>
//========================================================
// Model Initialization
//========================================================
static void mdlInitializeSizes(SimStruct *S)
{
ssSetNumSFcnParams(S,0); // Define the number of input port parameters.
if(ssGetNumSFcnParams(S)!=ssGetSFcnParamsCount(S))
return; // IF THE PARAMETER EXPECTED MISMATCHES WITH ACTUAL PARAMETER THEN RETURN 0;
if (!ssSetNumInputPorts(S, 2)) return;
ssSetInputPortWidth(S,0,DYNAMICALLY_SIZED); // Input port width is dynamically updatable
ssSetInputPortWidth(S,1,DYNAMICALLY_SIZED);
ssSetInputPortDirectFeedThrough(S,0,1);
ssSetInputPortDirectFeedThrough(S,1,1);
if(!ssSetNumOutputPorts(S,1)) return; // The output will be the reconstructed signal
ssSetOutputPortWidth(S,0,DYNAMICALLY_SIZED);
static void mdlInitializeSampleTimes(SimStruct *S)
{
ssSetSampleTime(S,0,INHERITED_SAMPLE_TIME); // Declares about the inherited Sample Time
ssSetOffsetTime(S,0,0);
}
static void mdlOutputs(SimStruct *S, int_T tid)
{
InputRealPtrsType U =ssGetInputPortRealSignalPtrs(S,0); // Imaginary Signal
InputRealPtrsType V =ssGetInputPortRealSignalPtrs(S,1); // Real Signal;
real_T *R= ssGetOutputPortRealSignal(S,0);
//real_T *I=ssGetOutputPortRealSignal(S,1);
int_T width = ssGetOutputPortWidth(S,0);
int_T i;
real_T Omega=(2*3.1415926535898/8);
UNUSED_ARG(tid);
// value of omega is assigned.
for (i=0; i<width; i++)
{
R[i]= ((*U[i]) * cos(Omega*i))-((*V[i]) * sin(Omega*i));
}
}
static void mdlTerminate(SimStruct *S)
{
}
#ifdef MATLAB_MEX_FILE
#include "simulink.c"
#else
#include "cg_sfun.h"
#endif
How to address the problem... Thanks and Regards.
Answers (0)
Categories
Find more on Create MATLAB S-Functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!