Baseline Correction

Baseline correction algorithms with a unified correct_baseline dispatcher.

For guidance on choosing between algorithms, see Baseline Algorithms.

Unified API

OpticalSpectroscopy.correct_baselineMethod
correct_baseline(y; method=:arpls, kwargs...) -> NamedTuple

Correct baseline and return both corrected spectrum and baseline.

Returns (y=corrected, baseline=baseline).

source
OpticalSpectroscopy.correct_baselineMethod
correct_baseline(x, y; kwargs...) -> NamedTuple

Version that also returns x values for convenience. Methods that use x-values (rubberband) receive real x, not dummy indices.

source
OpticalSpectroscopy.correct_baselineMethod
correct_baseline(s::Spectrum; method=:arpls, kwargs...) -> Spectrum

Baseline-correct a Spectrum. Returns a new Spectrum with shallow-copied metadata. The baseline itself is not returned; to retrieve it, call correct_baseline(s.x, s.y; method=method, kwargs...) directly, which returns (x=..., y=..., baseline=...).

source

Individual Algorithms

OpticalSpectroscopy.snip_baselineFunction
snip_baseline(y; iterations=40, decreasing=true) -> Vector

SNIP baseline correction using iterative peak clipping (Ryan et al., 1988).

Data are compressed with the log-log-square-root (LLS) operator before clipping (Morháč et al., Nucl. Instrum. Methods Phys. Res. A 401, 113 (1997), doi:10.1016/S0168-9002(97)01023-1). The LLS transform requires non-negative input, so data with a negative baseline (e.g. ΔA spectra) are shifted by their minimum first and shifted back afterwards — negative baselines are recovered, not floored at zero. Each clipping pass reads the previous pass's values through a separate buffer, so the result is independent of scan direction.

source
OpticalSpectroscopy.rubberband_baselineFunction
rubberband_baseline(x, y)

Rubber band baseline correction using the lower convex hull.

Equivalent to stretching a rubber band under the spectrum – the baseline follows the lower envelope. Used in OPUS for FTIR baseline correction.

Returns the baseline y-values (same length as input).

source
OpticalSpectroscopy.imodpoly_baselineFunction
imodpoly_baseline(x, y; poly_order=4, maxiter=100, tol=1e-3) -> Vector

Improved Modified Polynomial (I-ModPoly) baseline of Zhao et al., Appl. Spectrosc. 61, 1225 (2007), doi:10.1366/000370207782597003.

Iteratively fits a polynomial to a working spectrum. Each iteration computes DEV, the standard deviation of the residuals about the current fit, and reconstructs the working spectrum by clipping points above fit + DEV down to fit + DEV (peak removal that accounts for the noise level, unlike the plain ModPoly of Lieber & Mahadevan-Jansen 2003). Converges when the relative change |DEVᵢ − DEVᵢ₋₁| / DEVᵢ < tol.

source
OpticalSpectroscopy.rolling_ball_baselineFunction
rolling_ball_baseline(y; half_window=50, smooth_half_window=nothing) -> Vector

Rolling ball baseline estimation using morphological operations.

Applies erosion (rolling minimum) then dilation (rolling maximum) to estimate the baseline envelope, followed by moving-average smoothing.

Arguments

  • half_window::Int=50: Half-window size for morphological operations.
  • smooth_half_window::Int: Half-window for final smoothing (default: half_window).

Reference

M. A. Kneen and H. J. Annegarn, "Algorithm for fitting XRF, SEM and PIXE X-ray spectra backgrounds", Nucl. Instrum. Methods Phys. Res. B 109/110, 209 (1996). doi:10.1016/0168-583X(95)00908-6

source