pyfxp
Bit-true fixed-point arithmetic for Python, at compiled speed.
Model the datapath of your FPGA, ASIC or DSP firmware in Python. Pick the word
length, the rounding and the overflow behavior, and get exactly the numbers the
hardware will produce: on plain floats and NumPy arrays, from Python or inside
Numba @njit loops.
Documentation · Getting started · API reference · Comparison · Changelog · llms.txt
Installation
pip install pyfxp
A fixed-point FIR filter, end to end
import numpy as np
from numba import njit
from pyfxp import Q, fxp, to_hex
from pyfxp.constants import HALF_EVEN, SAT
x_spec = Q(1, 15, rnd=HALF_EVEN, ovf=SAT) # ADC samples: sQ1.15
h_spec = Q(1, 15, rnd=HALF_EVEN, ovf=SAT) # filter taps: sQ1.15
acc = x_spec.mul(h_spec).acc(32) # accumulator: sQ7.30, can't overflow
y_spec = Q(1, 15, rnd=HALF_EVEN, ovf=SAT) # output register
@njit # pyfxp runs inside compiled loops
def fir(x, h, acc, y_spec):
y = np.zeros_like(x)
for n in range(h.size - 1, x.size):
s = 0.0
for k in range(h.size):
s = fxp(s + x[n - k] * h[k], acc)
y[n] = fxp(s, y_spec)
return y
h = fxp(np.hanning(32) / np.hanning(32).sum(), h_spec)
x = fxp(0.9 * np.sin(np.linspace(0, 20, 10_000)), x_spec)
y = fir(x, h, acc, y_spec)
to_hex(y, y_spec) # expected values for your RTL testbench
Why pyfxp
- Bit-true. Nine rounding modes, wrap / saturate / error on overflow, formats up to 64 bits. Every mode is property-tested against an exact rational-arithmetic reference.
- Fast where it counts. About 5 ns per sample inside
@njitloops, and as fast as the fastest library on NumPy arrays. The only Python fixed-point library that works inside Numba. - Plain floats and NumPy. No custom number type: results are floats and float64 arrays, so
SciPy, Matplotlib and the model you already have keep working. Floats are exact up to 53
significant bits of the stored integer; wider formats need
to_int/to_hexfor bit-exact results. - Hardware-ready.
to_int,to_hexandto_bingive the stored integers and two's-complement patterns for$readmemhtest vectors, exact at every width. - Bit growth, computed.
a.mul(b),a.add(b)anda.acc(n)return the smallest format that holds the exact result, so intermediate values never overflow by accident. - Readable formats.
Q(3, 13, True, HALF_EVEN, SAT)prints assQ3.13(HALF_EVEN, SAT), and swapping the rounding and overflow arguments is an error, not a silent bug.
Performance
Quantizing to sQ8.12 with floor rounding and saturation, compared with the other
Python fixed-point libraries (log scale, lower is better):
On arrays, pyfxp and numfi are the fastest, within run-to-run noise of each other.
The difference that matters for DSP models is per-sample code: inside an @njit
loop pyfxp takes about 5 ns per value, while the other libraries can only be
called from Python, at 0.4 µs or more per call.
| pyfxp | numfi | APyTypes | fxpmath | fixedpoint | spfpm | |
|---|---|---|---|---|---|---|
| Result is a plain float / NumPy array | ✔ | — | — | — | — | — |
| Vectorized over NumPy arrays | ✔ | ✔ | ✔ | ✔ | — | — |
Quantize inside Numba @njit code |
✔ | — | — | — | — | — |
| Formats wider than 64 bits | — | ✔ | ✔ | ✔ | ✔ | ✔ |
See the full comparison for the method, the numbers and the details.
Quick Start
pyfxp offers two ways to convert numbers to a fixed-point format:
- fxpt → “typed” (parameter-based) Pass the format directly as function parameters.
- fxp → “spec-based” (explicit FxpSpec)
Build a reusable spec with
Q(...), then pass that spec.
Use the fxpt function when the format is simple or used in a single place:
from pyfxp import fxpt
from pyfxp.constants import SAT, HALF_EVEN # overflow & rounding modes
y = fxpt(
x=3.14159,
qi=3, # integer bits (including sign when signed=True)
qf=13, # fractional bits
signed=True, # signed or unsigned format
ovf=SAT, # overflow behavior: WRAP | SAT | ERROR
rnd=HALF_EVEN, # rounding: TRUNC, CEIL, TO_ZERO, AWAY, HALF_UP, ...
)
Use fxp when you want a reusable, readable definition. The fixed-point format is defined
using the Q(...) function once, and can be
reused for different fxp calls:
import numpy as np
from pyfxp import fxp, Q
from pyfxp.constants import SAT, HALF_EVEN
# Define the format once
Q3_13 = Q(qi=3, qf=13, signed=True, ovf=SAT, rnd=HALF_EVEN)
# Reuse it across your codebase
y1 = fxp(3.14159, Q3_13)
y2 = fxp(np.array([0.1, 0.2, 0.3]), Q3_13)
Q(...) returns an FxpSpec instance
which is basically a named tuple containing the specs used by fxp.
When specifying the fixed-point specs using either fxpt or Q,
all the following rounding and overflow modes are supported:
Rounding modes:
| Constant | Description |
|---|---|
TRUNC |
Bit truncation (rounds toward negative infinity) |
CEIL |
Round toward positive infinity |
TO_ZERO |
Round toward zero |
AWAY |
Round away from zero |
HALF_UP |
Round to nearest; ties toward positive infinity |
HALF_DOWN |
Round to nearest; ties toward negative infinity |
HALF_EVEN |
Round to nearest; ties to even |
HALF_ZERO |
Round to nearest; ties toward zero |
HALF_AWAY |
Round to nearest; ties away from zero |
Overflow modes:
| Constant | Description |
|---|---|
WRAP |
Wrap around on overflow (modular arithmetic) |
SAT |
Saturate at the maximum/minimum representable value |
ERROR |
Raise an exception on overflow |
The constants above are defined in the pyfxp.constants module.
They are used by both fxpt and fxp to specify fixed-point arithmetic behavior.
Metadata
Release files for pyfxp 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pyfxp-1.0.1.tar.gz | 76.6 kB | Without provenance | Details for pyfxp-1.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pyfxp-1.0.1-py3-none-any.whl | Python 3 | none | any | Details for pyfxp-1.0.1-py3-none-any.whl |
Total release size: 101.0 kB
Release files / pyfxp-1.0.1.tar.gz
| Download URL | pyfxp-1.0.1.tar.gz |
|---|---|
| Size | 76.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8a3ea9a1239766f7f26f1b60fc480029635e6266e5aa5f22387d9bb148493483
|
|
BLAKE2b-256 checksum How to use checksums |
9616186d4278015535371950c147db5992e6a94adcdfeb58d3b422e451f3cacb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
pdm/2.29.7 CPython/3.15.2 Linux/7.0.0-1020-azure
|
Release files / pyfxp-1.0.1-py3-none-any.whl
| Download URL | pyfxp-1.0.1-py3-none-any.whl |
|---|---|
| Size | 24.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9763b10dcee74d7db16964a6d6752931ba4b80507e26f8b2666f214e7742b892
|
|
BLAKE2b-256 checksum How to use checksums |
d66853654f4406fb944eb99f26a3a14fd1d6d7e0776ec0523cd20253e381f167
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
pdm/2.29.7 CPython/3.15.2 Linux/7.0.0-1020-azure
|
Security for pyfxp 1.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.