简述网站的制作流程,成都设计院招聘,最专业的微网站开发,启动网站建设的请示文章目录
前言
C MEX S-Function
算法原理
原始信号创建
编写S函数
仿真验证
Tips
分析和应用
总结 前言 见《开箱报告#xff0c;Simulink Toolbox库模块使用指南#xff08;一#xff09;——powergui模块》 见《开箱报告#xff0c;Simulink Toolbox库模块使用…文章目录
前言
C MEX S-Function
算法原理
原始信号创建
编写S函数
仿真验证
Tips
分析和应用
总结 前言 见《开箱报告Simulink Toolbox库模块使用指南一——powergui模块》 见《开箱报告Simulink Toolbox库模块使用指南二——MATLAB Fuction模块》 见《开箱报告Simulink Toolbox库模块使用指南三——Simscape 电路仿真模块》 见《开箱报告Simulink Toolbox库模块使用指南四——S-Fuction模块》
C MEX S-Function C MEX S-Function是使用C语言开发的一种S-Fuction具备前一篇文章中讲解的S-Fuction的全部基本特性。它对应S-Fuction中的Level2类型支持访问更广泛的 S-Function API 集并支持代码生成。由于汽车电子工程与C语言密不可分所以我们必须对C MEX S-Function重点关注。 Mathworks官方Help对该模块的说明如下所示。 本文以DFT算法为例介绍如何利用C MEX S-Function搭建项目需求中高度自定义的信号解耦模块。
算法原理 傅里叶变换告诉我们任何周期信号都可以分解为正弦波的叠加。具体的做法是将被求解的原始信号与目标频率的信号相乘然后再积分就得到了原始信号在该频率上的分量公式如下 原始信号S; 目标频率信号D_cos cos(2pi*w*t); 目标频率信号D_sin sin(2pi*w*t); 目标信号实部D_real dot(S,D_cos)/N*2; 目标信号虚部D_imag dot(S,D_sin)/N*2; 目标信号模数D_modl sqrt(D_real^2 D_imag^2);
原始信号创建 这里沿用前一篇文章中的电路方程模型见《开箱报告Simulink Toolbox库模块使用指南四——S-Fuction模块》 创建电压和电流信号如下 t(S) (0 : 4999)*0.0001; I(A) 4.235 0.035*sin(2pi * 50t pi); U(A) 3.529 0.071*sin(2pi * 50t); 在Matlab的命令窗口中运行该动态方程得到的电流和电压曲线与前一篇文章一致如下所示 编写S函数 根据官方的Basic C MEX S-Function模板写出的S函数完整代码如下
#define S_FUNCTION_NAME DFT_CMexSfunc //函数名字与C文件名一致
#define S_FUNCTION_LEVEL 2
#include simstruc.h //Matlab宏函数库static real_T Fs 10e3; //信号采样频率与信号源一致
static real_T L 5e3; //信号采样点个数两者根据Nyquist定理计算/* Function: mdlInitializeSizes * Abstract:* The sizes information is used by Simulink to determine the S-function* blocks characteristics (number of inputs, outputs, states, etc.).*/
static void mdlInitializeSizes(SimStruct *S)
{//一个算法参数Freq目标解算频率ssSetNumSFcnParams(S, 1); /* Number of expected parameters */if (ssGetNumSFcnParams(S) ! ssGetSFcnParamsCount(S)) {/* Return if number of expected ! number of actual parameters */return;}ssSetNumContStates(S, 0);ssSetNumDiscStates(S, 4); //离散状态的个数if (!ssSetNumInputPorts(S, 1)) return;ssSetInputPortWidth(S, 0, 1); //一个信号输入端口信号维度1ssSetInputPortRequiredContiguous(S, 0, true); /*direct input signal access*//** Set direct feedthrough flag (1yes, 0no).* A port has direct feedthrough if the input is used in either* the mdlOutputs or mdlGetTimeOfNextVarHit functions.*/ssSetInputPortDirectFeedThrough(S, 0, 1);if (!ssSetNumOutputPorts(S, 1)) return;ssSetOutputPortWidth(S, 0, 1); //一个信号输出端口信号维度1ssSetNumSampleTimes(S, 1);ssSetNumRWork(S, 0);ssSetNumIWork(S, 0);ssSetNumPWork(S, 0);ssSetNumModes(S, 0);ssSetNumNonsampledZCs(S, 0);/* Specify the operating point save/restore compliance to be same as a * built-in block */ssSetOperatingPointCompliance(S, USE_DEFAULT_OPERATING_POINT);ssSetOptions(S, 0);
}/* 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, CONTINUOUS_SAMPLE_TIME);ssSetSampleTime(S, 0, 0.001); //算法运行周期0.001s不同于信号采样频率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){//离散状态赋初值real_T Count 1;real_T t 0;real_T cos_integ 0;real_T sin_integ 0;real_T *x0 ssGetRealDiscStates(S);*x0 Count;*x0 t;*x0 cos_integ;*x0 sin_integ;}
#endif /* MDL_INITIALIZE_CONDITIONS */#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 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.*/
static void mdlOutputs(SimStruct *S, int_T tid)
{real_T real 0;real_T imag 0;real_T modl 0;real_T *y ssGetOutputPortSignal(S,0);real_T *x ssGetRealDiscStates(S);if(x[0]L1){real x[2]/L*2;imag x[3]/L*2;modl sqrt(real*real imag*imag);y[0] modl; //解算结果输出}
}#define 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){real_T Sr_cos;real_T Sr_sin;real_T T;real_T Freq (real_T) *mxGetPr(ssGetSFcnParam(S,0));real_T *x ssGetRealDiscStates(S);const real_T *u (const real_T*) ssGetInputPortSignal(S,0);if(x[0]L){ Sr_cos cos(2*3.14 * Freq*x[1]);Sr_sin sin(2*3.14 * Freq*x[1]);x[2] x[2] u[0]*Sr_cos;x[3] x[3] u[0]*Sr_sin;x[0] x[0] 1;T 1/Fs;x[1] x[1] T;}}
#endif /* MDL_UPDATE *//* 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)
{
}C代码编写好之后用Matlab指令 mex DFT_CMexSfunc.c 进行编译。如果代码没有错误编译成功后会看到如下提示 仿真验证 将上述编写好的C MEX S-Fuction模块放入Simulink模型中进行验证如下所示 运行上述模型得到的电流和电压如上图所示也与前一篇文章一致。 至此可以证明该C MEX S-Fuction模块可以较好地求解耦合信号中的自信号分量。
Tips C MEX S-Fuction模块能够让使用者充分自由地开发Simulink模块一方面是跨语言的程序开发方式只需要include其他c文件的头文件即可调用其中已开发和验证好的c函数。另一方面是大量的能够与Simulink引擎交互的的宏函数使得开发人员有了更大的发挥空间。一些常用的必须熟练掌握宏函数如下
1.系统输入宏函数
ssSetNumInputPorts(S, 1)
ssSetInputPortWidth(S, 0, 1)
ssSetInputPortDirectFeedThrough(S, 0, 1)
real_T *u (real_T*) ssGetInputPortSignal(S,0);
Temp u[0];
2.系统参数宏函数
ssSetNumSFcnParams(S, 1);
real_T Pa1 (real_T) *mxGetPr(ssGetSFcnParam(S,0));
//real_T Pa2 (real_T) *mxGetPr(ssGetSFcnParam(S,1));
Temp Pa1;
3.系统周期宏函数
ssSetSampleTime(S, 0, 0.001);
ssSetOffsetTime(S, 0, 0.0);
4.系统状态宏函数
ssSetNumDiscStates(S, 4);
real_T *x ssGetRealDiscStates(S);
*x a;
*x b;
*x c;
*x d;
x[0] x[0] 1;
x[1] x[1] 1;
x[2] x[2] 1;
x[3] x[3] 1;
5.系统输出宏函数
ssSetNumOutputPorts(S, 1)
ssSetOutputPortWidth(S, 0, 1)
real_T *y ssGetOutputPortSignal(S,0);
y[0] a;
//y[1] b;
//y[2] c;
//y[3] d;
分析和应用 C MEX S-Fuction是S-Fuction的一种他们的相同点一样不同点在于灵活度和自由度进一步延伸功能进一步扩展。比如本文举例的DFT求解模块是在原有DFT算法的基础上进一步裁剪只求解目标期望频率上的信号分量并且把原本算法中集中计算的几个向量积分、乘除、开方等运算分解到每一个运算周期中变成单个变量的运算需要时还可以灵活调整指定N个周期把算法执行完以此通过延长运算时间来节省算法对单个周期硬件算力的开销不仅能够保证整个系统的实时性能还能大大提高算法求解得数据量以此提高求解精度。同时本文举例的DFT求解求解算法也是以前开发的经过验证的模块功能利用C MEX S-Fuction提供的C函数调用机制无缝衔接的移植了过来。
总结 以上就是本人在使用C MEX S-Fuction模块时一些个人理解和分析的总结首先介绍了该模块的背景知识然后展示它的使用方法最后分析了该模块的特点和适用场景。 后续还会分享另外几个最近总结的Simulink Toolbox库模块欢迎评论区留言、点赞、收藏和关注这些鼓励和支持都将成文本人持续分享的动力。 另外上述例程使用的Demo工程可以到笔者的主页查找和下载。 版权声明原创文章转载和引用请注明出处和链接侵权必究 文章转载自: http://www.morning.sqhtg.cn.gov.cn.sqhtg.cn http://www.morning.mzjbz.cn.gov.cn.mzjbz.cn http://www.morning.ttvtv.cn.gov.cn.ttvtv.cn http://www.morning.jycr.cn.gov.cn.jycr.cn http://www.morning.pjzcp.cn.gov.cn.pjzcp.cn http://www.morning.rqmr.cn.gov.cn.rqmr.cn http://www.morning.jrqw.cn.gov.cn.jrqw.cn http://www.morning.kmbgl.cn.gov.cn.kmbgl.cn http://www.morning.kdnrp.cn.gov.cn.kdnrp.cn http://www.morning.csdgt.cn.gov.cn.csdgt.cn http://www.morning.jlxld.cn.gov.cn.jlxld.cn http://www.morning.rwfp.cn.gov.cn.rwfp.cn http://www.morning.rnygs.cn.gov.cn.rnygs.cn http://www.morning.ntgsg.cn.gov.cn.ntgsg.cn http://www.morning.jbmbj.cn.gov.cn.jbmbj.cn http://www.morning.khxwp.cn.gov.cn.khxwp.cn http://www.morning.twmp.cn.gov.cn.twmp.cn http://www.morning.mrttc.cn.gov.cn.mrttc.cn http://www.morning.ylph.cn.gov.cn.ylph.cn http://www.morning.xmyrn.cn.gov.cn.xmyrn.cn http://www.morning.lgnbr.cn.gov.cn.lgnbr.cn http://www.morning.zmtrk.cn.gov.cn.zmtrk.cn http://www.morning.nrll.cn.gov.cn.nrll.cn http://www.morning.lbxhy.cn.gov.cn.lbxhy.cn http://www.morning.rwqj.cn.gov.cn.rwqj.cn http://www.morning.ssxlt.cn.gov.cn.ssxlt.cn http://www.morning.qsctt.cn.gov.cn.qsctt.cn http://www.morning.btjyp.cn.gov.cn.btjyp.cn http://www.morning.jtmql.cn.gov.cn.jtmql.cn http://www.morning.ummpdl.cn.gov.cn.ummpdl.cn http://www.morning.c7510.cn.gov.cn.c7510.cn http://www.morning.kzdwt.cn.gov.cn.kzdwt.cn http://www.morning.mbpzw.cn.gov.cn.mbpzw.cn http://www.morning.hqxyt.cn.gov.cn.hqxyt.cn http://www.morning.pqfbk.cn.gov.cn.pqfbk.cn http://www.morning.hphqy.cn.gov.cn.hphqy.cn http://www.morning.sdhmn.cn.gov.cn.sdhmn.cn http://www.morning.ybhrb.cn.gov.cn.ybhrb.cn http://www.morning.kfclh.cn.gov.cn.kfclh.cn http://www.morning.wdjcr.cn.gov.cn.wdjcr.cn http://www.morning.prxqd.cn.gov.cn.prxqd.cn http://www.morning.cspwj.cn.gov.cn.cspwj.cn http://www.morning.lnbcx.cn.gov.cn.lnbcx.cn http://www.morning.nkjxn.cn.gov.cn.nkjxn.cn http://www.morning.xckqs.cn.gov.cn.xckqs.cn http://www.morning.qtbnm.cn.gov.cn.qtbnm.cn http://www.morning.tqbqb.cn.gov.cn.tqbqb.cn http://www.morning.kjcfz.cn.gov.cn.kjcfz.cn http://www.morning.hlnrj.cn.gov.cn.hlnrj.cn http://www.morning.wngpq.cn.gov.cn.wngpq.cn http://www.morning.bnbzd.cn.gov.cn.bnbzd.cn http://www.morning.gkjyg.cn.gov.cn.gkjyg.cn http://www.morning.kdfqx.cn.gov.cn.kdfqx.cn http://www.morning.gfpyy.cn.gov.cn.gfpyy.cn http://www.morning.sbjhm.cn.gov.cn.sbjhm.cn http://www.morning.lcwhn.cn.gov.cn.lcwhn.cn http://www.morning.dzrcj.cn.gov.cn.dzrcj.cn http://www.morning.rpdmj.cn.gov.cn.rpdmj.cn http://www.morning.rbsmm.cn.gov.cn.rbsmm.cn http://www.morning.dlhxj.cn.gov.cn.dlhxj.cn http://www.morning.rqmr.cn.gov.cn.rqmr.cn http://www.morning.hrkth.cn.gov.cn.hrkth.cn http://www.morning.hrzhg.cn.gov.cn.hrzhg.cn http://www.morning.lpgw.cn.gov.cn.lpgw.cn http://www.morning.bpwdc.cn.gov.cn.bpwdc.cn http://www.morning.ztnmc.cn.gov.cn.ztnmc.cn http://www.morning.cttti.com.gov.cn.cttti.com http://www.morning.ymqrc.cn.gov.cn.ymqrc.cn http://www.morning.tynqy.cn.gov.cn.tynqy.cn http://www.morning.fnjrh.cn.gov.cn.fnjrh.cn http://www.morning.sfswj.cn.gov.cn.sfswj.cn http://www.morning.fcqlt.cn.gov.cn.fcqlt.cn http://www.morning.fbpyd.cn.gov.cn.fbpyd.cn http://www.morning.rcmwl.cn.gov.cn.rcmwl.cn http://www.morning.vnuwdy.cn.gov.cn.vnuwdy.cn http://www.morning.ai-wang.cn.gov.cn.ai-wang.cn http://www.morning.wnwjf.cn.gov.cn.wnwjf.cn http://www.morning.fjlsfs.com.gov.cn.fjlsfs.com http://www.morning.rqsr.cn.gov.cn.rqsr.cn http://www.morning.gmwdl.cn.gov.cn.gmwdl.cn