Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 36 ยท Code Generation

Fixed-Point and Data Types in Simulink: fixdt, Scaling and Overflow

Understand Simulink data types and fixed-point design: fixdt, fraction length, ranges, precision, overflow, saturation, rounding and the Fixed-Point Tool workflow.

Many microcontrollers have no floating-point unit, or a single-precision one only. Doing math with integers is faster, smaller and more predictable. Fixed-point arithmetic stores a real number as an integer with an implied binary point. Simulink lets you simulate fixed-point behavior exactly, including rounding and overflow, before you generate code. The tools are Simulink with Fixed-Point Designer, which cannot run on this site; the Python stand-ins below implement the same arithmetic and are labelled.

Besides double and single, Simulink has int8, uint8, int16, uint16, int32, uint32, int64, uint64, boolean, enumerations, fixed-point types, buses (structures) and half. Choose per signal:

  • In a block dialog: the Data Types tab, Output data type.
  • On a Inport/Outport: Signal Attributes > Data type.
  • Display data types: Debug > Information Overlays > Signal Dimensions / Port Data Types (the exact menu varies by release) shows double, int16, fixdt(1,16,8) on the diagram.
  • Data Type Conversion block: converts explicitly, with Rounding mode and Saturate on integer overflow.

A fixed-point type is fixdt(Signed, WordLength, FractionLength):

MATLAB
fixdt(1, 16, 8)    % signed, 16 bits, 8 fractional bits:  sfix16_En8
fixdt(0, 8, 4)     % unsigned, 8 bits, 4 fractional bits: ufix8_En4
fixdt(1, 16, 0)    % same as int16

The stored integer q represents the value q * 2^(-fraction length). The resolution is 2^(-FL) and the range for a signed type is -2^(WL-1-FL) to 2^(WL-1-FL) - 2^(-FL). So sfix16_En8 covers -128 to 127.996 in steps of 0.0039.

flowchart LR
    R["Real value 3.14159"] --> Q["Scale: x * 2^8 = 804.2"]
    Q --> I["Round to integer 804"]
    I --> S["Stored int16: 804"]
    S --> B["Back: 804 / 2^8 = 3.140625"]

The stored integer 804 is the number 3.140625: an error of about 0.0009 appeared when we rounded.

The more fractional bits, the better the precision but the smaller the range. This range versus precision trade-off is the core of fixed-point design.

Stand-in: quantize, range and overflow

The class below mimics a signed fixed-point type with a choice of rounding and overflow behavior (it is not the Simulink implementation, but follows the same rules).

Python Stand-in: a fixed-point number with rounding and saturation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Look at the last line: with wrap on overflow a value that is too large becomes a large negative number, a dangerous bug in a controller. Saturate clamps to the maximum instead. Simulink's block option Saturate on integer overflow chooses between them, and for safety-relevant control it should usually be on (at the cost of extra code).

Rounding modes

Simulink offers: Floor (toward minus infinity; cheapest, the usual default for arithmetic), Ceiling, Zero (truncate), Nearest (ties toward positive infinity), Round (ties away from zero), Convergent (ties to even, avoids bias) and Simplest (picks the cheapest code that still honors the rest of the settings). Floor has a bias of half a least significant bit, which matters in long accumulations; Nearest or Convergent removes it at some cost.

Arithmetic growth

When two fixed-point numbers are multiplied or added, the result needs more bits unless you cut it back:

  • Add with the same scaling: result needs one more bit.
  • Multiply with word lengths W1 and W2 and fraction lengths F1 and F2: the exact product needs W1 + W2 bits and F1 + F2 fraction bits.
  • Simulink lets you set the output data type of each block; the arithmetic is done at full precision internally and then converted to the output type, which is where quantization and overflow occur.

The next stand-in multiplies two numbers and shows how to choose the output type.

Python Stand-in: multiply and convert to a narrower output type

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With 12 fractional bits in a 16-bit word the range is only +/-8, which is enough here, but a product above 8 would saturate. Choosing the output type means picking the fraction length that still covers the expected range.

  1. Build and verify the floating-point model. Keep it as the reference.
  2. Collect ranges: run simulations (and test cases) with Fixed-Point Tool (Apps > Fixed-Point Tool, or the Fixed-Point Advisor) in the Collect Ranges mode. The tool records min and max of every signal. You can also specify Design minimum and maximum on ports and parameters.
  3. Propose data types: the tool suggests word length and fraction length for each signal from the ranges and a safety margin (for example 10 to 20%) so that no overflow occurs.
  4. Apply the proposals and simulate in fixed-point. The tool shows overflows and compares results with the floating-point baseline (Compare runs).
  5. Tune: increase fraction lengths where precision is lacking, or reduce word lengths where the range is wasted. Iterate.
  6. Generate code and verify with SIL testing.

Useful settings: Model Settings > Diagnostics > Data Validity > Signals > Wrap on overflow / Saturate on overflow / Inf or NaN block output (set diagnostics to error during development so problems appear early), and Fixed-point instrumentation mode to record data.

You can reduce effort with Fixed-Point Converter for MATLAB code, and Fixed-Point Designer's Data Type Optimization that searches for the smallest types meeting an error tolerance.

Which blocks need care

  • Integrators and accumulators: states grow; give them more headroom and saturate.
  • Division: expensive in fixed point; use reciprocals or shifts where possible.
  • Lookup tables: store tables in a narrower type and interpolate with a wider one.
  • Trigonometry: use a lookup table (Sine/Cosine with Table lookup approximation), or CORDIC.
  • Filter coefficients: quantize to the type and check frequency response afterward, because quantization changes the poles.

Common mistakes

  • Letting everything default to double, so the generated code uses software floating-point.
  • Too tight a range from collecting data from only one scenario; add margin and test corner cases.
  • Wrap-around hiding overflow in accumulators.
  • Mixed scaling in a sum, which forces extra shifts. Use the same fraction length for signals that are added.
  • Comparing with == on quantized values: compare with a tolerance or compare integers.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What range does sfix16_En8 (fixdt(1,16,8)) cover?

    Signed 16 bits with 8 fractional bits gives integer part range -128..127 and resolution 1/256.
  2. 2. What happens with wrap on overflow if a value is slightly above the maximum?

    Integer arithmetic wraps; saturation must be requested to clamp.
  3. 3. What does increasing the fraction length (with the same word length) do?

    Bits move from the integer part to the fractional part.
  4. 4. What is the purpose of collecting ranges with the Fixed-Point Tool?

    Recorded min and max drive the proposed word and fraction lengths.

Summary

  • A fixed-point type fixdt(s, WL, FL) stores an integer scaled by 2^-FL; precision and range trade off.
  • Choose rounding (Floor, Nearest, Convergent) and overflow handling (saturate or wrap) deliberately.
  • Products grow in width; set the output type of each block to control quantization.
  • Use the Fixed-Point Tool: collect ranges, propose types, apply, compare with the floating-point reference, iterate.
  • Plan for accumulators, division and filter coefficients, which are the typical trouble spots.

Next: SIL, PIL and deployment to hardware