Open Lab/Preprocessing · Method
Baseline Correction
AsLS
A baseline estimation approach that combines smoothness penalization with asymmetric weighting to separate a slowly varying background from spectral peaks.
What is Baseline Correction?
Spectral measurements can contain a slowly varying background, or baseline, in addition to the analytical signal of interest. Baseline correction estimates that background and subtracts it from the measured signal.
The difficulty is to estimate the baseline without treating analytical peaks as part of the background. Asymmetric Least Squares (AsLS) is one widely used approach based on penalized smoothing and asymmetric weights.
Baseline origin depends on the analytical technique. Not every slowly varying feature is unwanted, and not every spectroscopic baseline has the same physical cause.
Why use it?
Baseline variation can interfere with peak height or peak area interpretation, with comparison between spectra, and with downstream multivariate analysis, depending on the measurement and on the analytical objective.
In Raman spectroscopy, fluorescence can produce a broad background that AsLS-type methods have been used to address. The same mathematical idea is not restricted to Raman data.
Baseline correction should not be assumed to improve every model, and it should not be applied automatically without inspecting the corrected spectra.
How does AsLS work?
AsLS estimates a smooth curve beneath the analytical peaks. It balances two objectives: the estimated baseline should remain close to the data, and it should remain smooth.
A smoothness penalty, built from second differences, discourages rapid curvature. Asymmetric weights are then used because points above the estimated baseline are more likely to belong to positive analytical peaks. Those points receive less influence in the next fit.
- Initialize the weights.
- Estimate a smooth baseline for the current weights.
- Compare the data with that baseline.
- Downweight points that lie above the baseline.
- Refit.
- Repeat until the chosen iteration limit is reached.
Mathematics & algorithm
Let be the observed spectrum and let be the estimated baseline. For fixed weights, AsLS is a weighted penalized least squares problem. The second-difference penalty is the smoothness device used in Whittaker-type smoothing.
Mathematical foundation
- observed spectrum
- estimated baseline
- asymmetric weight at point i
- diagonal matrix of the weights
- smoothness penalty parameter, λ > 0
- second-difference matrix of shape (n − 2) × n
- second finite difference of the baseline
- asymmetry parameter, 0 < p < 1
Interpretation
Larger λ imposes a smoother baseline. Smaller λ allows a more flexible baseline. For positive peaks, p is typically chosen below 0.5 so that points above the current baseline are downweighted. No single (λ, p) pair is universally appropriate. After the weights are updated, Equation 3 is solved again. The corrected spectrum is y − z. For spectra dominated by negative peaks, the inequality in Equation 4 would need to be reconsidered.
Asymmetric Least Squares Baseline Correction
InputSpectrum y, smoothness λ, asymmetry p, maximum iterations K
OutputBaseline and corrected spectrum
- 01 for all
- 02construct second-difference matrix
- 03for to do
- 04
- 05
- 06for to do
- 07if then
- 08
- 09else
- 10
- 11end if
- 12end for
- 13end for
- 14
- 15return
This pseudocode is a SPARKS representation of Equations 1 to 4. It is not a verbatim extract from Eilers and Boelens. D is the (n − 2) × n second-difference operator with diagonals 1, −2, 1.
Visual example
Positive peaks sit on a smooth background. The dashed trace is the AsLS baseline estimated from that observed spectrum. The right panel is the residual after subtraction.
Code
The functions below are SPARKS implementations of Equations 1 to 4. They are not copied from Eilers and Boelens. Python builds as an second-difference operator and solves Equation 3 with a sparse linear solver. MATLAB uses diff(speye(n), 2), which produces the same operator. Both return . A row-wise wrapper is included for samples-by-variables matrices.
import numpy as npfrom scipy import sparsefrom scipy.sparse.linalg import spsolve def asymmetric_least_squares(y, lam=1e5, p=0.01, n_iter=10): y = np.asarray(y, dtype=float) if y.ndim != 1: raise ValueError("y must be one dimensional.") if lam <= 0: raise ValueError("lam must be positive.") if not 0 < p < 1: raise ValueError("p must lie between 0 and 1.") n = y.size if n < 3: raise ValueError("y must contain at least three points.") D = sparse.diags( [1.0, -2.0, 1.0], [0, 1, 2], shape=(n - 2, n), format="csc", ) penalty = lam * (D.T @ D) w = np.ones(n) for _ in range(n_iter): W = sparse.spdiags(w, 0, n, n) z = spsolve(W + penalty, w * y) w = np.where(y > z, p, 1.0 - p) return y - z, z def asls_spectra(X, lam=1e5, p=0.01, n_iter=10): X = np.asarray(X, dtype=float) if X.ndim != 2: raise ValueError("X must be a 2D array of samples by spectral variables.") corrected = np.empty_like(X) baselines = np.empty_like(X) for i, spectrum in enumerate(X): corrected[i], baselines[i] = asymmetric_least_squares( spectrum, lam=lam, p=p, n_iter=n_iter ) return corrected, baselines Practical notes
- AsLS assumes the baseline is smoother than the analytical peak structure.
- λ controls smoothness. Larger λ yields a smoother baseline. Smaller λ yields a more flexible baseline.
- p controls asymmetric weighting. For positive peaks, smaller p downweights points above the current baseline more strongly.
- Parameter choice depends on signal scale, sampling and baseline structure. No single setting is appropriate across techniques.
- Strongly overlapping or very broad analytical features can make baseline estimation difficult. A baseline that is too flexible can remove meaningful signal.
References
- 1.
Eilers, P. H. C., & Boelens, H. F. M. (2005). Baseline Correction with Asymmetric Least Squares Smoothing. Leiden University Medical Centre report.
- 2.
Eilers, P. H. C. (2003). A Perfect Smoother. Analytical Chemistry, 75(14), 3631-3636.
doi:10.1021/ac034173t - 3.
He, S., Zhang, W., Liu, L., Huang, Y., He, J., Xie, W., Wu, P., & Du, C. (2014). Baseline correction for Raman spectra using an improved asymmetric least squares method. Analytical Methods, 6(12), 4402-4407.
doi:10.1039/C4AY00068D
