在波形生成器块的代码执行过程中,在输出波形之间切换
This example shows how to generate code that enables you to switch between stimulus waveforms during code execution.
对于波形生成器块,您无法制作波形的参数,例如振幅和相移,在生成的代码中可调。取而代之的是,您可以生成代码,使您可以从块中指定的一组波形变体中选择一个活动波形。在执行代码期间,您可以通过调整全局结构字段的值来激活变体。
您必须设置模型配置参数Default parameter behavior至Tunable
。然后,默认情况下,模型中的块参数在生成的代码中显示可调。这些参数可以为大型模型消耗大量内存。
Create the modelex_switch_waveform
。
open_system('ex_switch_waveform')
In the Waveform Generator block, configure this waveform:
正方形(amp,10,0,tuncycle)
In the base workspace, create the variablesamp
and占空比
。
AMP = 2.71;tuncycle = 50;
此波形表示您希望该应用程序在执行开始时使用的基线刺激。
假设在执行过程中,您要观察将波形频率从10到15和相移从0更改为0.5的效果。在波形生成器块中,添加这些波形变体:
square(amp,10,0.5,dutyCycle) square(amp,15,0,dutyCycle) square(amp,15,0.5,dutyCycle)
设置模型配置参数Default parameter behavior至Tunable
。
set_param('ex_switch_waveform',,,,'默认标准级'',,,,'Tunable')
从模型生成代码。
slbuild('ex_switch_waveform')
###启动构建过程:ex_switch_waveform ###成功完成代码生成:ex_switch_waveform构建摘要摘要摘要构建:模型动作重建原因=========================================================================================================== ex_switch_wave_waveform代码生成的代码生成信息文件。1个型号中的1个型号(0个模型已经最新)构建持续时间:0H 0M 14.841S
生成的文件ex_switch_waveform.h
定义存储模型可调节参数数据的标准结构类型。该结构包含一个值代表活动波形的字段。
file = fullfile('ex_switch_waveform_grt_rtw',,,,'ex_switch_waveform.h');rtwdemodbtype(文件,'/ *参数(默认存储) */',,,,...'/ *实时模型数据结构 */',1,0)
/ *参数(默认存储) */ struct P_EX_SWITCH_WAVEFORM_T_T_ {real_t waveformgeneratorstimulus_selec;/ *蒙版参数:waveformgeneratorStimulus_selec *引用:'/switch' */real_t gaincontrolsystem_gain;/ *表达式:3.5 *引用:' /gain(控制系统)' */};
文件ex_switch_waveform_data.c
定义一个全局结构变量,并将字段值初始化为1
。This value represents the baseline waveform.
file = fullfile('ex_switch_waveform_grt_rtw',,,,'ex_switch_waveform_data.c');rtwdemodbtype(文件,'/ *块参数(默认存储) */',,,,'};',1,1)
/* Block parameters (default storage) */ P_ex_switch_waveform_T ex_switch_waveform_P = { /* Mask Parameter: WaveformGeneratorStimulus_Selec * Referenced by: '/Switch' */ 1.0, /* Expression: 3.5 * Referenced by: ' /Gain (Control System)' */ 3.5 };
文件ex_switch_waveform.c
定义模型执行函数。该功能使用switch
语句确定活动波形的值,然后计算根级输出块的值,OUT1
。
file = fullfile('ex_switch_waveform_grt_rtw',,,,'ex_switch_waveform.c');rtwdemodbtype(文件,'switch ((int32_T)',,,,...'ex_switch_waveform_Y.Out1 =',1,1)
switch((int32_t)ex_switch_waveform_p.waveformgeneratorstimulus_selec){情况1:temp = temp- floor(temp)<= 0.5?2.71:-2.71;休息;情况2:temp = temp_0-落地(temp_0)<= 0.5?2.71:-2.71;休息;案例3:temp = temp_1-地板(temp_1)<= 0.5?2.71:-2.71;休息;默认值:temp = temp_2-落地(temp_2)<= 0.5? 2.71 : -2.71; break; } /* End of MultiPortSwitch: '/MultiportSwitch' */ /* Outport: ' /Out1' incorporates: * Gain: ' /Gain (Control System)' */ ex_switch_waveform_Y.Out1 = ex_switch_waveform_P.GainControlSystem_Gain * temp;
大调的ing code execution, to change the active waveform, adjust the value of the structure field in the global parameters structure.