中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當前位置: 首頁 > news >正文

合肥知名網(wǎng)站制作上海關(guān)鍵詞優(yōu)化排名哪家好

合肥知名網(wǎng)站制作,上海關(guān)鍵詞優(yōu)化排名哪家好,wordpress游戲主題下載,辦理建設(shè)銀行卡網(wǎng)站文章目錄 前言 C MEX S-Function 算法原理 原始信號創(chuàng)建 編寫S函數(shù) 仿真驗證 Tips 分析和應(yīng)用 總結(jié) 前言 見《開箱報告,Simulink Toolbox庫模塊使用指南(一)——powergui模塊》 見《開箱報告,Simulink Toolbox庫模塊使用…

文章目錄

前言

C MEX S-Function

算法原理

原始信號創(chuàng)建

編寫S函數(shù)

仿真驗證

Tips

分析和應(yīng)用

總結(jié)


前言

????????見《開箱報告,Simulink Toolbox庫模塊使用指南(一)——powergui模塊》

????????見《開箱報告,Simulink Toolbox庫模塊使用指南(二)——MATLAB Fuction模塊》

????????見《開箱報告,Simulink Toolbox庫模塊使用指南(三)——Simscape 電路仿真模塊》

????????見《開箱報告,Simulink Toolbox庫模塊使用指南(四)——S-Fuction模塊》

C MEX S-Function

????????C MEX S-Function是使用C語言開發(fā)的一種S-Fuction,具備前一篇文章中講解的S-Fuction的全部基本特性。它對應(yīng)S-Fuction中的Level2類型,支持訪問更廣泛的 S-Function API 集,并支持代碼生成。由于汽車電子工程與C語言密不可分,所以我們必須對C MEX S-Function重點關(guān)注。

????????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;

????????目標信號模數(shù):D_modl = sqrt(D_real^2 + D_imag^2);

原始信號創(chuàng)建

????????這里沿用前一篇文章中的電路方程模型,見《開箱報告,Simulink Toolbox庫模塊使用指南(四)——S-Fuction模塊》

????????創(chuàng)建電壓和電流信號如下:

????????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的命令窗口中運行該動態(tài)方程,得到的電流和電壓曲線,與前一篇文章一致,如下所示:

編寫S函數(shù)

????????根據(jù)官方的Basic C MEX S-Function模板,寫出的S函數(shù)完整代碼如下:

#define S_FUNCTION_NAME  DFT_CMexSfunc    //函數(shù)名字,與C文件名一致
#define S_FUNCTION_LEVEL 2
#include "simstruc.h"    //Matlab宏函數(shù)庫static real_T Fs = 10e3;    //信號采樣頻率,與信號源一致
static real_T L = 5e3;      //信號采樣點個數(shù),兩者根據(jù)Nyquist定理計算/* Function: mdlInitializeSizes ===============================================* Abstract:*    The sizes information is used by Simulink to determine the S-function*    block's characteristics (number of inputs, outputs, states, etc.).*/
static void mdlInitializeSizes(SimStruct *S)
{//一個算法參數(shù)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);      //離散狀態(tài)的個數(shù)if (!ssSetNumInputPorts(S, 1)) return;ssSetInputPortWidth(S, 0, 1);      //一個信號輸入端口,信號維度1ssSetInputPortRequiredContiguous(S, 0, true); /*direct input signal access*//** Set direct feedthrough flag (1=yes, 0=no).* 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){//離散狀態(tài)賦初值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]==L+1){real = x[2]/L*2;imag = x[3]/L*2;modl = sqrt(real*real + imag*imag);y[0] = modl;       //解算結(jié)果輸出}
}#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模塊能夠讓使用者充分自由地開發(fā)Simulink模塊,一方面是跨語言的程序開發(fā)方式,只需要include其他c文件的頭文件,即可調(diào)用其中已開發(fā)和驗證好的c函數(shù)。另一方面是大量的能夠與Simulink引擎交互的的宏函數(shù),使得開發(fā)人員有了更大的發(fā)揮空間。一些常用的,必須熟練掌握宏函數(shù)如下:

1.系統(tǒng)輸入宏函數(shù)

ssSetNumInputPorts(S, 1)

ssSetInputPortWidth(S, 0, 1)

ssSetInputPortDirectFeedThrough(S, 0, 1)

real_T *u = (real_T*) ssGetInputPortSignal(S,0);

Temp = u[0];

2.系統(tǒng)參數(shù)宏函數(shù)

ssSetNumSFcnParams(S, 1);?

real_T Pa1 = (real_T) *mxGetPr(ssGetSFcnParam(S,0));

//real_T Pa2 = (real_T) *mxGetPr(ssGetSFcnParam(S,1));

Temp = Pa1;

3.系統(tǒng)周期宏函數(shù)

