LibXtract is a portable C99 library of feature extraction functions for time series and other numeric data. Each function takes an array of doubles and returns a number or a vector that describes it: a statistic, a measure of spectral shape, a pitch estimate, a measure of complexity. The library has a full set of audio features, and its statistical, complexity and moving-window functions do not assume what the numbers represent, so they apply to prices, sensor readings and physiological signals as they do to audio.
It has no external dependencies.
The library has more than 100 features:
- Statistics: mean, variance, standard deviation, average deviation, skewness, kurtosis, median, median absolute deviation, extrema, RMS amplitude, zero-crossing rate
- Distribution: the levels of a vector on a scale of equal steps, and the fraction of its elements at each level
- Moving windows: moving sum, average, variance, standard deviation, maximum and minimum, and the exponentially weighted average and variance
- Complexity: permutation, sample and approximate entropy, Lempel-Ziv complexity, the Petrosian, Katz and Higuchi fractal dimensions, the Hjorth parameters
- Spectrum: magnitude and power spectra, peak and harmonic spectra, and the centroid, spread, flatness, rolloff, slope, contrast and entropy of a spectrum
- Periodicity and pitch: autocorrelation, the average magnitude and squared difference functions, a periodicity measure, and fundamental frequency by the AMDF, wavelet, McLeod and harmonic product spectrum methods
- Change: flux and the Kullback-Leibler and Itakura-Saito divergences between successive frames
- Several series: the covariance and correlation matrices of a set of series, their average correlation and the variances of their principal components, and for two series the Pearson correlation coefficient, the cross-correlation with the lag of its peak and the coherence at each frequency
- Grids: the moments of a grid of values up to the third order, about its origin and about its centroid, their normalised form and the seven Hu moments, and the co-occurrence matrix of its levels with its contrast, correlation, angular second moment and inverse difference moment, and its local binary patterns
- Envelope: attack time, decay time, decay rate, temporal centroid
- Audio: loudness, sharpness, Bark bands, MFCCs, gammatone cepstral coefficients, linear prediction coefficients, chroma, the constant-Q transform
The API reference lists every function with its arguments.
A feature at validation level 3 is tested against expected values proved from its definition in Lean (the spec directory). CI tests the mean, variance and standard deviation on Linux, macOS and Windows against the certified values of the NIST Statistical Reference Datasets. CONTRIBUTING.md describes the reference values and how a feature's validation level follows from them.
The feature functions share one signature:
int xtract_feature(xtract_context *ctx, const double *data, const int N, const void *argv, double *result);ctx is a context made by xtract_context_new, or NULL. data points to N input values, argv points to the arguments the feature needs, and the function writes its output to result and returns a status code.
A feature that depends on another takes it as an argument. The variance takes the mean, the skewness takes the mean and the standard deviation, and a spectral feature takes a spectrum from xtract_magnitude_spectrum:
double mean, variance;
xtract_mean(NULL, data, N, NULL, &mean);
xtract_variance(NULL, data, N, &mean, &variance);A value is therefore computed once however many features use it, and features can be combined: the irregularity of a set of MFCCs is a valid call. Functions that work from precomputed tables, such as the mel filterbank and the constant-Q kernel, have an xtract_init_ function to call first.
No feature function allocates memory. A function that needs working memory or a transform, such as xtract_median or xtract_spectrum, takes it from a context, and every other function accepts NULL. A context is created for the largest N it will be used with:
xtract_context *ctx = xtract_context_new(N);
double median;
xtract_median(ctx, data, N, NULL, &median);
xtract_context_delete(ctx);One thread uses a context at a time. The API reference says which functions need a context and how large.
examples/ holds complete programs. finance computes the rolling and exponentially weighted volatility of a price series and finds a change of volatility regime; simpletest runs pitch tracking, MFCCs and onset detection over an audio file.
- A C99 compiler: GCC, Clang, or Visual Studio 2019 or later on Windows
- CMake 3.21 or later
The library computes the FFT with the bundled Ooura FFT on every platform. On macOS it also links Apple's Accelerate framework, which the mean, the sum and three features like them use.
cmake --preset default # configure into build/default
cmake --build --preset default # build the library, tests, examples and benchmarks
ctest --preset default # run the testsOn Windows the default preset uses the Visual Studio generator; pass -G Ninja from a Developer Command Prompt to use Ninja instead. No MSYS2 or other POSIX layer is needed.
Each of the other presets builds under its own directory in build/. Configure and build one as above with its name in place of default, then run it:
| Preset | Run with | Purpose |
|---|---|---|
default |
ctest --preset default |
Release build of the library, tests, examples and benchmarks |
asan |
ctest --preset asan |
The tests under AddressSanitizer and UBSan |
coverage |
cmake --build --preset coverage --target coverage |
Line and branch coverage of the test suite (needs lcov) |
fuzz |
ctest --preset fuzz |
The libFuzzer harnesses (needs a clang with libFuzzer) |
rtsan |
ctest --preset rtsan |
The tests under RealtimeSanitizer, which reports a feature function that allocates, locks or blocks (needs clang 20 or later) |
mutation |
cmake --build --preset mutation --target mutation |
Mutation testing with Mull (needs clang-18 and mull-18; CI runs it). With -DXTRACT_MUTATION_DIFF_REF=origin/develop only the lines that differ from origin/develop are mutated |
The default preset also has targets that run a tool, each built with cmake --build --preset default --target <name>:
| Target | Purpose |
|---|---|
analyze |
The clang static analyzer over the first-party sources |
cppcheck |
cppcheck over the first-party sources |
format, format-check |
Apply or check the clang-format style |
prose-check |
Vale over the Markdown files, the changelog, the comments and the licence headers, as described in CONTRIBUTING.md (needs Vale) |
version-check |
Check that VERSION is the newest release in CHANGELOG.md or a later version |
bench |
The benchmarks |
doc |
The API reference (needs Doxygen) |
Configuring with -DXTRACT_NIST_TESTS=ON downloads the NIST Statistical Reference Datasets into the build directory and adds the tests against their certified values (needs Python 3 and a network connection).
The CBMC proofs of the feature functions have their own GNU make build, make -C verify, which CI builds on every pull request and runs on every push to main and develop. See verify/README.md.
cmake --install build/default # install to /usr/local
cmake --install build/default --prefix /somewhere # install elsewhereThe install places the library, the headers, a CMake package and a pkg-config file under the prefix, so a downstream CMake project can call find_package(xtract) and link xtract::xtract, and any other build can use pkg-config --cflags --libs xtract. BUILD_SHARED_LIBS=ON builds a shared library.
The bindings are generated with SWIG 4.2 or later. The CMake options XTRACT_BUILD_PYTHON, XTRACT_BUILD_JAVA and XTRACT_BUILD_NODE are AUTO by default, which builds each binding whose toolchain is found: Python with its development headers, a JDK, or node with the Node.js headers and node-addon-api installed under swig/ (npm install --prefix swig node-addon-api). Set an option to ON to fail when the toolchain is missing, or OFF to skip it:
cmake --preset default -DXTRACT_BUILD_PYTHON=ON -DXTRACT_BUILD_JAVA=OFF -DXTRACT_BUILD_NODE=OFF
cmake --build --preset default
ctest --preset default -R bindingsThe modules are written under build/default/swig/<language>.
The API reference for the current release is published at https://www.jamiebullock.com/LibXtract/. It is generated from the Doxygen comments in the public headers and rebuilt from main by the Docs workflow on every push.
To build it locally, with Doxygen installed:
cmake --build --preset default --target docThe output is doc/html/index.html.
LibXtract follows Semantic Versioning. The public API
is the set of headers installed under include/xtract/. From 1.6.0, releases
within 1.x keep source compatibility: functions and XTRACT_* values are
added, never removed or changed, and breaking changes wait for 2.0. Binary
compatibility is not guaranteed between minor releases, so recompile and
relink dependent code when moving to a new one. A correctness fix may change a
feature's numerical output. The changelog says when.
The main branch is the newest release, and develop is the work towards the
next one.
CONTRIBUTING.md covers how to report a bug or propose a change, the code standard, and the terms contributions are accepted under. Report a security problem privately, as SECURITY.md describes.
zlib. See LICENSE. The bundled third-party code under src/ keeps its own
licences, listed in DEPENDENCIES.md.