Introducing Fixed-Point Conversion from MATLAB and Simulink Cynthia Cudicini Senior Application Engineer 2014 The MathWorks, Inc. 1
Fixed-Point Concepts What is fixed-point? Fixed-point targets Design Challenges Introducing Fixed-Point Designer Fixed-Point Designer tools Floating-Point to Fixed-Point Conversion Workflows Using the Fixed-Point Tool in Simulink Using the Fixed-Point Conversion Tool in MATLAB Using the separation of algorithm from data types in MATLAB and SIMULINK Code Generation from fixed-point design C and HDL 2
Fixed-Point Concepts What is fixed-point? Fixed-point targets Design Challenges Introducing Fixed-Point Designer Fixed-Point Designer tools Floating-Point to Fixed-Point Conversion Workflows Using the Fixed-Point Tool in Simulink Using the Fixed-Point Conversion Tool in MATLAB Using the separation of algorithm from data types in MATLAB Code Generation from fixed-point design C and HDL 3
What is fixed-point? Floating-point: Sign bit Exponent width (determines range) Mantissa / Fraction bits (determines precision) Floating radix / decimal point Same word size, we can increase both precision and range IEEE 754 single-precision binary floating-point format: binary32* Fixed-point: Sign bit Whole number part (determines range) Fractional part (determines precision) Fixed radix / decimal point Fixed size, trade-off between precision and range *Picture from http://en.wikipedia.org/wiki/single_precision_floating-point_format 4
Fixed-Point Targets Fixed-Point DSPs (TI, Analog Devices, etc.) Fixed word lengths Less expensive Use less power - good for battery powered applications Higher clock speeds FPGAs (Xilinx, Altera, etc.) Designer can select word lengths Every bit increased word length - more silicon area and increased power usage 5
Fixed Point Tradeoff Consideration Floating Point Fixed Point Automatic Fixed Point conversion RAM and ROM Consumption Execution Speed Hardware power consumption Embedded Hardware Cost Development time Implementation complexity Error Prone 6
Example: Fixed-Point C hand implementation void differentialeq( void ) { /* Implements a fixed point first order difference equation */ int Prod; long Accum; static short lastval=0; short a=0x7eb8; // 0.99 in s16,15 short oneminusa=0x0148; //.01 in s16,15 short temp; Prod = galg_in1 * galg_in1; temp = Prod >> 15; Accum = a*lastval + oneminusa*temp; Convert variables to integer types Need lots of comments to understand code Keep track of binary point location galg_out1 = (short)(accum >> 15); lastval = galg_out1; } No saturation or rounding 7
Fixed-Point Concepts What is fixed-point? Fixed-point targets Design Challenges Introducing Fixed-Point Designer Fixed-Point Designer tools Floating-Point to Fixed-Point Conversion Workflows Using the Fixed-Point Tool in Simulink Using the Fixed-Point Conversion Tool in MATLAB Using the separation of algorithm from data types in MATLAB Code Generation from fixed-point design C and HDL 8
MATLAB / SIMULINK Algorithm Design FIXED-POINT DESIGNER Fixed-Point Conversion MATLAB CODER SIMULINK CODER EMBEDDED CODER HDL CODER Gen Gen C/C++ VHDL/Verilog FPGA MCU ASIC DSP FPGA ASIC 9
Tools for Converting a Design to Fixed Point Fixed-Point Tool for Simulink Requires: Works on: Fixed-Point Designer Simulink Simulink Blocks Stateflow Charts Fixed-Point Conversion Tool for MATLAB Requires: Fixed-Point Designer MATLAB Works on: MATLAB Code Instrumented Code Generation Report for MATLAB Requires: Works on: Fixed-Point Designer MATLAB Code MATLAB Function Blocks 10
Fixed-Point Concepts What is fixed-point? Fixed-point targets Design Challenges Introducing Fixed-Point Designer Fixed-Point Designer tools Floating-Point to Fixed-Point Conversion Workflows Using the Fixed-Point Tool in Simulink Using the Fixed-Point Conversion Tool in MATLAB Using the separation of algorithm from data types in MATLAB Code Generation from fixed-point design C and HDL 11
Workflow using Fixed-Point Tool in Simulink: Curve Fitting 1. Prepare for Fixed-Point Conversion 2. Collect Range Information 3. Propose Datatypes 4. Apply Datatypes 5. Compare Results 6. Generate Code 12
Workflow using Fixed-Point Conversion Tool in MATLAB: Kalman Filter 1. Prepare for Fixed-Point Conversion 2. Collect Range Information 3. Propose Datatypes 4. Generate Fixed- Point MATLAB Code 5. Test Numerics 6. Generate Code 13
Separate algorithm from datatype specification Why? Separate database to store large scale data Clarity and readability Portability You can use the same algorithm and switch between different data type specifications Implement in doubles mode for benchmarking Implement in single precision mode for certain micro processors and DSPs Have multiple fixed point implementations for different targets 14
Separate algorithm from datatype specification How? Define Types in MATLAB file airtempsensortype = numerictype(1,16,12) Algorithm in separate file: MATLAB or Simulink Refers to types by name airtempthreshold = fi(145,airtempsensortype) 15
Workflow with separate algorithm from datatypes in MATLAB: FIR Filter 1. Prepare for Fixed-Point Conversion 2. Collect Range Information 3. Propose Datatypes 4. Populate types table with fixedpoint types 5. Compare Results 6. Generate Code 16
Fixed-Point Concepts What is fixed-point? Fixed-point targets Design Challenges Introducing Fixed-Point Designer Fixed-Point Designer tools Floating-Point to Fixed-Point Conversion Workflows Using the Fixed-Point Tool in Simulink Using the Fixed-Point Conversion Tool in MATLAB Using the separation of algorithm from data types in MATLAB Code Generation from fixed-point design C and HDL 17
Code Generation Automatically generate readable, traceable HDL and C code Automatically generate C and HDL code from Simulink, MATLAB and Stateflow Full bi-directional traceability!! Requirements 18
Summary MATLAB / SIMULINK Algorithm Design Fixed-Point Designer Conversion Workflow MATLAB/Simulink SIMULINK CODER EMBEDDED CODER FIXED-POINT DESIGNER Fixed-Point Conversion MATLAB CODER HDL CODER Code generation C and HDL Gen Gen Gen Gen C/C++ VHDL/Verilog FPGA MCU ASIC DSP FPGA ASIC 19
Next Steps Visit the product page for more information www.mathworks.com/products/fixed-point-designer Questions? 21