Skip to main content

fracmem

Tests License: MIT

Fractional-order derivatives as a fixed-size recursive filter: constant compute and memory per sample, so they run on a microcontroller.

An exact fractional derivative needs the entire signal history. fracmem fits a small filter once in Python, then deploys it as plain C.

Install

pip install fracmem

Requires numpy and scipy. Deploying needs only a C compiler.

Usage

import numpy as np
from fracmem import CompressedFractionalFilter

train = [np.random.randn(3000).cumsum() * 0.01 for _ in range(8)]

f = CompressedFractionalFilter(alpha=0.5, h=0.01, L=32, p=16)
f.fit(train, j_max=10_000)       # once, offline
y = f.predict(signal)            # batch

f.reset_stream()
y_k = f.step(x_k)                # or one sample at a time

definition="rl" (default, same as Grünwald–Letnikov) or "caputo".

How it works

The derivative is a weighted sum over all past samples, with power-law weights w_j ~ j^(-alpha-1).

  • The latest L samples are computed exactly.
  • Older samples (the tail) are replaced by p exponential modes, each updated with one multiply-add per sample. Decay rates come from a Gamma-function integral identity, not from data.
  • A few training signals fit the readout weights by cross-validated ridge regression.

Cost is O(L+p) compute and O(p) memory per sample.

Embedded C

from fracmem.embedded import export_c
export_c(f, "device_filter.c")
#include "fracmemfilter.h"
filtSetup();
float y = fracmemStep(&filt, x_k);

No heap allocation. See examples/ for the full fit, export, compile and verify round trip.

Background

Builds on the sum-of-exponentials construction of Jiang, Zhang, Zhang and Zhang, and related work by Lubich and Schädle, and by Baffet and Hesthaven.

License

MIT

Metadata

Release files for fracmem 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fracmem 0.2.0
File Size Uploaded
fracmem-0.2.0.tar.gz 19.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fracmem 0.2.0
File Interpreter ABI Platform
fracmem-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 35.3 kB

Release files / fracmem-0.2.0.tar.gz

Download URL fracmem-0.2.0.tar.gz
Size 19.1 kB
Tags Source
SHA-256 checksum
How to use checksums
40eb133d52bae70866e967cb6ae1311b3d7df5dc3db9ac5c8e46e903d416e97f
BLAKE2b-256 checksum
How to use checksums
509ec5043423743af122323f379b02df5a41f3dfa4d698892b8c7b32094dbaec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / fracmem-0.2.0-py3-none-any.whl

Download URL fracmem-0.2.0-py3-none-any.whl
Size 16.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
917da7d4fc35dc716cd665eb072fcc24b4b8e92358eb833f297b5b48280d3753
BLAKE2b-256 checksum
How to use checksums
e602a230d42a3a401ffad2c972ee54e9e5882c28c91c68a2463e535ab55457ea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page