speedstat

package
v0.1.18 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package speedstat computes speed distribution statistics (histogram and weighted percentiles) over a GPX track.

Raw point-to-point speed is noisy at poor GPS signal: short time/distance deltas between jittery fixes produce implausible spikes. Samples merges consecutive points until both a minimum duration and a minimum distance are reached before trusting their speed, optionally skipping points with a bad HDOP fix. This keeps every meter and every second accounted for (merged, never dropped), so distance/time totals stay exact and percentages computed from the histogram remain meaningful.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ByDistance

func ByDistance(s Sample) float64

ByDistance weighs a sample by the distance it covers.

func ByDuration

func ByDuration(s Sample) float64

ByDuration weighs a sample by the time it covers.

Types

type Bucket

type Bucket struct {
	FromKmh  float64
	ToKmh    float64
	Count    int
	Distance float64 // meters
	Duration float64 // seconds
}

Bucket is one histogram bin, covering speeds in [FromKmh, ToKmh).

type Histogram

type Histogram struct {
	BinWidth float64
	Buckets  []Bucket

	// Samples are the non-excluded samples, kept for percentile queries.
	Samples []Sample

	TotalDistance float64 // meters
	TotalDuration float64 // seconds

	// Excluded aggregates samples dropped for exceeding Options.MaxSpeedKmh.
	Excluded Bucket
}

Histogram holds bucketed samples plus totals needed to turn buckets and percentiles into shares of the overall distance/time.

func NewHistogram

func NewHistogram(samples []Sample, opt Options) Histogram

NewHistogram buckets samples by speed, weighting each bucket by both the distance and time it covers.

func (Histogram) SpeedAbove

func (h Histogram) SpeedAbove(coveragePct float64, weightFn WeightFunc) float64

SpeedAbove returns the speed threshold X such that coveragePct percent of the total weight (distance or time, per weightFn) was covered at speed at or above X. For example SpeedAbove(99, ByDistance) answers "99% of distance was covered at speed above X km/h".

type Options

type Options struct {
	// MinInterval is the minimum duration a sample must span before its speed
	// is trusted. Shorter spans are merged into the following sample.
	MinInterval time.Duration

	// MinDistance is the minimum distance (in meters) a sample must span
	// before its speed is trusted. Shorter spans are merged into the
	// following sample.
	MinDistance float64

	// MaxHDOP discards points whose horizontal dilution of precision exceeds
	// this value, bridging the gap between the surrounding good fixes. Zero
	// disables HDOP filtering.
	MaxHDOP float64

	// MaxSpeedKmh excludes samples faster than this from the histogram and
	// percentiles, reporting them separately as GPS noise. Zero disables
	// this clamp.
	MaxSpeedKmh float64

	// BinWidth is the histogram bucket width, km/h. Defaults to 5 if zero.
	BinWidth float64
}

Options configures how raw points are turned into speed Samples and how Samples are bucketed into a Histogram.

type Sample

type Sample struct {
	SpeedKmh float64
	Distance float64 // meters
	Duration float64 // seconds
}

Sample is a speed measurement merged from one or more consecutive points.

func Samples

func Samples(gpxFile *gpx.GPX, opt Options) []Sample

Samples extracts speed samples from every track segment in gpxFile.

type WeightFunc

type WeightFunc func(Sample) float64

WeightFunc selects which sample weight to use for a coverage/percentile query: distance-weighted or time-weighted.

Jump to

Keyboard shortcuts

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