Optimize DSP Designs and Code using Fixed-Point Designer MathWorks Korea 이웅재부장 Senior Application Engineer 2013 The MathWorks, Inc. 1
Agenda Fixed-point concepts Introducing Fixed-Point Designer Overview of floating-point to fixed-point workflows Workflow demos Simulink Based MATLAB Based Code generation from fixed-point design C and HDL Summery 2
Real Workflow Story 1 TMS320C6000 Word Floating Point C Use IDE or makefile Debug using break points and watch variables in memory Verify using File Input / File Output Multiple teams, single location Share Word documents, C code 3
Real Workflow Story 2 TMS320C6000 PDF MATLAB MATLAB Floating Point C Floating Point C Fixed-Point C CCS for compile link download Debug: break points, watch windows Verify using MATLAB test data Multiple teams across the globe Share PDF, MATLAB Code, C code 4
Design Challenges Demand for higher performance drives increasing complexity 30%+ of time spent on verification 25%+ of design time spent on fixedpoint conversion 5
Introduction Variety of Measurements & Scales E.g., Fahrenheit vs Celsius Room temperature measurement 50 60 deg F 10 15.5 deg C F = (C*9/5) + 32 9/5: Scaling, 32: Bias (offset) Characteristics of Representation in Digital Systems Representations in digital systems is only an approximation of real world values Range: Limits of representation Precision: Distance between successive numbers 6
What is floating-point? Characterized by SIGN bit, MANTISSA(Fraction) and EXPONENT IEEE 754 Single Precision format (Normalized): 32 bits word size(single) Use of three separate fields increases both range (Exponent size) and precision (Fraction size) of floating point numbers Above picture from http://en.wikipedia.org/wiki/single_precision_floating-point_format 7
What is fixed-point? Characterized by single WORD with fixed RADIX Point Use fractional numbers without floating point For a fixed size, trade-off between Precision and Range 8
Fixed-Point Targets Digital Signal Processors (TI, Analog Devices, etc.) Fixed-point DSPs are considerably less expensive than floating-point DSPs Fixed-point DSPs use less power and are good for battery powered applications Clock speeds for fixed-point DSPs are much higher Fixed word lengths Field Programmable Gate Arrays (Xilinx, Altera, etc.) In fixed-point FPGA implementations every bit of increased word length results in more silicon area used and increased power usage Designer can select word lengths 9
Fixed Point Tradeoff Consideration Fixed Point Floating Point RAM and ROM consumption Small Large Execution time Faster Slower Hardware power consumption Low High Embedded Hardware Cost Lower Higher Development time Long Short Implementation complexity Error Prone More. Need to keep track of word lengths, fractional scaling. Write your own rounding methods and saturation code Harder to develop. More prone to quantization and overflow errors due to smaller range. More prone to programming errors Less Easier to develop 10
Fixed Point Drawbacks & Solutions Drawback Longer development time Solution Using Simulation, automated verification and automated scaling tools will shorten development time and resources Drawback Error prone due to implementation complexity Solution Using rapid prototyping, in-the-loop testing, and production code generation helps reduce errors significantly Drawbacks Quantization errors due to smaller dynamic range Solution Quantization errors can be reduced by selecting appropriate SCALING (position of Radix Point) and WORD SIZE (limited by Hardware) 11
How Does Fixed-Point Work on my DSP? The chip does integer math, not fixed-point. Fixed-point is a do-it-yourself world. Addition and subtraction: You align the binary point (using >> or <<), then add. Multiplication: Integer multiply, then you interpret binary point of product. Division: You probably can t use /. You write a function or make a library function call. Sqrt: Can t use standard math library. You write another function. Rounding and/or saturation: You want it? You do it. 12
Example: Fixed-Point C implementation void differentialeq( void ) { /* Implements a fixed point first order difference equation */ Convert variables to integer types 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; 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 13
Introducing Fixed-Point Designer Simulink Fixed Point Unified Fixed-Point Toolbox and Simulink Fixed Point Products Fixed-Point Designer Supports MATLAB and Simulink workflows Fixed-Point Toolbox 14
3 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 Coder Works on: MATLAB Code Instrumented Code Generation Report for MATLAB Requires: Works on: Fixed-Point Designer MATLAB Code MATLAB Function Blocks 15
Simulink Workflow for Fixed Point Design Existing model (Floating-Point / Fixed-Point) Prepare for fixed point conversion Collect Range Information Propose Data Types Apply Data Types Compare Result Generate Code Yes Are results satisfactory? No 16
Prepare your model for fixed-point conversion by using Fixed-Point Advisor Select a task in the listed order. Run the task. If passed, move to the next task. If failed, fix the error, run the task again, and move to the next task. If there is a warning, If important, take the recommended action(s), run the task again and move on to the next task when the issue is resolved. If not critical, ignore the warning and move to the next task. Return to Fixed-Point Tool. 17
Workflow Demo 1: Envelop Detector Fixed-Point Tool in Simulink Logging to capture dynamic range of signals Data Type Override to switch between floating- and fixedpoint data types Auto-scale to set fractional precision based on recorded dynamic range 18
MATLAB Workflow for Fixed Point Design Existing Code (Floating-Point) Existing Stimulus (Floating-Point) Prepare for fixed-point conversion Make Project Set Input Type Collect Range Information Type Proposal Generate fixed point MATLAB code Test Numerics Generate Code Yes Are results satisfactory? No 19
Workflow Demo 2: Kalman Filter Fixed-Point Conversion Tool in MATLAB 20
Code Generation for Fixed-Point 23
Benefit of using both C/HDL auto code generation tools Separation between Design and Implementation Target Devices can be chosen at the final stage. Software and Hardware Partitioning can be easily made. 24
Code Generation Products for C/C++ Embedded Coder Embedded Coder Automatically generate C and C++ optimized for embedded systems Simulink Coder Simulink Coder Automatically generate C and C++ from Simulink models and Stateflow charts MATLAB Coder MATLAB Coder Automatically generate C and C++ from MATLAB code 25
Code Generation Products for VHDL/Verilog HDL Coder HDL Coder Automatically generate VHDL or Verilog from MATLAB code and Simulink Model MATLAB Coder MATLAB Coder Automatically generate C and C++ from MATLAB code 26
Simple example: C Code ficodegen7.c fixedcodegen_y.result = (int16_t)((fixedcodegen_u.in1 << 2) + fixedcodegen_u.in2) * fixedcodegen_u.in3; ficodegen7.h /* External Input*/ typedef struct { int8_t In1; int16_t In2; int8_t In3; } ExternalInputs_fixedCodeGen; /* External Output*/ typedef struct { int32_t Result; } ExternalOutputs_fixedCodeGen; 27
Simple example: HDL Code (vhdl case) Entity Part Architecture Body Part Synthesis Result (Precision RTL) 28
Code Generation Demo: LPF Simulink workflow Supports automated code generation to C or HDL C/C++ HDL 29
Code Generation Demo: LPF MATLAB workflow C/C++ MATLAB Code HDL 30
Summary Number System Concepts Fixed-Point Designer MATLAB/Simulink Algorithm Design Fixed Point Designer Fixed-Point Conversion MATLAB Coder Conversion Workflow MATLAB/Simulink Simulink Coder Embedded Coder HDL Coder Code generation Gen Gen Gen Gen C and HDL C/C++ VHDL/Verilog FPGA MCU ASIC DSP FPGA ASIC 31