Open Lab/Preprocessing · Method
Spectral Derivatives
Derivatives
First and second derivatives of spectral signals used to emphasize changes in slope and curvature while reducing low order baseline contributions.
What are Spectral Derivatives?
A spectral derivative describes how measured intensity changes with respect to the spectral coordinate. For a spectrum , the first derivative is the local rate of change of intensity with wavelength or another spectral axis.
The second derivative is the rate of change of the first derivative. It therefore describes local curvature. First and second derivatives are widely used forms of spectral preprocessing.
Why use them?
Derivatives act on baseline terms of low polynomial degree in a simple way. If with a constant offset , then because the derivative of a constant is zero. The first derivative therefore removes a constant additive baseline component.
If , then because the second derivative of a linear baseline is zero. The second derivative therefore removes constant and linear baseline contributions. This does not extend automatically to arbitrary curved baselines.
Derivatives can also emphasize local changes in spectral shape and help reveal overlapping features. Numerical differentiation also increases sensitivity to high-frequency noise. That is why derivative calculation in spectroscopy is often combined with smoothing.
How do they work?
Imagine moving across a spectrum from left to right. The first derivative asks how quickly intensity is changing. A positive value means the spectrum is rising. A zero value means the local slope is zero, typically near a peak maximum or a trough. A negative value means the spectrum is falling.
The second derivative asks how that slope itself is changing. It therefore represents curvature.
Experimental spectra are discrete measurements, not a continuous function. Numerical differentiation estimates derivatives from neighboring spectral points. The simple central formulas below are approximations. They are not exact analytical derivatives.
Savitzky-Golay filtering is a related but distinct method. It fits a local polynomial by least squares and obtains derivative estimates from that fitted polynomial. This page treats the derivative transformation. The Savitzky Golay page treats that local polynomial framework.
Mathematics & algorithm
Let be intensity as a function of the spectral coordinate. For uniformly spaced samples with spacing , central finite differences provide a standard numerical approximation.
Mathematical foundation
- measured spectrum
- spectrum without the stated baseline terms
- uniform spacing of the spectral coordinate
- interior spectral index
- numerical first derivative at point j
- numerical second derivative at point j
- linear slope and constant offset of a baseline
Interpretation
Equation 3 is the interior first central difference. Equation 4 is the compact second-difference stencil. Equation 5 is the uniform-grid result of applying Equation 3 twice. The Python and MATLAB snippets use successive first derivatives, so their interior second derivative follows Equation 5, not Equation 4. Both stencils are second-order accurate. They are not the same discrete operator. Boundary samples are not defined by these interior formulas. From Equation 6, a first derivative removes the constant b and leaves the slope a as a constant contribution. A second derivative removes both b and aλ. Arbitrary nonlinear baseline curvature is not removed by this identity.
Spectral Derivatives by Central Differences
InputSpectral matrix X ∈ ℝⁿˣᵖ, spacing Δλ, derivative order d ∈ {1, 2}
OutputDerivative matrix D
- 01validate derivative order
- 02for to do
- 03 ← spectrum
- 04if then
- 05for to do
- 06
- 07end for
- 08else if then
- 09apply the operator twice (Equation 5)
- 10for to do
- 11
- 12end for
- 13end if
- 14end for
- 15return
This pseudocode is a SPARKS representation of Equations 3 and 5. It is not a verbatim extract from the cited papers. Boundary points are left undefined here. Order 2 applies the first-derivative operator twice, matching the Python and MATLAB snippets. Equation 4 is the compact second-difference stencil and is not used by this algorithm. NumPy gradient and MATLAB gradient fill the edges with documented one-sided or higher-order one-sided differences.
Visual example
Overlapping peaks sit on a linear baseline. The first derivative shows slope and a sign change at a peak maximum. The second derivative shows curvature and removes that linear baseline.
Code
The functions below estimate first and second derivatives from discrete spectra. They are SPARKS implementations of numerical differentiation, not copies from the cited papers. Python uses NumPy np.gradient with edge_order=2. MATLAB uses gradient, which applies central differences in the interior and one-sided differences at the edges. For order 2, both snippets apply that first-derivative operator twice, which corresponds to Equation 5 on a uniform grid. Neither function is Savitzky-Golay.
For noisy spectra, derivative estimates are commonly combined with smoothing. A short SciPy Savitzky-Golay pointer is included as a comment. The full method is on the Savitzky Golay page.
import numpy as np def spectral_derivative(X, x_axis, order=1): X = np.asarray(X, dtype=float) x_axis = np.asarray(x_axis, dtype=float) if X.ndim != 2: raise ValueError( "X must be a 2D array of samples by spectral variables." ) if x_axis.ndim != 1 or x_axis.size != X.shape[1]: raise ValueError( "x_axis must contain one coordinate per spectral variable." ) if order not in (1, 2): raise ValueError("order must be 1 or 2.") derivative = np.gradient(X, x_axis, axis=1, edge_order=2) if order == 2: derivative = np.gradient( derivative, x_axis, axis=1, edge_order=2 ) return derivative # Smoothed derivative: see the Savitzky Golay page.# from scipy.signal import savgol_filter# first_derivative = savgol_filter(# spectrum, window_length=11, polyorder=3, deriv=1, delta=delta# ) Practical notes
- First derivatives describe local slope. Second derivatives describe local curvature.
- A first derivative eliminates a constant additive baseline mathematically. A second derivative eliminates constant and linear baseline components mathematically.
- Differentiation can amplify high-frequency noise. For noisy spectra, derivative estimation is commonly combined with smoothing.
- Savitzky-Golay provides one widely used framework for smoothed derivative estimation through local polynomial fitting.
- Higher derivative orders generally increase sensitivity to noise and are not the focus of this introductory resource.
References
- 1.
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 - 2.
Savitzky, A., & Golay, M. J. E. (1964). Smoothing and Differentiation of Data by Simplified Least Squares Procedures. Analytical Chemistry, 36(8), 1627-1639.
doi:10.1021/ac60214a047
