sf_fxptprecision (Simulink model) | Open this Model |
Fixed-Point Multiplication
This demonstration illustrates how Fixed-Point mathematics can be used within your Stateflow chart. The chart takes two fixed-point inputs from Simulink: 'in1' is an unsigned 8 bit integer with a slope bias of 2^-2 and 'in2' is an unsigned 8 bit integer with a slope bias of 2^-3. The chart provides two outputs: 'out1' which is an unsigned 8 bit integer with a slope bias of 2^-2 and 'out2' which is an unsigned 8 bit integer with a slope bias of 2^-4.
'out1' and 'out2' are calculated the same way: in1 * in2. This model demonstrates how you obtain different results when specifiying different values for your fixed-point output. In this particular case, 'out2' is more accurate than 'out1' but is still a truncated result. In order to obtain the correct result, you need to increase the bit (or register) size of the output and also increase the slope bias (in other words shift the radix point). For example, if you select the Data properties of 'out2' and change the integer size to a uint16 with a scaling value of 2^-5, then you will receive the correct result: 13.65625