Skip to main content

FFT as native Polars expressions — forward/inverse FFT, magnitude, phase, and power spectrum, powered by RustFFT.

Project description

polars-rfft

FFT as native Polars expressions. Built with Rust (RustFFT) and PyO3 for zero-copy, vectorized computation over Polars DataFrames.

Install

pip install polars-rfft

Quick start

import polars as pl
from polars_rfft import rfft

df = pl.DataFrame({"signal": [1.0, 0.0, -1.0, 0.0, 1.0, 0.0, -1.0, 0.0]})

# Forward FFT — returns struct {re, im}
df.with_columns(spectrum=rfft(pl.col("signal")).fft())

Roundtrip: FFT → IFFT

df.with_columns(
    recovered=rfft(rfft(pl.col("signal")).fft()).ifft_real()
)

Magnitude, phase, power spectrum

fft_expr = rfft(pl.col("signal")).fft()
df.with_columns(
    mag=rfft(fft_expr).magnitude(),
    phase=rfft(fft_expr).phase(),
    power=rfft(fft_expr).power_spectrum(),
)

Complex-valued input

df = pl.DataFrame({
    "z": [{"re": 1.0, "im": 0.0}, {"re": 0.0, "im": 1.0},
           {"re": -1.0, "im": 0.0}, {"re": 0.0, "im": -1.0}]
})
df.with_columns(spectrum=rfft(pl.col("z")).fft_complex())

API reference

Complex values are represented as struct columns with fields re (real) and im (imaginary).

Method Input Output Description
fft() f64 struct{re,im} Forward FFT on real signal
fft_complex() struct{re,im} struct{re,im} Forward FFT on complex signal
ifft() struct{re,im} struct{re,im} Inverse FFT (normalized by 1/N)
ifft_real() struct{re,im} f64 Inverse FFT, real part only
magnitude() struct{re,im} f64
phase() struct{re,im} f64 atan2(im, re) in radians
power_spectrum() struct{re,im} f64

All methods are accessed via the rfft() wrapper:

from polars_rfft import rfft

rfft(pl.col("signal")).fft()          # forward
rfft(pl.col("spectrum")).ifft()       # inverse
rfft(pl.col("spectrum")).magnitude()  # magnitude

Direct functions

For lower overhead on smaller signals, fft_direct and ifft_direct bypass the Polars expression engine and operate directly on Series:

from polars_rfft import fft_direct, ifft_direct

re, im = fft_direct(df["signal"])           # Series → (Series, Series)
re, im = ifft_direct(re, im)               # (Series, Series) → (Series, Series)

Performance

Benchmarked end-to-end against numpy on signals of varying length. Median of 7 runs after warmup.

Expression API (rfft().fft())

Signal length Operation polars-rfft numpy Speedup
1,024 fft 0.14 ms 0.02 ms 0.2x
4,096 fft 0.24 ms 0.08 ms 0.3x
16,384 fft 0.66 ms 0.99 ms 1.5x
65,536 fft 2.2 ms 4.7 ms 2.1x
262,144 fft 10.2 ms 22.1 ms 2.2x
1,048,576 fft 33.9 ms 53.3 ms 1.9x

Direct API (fft_direct())

Bypasses Polars expression dispatch for ~0.1ms less overhead:

Signal length Operation fft_direct numpy Speedup
64 fft 0.014 ms 0.013 ms 0.9x
256 fft 0.018 ms 0.013 ms 0.7x
1,024 fft 0.040 ms 0.022 ms 0.6x
4,096 fft 0.14 ms 0.08 ms 0.5x
16,384 fft 0.64 ms 0.99 ms 1.5x
65,536 fft 2.1 ms 4.7 ms 2.3x
262,144 fft 9.9 ms 22.1 ms 2.2x
1,048,576 fft 27.9 ms 53.3 ms 1.9x

