API reference

All public names are importable from fastgoertzel. The signatures below are those of the shipped type stubs (fastgoertzel/_core.pyi), which CI checks against the compiled module. ArrayLike is anything NumPy converts to a 1-D float64 array; str arguments named scale accept "amplitude", "magnitude" or "power".

One frequency

fastgoertzel.goertzel(x: ArrayLike, frequency: float, *, fs: float | None = None, scale: str = 'amplitude') → tuple[float, float]

Amplitude and phase of x at exactly frequency (generalized Goertzel).

Parameters:
  • x (array_like) – 1-dimensional sequence of samples. Converted to a contiguous float64 array.

  • frequency (float) – Frequency to evaluate. In cycles per sample, [0, 1), unless fs is given, in which case it is in the same unit as fs and must be in [0, fs). Any value is evaluated exactly; nothing is rounded to a DFT bin. Values above half the sample rate alias onto their mirror image.

  • fs (float, optional) – Sample rate. frequency / fs is the normalized frequency.

  • scale ({'amplitude', 'magnitude', 'power'}) – 'amplitude' (default) returns 2|X|/N, the amplitude of a sinusoid at that frequency (|X|/N at DC and Nyquist). 'magnitude' returns |X| and 'power' returns |X|**2, both without normalization.

Returns:

tuple[float, float] – (value, phase). phase is in radians, cosine-referenced as in numpy.fft: cos(2*pi*f*n + p) reports p.

Raises:

ValueError – Empty or non-1-D x; frequency outside its range or not finite; fs not positive and finite; unknown scale.

Notes

Single-threaded by design (predictable latency). Signals of 1024 samples or more are split into independent blocks of at least 512 samples (up to eight) so that the recurrence’s dependency chain does not limit throughput; the blocks are combined exactly.

Examples

>>> import numpy as np, fastgoertzel
>>> t = np.arange(4410)  # 44 whole cycles of 440 Hz at 44.1 kHz
>>> x = np.cos(2 * np.pi * 440 / 44100 * t + 0.25)
>>> amp, phase = fastgoertzel.goertzel(x, 440, fs=44100)
>>> round(amp, 3), round(phase, 3)
(1.0, 0.25)
fastgoertzel.goertzel_complex(x: ArrayLike, frequency: float, *, fs: float | None = None) → complex

Unscaled complex DTFT value X = sum x[n] exp(-2j*pi*f*n) at frequency.

Same arguments and validation as goertzel(). Use this when you need the complex value or want to avoid the sqrt/atan2 of the amplitude form.

fastgoertzel.goertzel_bin(x: ArrayLike, k: int, *, scale: str = 'amplitude') → tuple[float, float]

Amplitude and phase of DFT bin k of x (k in [0, len(x))).

Equivalent to goertzel(x, k / len(x)) but with the twiddles computed in exact integer arithmetic, so it matches numpy.fft.fft(x)[k] as closely as the recurrence allows. This is what versions 1.x computed for every call after truncating frequency * len(x) to an integer.

Several frequencies

fastgoertzel.goertzel_batch(x: ArrayLike, frequencies: ArrayLike, *, fs: float | None = None, scale: str = 'amplitude', threads: int | None = None, out: NDArray[np.float64] | None = None) → NDArray[np.float64]

goertzel() for every entry of frequencies in one call.

Parameters:
  • x (array_like) – 1-dimensional sequence of samples.

  • frequencies (array_like) – 1-dimensional sequence of frequencies (cycles per sample, or in the unit of fs when given).

  • fs – As in goertzel().

  • scale – As in goertzel().

  • threads (int, optional) – OpenMP thread count for this call. None uses the OpenMP default (OMP_NUM_THREADS); 1 forces serial execution. Ignored in builds without OpenMP. Results do not depend on the thread count.

  • out (numpy.ndarray, optional) – Preallocated result array: float64, C-contiguous, writeable, shape (len(frequencies), 2), not sharing memory with x. The call then allocates nothing and returns out.

Returns:

numpy.ndarray – Shape (len(frequencies), 2): column 0 is the scaled value, column 1 the phase. Row i is goertzel(x, frequencies[i], ...) up to rounding.

Notes

Up to eight frequencies share one pass over the signal; fewer frequencies are padded with signal blocks of at least 512 samples so that up to eight independent chains run, and large batches use more lanes still. The parallel region is entered only when len(x) * len(frequencies) >= 65536. The GIL is released during computation.

