approximate functions with a direct lookup table
using the lookup table optimizer, you can generate a direct lookup table approximating a simulink® block, or a function. direct lookup tables are efficient to implement on hardware because they do not require any calculations.
generate a two-dimensional direct lookup table approximation
create a functionapproximation.problem
object specifying the function for which to generate the approximate. to generate a direct lookup table, set the interpolation method to none
in the functionapproximation.options
object.
problem = functionapproximation.problem('atan2'); problem.inputtypes = [numerictype(0,4,2) numerictype(0,8,4)]; problem.outputtype = fixdt(0,8,7); problem.options.interpolation = "none"; problem.options.abstol = 2^-4; problem.options.reltol = 0; problem.options.wordlengths = 1:8;
use the solve
method to generate the optimal lookup table.
solution = solve(problem)
| id | memory (bits) | feasible | table size | intermediate wls | tabledata wl | error(max,current) | | 0 | 32768 | 1 | [16 256] | [4 8] | 8 | 6.250000e-02, 3.902460e-03 | | 1 | 28672 | 1 | [16 256] | [4 8] | 7 | 6.250000e-02, 7.811287e-03 | | 2 | 24576 | 1 | [16 256] | [4 8] | 6 | 6.250000e-02, 1.561990e-02 | | 3 | 16384 | 1 | [16 128] | [4 7] | 8 | 6.250000e-02, 6.242016e-02 | | 4 | 14336 | 1 | [16 128] | [4 7] | 7 | 6.250000e-02, 5.707978e-02 | | 5 | 12288 | 1 | [16 128] | [4 7] | 6 | 6.250000e-02, 5.870371e-02 | | 6 | 10240 | 0 | [16 128] | [4 7] | 5 | 6.250000e-02, 8.585766e-02 | | 7 | 8192 | 0 | [16 128] | [4 7] | 4 | 6.250000e-02, 1.020576e-01 | best solution | id | memory (bits) | feasible | table size | intermediate wls | tabledata wl | error(max,current) | | 5 | 12288 | 1 | [16 128] | [4 7] | 6 | 6.250000e-02, 5.870371e-02 | solution = 1x1 functionapproximation.lutsolution with properties: id: 5 feasible: "true"
use the compare
method to compare the output of the original function and the approximate.
compare(solution);
use the approximate
method to generate a simulink™ subsystem containing the generated direct lookup table.
approximate(solution)
generate a direct lookup table approximation for a subsystem
this example shows how to approximate a simulink™ subsystem with a direct lookup table.
open the model containing the subsystem to approximate.
functiontoapproximate = 'ex_direct_approximation/mathexpression'; open_system('ex_direct_approximation');
to generate a direct lookup table, set the interpolation method to none
.
problem = functionapproximation.problem(functiontoapproximate);
problem.options.interpolation = 'none';
problem.options.reltol = 0;
problem.options.abstol = 0.2;
problem.options.wordlengths = [7 8 9 16];
solution = solve(problem);
| id | memory (bits) | feasible | table size | intermediate wls | tabledata wl | error(max,current) | | 0 | 2097152 | 1 | 65536 | 16 | 32 | 2.000000e-01, 0.000000e 00 | | 1 | 896 | 0 | 128 | 7 | 7 | 2.000000e-01, 2.265625e 00 | | 2 | 1024 | 0 | 128 | 7 | 8 | 2.000000e-01, 2.265625e 00 | | 3 | 1152 | 0 | 128 | 7 | 9 | 2.000000e-01, 2.265625e 00 | | 4 | 2048 | 0 | 128 | 7 | 16 | 2.000000e-01, 2.267090e 00 | best solution | id | memory (bits) | feasible | table size | intermediate wls | tabledata wl | error(max,current) | | 0 | 2097152 | 1 | 65536 | 16 | 32 | 2.000000e-01, 0.000000e 00 |
compare the original subsystem behavior to the lookup table approximation.
compare(solution);
generate a new subsystem containing the lookup table approximation.
approximate(solution);
replace the original subsystem with the new subsystem containing the lookup table approximation.
replacewithapproximate(solution);
you can revert your model back to its original state using the reverttooriginal
function. this function places the original subsystem back into the model.
reverttooriginal(solution);