Downsampling functions
ToolkitTimescaleDB Toolkit functions are available under Timescale Community Edition. They are automatically included with Timescale, but must be installed separately for self-hosted TimescaleDB. Click to learn more.Introduction
Downsample your data to visualize trends while preserving fewer data points. Downsampling replaces a set of values with a much smaller set that is highly representative of the original data. This is particularly useful for graphing applications.
Warning
This function group includes some experimental functions. Experimental functions might change or be removed in future releases. We do not recommend using them in production. Experimental functions are marked with an Experimental tag.
Function
- asap_smooth
- Downsample a time series using the ASAP smoothing algorithm
- gp_lttb
- ExperimentalDownsample a time series using the Largest Triangle Three Buckets method, while preserving gaps in original data
- lttb
- Downsample a time series using the Largest Triangle Three Buckets method
asap_smooth(ts TIMESTAMPTZ,value DOUBLE PRECISION,resolution INT) RETURNS Timevector
Downsample your data with the ASAP smoothing algorithm. This algorithm preserves the approximate shape and larger trends of the input data, while minimizing the local variance between points.
Required arguments
Name | Type | Description |
---|---|---|
ts | TIMESTAMPTZ | Timestamps for each data point |
value | DOUBLE PRECISION | The value at each timestamp |
resolution | INT | The approximate number of points to return. Determines the horizontal resolution of the resulting graph. |
Returns
Column | Type | Description |
---|---|---|
asap_smooth | Timevector | An object representing a series of values occurring at set intervals from a starting time. It can be unpacked with unnest . For more information, see the documentation on timevectors. |
Examples
This example uses a table called metrics
, with columns for date
and reading
. The columns contain measurements that have been accumulated over a large interval of time. This example takes that data and provides a smoothed representation of approximately 10 points, but that still shows any anomalous readings::
SET TIME ZONE 'UTC';CREATE TABLE metrics(date TIMESTAMPTZ, reading DOUBLE PRECISION);INSERT INTO metricsSELECT'2020-1-1 UTC'::timestamptz + make_interval(hours=>foo),(5 + 5 * sin(foo / 12.0 * PI()))FROM generate_series(1,168) foo;SELECT * FROM unnest((SELECT asap_smooth(date, reading, 8)FROM metrics));
time | value------------------------+---------------------2020-01-01 01:00:00+00 | 5.36648145657226652020-01-01 21:00:00+00 | 5.9494692640906442020-01-02 17:00:00+00 | 5.5829878075183772020-01-03 13:00:00+00 | 4.6335185434277332020-01-04 09:00:00+00 | 4.0505307359093572020-01-05 05:00:00+00 | 4.4170121924816232020-01-06 01:00:00+00 | 5.3664814565722682020-01-06 21:00:00+00 | 5.949469264090643
gp_lttb(ts TIMESTAMPTZ,value DOUBLE PRECISION,resolution INT[, gapsize INTERVAL]) RETURNS Timevector
Downsample your data with the Largest Triangle Three Buckets algorithm, while preserving gaps in the underlying data. This method is a specialization of the LTTB algorithm.
Required arguments
Name | Type | Description |
---|---|---|
ts | TIMESTAMPTZ | Timestamps for each data point |
value | DOUBLE PRECISION | The value at each timestamp |
resolution | INT | The approximate number of points to return. Determines the horizontal resolution of the resulting graph. |
Optional arguments
Name | Type | Description |
---|---|---|
gapsize | INTERVAL | Minimum gap size to divide input on |
Returns
Column | Type | Description |
---|---|---|
gp_lttb | Timevector | An object representing a series of values occurring at set intervals from a starting time. It can be unpacked with unnest . For more information, see the documentation on timevectors. |
Examples
This example uses a table with raw data generated as a sine wave, and removes a day from the middle of the data. You can use gap preserving LTTB to downsample the data while keeping the bounds of the missing region:
SET TIME ZONE 'UTC';CREATE TABLE metrics(date TIMESTAMPTZ, reading DOUBLE PRECISION);INSERT INTO metricsSELECT'2020-1-1 UTC'::timestamptz + make_interval(hours=>foo),(5 + 5 * sin(foo / 24.0 * PI()))FROM generate_series(1,168) foo;DELETE FROM metrics WHERE date BETWEEN '2020-1-4 UTC' AND '2020-1-5 UTC';SELECT time, valueFROM unnest((SELECT toolkit_experimental.gp_lttb(date, reading, 8)FROM metrics))
time | value-----------------------+-------------------2020-01-01 01:00:00+00 | 5.6526309611002572020-01-02 12:00:00+00 | 02020-01-03 23:00:00+00 | 5.6526309611002552020-01-05 01:00:00+00 | 5.6526309611002592020-01-05 13:00:00+00 | 9.9572243068690512020-01-06 12:00:00+00 | 02020-01-07 10:00:00+00 | 9.829629131445342020-01-08 00:00:00+00 | 5.000000000000004
lttb(ts TIMESTAMPTZ,value DOUBLE PRECISION,resolution INT) RETURNS Timevector
Downsample your data with the Largest Triangle Three Buckets algorithm. This algorithm tries to retain visual similarity between the downsampled data and the original dataset.
Required arguments
Name | Type | Description |
---|---|---|
ts | TIMESTAMPTZ | Timestamps for each data point |
value | DOUBLE PRECISION | The value at each timestamp |
resolution | INT | The approximate number of points to return. Determines the horizontal resolution of the resulting graph. |
Returns
Column | Type | Description |
---|---|---|
lttb | Timevector | An object representing a series of values occurring at set intervals from a starting time. It can be unpacked with unnest . For more information, see the documentation on timevectors. |
Examples
This example uses a table with raw data generated as a sine wave. You can use LTTB to dramatically reduce the number of points while still capturing the peaks and valleys in the data:
SET TIME ZONE 'UTC';CREATE TABLE metrics(date TIMESTAMPTZ, reading DOUBLE PRECISION);INSERT INTO metricsSELECT'2020-1-1 UTC'::timestamptz + make_interval(hours=>foo),(5 + 5 * sin(foo / 24.0 * PI()))FROM generate_series(1,168) foo;SELECT time, valueFROM unnest((SELECT lttb(date, reading, 8)FROM metrics))
time | value------------------------+---------------------2020-01-01 01:00:00+00 | 5.6526309611002572020-01-01 13:00:00+00 | 9.9572243068690532020-01-02 11:00:00+00 | 0.042775693130947982020-01-03 11:00:00+00 | 9.9572243068690512020-01-04 13:00:00+00 | 0.042775693130947092020-01-05 16:00:00+00 | 9.3301270189221912020-01-06 20:00:00+00 | 2.49999999999999962020-01-08 00:00:00+00 | 5.000000000000004
Keywords
Found an issue on this page?Report an issue or Edit this page in GitHub.