Released "sablib": A C++ Library for Smoothing and Baseline Estimation

Background,Baseline,C++,Eigen,sablib,Signal Processing,Spectrum

Introduction

On this blog, I have introduced various algorithms for smoothing noisy data and estimating backgrounds (baselines), and implemented them in C++ and Python.

These techniques are indispensable in analytical chemistry data processing (such as spectral analysis). However, since I found that there aren’t many readily available implementations for C++, I decided to consolidate the code I’ve shared into a single library. That is how "sablib," which I am introducing today, came to be.

What is sablib?

sablib is a C++ library that brings together algorithms for smoothing and baseline estimation. It utilizes the linear algebra library Eigen and leverages sparse matrices to process large amounts of data at high speeds. While it is based on the code introduced in my blog posts, much of it has been reviewed and rewritten. I have also included some algorithms that haven’t been featured on the blog yet.

Requirements

  • A compiler supporting C++17 or later.
  • Eigen 3.4.0 or later.
  • CMake 3.10 or later.

Main Features

sablib provides two primary sets of functions: "Smoothing" and "Baseline Estimation."

1. Smoothing

  • Savitzky-Golay Filter
  • Whittaker Smoother
  • P-Spline: Penalized B-Spline smoothing.
  • Moving Average: Simple Moving Average (SMA) and Weighted Moving Average (WMA).

2. Baseline Estimation

  • Classical Methods: Linear/Polynomial fitting and Cubic Spline interpolation.
  • Simple Moving Average (Iterative)
  • AsLS (Asymmetric Least Squares)
  • airPLS (Adaptive Iteratively Reweighted Penalized Least Squares)
  • arPLS (Asymmetrically Reweighted Penalized Least Squares)
  • BEADS (Baseline Estimation And Denoising using Sparsity): An algorithm that performs denoising and baseline estimation simultaneously.

Usage

Building sablib

Clone the repository:

git clone https://github.com/Izadori/sablib.git

Build using CMake:

cd sablib
mkdir build
cd build
cmake ..
cmake --build .

If Eigen3 is not found, try using cmake .. -DCMAKE_PREFIX_PATH="/path/to/Eigen3".

Integration

For CMake projects, you can easily add it as follows:

find_package(Eigen3 REQUIRED) # Eigen 3.4.0 or later is required
set(SABLIB_DIR "/path/to/sablib")
include_directories(${SABLIB_DIR})
find_library(SABLIB_LIB NAMES sablib libsablib PATHS "${SABLIB_DIR}/build")
target_link_libraries(my_app PRIVATE ${SABLIB_LIB} Eigen3::Eigen)

Code Example (Baseline Estimation using arPLS)

#include <vector>
#include "sablib/sablib.h"

int main()
{
    std::vector<double> signal = { /* observed data */ };

    // Estimate baseline using the arPLS algorithm
    // Parameters: signal, smoothing parameter lambda
    double lambda = 1e5;
    std::vector<double> baseline = sablib::BaselineArPLS(signal, lambda);

    // Calculate corrected signal
    std::vector<double> corrected(signal.size());
    for(size_t i = 0; i < signal.size(); ++i) {
        corrected[i] = signal[i] - baseline[i];
    }

    return 0;
}

Conclusion

sablib is still a work in progress. I plan to continue adding and refining algorithms in the future. I also intend to introduce more of the algorithms included in sablib here on this blog.

Bug reports, feature requests, and pull requests are all very welcome. If you find this project interesting, giving it a star on GitHub would be a great encouragement!