tukey

module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 30, 2026 License: MIT

README

tukey

Offline Digital Signal Processing (DSP) for GO, matching SciPy and MATLAB functionality.

import "github.com/cxd309/tukey/dsp"

f, err := dsp.Butter(2, dsp.Lowpass(0.3))
b, a, err := f.BA()
y, err := dsp.FiltFilt(b, a, x)

// higher orders and narrow bands: second-order sections
f, err = dsp.Butter(8, dsp.Bandpass(0.30, 0.32))
sos, err := f.SOS()
y, err = dsp.SOSFiltFilt(sos, x)

Named after John Tukey, who co-invented the FFT and considered by some as the father of Data Science.

Why

This library was designed to support the development of appliations reliant on DSP in GO.

A survey of existing tools found some coverage but was specifically lacking zero-phase filtering which was essential for an application that we needed to develop.

Therefore the decision was made to build a library to support the application being developed and to make it public source so that others might not encounter the same speedbump in development.

The specific application was porting from an existing MATLAB web app which heavily relied on both IIR filter design and filtfilt, the forward-backward filtering that removes phase-distortion.

Phase distortion matters whenever the location of a feature in a signal matters. A causal filter delays each frequency by a different amount, which moves features: in the example below, a pulse at sample 50 comes at sample 54 while FiltFilt smooths it and leaves it at 50.

Python development with SciPy was considered but the I preferred GO tooling for web app development and therefore decided to develop this package instead. I have used SciPy to compare outputs against, as a comparison test.

Compatibility contract

Every function matches its scipy.signal equivalent to within a measured tolerance.

Function SciPy equivalent Worst measured Tested to
Butter butter, as (b, a) 7.4e-15 1e-13
Butter + SOS butter(output='sos') 7.3e-15 1e-13
ZPK.SOS zpk2sos 1.1e-16 1e-14
Filter lfilter 5.3e-16 1e-14
FilterState lfilter with zi 2.3e-16 1e-14
FilterZi lfilter_zi 1.7e-16 1e-14
FiltFilt filtfilt 7.7e-16 1e-14
SOSFilter sosfilt, with and without zi 4.1e-14 1e-12
SOSFilterZi sosfilt_zi 9.8e-17 1e-14
SOSFiltFilt sosfiltfilt 1.7e-13 1e-11
Freqz freqz 1.2e-15 1e-13
SOSFreqz freqz_sos 3.7e-13 1e-11

For scale, 1e-16 is ~ 1 ULP of a float64. The larger SOS figures all come from one deliberately difficult test filter (order 8, cutoff 0.01), whose poles sit so close to z = 1 that single rounding differences between Go and SciPy are amplified; every other filter agrees within a few ULP.

This table is generated by:

go run ./cmd/tolerances

Porting from MATLAB

Function calls map one-to-one. Where MATLAB's behaviour differs from SciPy's, SciPy is chosen and the default with the option to implement MATLAB behaviour.

MATLAB tukey
[b, a] = butter(n, Wn) f, err := dsp.Butter(n, dsp.Lowpass(Wn)), b, a, err := f.BA()
butter(n, Wn, 'high') dsp.Butter(n, dsp.Highpass(Wn))
butter(n, [W1 W2]) dsp.Butter(n, dsp.Bandpass(W1, W2))
butter(n, [W1 W2], 'stop') dsp.Butter(n, dsp.Bandstop(W1, W2))
[z, p, k] = butter(n, Wn) f, err := dsp.Butter(n, dsp.Lowpass(Wn)): f.Zeros, f.Poles, f.Gain
y = filter(b, a, x) y, err := dsp.Filter(b, a, x)
y = filtfilt(b, a, x) y, err := dsp.FiltFilt(b, a, x, dsp.MATLABPadLen())
[h, w] = freqz(b, a, n) w, h, err := dsp.Freqz(b, a, n) (note the order)
y = sosfilt(sos, x) y, err := dsp.SOSFilter(sos, x)

Frequencies are normalised to Nyquist in both: for a frequency fHz at sample rate fs, use f / (fs/2). Freqz returns frequencies in rad/sample, as MATLAB's freqz does.

MATLABPadLen uses MATLAB's filtfilt padding length, 3*(nfilt-1), instead of SciPy's 3*nfilt. Only samples near the ends of the output are affected.

Some mappings are only verified against SciPy, so check these when porting:

  • Second-order sections: f.SOS() pairs and orders sections as SciPy's zpk2sos does, with the overall gain in the first section. MATLAB's [sos, g] = zp2sos(z, p, k) returns the gain g separately, and may pair and order sections differently
  • SOSFiltFilt with MATLABPadLen applies the same 3*(ntaps-1) rule. This hasn't been verified against MATLAB's filtfilt(sos, g, x)
  • FilterState matches SciPy's lfilter(b, a, x, zi). MATLAB's [y, zf] = filter(b, a, x, zi) has the same form, but its state convention hasn't been verified

Conventions

  • Offline and stateless
    • The whole signal is input as a slice and a new slice is returned
    • Inputs never modified
    • All functions are safe for concurrent use
    • For block processing, FilterState and SOSFilterState take a starting state and return the final one
  • Errors and Panics
    • Error for an invalid input, this is a 'user' error
    • Panic for an internal error, this is a 'developer' error
    • Errors wrap an error sentinel so can be checked with errors.Is
  • Stability checking
    • Filtering functions assume the filter is stable
    • Same as SciPy and MATLAB

Roadmap

tukey is currently in initial development and the interface will signficantly change until v1.0.

The library will grow into three packages, mirroring SciPy:

Package Mirrors For
tukey/dsp scipy.signal Filter design and filtering, spectral estimation, peaks, analysis
tukey/fft scipy.fft FFTs
tukey/window scipy.signal.windows Window functions
Version Status Theme Adds
v0.1 Complete Zero-phase filtering Butter, Filter, FiltFilt
v0.2 Complete Filter representations Filters as zeros/poles/gain with (b, a) and second-order-sections output, SOS filtering, filter state for block processing, frequency response
v0.3 Planned FFT tukey/fft
v0.4 Planned window tukey/window
tbd Concept Signal analysis
tbd Concept Filter design breadth

Two functions have no SciPy equivalent and will be checked against MATLAB instead: a moving-RMS envelope, and FindSignal (template search).

Out of scope
  • Streaming objects that hold state: tukey stays stateless, and block processing is supported by functions that take and return filter state
  • Adaptive filters, wavelets, Parks–McClellan (remez) FIR design
  • Audio I/O and file formats

Development

just test       # go vet + go test (unit and compare to SciPy reference vectors)
just tolerances # print measured error per function against SciPy
just fixtures   # regenerate reference vectors

Reference vectors are committed in /testdata/, so python is not a direct testing dependency.

Licence

MIT

Directories

Path Synopsis
cmd
tolerances command
Command tolerances reports the error tukey actually achieves compares against each SciPy refrence-vector suite next to the tolerance its test enforce
Command tolerances reports the error tukey actually achieves compares against each SciPy refrence-vector suite next to the tolerance its test enforce
Package dsp is offline, zero-phase signal conditioning for Go: IIR filter design, filtering as (b, a) or second-order sections, and frequency response, matching SciPy
Package dsp is offline, zero-phase signal conditioning for Go: IIR filter design, filtering as (b, a) or second-order sections, and frequency response, matching SciPy
internal
conformance
Package conformance checks tukey's public API against the SciPy reference vectors in testdata/
Package conformance checks tukey's public API against the SciPy reference vectors in testdata/

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL