This example shows some best practices for debugging your fixed-point code when you need more than out of the box conversion.
Create a local working folder, for example,
In your local working folder, create the following files.
From the apps gallery, open the Fixed-Point Converter app.
On the Select page, browse to the
kalman_filter.m file and click
Click Next. On the Define Input
Types page, browse to the
file. Click Autodefine Input Types.
The test file runs and plots the input noisy signal, the filtered signal, the ideal filtered signal, and the difference between the filtered and the ideal filtered signal.
Click Next. On the Convert to Fixed Point page, click Analyze to gather range information and data type proposals using the default settings.
Click Convert to apply the proposed data types.
Click the Test arrow and select the Log inputs
and outputs for comparison plots check box. Click
Test. The Fixed-Point Converter runs the test file
kalman_filter_tb.m to test the generated fixed-point
code. Floating-point and fixed-point simulations are run, with errors calculated
for the input and output variables.
The generated plots show that the current fixed-point implementation does not produce good results.
The error for the output variable
y is extremely high, at
over 282 percent.
Log any function input and output variables that you suspect are the cause of numerical issues to the output arguments of the top-level function.
kalman_filter in the Source Code
pane to return to the floating-point code.
When you select the Log inputs and outputs for comparison
plots option during the Test phase, the
input and output variables of the top level-function,
kalman_filter are automatically logged for
kalman_filter function calls the
matrix_solve function, which contains calls to several
other functions. To investigate whether numerical issues are originating in the
matrix_solve function, select
matrix_solve in the
Source Code pane.
In the Log Data column, select the function input and
output variables that you want to log. In this example, select all three,
The generated plot shows a large error for the output variable of the
On the Convert to Fixed Point page, click Settings.
Under fimath, set the Rounding
Nearest. Set the
Overflow action to
Click Convert to apply the new settings.
Click the arrow next to Test and ensure that Log inputs and outputs for comparison plots is selected. Enable logging for any function input or output variables. Click Test.
Examine the plot for top-level function output variable,
fimath settings improve the output,
but some error still remains.
Adjusting the default word length improves the accuracy of the generated fixed-point design.
Click Settings and change the default word length to
32. Click Convert to apply the new
Click Test. The error for the output variable
y is accumulating.
Close the Fixed-Point Converter and plot window.
kalman_stm function computes the state transition matrix, which
is a constant. You do not need to convert this function to fixed point. To avoid
unnecessary quantization through computation, replace this function with a
double-precision constant. By replacing the function with a constant, the state
transition matrix undergoes quantization only once.
kalman_filter function in the Source
Code pane. Edit the
Replace the call to the
kalman_stm function with the
A = [0.992114701314478, -0.125333233564304; 0.125333233564304, 0.992114701314478];
Click Analyze to refresh the proposals.
Click Convert to apply the new proposals.
Click Test. The error on the plot for the functions
y is now on the order of
10-6, which is acceptable for this design.