Open Lab/Preprocessing · Method

Detrending

DT

A spectrum wise preprocessing method that removes smooth baseline trends by fitting and subtracting a polynomial from each spectrum.

SpectroscopyNIRBaseline CorrectionPolynomialPythonMATLABTutorial
Wavelength (nm)Intensity
Wavelength (nm)DT
01

What is Detrending?

Spectral detrending is a preprocessing transformation that removes a smooth baseline trend from each spectrum independently.

Barnes, Dhanoa and Lister introduced detrending for near-infrared diffuse reflectance spectra in order to account for baseline shift and curvilinearity. Their implementation used a second-degree polynomial regression against the spectral coordinate.

The fitted polynomial contribution is subtracted. What remains is the residual spectral signal after that smooth trend has been removed.

02

Why use it?

Spectra can contain wavelength-dependent baseline variation that is not the spectral structure of primary analytical interest. In diffuse reflectance NIR, Barnes et al. discussed baseline shift and curvilinearity associated with the physical behavior of powdered or densely packed samples.

Detrending models a smooth trend across the spectral axis and subtracts it. This can make a spectrum less dominated by broad baseline variation before further analysis.

The method does not remove all scatter, and it does not isolate pure chemical information. It should not be assumed to improve every calibration, and it is not required in every application.

03

How does it work?

The operation is row-wise. Each spectrum is processed from its own intensities and from the spectral coordinate. Other spectra are not used.

  1. Take the spectral coordinate, such as wavelength.
  2. Construct a polynomial basis in that coordinate.
  3. Fit the spectrum as a function of the coordinate by least squares.
  4. Estimate the smooth polynomial trend.
  5. Subtract the fitted trend from the measured spectrum.
  6. Return the residual spectrum.

Classical Barnes detrending uses a second-degree polynomial. The default demonstration below follows that choice.

Barnes et al. presented detrending together with Standard Normal Variate. SNV centers and scales each spectrum. Detrending removes a wavelength-dependent polynomial trend. They may be used as a sequence. Detrending itself is not SNV, and the code on this page does not apply SNV.

04

Mathematics & algorithm

Let be the intensity of spectrum at spectral coordinate . For quadratic detrending, a second-degree polynomial is fitted to that spectrum.

Mathematical foundation

(1)
(2)
(3)
(4)
(5)
spectrum / sample index
spectral variable index
number of spectral variables
spectral coordinate of variable j
measured intensity
quadratic, linear and constant trend coefficients
fitted polynomial trend
residual after the polynomial fit
polynomial design matrix for one spectrum
coefficient vector for the polynomial columns of P

Interpretation

Equation 3 is the detrended spectrum: the residual after subtracting the fitted trend. Equivalently, and . The least squares residual is orthogonal to the columns of .

When wavelength values are available, those coordinates can be used in . The educational code below uses a normalized coordinate if no axis is supplied. That normalization is a numerical convenience. It is not part of the historical Barnes definition. The polynomial trend depends on the chosen spectral coordinate, so the same degree is not automatically equivalent under a change of axis scaling.

Algorithm 1

Spectral Detrending

InputSpectral matrix X ∈ ℝⁿˣᵖ, spectral coordinate λ, polynomial degree d

OutputDetrended matrix

  1. 01construct polynomial design matrix from
  2. 02for to do
  3. 03 ← spectrum
  4. 04estimate by least squares
  5. 05
  6. 06
  7. 07
  8. 08end for
  9. 09return

This pseudocode is a SPARKS representation of Equations 1 to 5. It is not a verbatim extract from Barnes et al. The demonstration uses d = 2, the classical second-degree choice.

05

Visual example

One spectrum contains peaks plus a quadratic trend. The dashed trace is the fitted second-degree polynomial. Subtracting that fit removes the trend and leaves the residual peak structure.

Wavelength (nm)IntensityObserved and trend
Wavelength (nm)DTDetrended
Controlled quadratic trend. Left: observed spectrum and fitted trend. Right: spectrum after subtracting the fit.
06

Code

The functions below are SPARKS implementations of Equations 1 to 5. They are not copied from Barnes et al. Both operate on a matrix with rows as samples and columns as spectral variables. Each spectrum is fitted independently. The default degree is 2. If no spectral axis is supplied, a normalized coordinate from to is used. SNV is not applied.

import numpy as np def detrend_spectra(X, x_axis=None, degree=2):    X = np.asarray(X, dtype=float)     if X.ndim != 2:        raise ValueError(            "X must be a 2D array of samples by spectral variables."        )     n_samples, n_variables = X.shape     if x_axis is None:        x_axis = np.linspace(-1.0, 1.0, n_variables)    else:        x_axis = np.asarray(x_axis, dtype=float)     if x_axis.shape != (n_variables,):        raise ValueError(            "x_axis must contain one value per spectral variable."        )     if degree < 0 or degree >= n_variables:        raise ValueError(            "degree must be nonnegative and smaller than the number of variables."        )     P = np.vander(x_axis, N=degree + 1, increasing=True)    X_dt = np.empty_like(X)     for i, spectrum in enumerate(X):        coefficients, *_ = np.linalg.lstsq(P, spectrum, rcond=None)        trend = P @ coefficients        X_dt[i] = spectrum - trend     return X_dt 
07

Practical notes

  • Classical Barnes detrending uses a second-degree polynomial fitted independently to each spectrum.
  • The spectral coordinate is part of the model. When wavelengths are available, they can be used. A normalized axis is only a numerical convenience.
  • A more flexible polynomial can follow more complex baseline shapes. An unnecessarily flexible trend can also remove meaningful spectral structure.
  • Detrending and SNV address different forms of variation. They may be combined as a sequence when that choice is appropriate for the dataset. They are not the same transformation.
  • Preprocessing should be evaluated for the specific dataset and analytical objective. Detrending is not required in every application.
08

References

  1. 1.

    Barnes, R. J., Dhanoa, M. S., & Lister, S. J. (1989). Standard Normal Variate Transformation and De-Trending of Near-Infrared Diffuse Reflectance Spectra. Applied Spectroscopy, 43(5), 772-777.

    doi:10.1366/0003702894202201
  2. 2.

    Rinnan, Å., van den Berg, F. W. J., & Engelsen, S. B. (2009). Review of the most common pre-processing techniques for near-infrared spectra. TrAC Trends in Analytical Chemistry, 28(10), 1201-1222.

    doi:10.1016/j.trac.2009.07.007