ssSetSampleTime(S, 0, 0.001);

ssSetOffsetTime(S, 0, 0.0);

4.系統(tǒng)狀態(tài)宏函數(shù)

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.系統(tǒng)輸出宏函數(shù)

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;

分析和應(yīng)用

??????? C MEX S-Fuction是S-Fuction的一種,他們的相同點一樣,不同點在于靈活度和自由度進一步延伸,功能進一步擴展。比如本文舉例的DFT求解模塊,是在原有DFT算法的基礎(chǔ)上進一步裁剪,只求解目標期望頻率上的信號分量,并且把原本算法中集中計算的幾個向量積分、乘除、開方等運算分解到每一個運算周期中,變成單個變量的運算(需要時還可以靈活調(diào)整指定N個周期把算法執(zhí)行完),以此通過延長運算時間來節(jié)省算法對單個周期硬件算力的開銷,不僅能夠保證整個系統(tǒng)的實時性能,還能大大提高算法求解得數(shù)據(jù)量,以此提高求解精度。同時本文舉例的DFT求解求解算法也是以前開發(fā)的,經(jīng)過驗證的模塊功能,利用C MEX S-Fuction提供的C函數(shù)調(diào)用機制,無縫銜接的移植了過來。

總結(jié)

????????以上就是本人在使用C MEX S-Fuction模塊時,一些個人理解和分析的總結(jié),首先介紹了該模塊的背景知識,然后展示它的使用方法,最后分析了該模塊的特點和適用場景。

????????后續(xù)還會分享另外幾個最近總結(jié)的Simulink Toolbox庫模塊,歡迎評論區(qū)留言、點贊、收藏和關(guān)注,這些鼓勵和支持都將成文本人持續(xù)分享的動力。

????????另外,上述例程使用的Demo工程,可以到筆者的主頁查找和下載。


????????版權(quán)聲明,原創(chuàng)文章,轉(zhuǎn)載和引用請注明出處和鏈接,侵權(quán)必究!

http://m.risenshineclean.com/news/62329.html

相關(guān)文章:

  • 松原網(wǎng)站建設(shè)網(wǎng)站建設(shè)的好公司
  • 做網(wǎng)站網(wǎng)頁掙錢不免費刷seo
  • 深圳做網(wǎng)站公司地點十大免費網(wǎng)站推廣平臺
  • 百度搜索量seo要點
  • 圖床網(wǎng)站怎么做廣州seo團隊
  • 石家莊高鐵站123網(wǎng)址之家
  • 秦皇島網(wǎng)站制作方案電商網(wǎng)站怎樣優(yōu)化
  • 網(wǎng)站建設(shè)公司的網(wǎng)銷好做嗎百度輸入法免費下載
  • wordpress使用步驟杭州seo網(wǎng)站推廣排名
  • 云南房產(chǎn)網(wǎng)站建設(shè)seo的理解
  • 鹽城z做網(wǎng)站上海專業(yè)的seo公司
  • 專業(yè)網(wǎng)站建設(shè)策劃網(wǎng)絡(luò)營銷和網(wǎng)絡(luò)推廣
  • 做測算的網(wǎng)站影視后期培訓(xùn)機構(gòu)全國排名
  • 湖南網(wǎng)站建設(shè) 真好磐石網(wǎng)絡(luò)免費推廣網(wǎng)址
  • 山東青島網(wǎng)站建設(shè)樂天seo視頻教程
  • 做門窗投標網(wǎng)站優(yōu)化模型
  • 小學(xué)課程建設(shè)網(wǎng)站目標新網(wǎng)域名查詢
  • 百度免費做網(wǎng)站百度在線使用網(wǎng)頁版
  • spring boot 做網(wǎng)站關(guān)鍵詞優(yōu)化推廣公司
  • 手機網(wǎng)站自動適應(yīng)短鏈接在線生成器
  • 山東平臺網(wǎng)站建設(shè)制作百度網(wǎng)頁版下載安裝
  • 住建部網(wǎng)站2015年城市建設(shè)統(tǒng)計seo優(yōu)化的優(yōu)點
  • 奉賢區(qū)網(wǎng)站建設(shè)收錄網(wǎng)站排名
  • 福田祥菱q雙排小貨車報價及圖片廈門seo推廣外包
  • 天津自己制作網(wǎng)站網(wǎng)站新站整站排名
  • 網(wǎng)站開發(fā)網(wǎng)頁設(shè)計游戲代理加盟平臺
  • seo兼職論壇手機優(yōu)化大師官網(wǎng)
  • 怎么快速推廣網(wǎng)站萬網(wǎng)域名交易
  • 建行信用卡網(wǎng)站登錄網(wǎng)站域名查詢系統(tǒng)
  • 那個網(wǎng)站做外貿(mào)好百度大搜是什么