# tune

## Description

runs the `tunedProperties`

= tune(`tuner`

,`detectionLog`

,`truth`

)`trackingFilterTuner`

object, tunes the filter based on the
detection log and the truth data, and returns the tuned filter properties.

## Examples

### Customize Tunable Properties and Tune Tracking Filter

Load the tuning data containing the truth and detection data. The truth data has the position and velocity of one target for a duration of 9.5 seconds. The detection data has object detections of ten Monte-Carlo runs for the same period.

load("filterTuningData.mat","truth","detlog");

Create a `trackingFilterTuner`

object. Specify the `FilterInitializationFcn`

property as `"initcvkf"`

that corresponds to a `trackingKF`

filter object with a constant velocity model.

`tuner = trackingFilterTuner(FilterInitializationFcn ="initcvkf");`

You can obtain the filter by evaluating the initialization function on an object detection.

filter = feval(tuner.FilterInitializationFcn,detlog{1})

filter = trackingKF with properties: State: [6x1 double] StateCovariance: [6x6 double] MotionModel: '3D Constant Velocity' ProcessNoise: [3x3 double] MeasurementModel: [3x6 double] MeasurementNoise: [3x3 double] MaxNumOOSMSteps: 0 EnableSmoothing: 0

To customize the tunable properties of the filter, first get the default tunable properties of the filter.

tps = tunableProperties(filter)

tps = Tunable properties for object of type: trackingKF Property: ProcessNoise PropertyValue: [1 0 0;0 1 0;0 0 1] TunedQuantity: Square root IsTuned: true TunedQuantityValue: [1 0 0;0 1 0;0 0 1] TunableElements: [1 4 5 7 8 9] LowerBound: [0 0 0 0 0 0] UpperBound: [10 10 10 10 10 10] Property: StateCovariance PropertyValue: [3.53553390593274 0 0 0 0 0;0 100 0 0 0 0;0 0 3.53553390593274 0 0 0;0 0 0 100 0 0;0 0 0 0 3.53553390593274 0;0 0 0 0 0 100] TunedQuantity: Square root of initial value IsTuned: false

Based on the display, the tuner tunes only the `ProcessNoise`

property, which is a 3-by-3 matrix. Change the tunable elements to be only the diagonal elements by using the `setPropertyTunability`

object function. Set the lower and upper bounds for tuning the diagonal elements.

setPropertyTunability(tps,"ProcessNoise",TunableElements=[1 5 9], ... LowerBound=[0.01 0.01 0.01],UpperBound = [20 20 20])

To enable custom tunable properties, set the `TunablePropertiesSource`

and `CustomTunable`

properties to `"Custom"`

and `tps`

, respectively.

```
tuner.TunablePropertiesSource = "Custom";
tuner.CustomTunableProperties = tps;
```

Using the `tune`

object function, tune the filter with the detection log and the truth data.

tune(tuner,detlog,truth);

Iter RMSE Step Size 0 9.2177 1 9.1951 0.1509 2 9.0458 1.5108 3 9.0456 0.0186 4 9.0452 0.0705 5 9.0452 0.0068 6 9.0452 0.0016 7 9.0451 0.1422 8 9.0450 0.0600 9 9.0450 0.0182 10 9.0450 0.0105

Generate the filter initialization function after tuning by using the `exportToFunction`

object function.

`exportToFunction(tuner,"tunedInitFcn")`

Obtain the tuned filter by evaluating the tuned initialization function on an object detection. Show the tuned process noise.

tunedFilter = tunedInitFcn(detlog{1})

tunedFilter = trackingKF with properties: State: [6x1 double] StateCovariance: [6x6 double] MotionModel: '3D Constant Velocity' ProcessNoise: [3x3 double] MeasurementModel: [3x6 double] MeasurementNoise: [3x3 double] MaxNumOOSMSteps: 0 EnableSmoothing: 0

tunedFilter.ProcessNoise

`ans = `*3×3*
0.0001 0 0
0 0.0156 0
0 0 0.0001

## Input Arguments

`tuner`

— Tracking filter tuner

`trackingFilterTuner`

object

Tracking filter tuner, specified as a `trackingFilterTuner`

object.

`detectionLog`

— Detection log

cell arrays of `objectDetection`

objects

Detection log, specified as cell arrays of `objectDetection`

objects.

Specify `detectionLog`

based on the number of truth targets that you specify in the `truth`

input argument.

One truth target — Specify

`detectionLog`

as a*T*-by-*N*cell array of`objectDetection`

objects.*N*is the number of Monte-Carlo runs. The number of time steps*T*does not have to be equal to the number of rows in the truth table. The timestamp of each detection does not have to match the time in the truth table.Multiple truth targets — Specify

`detectionLog`

as an*M*-element cells, where each cell contains a cell array of`objectDetection`

objects for the corresponding truth timetable.*M*is the number of truth tables. Across the*M*cells, the detection time, the number of detections, and the number of Monte-Carlo runs do not have to be the same.

`truth`

— Truth data

structure | table | timetable | cell array of structures | cell array of tables | cell array of timetables

Truth data, specified as a structure, table, timetable, cell array of structures, cell array of tables, cell array of timetables.

You can specify `truth`

for one or more truth targets.

One truth target — Specify

`truth`

as a truth structure, table, or timetable.When specified as a structure, the structure must contain a field

`Time`

and either`Position`

and`Velocity`

fields or a`State`

field.`tune`

converts the structure to a timetable and uses it as defined below.When specified as a table, or timetable, the

`truth`

must have these variables as columns:`Time`

— Time of the truth information, specified as a single, double, or`duration`

in each table row.`Position`

— Position of the target at the time, specified as a 1-by-3 real-valued vector in each table row.`Velocity`

— Velocity of the target at the time, specified as a 1-by-3 real-valued vector in each table row. This variable is optional.`State`

— State of the target at the time, specified as 1-by-*S*real-valued vector in each table row, where*S*is dimension of the state.**Note**When you specify the

`State`

column, the tuner ignores the`Position`

and`Velocity`

columns if specified.The definition of the

`State`

column must be exactly the same as the state definition in the tuned filter. For example, if the filter defines its state as [*x**y**v**q*], where*x*and*y*are positions in a 2-D rectangular frame,*v*is the speed, and*q*is the heading angle, you must specify the timetable with a`State`

column in the same [*x**y**v**q*] definition.

Multiple truth targets — Specify

`truth`

as an*M*-element cell array of truth structures, tables, or timetables. Each truth structure, table, or timetable can have different initial and end times.

## Output Arguments

`tunedProperties`

— Tuned properties

structure

Tuned properties, returned as a structure. For different tracking filter objects,
this structure can have different fields since the tunable properties of each tracking
filter can be different. For example, for the `trackingEKF`

object, this structure has two fields:
`ProcessNoise`

and `StateCovariance`

.

You can use the `setTunedProperties`

object function of each filter object to apply the
tuned properties.

## Version History

**Introduced in R2022b**

### R2024a: Specify truth tuning data using tables or structure

When specifying the truth timetable input, you can use tables or structures. Previously, you could only use timetables.

### R2023b: Specify truth tuning data using state variable

When specifying the truth timetable input of the `tune`

function,
you can use a column called `State`

to describe the truth target state.
Previously, you had to use the `Position`

and `Velocity`

columns to describe the truth target state. Using the `State`

column
provides more flexibility on defining target state.

Note that when the truth timetable contains the `State`

column, the
tuner ignores the `Position`

and `Velocity`

columns if
specified.

## MATLAB Command

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)