This example shows how to replace a function in a MATLAB Function block with a lookup table approximation and convert the system under design to use fixed-point data types.
When working with MATLAB Function blocks, some functions and MATLAB language constructs are not supported for the automated fixed-point conversion workflows. To work around unsupported functions:
Identify Unsupported Functions in MATLAB Function Blocks in System Under Design
Open the ex_mySinh
model.
Create a DataTypeWorkflow.Converter
object for the system under design, SubsystemA
, which contains the MATLAB Function block of interest.
Use the getUnsupportedFunctionsForSystem
function to identify unsupported functions used by MATLAB Function blocks in the system under design.
unsupportedFunctions = 1×1 cell array
{'sinh'}
The system under design contains a MATLAB Function block which computes the hyperbolic sine of the input. The sinh
function is not supported for fixed-point conversion.
function y = my_sinh(u)
%#codegen
y = sinh(u);
Generate Optimized Lookup Table Approximation for Unsupported Function
Use the FunctionApproximation.compressLookupTables
object to generate an optimized lookup table replacement for the sinh
function.
Create a FunctionApproximation.Options
object and set the ApproximateSolutionType
to 'MATLAB'
to output the optimized lookup table as a MATLAB function.
Create a FunctionApproximation.Problem
object to define the approximation problem.
Specify additional constraints on the function approximation problem.
Use the solve
method to solve the optimization problem. The software displays the iterations of the optimization process.
Searching for fixed-point solutions.
| ID | Memory (bits) | Feasible | Table Size | Breakpoints WLs | TableData WL | BreakpointSpecification | Error(Max,Current) |
| 0 | 64 | 1 | 2 | 16 | 16 | EvenSpacing | 7.812500e-03, 1.675454e-03 |
| 1 | 64 | 1 | 2 | 16 | 16 | EvenPow2Spacing | 7.812500e-03, 1.675454e-03 |
Best Solution
| ID | Memory (bits) | Feasible | Table Size | Breakpoints WLs | TableData WL | BreakpointSpecification | Error(Max,Current) |
| 1 | 64 | 1 | 2 | 16 | 16 | EvenPow2Spacing | 7.812500e-03, 1.675454e-03 |
Save the best solution in the current working directory.
Proposed Fixed-Point Data Types for System Under Design
Simulate the model and store the results in a run titled InitialRun
.
Collect ranges for the model. Use a shortcut to override the system with double-precision data types and enable instrumentation.
Propose data types for the system.
Register Function Replacements
Create a DataTypeWorkflow.FunctionReplacement
object to manage custom function replacements for the converter
object.
Use the insert
method to register a function replacement. For this example, replace the unsupported sinh
function in the MATLAB Function block with the lookup table approximation, sinhApproximate
.
Use the entries
method to display a list of all registered function replacements.
ans=1×2 table
Key Value
______ _________________
"sinh" "sinhApproximate"
Use the lookup
method to look up a registered function replacement for a particular function.
You can use the remove
method to remove registered function replacements.
functionReplacement.remove('sinh');
Apply Function Replacements and Proposed Data Types
Use the applyDataTypes
method to apply the registered function replacements to MATLAB Function blocks in the system under design and apply proposed fixed-point data types to the model.