Floating-Point Multiplication to Handle a Net Slope Correction
This example shows how to use floating-point multiplication to handle a net slope correction. When converting floating-point data types to fixed-point data types in the generated code, a net slope correction is one method of scaling fixed-point data types. Scaling the fixed-point data types avoids overflow conditions and minimizes quantization errors.
For processors that support efficient multiplication, using floating-point multiplication to handle a net slope correction improves code efficiency. If the net slope correction has a value that is not a power of two, using division improves precision.
Note: This example requires a Fixed-Point Designer™ license.
Example
In the model FloatMultiplicationNetSlope
, a Convert block converts an input signal from a floating-point data type to a fixed-point data type. The net slope correction has a value of 3
.
model = 'FloatMultiplicationNetSlope';
open_system(model);
Generate Code
Build the model.
slbuild(model)
### Starting build procedure for: FloatMultiplicationNetSlope ### Successful completion of build procedure for: FloatMultiplicationNetSlope Build Summary Top model targets: Model Build Reason Status Build Duration ============================================================================================================================== FloatMultiplicationNetSlope Information cache folder or artifacts were missing. Code generated and compiled. 0h 0m 12.188s 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 14.82s
In these lines of FloatMultiplicationNetSlope.c
code, the code generator divides the input signal by 3.0F
.
cfile = fullfile('FloatMultiplicationNetSlope_grt_rtw',... 'FloatMultiplicationNetSlope.c'); coder.example.extractLines(cfile,'/* Model step', '/* Model initialize function */', 1, 0);
/* Model step function */ void FloatMultiplicationNetSlope_step(void) { /* Outport: '<Root>/Output' incorporates: * DataTypeConversion: '<Root>/Data Type Conversion' * Inport: '<Root>/Input' */ FloatMultiplicationNetSlope_Y.Output = (int16_T)(real32_T)floor (FloatMultiplicationNetSlope_U.Input / 3.0F); }
Enable Optimization
Open the Configuration Parameters dialog box.
On the Math and Data Types pane, select Use floating-point multiplication to handle net slope corrections. This optimization is on by default.
Alternatively, you can use the command-line API to enable the optimization.
set_param(model, 'UseFloatMulNetSlope', 'on');
Generate Code with Optimization
slbuild(model)
### Starting build procedure for: FloatMultiplicationNetSlope ### Successful completion of build procedure for: FloatMultiplicationNetSlope Build Summary Top model targets: Model Build Reason Status Build Duration ========================================================================================================== FloatMultiplicationNetSlope Generated code was out of date. Code generated and compiled. 0h 0m 8.8546s 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 9.8448s
In the optimized code, the code generator multiplies the input signal by the reciprocal of 3.0F
, that is 0.333333343F
.
coder.example.extractLines(cfile,'/* Model step', '/* Model initialize function */', 1, 0);
/* Model step function */ void FloatMultiplicationNetSlope_step(void) { /* Outport: '<Root>/Output' incorporates: * DataTypeConversion: '<Root>/Data Type Conversion' * Inport: '<Root>/Input' */ FloatMultiplicationNetSlope_Y.Output = (int16_T)(real32_T)floor (FloatMultiplicationNetSlope_U.Input * 0.333333343F); }
Close the model and the code generation report.
bdclose(model)
See Also
Use floating-point multiplication to handle net slope corrections