This is a straightforward C++ port of the reference implementation of the Largest Triangle Three Buckets (LTTB) downsampling algorithm described in the paper "Downsampling time series for visual representation" by Sveinn Steinarsson. It is a single header, with a single class template that allows using different structures and data types.
This is a single-header library with no dependencies outside the C++ standard library, so there are many ways to incorporate it into your project:
Copy include/lttb.hpp into your project, make sure it's available from your compiler's include path, and include it from your source:
#include "lttb.hpp"In classic mode, install the lttb package:
vcpkg install lttbIn manifest mode, add lttb to your project's vcpkg.json:
vcpkg add port lttbThen use the CMake package from your project in either mode:
find_package(lttb CONFIG REQUIRED)
target_link_libraries(your-target PRIVATE lttb::lttb)Include the header from your source:
#include "lttb.hpp"Clone this repository into your source tree, for example as a subrepo under external/lttb-cpp, then add it from your top-level CMakeLists.txt:
add_subdirectory(external/lttb-cpp)
target_link_libraries(your-target PRIVATE lttb::lttb)The lttb::lttb target adds the library's include directory and requires C++11.
Include the header from your source:
#include "lttb.hpp"This repository includes an optional MCP server that helps AI coding tools generate and review lttb integrations. The server is dependency-free and runs over stdio.
Clone the upstream repository if you do not already have it locally:
git clone https://github.com/parkertomatoes/lttb-cpp.gitcodex mcp add lttb-helper -- python3 /path/to/lttb-cpp/mcp/lttb_mcp.pyclaude mcp add --transport stdio lttb-helper -- python3 /path/to/lttb-cpp/mcp/lttb_mcp.pyTo share the Claude Code MCP configuration with other contributors, add it with project scope from the repository root:
claude mcp add --transport stdio --scope project lttb-helper -- python3 ./mcp/lttb_mcp.pyAdd a stdio server entry like this:
{
"mcpServers": {
"lttb-helper": {
"command": "python3",
"args": [
"/path/to/lttb-cpp/mcp/lttb_mcp.py"
]
}
}
}The MCP exposes helpers for API reference, CMake snippets, C++ usage snippets, and common integration checks.
Create a typedef to specify your time series datapoint type
#include "lttb.hpp"
struct ExamplePoint {
float x;
float y;
};
using PointLttb = LargestTriangleThreeBuckets<ExamplePoint, float, &ExamplePoint::x, &ExamplePoint::y>Then use the static method Downsample in the class. It can be used with iterators
std::vector<ExamplePoint> in = GetYourInputsFromSomewhere();
std::vector<ExamplePoint> out;
PointLttb::Downsample(in.begin(), in.size(), std::back_inserter(out), 50);...or pointers:
ExamplePoint in[500];
GetYourInputsFromSomewhere(in);
ExamplePoint out[50];
PointLttb::Downsample(in, 500, out, 50);