MATLAB Examples

Reduce Memory Consumption by Storing Parameter Value in Small Data Type

When you use a parameter object (for example, Simulink.Parameter) to set block parameter values, you can configure the object to appear in the generated code as a tunable global variable. By default, the parameter object and the corresponding global variable typically uses the same data type as the signal or signals on which the block operates. For example, if the input signal of a Gain block uses the data type int16, the parameter object typically use the same data type. To reduce the amount of memory that this variable consumes, specify that the variable use a smaller integer data type such as int8.

Contents

Store Parameter Value in Integer Data Type

Open the example model rtwdemo_basicsc and configure it to show the generated names of blocks.

load_system('rtwdemo_basicsc')
set_param('rtwdemo_basicsc','HideAutomaticNames','off')
open_system('rtwdemo_basicsc')

In the model, select Display > Signals and Ports > Port Data Types. Many of the signals in the model use the data type single.

Select View > Model Data.

In the Model Data Editor, inspect the Parameters tab.

Click the Show/refresh additional information button.

Next to the Filter contents box, click the Filter using selection button.

In the model, click the Gain block. The Model Data Editor shows one row that corresponds to the Gain parameter of the block and one row that corresponds to the MATLAB variable K1, which sets the parameter value to 2. K1 resides in the base workspace.

At the command prompt, convert K1 into a Simulink.Parameter object.

K1 = Simulink.Parameter(K1);

In the Model Data Editor, click the Show/refresh additional information button again.

In the model, clear the selection of the Gain block and select the Gain block again. Because K1 is now a parameter object, you can configure properties such as data type, minimum value, and maximum value.

In the Model Data Editor, use the Data Type column to apply the data type int8 to K1.

K1.DataType = 'int8';

Set the Change view drop-down list to Code.

Use the Storage Class column to apply the storage class ExportedGlobal to K1. With this setting, K1 appears in the generated code as a global variable.

K1.CoderInfo.StorageClass = 'ExportedGlobal';

In the model, select View > Property Inspector. The Property Inspector shows you the properties and parameters of the selected Gain block.

In the Property Inspector, under Parameter Attributes, set Parameter data type to Inherit: Inherit from 'Gain'. With this setting, the Gain parameter of the block inherits the int8 data type from the parameter object.

set_param('rtwdemo_basicsc/Gain','ParamDataTypeStr',...
    'Inherit: Inherit from ''Gain''')

Generate code from the model.

rtwbuild('rtwdemo_basicsc')
### Starting build procedure for model: rtwdemo_basicsc
### Successful completion of build procedure for model: rtwdemo_basicsc

The generated file rtwdemo_basicsc.c defines the global variable K1 by using the data type int8_T, which corresponds to the data type int8 in Simulink.

file = fullfile('rtwdemo_basicsc_grt_rtw','rtwdemo_basicsc.c');
rtwdemodbtype(file,'/* Exported block parameters */','int8_T K1 = 2;',1,1)
/* Exported block parameters */
int8_T K1 = 2;                         /* Variable: K1

The code algorithm in the model step function uses K1 to calculate the output of the Gain block. The algorithm casts K1 to the data type real32_T (single) because the signals involved in the calculation use the data type real32_T.

rtwdemodbtype(file,' rtwdemo_basicsc_DW.X =','10U);',1,1)
    rtwdemo_basicsc_DW.X = (real32_T)K1 * look1_iflf_binlx
      (rtwdemo_basicsc_U.input2, rtCP_Table1_bp01Data, rtCP_Table1_tableData,
       10U);

Store Fixed-Point Parameter Value in Smaller Integer Data Type

Suppose you configure the signals in your model to use fixed-point data types. You want a gain parameter to appear in the generated code as a tunable global variable. You know the range of real-world values that you expect the parameter to assume (for example, between 0 and 4). If you can meet your application requirements despite reduced precision, you can reduce memory consumption by configuring the parameter to use a different data type than the input and output signals of the block.

Open the example model fxpdemo_direct_form2 and configure it to show the generated names of blocks.

load_system('fxpdemo_direct_form2')
set_param('fxpdemo_direct_form2','HideAutomaticNames','off')
open_system('fxpdemo_direct_form2')

Update the model diagram. Signals in this model use signed fixed-point data types with a word length of 16 and binary-point-only scaling.

Open the Gain5 block dialog box. The Gain parameter is set to 1.85. Suppose you want to configure this parameter.

Set Gain to myGainParam and click Apply.

Click the action button (with three vertical dots) next to the parameter value. Select Create.

In the Create New Data dialog box, set Value to Simulink.Parameter(1.85) and click Create. The Simulink.Parameter object myGainParam appears in the base workspace.

In the myGainParam property dialog box, set Storage class to ExportedGlobal and click OK. With this setting, myGainParam appears in the generated code as a global variable.

In the block dialog box, on the Parameter Attributes tab, set Parameter minimum to 0 and Parameter maximum to 4.

Set Parameter data type to fixdt(0,8) and click Apply.

Click the Show Data Type Assistant button. The Data Type Assistant shows that the expression fixdt(0,8) specifies an unsigned fixed-point data type with a word length of 8 and best-precision scaling. When you simulate or generate code, the block parameter chooses a fraction length (scaling) that enables the data type to represent values between the parameter minimum and maximum (0 and 4) with the best possible precision.

In the Data Type Assistant, set Scaling to Binary point. Click Calculate Best-Precision Scaling, Fixed-point details, and Refresh Details. The information under Fixed-point details shows that a fraction length of 5 can represent the parameter values with a precision of 0.03125.

Set Scaling back to Best precision and click OK. In this example, when you simulate or generate code, the block parameter chooses a fraction length of 5.

You can use these commands at the command prompt to create the object and configure the block:

myGainParam = Simulink.Parameter(1.85);
myGainParam.CoderInfo.StorageClass = 'ExportedGlobal';
set_param('fxpdemo_direct_form2/Gain5','Gain','myGainParam')
set_param('fxpdemo_direct_form2/Gain5','ParamMin','0','ParamMax','4')
set_param('fxpdemo_direct_form2/Gain5','ParamDataTypeStr','fixdt(0,8)')

Configure the model to produce a code generation report. To reduce clutter in the Command Window, clear the configuration parameter Verbose build.

set_param('fxpdemo_direct_form2','GenerateReport','on',...
    'LaunchReport','on','RTWVerbose','off')

Generate code from the model.

evalc('rtwbuild(''fxpdemo_direct_form2'')');

The generated file fxpdemo_direct_form2.c defines the global variable myGainParam by using the data type uint8_T, which corresponds to the specified word length, 8. The code initializes the variable by using an integer value that, given the fraction length of 5, represents the real-world parameter value 1.85.

file = fullfile('fxpdemo_direct_form2_grt_rtw','fxpdemo_direct_form2.c');
rtwdemodbtype(file,'/* Exported block parameters */','uint8_T myGainParam = 59U;',1,1)
/* Exported block parameters */
uint8_T myGainParam = 59U;             /* Variable: myGainParam

The code algorithm uses myGainParam to calculate the output of the Gain5 block. The algorithm uses a C shift to scale the result of the calculation.

rtwdemodbtype(file,'/* Gain: ''<Root>/Gain5'' */',...
'/* Gain: ''<Root>/Gain'' incorporates:',1,0)
  /* Gain: '<Root>/Gain5' */
  fxpdemo_direct_form2_B.Gain5 = (int16_T)((myGainParam *
    fxpdemo_direct_form2_B.UnitDelay1) >> 5);