Tune Phase Parameter of Sine Wave Block During Code Execution

Under certain conditions, you cannot configure the Phase parameter of a Sine Wave block to appear in the generated code as a tunable global variable (for more information, see the block reference page). This example shows how to generate code so that you can tune the phase during execution.

Create the model ex_phase_tunable by using a Digital Clock block.

open_system('ex_phase_tunable')

Set Default parameter behavior to Tunable so that the parameters of the Sine Wave block appear in the generated code as tunable fields of the global parameter structure.

set_param('ex_phase_tunable','DefaultParameterBehavior','Tunable')

Generate code from the model.

rtwbuild('ex_phase_tunable')
### Starting build procedure for model: ex_phase_tunable
### Successful completion of code generation for model: ex_phase_tunable

In the code generation report, view the file ex_phase_tunable.c. The code algorithm in the model step function calculates the Sine Wave block output. The parameters of the block, including Phase, appear in the code as tunable structure fields.

file = fullfile('ex_phase_tunable_grt_rtw','ex_phase_tunable.c');
rtwdemodbtype(file,'/* Outport: ''<Root>/Out1'' incorporates:',...
    'ex_phase_tunable_P.SineWave_Bias;',1,1)
  /* Outport: '<Root>/Out1' incorporates:
   *  DigitalClock: '<Root>/Digital Clock'
   *  Sin: '<Root>/Sine Wave'
   */
  ex_phase_tunable_Y.Out1 = sin(ex_phase_tunable_P.SineWave_Freq *
    (((ex_phase_tunable_M->Timing.clockTick1+
       ex_phase_tunable_M->Timing.clockTickH1* 4294967296.0)) * 1.0) +
    ex_phase_tunable_P.SineWave_Phase) * ex_phase_tunable_P.SineWave_Amp +
    ex_phase_tunable_P.SineWave_Bias;

During code execution, you can assign new values to the structure field that corresponds to the Phase parameter.

Related Topics