Fixed-Point Arithmetic and Numerical Representations
What You'll Learn
- 1Represent values with Q format scaling
- 2Choose word lengths from error budgets
- 3Handle overflow with saturation and scaling
- 4Explain quantization effects in filters
Many microcontrollers lack a floating point unit, and even those that have one may prefer integer math inside tight control loops. Fixed point arithmetic stores numbers as integers with an implied binary point, so Q15 represents values from minus one up to just below one with fifteen fractional bits. The engineer must manage scaling, rounding, and overflow explicitly. Getting these details right is the difference between a stable motor controller and one that oscillates at low speed.
Formats, Scaling, and Conversion
A Q format names the number of fractional bits, so Q31 in a 32 bit word reserves one sign bit and thirty fractional bits. Converting a real value to fixed point multiplies by two raised to the fractional bits and rounds to an integer; converting back divides by the same factor. Double precision intermediate products avoid losing bits during multiply and accumulate steps.
Choosing the scale comes from the worst case magnitude and required resolution. Measure the smallest signal that matters and the largest transient expected, then confirm the format keeps both with margin and adequate headroom for accumulation.
Overflow, Saturation, and Shifting
Multiplication of two Q15 values produces a 30 bit product that must be shifted right by fifteen to restore scale, and that shift introduces rounding loss. Overflow wraps silently in two complement arithmetic, so saturation clamping to the format limits is usually safer in control paths.
Block scaling moves headroom between stages of a filter or transform, trading resolution for range. Debug builds should assert when saturation triggers often, since frequent clamping signals a scaling plan that needs revision.
Quantization, Noise, and Fixed Point Filters
Quantization adds a step of one LSB of noise roughly uniformly distributed, which sets a noise floor for ADC processing and IIR filters. Small coefficients can vanish after truncation, turning a pole into a different location and sometimes pushing a filter toward instability.
Design filters in floating point first, verify behavior, then convert with simulated quantization at every node. Simulation with real input data reveals limit cycles and truncation bias long before hardware testing.
Common Fixed-Point Formats
| Format | Range | Fractional Resolution |
|---|---|---|
| Q15 in 16 bits | Minus one to just below one | About three times ten to minus five |
| Q31 in 32 bits | Minus one to just below one | About four times ten to minus ten |
| Q1.15 signed | Minus one to just below one | Standard audio sample scale |
| Q16.16 | Minus thirty two thousand to thirty two thousand | Good for control units |
| Unsigned Q0.32 | Zero to just below one | Ratios and normalized gains |
Real-World Examples
- Fixed point PID controller â Error, integral, and derivative terms accumulate in Q31 with saturation guards on the output command.
- Audio sample scaling â 16 bit PCM samples in Q15 are mixed with gains stored in Q15 and clipped at full scale.
- Sin table interpolation â A quarter wave table indexed by a phase accumulator delivers fast sine values without a library call.
- IIR filter quantization check â Simulating coefficient rounding confirms the poles stay inside the unit circle across temperature.
đSimulate Quantization Early
Common Mistakes
Forgetting to rescale after multiply
Fix: Products double the fractional width and must be shifted back or every result drifts out of range.
Letting arithmetic wrap silently
Fix: Wrap around turns a small overshoot into a full scale inversion in a control loop.
Designing filters without quantization
Fix: Coefficient rounding can move poles enough to change damping or cause limit cycles.
Key Takeaways
- Fixed point stores integers with an implied binary point defined by Q format.
- Saturation and deliberate scaling protect against wrap around damage.
- Multiplication doubles width and requires an explicit shift to restore scale.
- Quantization simulation predicts filter behavior before hardware exists.
Frequently Asked Questions
Why not just use floating point everywhere?
On cores without a unit, soft float is slow and code heavy, and fixed point often gives deterministic timing with less energy.
How do you pick the number of fractional bits?
From the smallest meaningful step and the largest expected magnitude, then verify with a quantization simulation.
What is saturation arithmetic?
Clamping results to the representable range instead of wrapping, which keeps transient errors bounded.
Interview Questions
Q1: Explain what happens when you multiply two Q15 numbers.
The product carries thirty fractional bits, so you shift right fifteen bits, usually with rounding, to return to Q15 scale.
Q2: How would you detect a scaling problem in the field?
Instrument saturation counters and log them; frequent clamping or lost small signals shows the scaling plan is wrong.
Extended Learning: Beyond the Basics
Key Concepts Summary
This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.
Prerequisite Knowledge
Before studying this topic in depth, ensure you understand:
- Basic definitions and terminology
- Core formulas and their derivations
- Common applications in real-world scenarios
- Typical pitfalls and misconceptions
Step-by-Step Walkthrough
Let us work through a complete example to solidify understanding:
Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.
Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.
Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.
Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.
Common Mistakes to Avoid
- Misidentifying the problem type â Always start by categorizing the problem
- Incorrect formula application â Verify the formula applies to your specific case
- Unit errors â Always check units are consistent throughout
- Sign errors â Pay careful attention to positive and negative values
- Rounding too early â Keep full precision until the final answer
- Forgetting constraints â Check that solutions satisfy all given conditions
Real-World Applications
Understanding this concept is essential for:
- Advanced problem solving in examinations
- Building intuition for more complex topics
- Developing analytical thinking skills
- Applying mathematical reasoning to practical situations
Practice Exercises
- Basic: Solve 5 problems of increasing difficulty using the core concept.
- Intermediate: Apply the concept to word problems with multiple steps.
- Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
- Challenge: Create your own problem that requires this concept plus additional knowledge.
Interview and Exam Tips
- Common question patterns: Identify which exam or interview formats test this topic
- Time management: Practice completing problems within time constraints
- Common traps: Know the typical mistakes examiners set as distractors
- Verification strategy: Develop a quick method to check your answer
Related Topics
- Previous Topic â Build on the foundation laid here
- Next Topic â See how this concept extends to more advanced material
- Practice Problems â Additional exercises with solutions
- Video Explanation â Visual walkthrough of key concepts
Quick Reference Card
| Concept | Formula/Rule | Key Insight | Common Use |
|---|---|---|---|
| Core Definition | See main text | Understand the intuition | Foundation for all applications |
| Key Formula | See main text | Memorize the derivation | Direct problem solving |
| Special Case | See main text | Recognize when it applies | Simplifies complex problems |
| Edge Case | See main text | Know the limitations | Avoids errors |
Further Reading
For students who want to go deeper:
- Standard textbooks on the subject
- Research papers applying this concept
- Online courses with video explanations
- Practice problem collections with detailed solutions