Getting Started
1. Prerequisites
Before using sablib, ensure that you have the following installed:
- C++ Compiler: Support for C++17 or later.
- CMake: Version 3.10 or later.
- Eigen 3: Version 3.4.0 or later.
2. Cloning the Repository
To clone the sablib repository from GitHub, use the following command:
git clone https://github.com/Izadori/sablib.git
3. Building the Library
sablib is built as a static library using CMake. Follow these steps to build the project:
cd sablib
mkdir build
cd build
cmake ..
cmake --build .
If Eigen3 is installed in a non-standard location, you might need to specify its path:
cmake .. -DCMAKE_PREFIX_PATH="path/to/Eigen3"
cmake --build .
After a successful build, the library file (e.g., libsablib.a or sablib.lib) will be created in the build directory.
4. Usage Example
Here is a simple example showing how to use the Whittaker smoother in your own C++ program.
Sample Program: main.cpp
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <vector>
#include "sablib/sablib.h"
int main()
{
const int N = 100;
std::vector<double> signal((size_t)N);
for (int i = 0; i < N; ++i) {
double t = (double)i / N;
double baseline = 0.5 * t + 0.1;
double peak = 2.0 * std::exp(-std::pow((t - 0.5) / 0.05, 2));
double noise = 0.05 * ((double)std::rand() / RAND_MAX - 0.5);
signal[i] = baseline + peak + noise;
}
double lambda = 1.0;
unsigned int s = 2;
try {
std::cout << "Original\tSmoothed" << std::endl;
for (size_t i = 0; i < signal.size(); ++i) {
std::cout << signal[i] << "\t" << smoothed[i] << std::endl;
}
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
return 0;
}
const std::vector< double > Whittaker(const std::vector< double > &y, const std::vector< double > &w, const double lambda, const unsigned int s)
Performs Whittaker smoothing (std::vector<double> version, with weights).
Building your project with CMake
To build your project and link against sablib, use a CMakeLists.txt like this:
cmake_minimum_required(VERSION 3.10)
project(my_sablib_project CXX)
set(CMAKE_CXX_STANDARD 17)
# Find Eigen3
find_package(Eigen3 REQUIRED NO_MODULE)
# Set the path to the sablib directory
set(SABLIB_DIR "/path/to/sablib")
# Include sablib headers
include_directories(${SABLIB_DIR})
# Add your executable
add_executable(my_app main.cpp)
# Link against the pre-built sablib library
# Assuming libsablib.a is in the build folder
find_library(SABLIB_LIB NAMES sablib libsablib PATHS "${SABLIB_DIR}/build")
target_link_libraries(my_app PRIVATE ${SABLIB_LIB} Eigen3::Eigen)
Manual Compilation (Command Line)
If you prefer not to use CMake, you can compile directly using your preferred compiler. Ensure you have the paths to sablib and Eigen3 headers correctly specified.
Using g++ (MinGW / Linux)
g++ main.cpp -std=c++17 \
-I/path/to/sablib \
-I/path/to/eigen3 \
-L/path/to/sablib/build \
-lsablib -o my_app
Using cl.exe (MSVC)
Open a Developer Command Prompt for Visual Studio and run:
cl /EHsc /std:c++17 main.cpp ^
/I \path\to\sablib ^
/I \path\to\eigen3 ^
/link /LIBPATH:\path\to\sablib\build ^
sablib.lib /out:my_app.exe