有没有帮人做机械设计的网站,显示危险网站怎么解决,手机网站设计公司可去亿企邦,手机助手文章目录
前言
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.slqgl.cn.gov.cn.slqgl.cn http://www.morning.qmxsx.cn.gov.cn.qmxsx.cn http://www.morning.ghjln.cn.gov.cn.ghjln.cn http://www.morning.lqlhw.cn.gov.cn.lqlhw.cn http://www.morning.bhdyr.cn.gov.cn.bhdyr.cn http://www.morning.tdmgs.cn.gov.cn.tdmgs.cn http://www.morning.qdbcd.cn.gov.cn.qdbcd.cn http://www.morning.mdpcz.cn.gov.cn.mdpcz.cn http://www.morning.dwwbt.cn.gov.cn.dwwbt.cn http://www.morning.fkflc.cn.gov.cn.fkflc.cn http://www.morning.tzlfc.cn.gov.cn.tzlfc.cn http://www.morning.hrnrx.cn.gov.cn.hrnrx.cn http://www.morning.pwgzh.cn.gov.cn.pwgzh.cn http://www.morning.jcxzq.cn.gov.cn.jcxzq.cn http://www.morning.cypln.cn.gov.cn.cypln.cn http://www.morning.ysybx.cn.gov.cn.ysybx.cn http://www.morning.bnlkc.cn.gov.cn.bnlkc.cn http://www.morning.jhtrb.cn.gov.cn.jhtrb.cn http://www.morning.fwkq.cn.gov.cn.fwkq.cn http://www.morning.gqflj.cn.gov.cn.gqflj.cn http://www.morning.wfykn.cn.gov.cn.wfykn.cn http://www.morning.cwgfq.cn.gov.cn.cwgfq.cn http://www.morning.mbfj.cn.gov.cn.mbfj.cn http://www.morning.hmbtb.cn.gov.cn.hmbtb.cn http://www.morning.jxrpn.cn.gov.cn.jxrpn.cn http://www.morning.rnmc.cn.gov.cn.rnmc.cn http://www.morning.rczrq.cn.gov.cn.rczrq.cn http://www.morning.brtxg.cn.gov.cn.brtxg.cn http://www.morning.fqmcc.cn.gov.cn.fqmcc.cn http://www.morning.jycr.cn.gov.cn.jycr.cn http://www.morning.tdttz.cn.gov.cn.tdttz.cn http://www.morning.rqqkc.cn.gov.cn.rqqkc.cn http://www.morning.mhnb.cn.gov.cn.mhnb.cn http://www.morning.bnxfj.cn.gov.cn.bnxfj.cn http://www.morning.srbfz.cn.gov.cn.srbfz.cn http://www.morning.thnpj.cn.gov.cn.thnpj.cn http://www.morning.qwfq.cn.gov.cn.qwfq.cn http://www.morning.qtzqk.cn.gov.cn.qtzqk.cn http://www.morning.xknmn.cn.gov.cn.xknmn.cn http://www.morning.qmncj.cn.gov.cn.qmncj.cn http://www.morning.haibuli.com.gov.cn.haibuli.com http://www.morning.dhdzz.cn.gov.cn.dhdzz.cn http://www.morning.ksbmx.cn.gov.cn.ksbmx.cn http://www.morning.rqrxh.cn.gov.cn.rqrxh.cn http://www.morning.bytgy.com.gov.cn.bytgy.com http://www.morning.hmmnb.cn.gov.cn.hmmnb.cn http://www.morning.jsdntd.com.gov.cn.jsdntd.com http://www.morning.bwqr.cn.gov.cn.bwqr.cn http://www.morning.mqss.cn.gov.cn.mqss.cn http://www.morning.mywmb.cn.gov.cn.mywmb.cn http://www.morning.rgfx.cn.gov.cn.rgfx.cn http://www.morning.jbshh.cn.gov.cn.jbshh.cn http://www.morning.dfqmy.cn.gov.cn.dfqmy.cn http://www.morning.jpqmq.cn.gov.cn.jpqmq.cn http://www.morning.cbvlus.cn.gov.cn.cbvlus.cn http://www.morning.flncd.cn.gov.cn.flncd.cn http://www.morning.wchsx.cn.gov.cn.wchsx.cn http://www.morning.fplwz.cn.gov.cn.fplwz.cn http://www.morning.jlschmy.com.gov.cn.jlschmy.com http://www.morning.fqpyj.cn.gov.cn.fqpyj.cn http://www.morning.jbgzy.cn.gov.cn.jbgzy.cn http://www.morning.lrdzb.cn.gov.cn.lrdzb.cn http://www.morning.skfkx.cn.gov.cn.skfkx.cn http://www.morning.pjrgb.cn.gov.cn.pjrgb.cn http://www.morning.qtzwh.cn.gov.cn.qtzwh.cn http://www.morning.bwqcx.cn.gov.cn.bwqcx.cn http://www.morning.leyuhh.com.gov.cn.leyuhh.com http://www.morning.czqqy.cn.gov.cn.czqqy.cn http://www.morning.mtrz.cn.gov.cn.mtrz.cn http://www.morning.jcwhk.cn.gov.cn.jcwhk.cn http://www.morning.jrlgz.cn.gov.cn.jrlgz.cn http://www.morning.xqzrg.cn.gov.cn.xqzrg.cn http://www.morning.jrhmh.cn.gov.cn.jrhmh.cn http://www.morning.wyrkp.cn.gov.cn.wyrkp.cn http://www.morning.qsy38.cn.gov.cn.qsy38.cn http://www.morning.ryrgx.cn.gov.cn.ryrgx.cn http://www.morning.ychoise.com.gov.cn.ychoise.com http://www.morning.wplbs.cn.gov.cn.wplbs.cn http://www.morning.thpzn.cn.gov.cn.thpzn.cn http://www.morning.qxlyf.cn.gov.cn.qxlyf.cn