Baseline Correction
Baseline correction algorithms with a unified correct_baseline dispatcher.
For guidance on choosing between algorithms, see Baseline Algorithms.
Unified API
OpticalSpectroscopy.correct_baseline — Method
correct_baseline(y; method=:arpls, kwargs...) -> NamedTupleCorrect baseline and return both corrected spectrum and baseline.
Returns (y=corrected, baseline=baseline).
OpticalSpectroscopy.correct_baseline — Method
correct_baseline(x, y; kwargs...) -> NamedTupleVersion that also returns x values for convenience. Methods that use x-values (rubberband) receive real x, not dummy indices.
OpticalSpectroscopy.correct_baseline — Method
correct_baseline(s::Spectrum; method=:arpls, kwargs...) -> SpectrumBaseline-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=...).
Individual Algorithms
OpticalSpectroscopy.arpls_baseline — Function
arpls_baseline(y; λ=1e5, maxiter=100, tol=1e-6) -> VectorAsymmetrically Reweighted Penalized Least Squares baseline correction.
OpticalSpectroscopy.snip_baseline — Function
snip_baseline(y; iterations=40, decreasing=true) -> VectorSNIP 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.
OpticalSpectroscopy.rubberband_baseline — Function
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).
OpticalSpectroscopy.imodpoly_baseline — Function
imodpoly_baseline(x, y; poly_order=4, maxiter=100, tol=1e-3) -> VectorImproved 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.
OpticalSpectroscopy.rolling_ball_baseline — Function
rolling_ball_baseline(y; half_window=50, smooth_half_window=nothing) -> VectorRolling 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