Expand description
§dsp-fixedpoint
no_std fixed-point arithmetic with explicit storage, accumulator, and
quantization boundaries.
§Type shape
Q<T, A, F> stores raw T bits scaled by 2^-F; products accumulate in A.
T: stored and signal valueA: wide accumulatorF: fractional bits
Aliases include Q32<F> = Q<i32, i64, F> and wrapping/unsigned variants.
§Wide MAC, late quantization
Put the coefficient on the left and the raw signal on the right:
use dsp_fixedpoint::Q32;
type C = Q32<28>;
let a = [C::from_f32(0.25), C::from_f32(-0.5)];
let x = [100_i32, 20];
let mut acc = a[0] * x[0]; // Q<i64, i32, 28>
acc += a[1] * x[1]; // still wide
let y = acc.quantize(); // one shift and narrowing conversion
assert_eq!(y, 15);The shape is Q<T, A, F> * T -> Q<A, T, F>. Accumulate products of that type,
then call quantize() once. Reversing the operands, T * Q -> T, quantizes each
product immediately.
§Construction
from_int and from_f32/from_f64 construct scaled values; from_bits
preserves raw bits. FromRatio constructs a coefficient from two raw values:
use dsp_fixedpoint::{FromRatio, Q32};
let gain = Q32::<28>::from_ratio(1, 3);
assert!((gain.as_f64() - 1.0 / 3.0).abs() < Q32::<28>::DELTA as f64);For positive F, FromRatio widens the numerator before scaling and division.
The denominator must be nonzero and the result must fit T.
§Operators
| Operation | Result | Quantization |
|---|---|---|
Q * T | Q<A, T, F> | none; wide product |
T * Q | T | immediate |
Q * Q, Q / Q | Q<T, A, F> | immediate |
Q + Q, Q - Q | Q<T, A, F> | none; equal F required |
T / Q | T | immediate |
mul_wide() and apply() spell Q * T and T * Q explicitly.
§Scale and conversion
Use .scale::<F1>() when changing fractional bits. Numeric traits convert the
represented value; from_bits/into_bits access the representation.
Q::<_, _, -128>::DELTA is invalid. One is available only when 1 is exactly
representable by T and F.
§Optional integrations
serde: transparent raw representation;serde::as_f32/as_f64for scaled values.defmt: compact decimal logging throughf32.bytemuck:Pod,Zeroable, andTransparentWrapperwhen the component types permit them.
Display is decimal. Binary, octal, and hexadecimal formatting place the radix
point according to F.
Modules§
- serde
- Serde adapters for fixed-point wire formats.
Structs§
- Q
- Fixed-point value with storage
T, accumulatorA, andFfractional bits.
Traits§
- Accu
- Conversion trait between base and accumulator type
- From
Ratio - Construct a value from the ratio of two raw values.
- Shift
- Shift summary trait
Type Aliases§
- P8
- Fixed point
u8withu16accumulator - P16
- Fixed point
u16withu32accumulator - P32
- Fixed point
u32withu64accumulator - P64
- Fixed point
u64withu128accumulator - Q8
- Fixed point
i8withi16accumulator - Q16
- Fixed point
i16withi32accumulator - Q32
- Fixed point
i32withi64accumulator - Q64
- Fixed point
i64withi128accumulator - V8
- Fixed point
Wrapping< u8 >withWrapping< u16 >accumulator - V16
- Fixed point
Wrapping< u16 >withWrapping< u32 >accumulator - V32
- Fixed point
Wrapping< u32 >withWrapping< u64 >accumulator - V64
- Fixed point
Wrapping< u64 >withWrapping< u128 >accumulator - W8
- Fixed point
Wrapping< i8 >withWrapping< i16 >accumulator - W16
- Fixed point
Wrapping< i16 >withWrapping< i32 >accumulator - W32
- Fixed point
Wrapping< i32 >withWrapping< i64 >accumulator - W64
- Fixed point
Wrapping< i64 >withWrapping< i128 >accumulator