fastgoertzel.goertzel_batch_complex(x: ArrayLike, frequencies: ArrayLike, *, fs: float | None = None, threads: int | None = None, out: NDArray[np.complex128] | None = None) → NDArray[np.complex128]

goertzel_complex() for every entry of frequencies; returns a complex128 array of shape (len(frequencies),). out may be a preallocated complex128 array of that shape, as in goertzel_batch().

Sliding windows

fastgoertzel.goertzel_sliding_batch(x: ArrayLike, window_size: int, frequencies: ArrayLike, *, step: int = 1, fs: float | None = None, scale: str = 'amplitude', threads: int | None = None, out: NDArray[np.float64] | None = None) → NDArray[np.float64]

goertzel_batch() over sliding windows of x.

Parameters:
  • x (array_like) – 1-dimensional sequence of samples.

  • window_size (int) – Length of each window, in [1, len(x)].

  • frequencies (array_like) – 1-dimensional sequence of frequencies.

  • step (int) – Hop between consecutive windows (default 1). Window w covers x[w*step : w*step + window_size] and there are (len(x) - window_size) // step + 1 windows.

  • fs – As in goertzel_batch().

  • scale – As in goertzel_batch().

  • threads – As in goertzel_batch().

  • out (numpy.ndarray, optional) – Preallocated result array: float64, C-contiguous, writeable, shape (n_windows, len(frequencies), 2), not sharing memory with x. With a reused out the call allocates nothing; see Notes.

Returns:

numpy.ndarray – Shape (n_windows, len(frequencies), 2).

Notes

When 4 * step < window_size consecutive windows are obtained with the sliding DFT update, which costs a constant amount per hop regardless of window_size; every 256 windows the value is recomputed exactly, so rounding error cannot accumulate. Otherwise each window is computed from scratch. Work is distributed over OpenMP threads by (frequency, 256-window chunk); results do not depend on the thread count. The GIL is released.

The result array is large (n_windows * len(frequencies) * 16 bytes) and, when freshly allocated, is page-faulted in by the writing threads, which serialises in the kernel and bounds the thread scaling. In a loop that calls this repeatedly, pass the same out each time.

fastgoertzel.goertzel_sliding_batch_complex(x: ArrayLike, window_size: int, frequencies: ArrayLike, *, step: int = 1, fs: float | None = None, threads: int | None = None, out: NDArray[np.complex128] | None = None) → NDArray[np.complex128]

goertzel_sliding_batch() returning the unscaled complex DTFT of every window.

Same arguments as goertzel_sliding_batch() without scale. Returns a complex128 array of shape (n_windows, len(frequencies)), or out if given. Skips the sqrt and atan2 per window, which dominate the cost of the (value, phase) form once the sliding update has made the window length irrelevant.

Streaming

class fastgoertzel.GoertzelFilter(frequency: float, *, fs: float | None = None)

Stateful Goertzel filter for streaming input.

Create it for one frequency, push samples as they arrive (scalars or arrays, any chunking), and read result() at any time; it is goertzel applied to everything pushed so far (equal to rounding). reset() clears the state. Instances are cheap and hold no buffers. Do not push to the same instance from two threads at once.

Examples

>>> import numpy as np, fastgoertzel
>>> flt = fastgoertzel.GoertzelFilter(440, fs=44100)
>>> for chunk in np.array_split(np.cos(2 * np.pi * 440 / 44100 * np.arange(4410)), 7):
...     flt.push(chunk)
>>> amp, phase = flt.result()
>>> round(amp, 6), flt.n_samples
(1.0, 4410)
property frequency

Frequency as passed to the constructor.

property fs

Sample rate, or None.

property n_samples

Number of samples pushed since construction or the last reset.

property normalized_frequency

Frequency in cycles per sample.

push(x: ArrayLike) → None

Feed a scalar or a 1-dimensional array of samples.

reset() → None

Forget all pushed samples.

result(*, scale: str = 'amplitude') → tuple[float, float]

(value, phase) over all samples pushed so far; see goertzel() for scale. Raises ValueError before the first push.

result_complex() → complex

Unscaled complex DTFT value over all samples pushed so far.

Module attributes

fastgoertzel.__version__: str

The package version.

_core._has_openmp: bool

Whether this build parallelises the batch and sliding functions with OpenMP.

_core._kernel: str

The hot-loop build selected at import, "avx2" or "baseline". The environment variable FASTGOERTZEL_KERNEL=baseline|avx2|auto overrides the choice.