At small sizes (< 4K), numpy is faster due to its highly optimized C/Fortran backend. At 16K+ elements, RustFFT's radix algorithms dominate — up to 2.3x faster than numpy. The direct API eliminates ~0.1ms of Polars expression dispatch overhead, matching numpy at very small sizes.

Run it yourself:

pip install numpy
python benchmarks/bench_fft.py --sizes 1024 16384 262144 1048576

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

polars_rfft-0.1.0.tar.gz (28.8 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

polars_rfft-0.1.0-cp39-abi3-win_amd64.whl (4.9 MB view details)

Uploaded CPython 3.9+Windows x86-64

polars_rfft-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (4.2 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

polars_rfft-0.1.0-cp39-abi3-macosx_11_0_arm64.whl (3.8 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

polars_rfft-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl (4.3 MB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

polars_rfft-0.1.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.7 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

File details

Details for the file polars_rfft-0.1.0.tar.gz.

File metadata

  • Download URL: polars_rfft-0.1.0.tar.gz
  • Upload date:
  • Size: 28.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for polars_rfft-0.1.0.tar.gz
Algorithm Hash digest
SHA256 db04f09a79a9e5ee188cc4f600de69702bf5d9cb33c0cae1e119681c3462dde7
MD5 4eddffee7ff83e0630cee2a96e52d298
BLAKE2b-256 dfe30d3b5e361d9646e0e6e0ecde070818d261bf8c43694fe1b1cad6712efb94

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_rfft-0.1.0.tar.gz:

Publisher: release.yml on mattbuck85/polars-rfft

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_rfft-0.1.0-cp39-abi3-win_amd64.whl.

File metadata

  • Download URL: polars_rfft-0.1.0-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 4.9 MB
  • Tags: CPython 3.9+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for polars_rfft-0.1.0-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 ec88c7135ed46d6fa6f8614175c28c3cc4623d6c65851aa0cf6d229f04a659cf
MD5 27727822f46ed19de6bf3debbaf2e304
BLAKE2b-256 4fbb6713dfe2f4fe007536ca3a8ab6065a601ca306579edcef479bfc27f45c49

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_rfft-0.1.0-cp39-abi3-win_amd64.whl:

Publisher: release.yml on mattbuck85/polars-rfft

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_rfft-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for polars_rfft-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 ec12bcb5f68be15a3f15e8e15b539b34d21cbedbf86942f6e85313244e889117
MD5 806bfda0667bb9f0225699383a600389
BLAKE2b-256 d94607f4853dfa63c39f120b0aa71ba2f00e4fcdd3577a9cc4df2485ad92d1cf

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_rfft-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on mattbuck85/polars-rfft

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_rfft-0.1.0-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for polars_rfft-0.1.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 68f8abdb12ea4371f69517bdfb5f4fd2e7edd5577dd84081e262bb14033a44bd
MD5 0d415ac40c18a2cc4ef654144a7140e4
BLAKE2b-256 146c92606e3442c2eea15bd162a7108e27c6c238777c366e9b20e831de72289a

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_rfft-0.1.0-cp39-abi3-macosx_11_0_arm64.whl:

Publisher: release.yml on mattbuck85/polars-rfft

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_rfft-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for polars_rfft-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 e3e62cd803635e7c6e2d667f1775fd6bc40745d8cb3be0f8e17d0fe00e10ad50
MD5 8627d82e2861a75e86422198278d868a
BLAKE2b-256 cc5e2b69cbb095947aaf1e1cff2c60b8c67677f7ff91d19a834cbe6c5b6f99d5

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_rfft-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl:

Publisher: release.yml on mattbuck85/polars-rfft

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_rfft-0.1.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for polars_rfft-0.1.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f036abf227d37b3063850857b44931077124063132b9d0cd8fa513a3873cfa99
MD5 ae618c867404074816b3f6ac724f0bbd
BLAKE2b-256 67fe58d351e32f93620269d12864aa5aca9db788d81a8be21c519c58fcd02665

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_rfft-0.1.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on mattbuck85/polars-rfft

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page