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 and . 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:
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'); end [a,b,estimatedoutput] = step(obj,output,input); end
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
andoutput
. 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 thestep
command to update the estimated parameters. during initialization you specify the nontunable properties of the object, such asestimationmethod
,orders
, anddatatype
.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 argumentsoutput
andinput
as double-precision scalars.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)); end
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 toisempty( )
.set arguments to system object constructors as compile-time constants when using the
codegen
command. for more information, seecoder.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
| | | | | | | | | | | codegen
(matlab coder)