Generate HDL-Compatible Lookup Table Function Replacements Using coder.approximate
This example shows MATLAB® code generation from a floating-point MATLAB design that is not ready for code generation. Use coder.approximate
function to generate a lookup table based MATLAB function. This newly generated function is ready for HDL code generation (not shown in this demo).
The MATLAB code used in this example is a sigmoid function, which is used for threshold detection and decision making problems. For example, neural networks use sigmoid functions with appropriate thresholds to train systems for learning patterns.
MATLAB Design
design_name = 'mlhdlc_approximate_sigmoid'; testbench_name = 'mlhdlc_approximate_sigmoid_tb';
The mlhdlc_approximate_sigmoid.m
file contains the MATLAB code for a sigmoid function.
type(design_name);
% Copyright 2014-2015 The MathWorks, Inc. function y = mlhdlc_approximate_sigmoid( x ) y = 1./(1+exp(-x)); end
The mlhdlc_approximate_sigmoid_tb.m
file is a MATLAB test bench script used for verifying the functionality and performance of the LUT-based sigmoid approximation generated by mlhdlc_approximate_sigmoid
function.
type(testbench_name);
% Copyright 2014-2015 The MathWorks, Inc. close all x = linspace(-10,10,1e3); for itr = 1e3:-1:1 y(itr) = mlhdlc_approximate_sigmoid( x(itr) ); end plot( x, y ); title('Sigmoid function')
Simulate the Design
Simulate the design with the testbench prior to code generation to make sure there are no runtime errors.
mlhdlc_approximate_sigmoid_tb;
Generate Fixed-Point Lookup-Table Replacements
Use coder.approximate
to generate a lookup-table based replacement function for mlhdlc_approximate_sigmoid
.
repCfg = coder.approximation('Function','mlhdlc_approximate_sigmoid','CandidateFunction',@mlhdlc_approximate_sigmoid,... 'NumberOfPoints',50,'InputRange',[-10,10],'FunctionNamePrefix','repsig_'); coder.approximate(repCfg);
### Generating approximation for 'mlhdlc_approximate_sigmoid' : <a href="matlab:edit('/tmp/Bdoc24b_2725827_3886577/tp51fc51e5/hdlcoder-ex41562150/repsig_lookuptable.m')">repsig_lookuptable.m</a> ### Generating testbench for 'mlhdlc_approximate_sigmoid' : <a href="matlab:edit('/tmp/Bdoc24b_2725827_3886577/tp51fc51e5/hdlcoder-ex41562150/repsig_lookuptable_tb.m')">repsig_lookuptable_tb.m</a> ### LookupTable replacement for function 'mlhdlc_approximate_sigmoid' used 50 data points
The fixed-point conversion completes with appropriate function replacements generates the MATLAB files repsig_lookuptable_tb
, and repsig_lookuptable
containing the testbench and design respectively.
Test the Replacement Functions
To visually see the degree of match between lookup-table based replacement function and the original function use the testbench,
repsig_lookuptable_tb();
iteration 1 repsig_lookuptable(-10) = 4.5398e-05 iteration 2 repsig_lookuptable(-9.8658) = 5.2923e-05 iteration 3 repsig_lookuptable(-9.7315) = 6.0447e-05 iteration 4 repsig_lookuptable(-9.5973) = 6.7972e-05 iteration 5 repsig_lookuptable(-9.4631) = 7.9134e-05 iteration 6 repsig_lookuptable(-9.3289) = 9.0451e-05 iteration 7 repsig_lookuptable(-9.1946) = 0.00010177 iteration 8 repsig_lookuptable(-9.0604) = 0.00011832 iteration 9 repsig_lookuptable(-8.9262) = 0.00013534 iteration 10 repsig_lookuptable(-8.7919) = 0.00015236 iteration 11 repsig_lookuptable(-8.6577) = 0.00017691 iteration 12 repsig_lookuptable(-8.5235) = 0.0002025 iteration 13 repsig_lookuptable(-8.3893) = 0.0002281 iteration 14 repsig_lookuptable(-8.255) = 0.00026448 iteration 15 repsig_lookuptable(-8.1208) = 0.00030297 iteration 16 repsig_lookuptable(-7.9866) = 0.00034146 iteration 17 repsig_lookuptable(-7.8523) = 0.00039538 iteration 18 repsig_lookuptable(-7.7181) = 0.00045325 iteration 19 repsig_lookuptable(-7.5839) = 0.00051112 iteration 20 repsig_lookuptable(-7.4497) = 0.000591 iteration 21 repsig_lookuptable(-7.3154) = 0.000678 iteration 22 repsig_lookuptable(-7.1812) = 0.00076501 iteration 23 repsig_lookuptable(-7.047) = 0.00088328 iteration 24 repsig_lookuptable(-6.9128) = 0.0010141 iteration 25 repsig_lookuptable(-6.7785) = 0.0011448 iteration 26 repsig_lookuptable(-6.6443) = 0.0013199 iteration 27 repsig_lookuptable(-6.5101) = 0.0015164 iteration 28 repsig_lookuptable(-6.3758) = 0.0017129 iteration 29 repsig_lookuptable(-6.2416) = 0.0019717 iteration 30 repsig_lookuptable(-6.1074) = 0.0022668 iteration 31 repsig_lookuptable(-5.9732) = 0.0025619 iteration 32 repsig_lookuptable(-5.8389) = 0.0029445 iteration 33 repsig_lookuptable(-5.7047) = 0.0033873 iteration 34 repsig_lookuptable(-5.5705) = 0.0038302 iteration 35 repsig_lookuptable(-5.4362) = 0.0043948 iteration 36 repsig_lookuptable(-5.302) = 0.0050586 iteration 37 repsig_lookuptable(-5.1678) = 0.0057224 iteration 38 repsig_lookuptable(-5.0336) = 0.0065543 iteration 39 repsig_lookuptable(-4.8993) = 0.0075477 iteration 40 repsig_lookuptable(-4.7651) = 0.0085411 iteration 41 repsig_lookuptable(-4.6309) = 0.0097641 iteration 42 repsig_lookuptable(-4.4966) = 0.011247 iteration 43 repsig_lookuptable(-4.3624) = 0.01273 iteration 44 repsig_lookuptable(-4.2282) = 0.014522 iteration 45 repsig_lookuptable(-4.094) = 0.016727 iteration 46 repsig_lookuptable(-3.9597) = 0.018932 iteration 47 repsig_lookuptable(-3.8255) = 0.021546 iteration 48 repsig_lookuptable(-3.6913) = 0.024806 iteration 49 repsig_lookuptable(-3.557) = 0.028067 iteration 50 repsig_lookuptable(-3.4228) = 0.031855 iteration 51 repsig_lookuptable(-3.2886) = 0.036636 iteration 52 repsig_lookuptable(-3.1544) = 0.041418 iteration 53 repsig_lookuptable(-3.0201) = 0.046856 iteration 54 repsig_lookuptable(-2.8859) = 0.053784 iteration 55 repsig_lookuptable(-2.7517) = 0.060711 iteration 56 repsig_lookuptable(-2.6174) = 0.068417 iteration 57 repsig_lookuptable(-2.4832) = 0.07828 iteration 58 repsig_lookuptable(-2.349) = 0.088142 iteration 59 repsig_lookuptable(-2.2148) = 0.098865 iteration 60 repsig_lookuptable(-2.0805) = 0.11256 iteration 61 repsig_lookuptable(-1.9463) = 0.12626 iteration 62 repsig_lookuptable(-1.8121) = 0.14081 iteration 63 repsig_lookuptable(-1.6779) = 0.15919 iteration 64 repsig_lookuptable(-1.5436) = 0.17757 iteration 65 repsig_lookuptable(-1.4094) = 0.19669 iteration 66 repsig_lookuptable(-1.2752) = 0.22024 iteration 67 repsig_lookuptable(-1.1409) = 0.2438 iteration 68 repsig_lookuptable(-1.0067) = 0.26785 iteration 69 repsig_lookuptable(-0.87248) = 0.29633 iteration 70 repsig_lookuptable(-0.73826) = 0.32481 iteration 71 repsig_lookuptable(-0.60403) = 0.35351 iteration 72 repsig_lookuptable(-0.4698) = 0.38561 iteration 73 repsig_lookuptable(-0.33557) = 0.41771 iteration 74 repsig_lookuptable(-0.20134) = 0.44984 iteration 75 repsig_lookuptable(-0.067114) = 0.48328 iteration 76 repsig_lookuptable(0.067114) = 0.51672 iteration 77 repsig_lookuptable(0.20134) = 0.55016 iteration 78 repsig_lookuptable(0.33557) = 0.58229 iteration 79 repsig_lookuptable(0.4698) = 0.61439 iteration 80 repsig_lookuptable(0.60403) = 0.64649 iteration 81 repsig_lookuptable(0.73826) = 0.67519 iteration 82 repsig_lookuptable(0.87248) = 0.70367 iteration 83 repsig_lookuptable(1.0067) = 0.73215 iteration 84 repsig_lookuptable(1.1409) = 0.7562 iteration 85 repsig_lookuptable(1.2752) = 0.77976 iteration 86 repsig_lookuptable(1.4094) = 0.80331 iteration 87 repsig_lookuptable(1.5436) = 0.82243 iteration 88 repsig_lookuptable(1.6779) = 0.84081 iteration 89 repsig_lookuptable(1.8121) = 0.85919 iteration 90 repsig_lookuptable(1.9463) = 0.87374 iteration 91 repsig_lookuptable(2.0805) = 0.88744 iteration 92 repsig_lookuptable(2.2148) = 0.90113 iteration 93 repsig_lookuptable(2.349) = 0.91186 iteration 94 repsig_lookuptable(2.4832) = 0.92172 iteration 95 repsig_lookuptable(2.6174) = 0.93158 iteration 96 repsig_lookuptable(2.7517) = 0.93929 iteration 97 repsig_lookuptable(2.8859) = 0.94622 iteration 98 repsig_lookuptable(3.0201) = 0.95314 iteration 99 repsig_lookuptable(3.1544) = 0.95858 iteration 100 repsig_lookuptable(3.2886) = 0.96336 iteration 101 repsig_lookuptable(3.4228) = 0.96815 iteration 102 repsig_lookuptable(3.557) = 0.97193 iteration 103 repsig_lookuptable(3.6913) = 0.97519 iteration 104 repsig_lookuptable(3.8255) = 0.97845 iteration 105 repsig_lookuptable(3.9597) = 0.98107 iteration 106 repsig_lookuptable(4.094) = 0.98327 iteration 107 repsig_lookuptable(4.2282) = 0.98548 iteration 108 repsig_lookuptable(4.3624) = 0.98727 iteration 109 repsig_lookuptable(4.4966) = 0.98875 iteration 110 repsig_lookuptable(4.6309) = 0.99024 iteration 111 repsig_lookuptable(4.7651) = 0.99146 iteration 112 repsig_lookuptable(4.8993) = 0.99245 iteration 113 repsig_lookuptable(5.0336) = 0.99345 iteration 114 repsig_lookuptable(5.1678) = 0.99428 iteration 115 repsig_lookuptable(5.302) = 0.99494 iteration 116 repsig_lookuptable(5.4362) = 0.99561 iteration 117 repsig_lookuptable(5.5705) = 0.99617 iteration 118 repsig_lookuptable(5.7047) = 0.99661 iteration 119 repsig_lookuptable(5.8389) = 0.99706 iteration 120 repsig_lookuptable(5.9732) = 0.99744 iteration 121 repsig_lookuptable(6.1074) = 0.99773 iteration 122 repsig_lookuptable(6.2416) = 0.99803 iteration 123 repsig_lookuptable(6.3758) = 0.99829 iteration 124 repsig_lookuptable(6.5101) = 0.99848 iteration 125 repsig_lookuptable(6.6443) = 0.99868 iteration 126 repsig_lookuptable(6.7785) = 0.99886 iteration 127 repsig_lookuptable(6.9128) = 0.99899 iteration 128 repsig_lookuptable(7.047) = 0.99912 iteration 129 repsig_lookuptable(7.1812) = 0.99923 iteration 130 repsig_lookuptable(7.3154) = 0.99932 iteration 131 repsig_lookuptable(7.4497) = 0.99941 iteration 132 repsig_lookuptable(7.5839) = 0.99949 iteration 133 repsig_lookuptable(7.7181) = 0.99955 iteration 134 repsig_lookuptable(7.8523) = 0.9996 iteration 135 repsig_lookuptable(7.9866) = 0.99966 iteration 136 repsig_lookuptable(8.1208) = 0.9997 iteration 137 repsig_lookuptable(8.255) = 0.99974 iteration 138 repsig_lookuptable(8.3893) = 0.99977 iteration 139 repsig_lookuptable(8.5235) = 0.9998 iteration 140 repsig_lookuptable(8.6577) = 0.99982 iteration 141 repsig_lookuptable(8.7919) = 0.99985 iteration 142 repsig_lookuptable(8.9262) = 0.99986 iteration 143 repsig_lookuptable(9.0604) = 0.99988 iteration 144 repsig_lookuptable(9.1946) = 0.9999 iteration 145 repsig_lookuptable(9.3289) = 0.99991 iteration 146 repsig_lookuptable(9.4631) = 0.99992 iteration 147 repsig_lookuptable(9.5973) = 0.99993 iteration 148 repsig_lookuptable(9.7315) = 0.99994 iteration 149 repsig_lookuptable(9.8658) = 0.99995 iteration 150 repsig_lookuptable(10) = 0.99995 ###### Error Metrics for replacement function ##### Absolute Error = '6.7763e-21 0.0018675' Relative Error = '0.00067538' Mean-square Error = '8.4089e-07'