generate code for online state estimation in matlab
you can generate c/c code from matlab® code
that uses , and objects for online state estimation. c/c code is generated
using the codegen
(matlab coder) command from matlab
coder™ software. 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™ software.
to generate c/c code for online state estimation:
create a function to declare your filter object as persistent, and initialize the object. you define the object as persistent to maintain the object states between calls.
function [correctedx] = ukfcodegen(output) % declare object as persistent. persistent obj; if isempty(obj) % initialize the object. obj = unscentedkalmanfilter(@vdpstatefcn,@vdpmeasurementfcn,[2;0]); obj.measurementnoise = 0.01; end % estimate the states. correctedx = correct(obj,output); predict(obj); end
the function creates an unscented kalman filter object for online state estimation of a van der pol oscillator with two states and one output. you use the previously written and saved state transition and measurement functions,
vdpstatefcn.m
andvdpmeasurementfcn.m
, and specify the initial state values for the two states as[2;0]
. hereoutput
is the measured output data. save theukfcodegen.m
function on the matlab path. alternatively, you can specify the full path name for this function.in the
ukfcodegen.m
function, the persistent object is initialized with conditionif isempty(obj)
to ensure that the object is initialized only once, when the function is called the first time. subsequent calls to the function only execute the and commands to update the state estimates. during initialization, you specify the nontunable properties of the object, such asstatetransitionfcn
(specified inukfcodegen.m
asvdpstatefcn.m
) andmeasurementfcn
(specified inukfcodegen.m
asvdpmeasurementfcn.m
). after that, you can specify only the tunable properties. for more information, see tunable and nontunable object properties.in the state transition and measurement functions you must use only commands that are supported for code generation. for a list of these commands, see (matlab coder). include the compilation directive
%#codegen
in these functions to indicate that you intend to generate code for the function. adding this directive instructs the matlab code analyzer to help you diagnose and fix violations that would result in errors during code generation. for an example, typevdpstatefcn.m
at the command line.generate c/c code and mex-files using the
codegen
(matlab coder) command from matlab coder software.codegen ukfcodegen -args {1}
the syntax
-args {1}
specifies an example of an argument to your function. the argument sets the dimensions and data types of the function argumentoutput
as a double-precision scalar.note
if you want a filter with single-precision floating-point variables, you must specify the initial value of the states as single-precision during object construction.
obj = unscentedkalmanfilter(@vdpstatefcn,@vdpmeasurementfcn,single([2;0]))
then to generate code, use the following syntax.
codegen ukfcodegen -args {{single(1)}
use the generated code.
use the generated c/c code to deploy online state 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 state estimation algorithms in matlab.
load the estimation data. suppose that your output data is stored in the
measured_data.mat
file.load measured_data.mat output
estimate the states by calling the generated mex-file.
for i = 1:numel(output) xcorrected = ukfcodegen_mex(output(i)); end
this example generates c/c code for compiling a mex-file. to generate code for other targets, see
codegen
(matlab coder) in the matlab coder documentation.
tunable and nontunable object properties
property type | extended kalman filter object | unscented kalman filter object | particle filter object |
---|---|---|---|
tunable properties that you can specify multiple times either during object construction, or afterward using dot notation | state , statecovariance ,
processnoise , and
measurementnoise | state , statecovariance ,
processnoise ,
measurementnoise , alpha ,
beta , and kappa |
particles and weights |
nontunable properties that you can specify only once, either during
object construction, or afterward using dot notation, but before using
the predict or correct
commands | statetransitionfcn ,
measurementfcn ,
statetransitionjacobianfcn , and
measurementjacobianfcn | statetransitionfcn and
measurementfcn | statetransitionfcn ,
measurementlikelihoodfcn ,
stateestimationmethod ,
stateorientation ,
resamplingpolicy and
resamplingmethod |
nontunable properties that you must specify during object construction | hasadditiveprocessnoise and
hasadditivemeasurementnoise | hasadditiveprocessnoise and
hasadditivemeasurementnoise |
see also
| |