Main Content

Generate Code for Online Parameter Estimation in MATLAB

This topic shows how to generate C/C++ code from online estimation MATLAB® code that uses a System object™. C/C++ code is generated using the codegen (MATLAB Coder) command from MATLAB Coder™. Use the generated code to deploy online estimation algorithms to an embedded target.

You can also deploy online estimation code by creating a standalone application using MATLAB Compiler™. MATLAB Compiler software supports System objects for use inside MATLAB functions, but does not support System objects for use in MATLAB scripts.

For Simulink® based workflows, use the online estimator blocks from System Identification Toolbox™, such as Recursive Least Squares Estimator and Recursive Polynomial Model Estimator. You can generate C/C++ code and Structured Text for the online estimation blocks using Simulink Coder and Simulink PLC Coder™.

Supported Online Estimation Commands

Code generation support is available for these online estimation System objects:

Code generation support is available only for the following System object commands:

Generate Code for Online Estimation

To generate code for online estimation:

  1. Create a function to declare your System object as persistent, and initialize the object. You define the System object as persistent to maintain the object states between calls.

    function [A,B,EstimatedOutput] = arxonline(output,input)
    % Declare System object as persistent
    persistent obj;
    if isempty(obj)
        obj = recursiveARX([1 2 2],'EstimationMethod','Gradient');
    [A,B,EstimatedOutput] = step(obj,output,input);

    The function creates a System object for online estimation of an ARX model of order [1 2 1], using the unnormalized gradient algorithm, and estimation data, input and output. Save this function on the MATLAB path. Alternatively, you can specify the full path name for this function.

    The persistent System object is initialized with condition if isempty(obj) to ensure that the object is initialized only once, when the function is called the first time. Subsequent calls to the function just execute the step command to update the estimated parameters. During initialization you specify the nontunable properties of the object, such as EstimationMethod, Orders, and DataType.

  2. Generate C/C++ code and MEX-files using the codegen (MATLAB Coder) command from MATLAB Coder.

    codegen arxonline -args {1,1}

    The syntax -args {1,1} specifies a set of example arguments to your function. The example arguments set the dimensions and data types of the function arguments outputand input as double-precision scalars.

  3. Use the generated code.

    • Use the generated C/C++ code to deploy online model estimation to an embedded target.

    • Use the generated MEX-file for testing the compiled C/C++ code in MATLAB. The generated MEX-file is also useful for accelerating simulations of parameter estimation algorithms in MATLAB.

      Load the estimation data. In this example, use a static data set for illustration.

      load iddata3
      output = z3.y;
      input = z3.u;

      Update the model parameters by calling the generated MEX-file.

      for i = 1:numel(input)
          [A,B,EstimatedOutput] = arxonline_mex(output(i),input(i));

Rules and Limitations When Using System Objects in Generated MATLAB Code

The following rules and limitations apply to using online estimation System objects when writing MATLAB code suitable for code generation.

Object Construction and Initialization

  • If System objects are stored in persistent variables, initialize objects once by embedding the object handles in an if statement with a call to isempty( ).

  • Set arguments to System object constructors as compile-time constants when using the codegen command. For more information, see coder.Constant (MATLAB Coder).

  • Do not initialize System objects properties with other MATLAB class objects as default values in code generation. Initialize these properties in the constructor.

Inputs and Outputs

  • Do not change the data type of the System object inputs.

  • Do not pass a System object as an example input argument to a function being compiled with codegen (MATLAB Coder).

  • Do not pass a System object to functions declared as extrinsic (functions called in interpreted mode) using the coder.extrinsic function. System objects returned from extrinsic functions and scope System objects that automatically become extrinsic can be used as inputs to another extrinsic function, but they do not generate code.

Cell Arrays

  • Cell arrays cannot contain System objects.

Tunable and Nontunable Properties of System Objects

  • The value assigned to a nontunable property must be a constant, and there can be at most one assignment to that property (including the assignment in the constructor).

  • You can set the tunable properties of online estimation System objects at construction time or by using dot notation after that.

See Also

| | | | | | | | | | | (MATLAB Coder)

Related Examples

More About