Documentation
¶
Overview ¶
Package resampler provides time-series data downsampling algorithms.
This package implements two downsampling strategies for reducing the number of data points in time-series data while preserving important characteristics:
- SimpleResampler: A fast, straightforward algorithm that selects every nth point
- LargestTriangleThreeBucket (LTTB): A perceptually-aware algorithm that preserves visual characteristics by selecting points that maximize the area of triangles formed with neighboring points
Both algorithms are designed to work with schema.Float data and handle NaN values appropriately. They require that the new sampling frequency is a multiple of the old frequency.
References:
- LTTB Algorithm: https://skemman.is/bitstream/1946/15343/3/SS_MSthesis.pdf
- Implementation adapted from: https://github.com/haoel/downsampling
Index ¶
Constants ¶
This section is empty.
Variables ¶
var MinimumRequiredPoints int = 1000
Default number of points required to trigger resampling. Otherwise, time series of original timestep will be returned without resampling
Functions ¶
func LargestTriangleThreeBucket ¶
func LargestTriangleThreeBucket(data []schema.Float, oldFrequency int64, newFrequency int64) ([]schema.Float, int64, error)
LargestTriangleThreeBucket (LTTB) performs perceptually-aware downsampling.
LTTB is a downsampling algorithm that preserves the visual characteristics of time-series data by selecting points that form the largest triangles with their neighbors. This ensures that important peaks, valleys, and trends are retained even when significantly reducing the number of points.
Algorithm Overview:
- The data is divided into buckets (except first and last points which are always kept)
- For each bucket, the algorithm selects the point that forms the largest triangle with the previous selected point and the average of the next bucket
- This maximizes the visual area and preserves important features
Time Complexity: O(n) where n is the number of input points Space Complexity: O(m) where m is the number of output points
Parameters:
- data: input time-series data points
- oldFrequency: original sampling frequency (points per time unit)
- newFrequency: target sampling frequency (must be a multiple of oldFrequency)
Returns:
- Downsampled data slice
- Actual frequency used (may be oldFrequency if downsampling wasn't performed)
- Error if newFrequency is not a multiple of oldFrequency
The function returns the original data unchanged if:
- Either frequency is 0
- newFrequency <= oldFrequency (no downsampling needed)
- The resulting data would have fewer than 1 point
- The original data has fewer than 100 points
- The downsampled data would have the same or more points than the original
NaN Handling: The algorithm properly handles NaN values and preserves them in the output when they represent the maximum area point in a bucket.
Example:
data := []schema.Float{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0}
downsampled, freq, err := LargestTriangleThreeBucket(data, 1, 2)
// Returns downsampled data preserving visual characteristics
References:
- Original paper: https://skemman.is/bitstream/1946/15343/3/SS_MSthesis.pdf
- Adapted from: https://github.com/haoel/downsampling/blob/master/core/lttb.go
func SetMinimumRequiredPoints ¶ added in v1.0.0
func SetMinimumRequiredPoints(setVal int)
func SimpleResampler ¶
func SimpleResampler(data []schema.Float, oldFrequency int64, newFrequency int64) ([]schema.Float, int64, error)
SimpleResampler performs simple downsampling by selecting every nth point.
This is the fastest downsampling method but may miss important features in the data. It works by calculating a step size (newFrequency / oldFrequency) and selecting every step-th point from the original data.
Parameters:
- data: input time-series data points
- oldFrequency: original sampling frequency (points per time unit)
- newFrequency: target sampling frequency (must be a multiple of oldFrequency)
Returns:
- Downsampled data slice
- Actual frequency used (may be oldFrequency if downsampling wasn't performed)
- Error if newFrequency is not a multiple of oldFrequency
The function returns the original data unchanged if:
- Either frequency is 0
- newFrequency <= oldFrequency (no downsampling needed)
- The resulting data would have fewer than 1 point
- The original data has fewer than 100 points
- The downsampled data would have the same or more points than the original
Example:
data := []schema.Float{1.0, 2.0, 3.0, 4.0, 5.0, 6.0}
downsampled, freq, err := SimpleResampler(data, 1, 2)
// Returns: [1.0, 3.0, 5.0], 2, nil
Types ¶
This section is empty.