stats

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package stats provides small, dependency-free numerical helpers — the descriptive statistics, histogram, mode, argmax, normalized cross-correlation and peak-finding that the signal-analysis paths reach for. They are the native Go equivalents of the handful of numpy/scipy routines the demod and sync-landscape analyses lean on (numpy.mean/std/histogram, numpy.argmax, scipy.signal.find_peaks, scipy.signal.correlate), so those operations live in one tested place instead of being re-derived inline.

Everything is float64/int and stdlib-only, matching the sibling leaf packages (window, fft) — no gonum, no CGO.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ArgMax

func ArgMax(x []float64) int

ArgMax returns the index of the largest value in x (numpy.argmax), or -1 for empty input. Ties resolve to the first (lowest) index.

func ArgMaxInt

func ArgMaxInt(x []int) int

ArgMaxInt returns the index of the largest value in an integer slice, or -1 for empty input. Ties resolve to the first (lowest) index. It generalises the "pick the winner by hit count" selection in the sync-landscape analysis.

func ArgMin

func ArgMin(x []float64) int

ArgMin returns the index of the smallest value in x (numpy.argmin), or -1 for empty input. Ties resolve to the first (lowest) index.

func Correlate

func Correlate(a, b []float64) (corr []float64, peakLag int)

Correlate returns the full normalized cross-correlation of a and b (numpy.correlate / scipy.signal.correlate, mode="full"), together with the lag of its peak. Each output sample is divided by sqrt(Σa²·Σb²), so a sequence correlated against itself peaks at exactly 1.0.

The output has length len(a)+len(b)-1. Index k corresponds to lag s = k-(len(b)-1): the value is Σ a[i]·b[i-s] over the overlapping region. peakLag is the lag s maximizing the correlation — equivalently, the offset into a at which b best aligns (so a pattern b embedded in a at index p peaks at peakLag == p). Returns (nil, 0) when either input is empty.

func FindPeaks

func FindPeaks(y []float64, opts PeakOpts) []int

FindPeaks returns the indices of the local maxima in y that satisfy opts, sorted by descending height (strongest first). A sample is a local maximum when it is strictly greater than its left neighbour and greater than or equal to its right neighbour (the ">=" right rule keeps the left-most index of a flat top, matching the carrier-search convention this generalises). Returns nil for empty input or when no peak clears the thresholds.

func Histogram

func Histogram(x []float64, nbins int) (counts []int, edges []float64)

Histogram bins x into nbins equal-width bins spanning [min(x), max(x)] (numpy.histogram). It returns the per-bin counts (length nbins) and the nbins+1 bin edges. Values equal to the right-most edge fall in the last bin, matching numpy. Returns (nil, nil) when nbins < 1 or x is empty.

func Max

func Max(x []float64) (val float64, idx int)

Max returns the largest value in x and its index, or (0, -1) for empty input.

func Mean

func Mean(x []float64) float64

Mean returns the arithmetic mean of x (numpy.mean), or 0 for empty input.

func Min

func Min(x []float64) (val float64, idx int)

Min returns the smallest value in x and its index, or (0, -1) for empty input.

func ModeInt

func ModeInt(x []int) int

ModeInt returns the most frequent value in x. Ties resolve toward the smaller value, matching the sync-landscape modal-spacing convention it generalises. Returns 0 for empty input.

func StdDev

func StdDev(x []float64, sample bool) float64

StdDev returns the standard deviation of x (sqrt of Variance), honouring the same sample/population choice as Variance.

func Variance

func Variance(x []float64, sample bool) float64

Variance returns the variance of x. When sample is true it applies Bessel's correction (divide by n-1, ddof=1, numpy.var(ddof=1)); otherwise it is the population variance (divide by n). Returns 0 when there are too few points for the requested estimator.

Types

type PeakOpts

type PeakOpts struct {
	// Height is an absolute minimum: peaks with y < Height are dropped. 0
	// disables the absolute floor.
	Height float64
	// RelHeight keeps only peaks at least RelHeight·max(peak) tall (a relative
	// floor, e.g. 0.01 ≈ 20 dB down). 0 disables it. When both Height and
	// RelHeight are set the stricter threshold wins.
	RelHeight float64
	// MinDistance is the minimum index spacing between kept peaks. When peaks
	// are closer, the taller one wins (greedy, strongest first). 0 disables
	// spacing suppression.
	MinDistance int
	// IncludeEnds treats the first and last samples as candidate maxima (a peak
	// at a band edge still qualifies). When false they are never peaks, matching
	// scipy's default.
	IncludeEnds bool
}

PeakOpts constrains FindPeaks, mirroring a minimal subset of scipy.signal.find_peaks. A zero value finds every interior local maximum.

Jump to

Keyboard shortcuts

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