optimize dsp designs and code - mathworks...optimize dsp designs and code using fixed-point designer...
TRANSCRIPT
1 © 2013 The MathWorks, Inc.
Optimize DSP Designs and Code using Fixed-Point Designer
MathWorks Korea
이웅재 부장
Senior Application Engineer
2
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
3
Real Workflow Story 1
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
TMS320C6000™
4
Real Workflow Story 2
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
TMS320C6000™
5
Design Challenges
Demand for higher performance drives increasing complexity
– 30%+ of time spent on verification
– 25%+ of design time spent on fixed-point conversion
6
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
7
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
8
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
9
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
10
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 More. Need to keep track of word
lengths, fractional scaling. Write
your own rounding methods and
saturation code
Less
Error Prone Harder to develop. More prone to
quantization and overflow errors
due to smaller range. More prone
to programming errors
Easier to develop
11
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)
12
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.
13
Example: Fixed-Point C 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; gAlg_out1 = (short)(Accum >> 15); lastVal = gAlg_out1; }
No saturation or rounding
Convert variables to integer types
Keep track of binary point location
Need lots of comments to
understand code
14
Unified Fixed-Point Toolbox
and Simulink Fixed Point
Products
Supports MATLAB and
Simulink workflows
Simulink
Fixed Point
Fixed-Point
Designer
Fixed-Point
Toolbox
Introducing Fixed-Point Designer
15
3 Tools for Converting a Design to Fixed Point
Instrumented Code Generation Report for MATLAB
Requires: Fixed-Point Designer
Works on: MATLAB Code
MATLAB Function Blocks
Fixed-Point Conversion Tool for MATLAB
Requires: Fixed-Point Designer
MATLAB Coder
Works on: MATLAB Code
Fixed-Point Tool for Simulink
Requires: Fixed-Point Designer
Simulink
Works on: Simulink Blocks
Stateflow Charts
16
Simulink Workflow for Fixed Point Design
Are results
satisfactory?
No Yes
Prepare for fixed
point conversion
Collect Range
Information
Propose Data Types
Apply Data Types
Compare Result
Existing model (Floating-Point /
Fixed-Point)
Generate
Code
17
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.
18
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 fixed-point data types
Auto-scale to set fractional precision based on recorded dynamic range
19
MATLAB Workflow for Fixed Point Design
Are results
satisfactory?
No Yes
Prepare for
fixed-point
conversion
Collect Range
Information
Type Proposal
Generate fixed point
MATLAB code
Test Numerics
Existing Code (Floating-Point)
Generate
Code
Existing Stimulus (Floating-Point)
Make Project
Set Input Type
20
Workflow Demo 2: Kalman Filter
Fixed-Point Conversion Tool in MATLAB
23
Code Generation for Fixed-Point
24
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.
25
Code Generation Products for C/C++
MATLAB® Coder™
Automatically generate C and C++ from
MATLAB code
Simulink® Coder™
Automatically generate C and C++ from
Simulink models and Stateflow charts
Embedded Coder™
Automatically generate C and C++
optimized for embedded systems
Simulink
Coder
Embedded Coder
MATLAB Coder
26
Code Generation Products for VHDL/Verilog
MATLAB® Coder™
Automatically generate C and C++ from
MATLAB code
MATLAB Coder
HDL Coder HDL Coder™
Automatically generate VHDL or Verilog
from MATLAB code and Simulink Model
27
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;
28
Simple example: HDL Code (vhdl case)
Entity Part
Architecture Body Part
Synthesis Result (Precision RTL)
29
Supports automated code generation to C or HDL
Code Generation Demo: LPF
Simulink workflow
C/C++ HDL
30
Code Generation Demo: LPF
MATLAB workflow
C/C++
HDL
MATLAB Code
31
Summary
MATLAB/Simulink
Algorithm Design
FPGA ASIC FPGA ASIC
VHDL/Verilog
Gen
FPGA ASIC
Simulink Coder
MCU DSP
C/C++
Fixed Point Designer
Fixed-Point Conversion
Gen
Number System Concepts
Fixed-Point Designer
Conversion Workflow
– MATLAB/Simulink
Code generation
– C and HDL
Gen
Gen
Embedded Coder HDL Coder
MATLAB Coder