Detrending & Baseline Correction❓

🧮 Calculator 🏠 Home

How to bring a drifting signal back to a flat zero baseline (baseline correction, leveling, or zeroing): mean removal, linear detrending, polynomial baseline fitting, and asymmetric least squares (ALS) — and why ordinary least-squares fitting fails when peaks are present.

Contents
  1. What Is Detrending? What Does the Signal Look Like After the Drift Is Removed?
  2. Methods
    1. Mean Removal
    2. Linear Detrending
    3. Polynomial Baseline
    4. Asymmetric Least Squares (ALS)
  3. Which One Should You Choose?
  4. Key Ideas
    1. Polynomial Order: Too Low Leaves a Trend; Too High Eats the Peaks
    2. Why Least Squares Is Pulled Up by Peaks
    3. ALS Parameters: λ for Stiffness, p for Asymmetry
    4. Trend and the FFT: Drift Fills the Low-Frequency Bins
  5. REFs

What Is Detrending? What Does the Signal Look Like After the Drift Is Removed?

A measurement rarely sits at zero. A spectrometer's background rises with wavelength, a sensor warms up and drifts over an hour, and a chromatogram rides on a slowly changing solvent signal. The features you want to study—peaks, oscillations, and events—sit on top of a slowly varying curve that is unrelated to them. This slow curve is called the baseline or trend.

Detrending, also called baseline correction—leveling or flattening when correcting a measured surface, and zeroing when establishing a true zero reference—estimates this baseline and subtracts it from the original signal. The residual signal is then centered around a flat zero baseline (Fig 1):

On the left a signal with three peaks riding on a curved line, its fitted baseline dashed in red; an arrow labelled subtract baseline; on the right the same three peaks on a flat zero line

Fig 1. Fit a baseline under the data, subtract it, and the peaks stand on zero

Every method on this page follows the same two steps. The only difference is how the baseline $b$ is estimated (Math 1):

$$d_i = y_i - b(x_i), \qquad i = 1, \dots, N$$

Math 1.

Here, $y_i$ is the measured value at $x_i$, $b(x_i)$ is the baseline at that point, $d_i$ is the detrended value, and $N$ is the number of data points.

Detrending is not smoothing: smoothing removes high-frequency noise while preserving the slow trend; detrending removes the slow trend while retaining everything else, including noise.

Consider an example with 100 data points, from $x = 0$ to $x = 99$. Three peaks, with heights of 3, 5, and 2, are located at $x = 25$, 50, and 75, respectively. They sit on a baseline that rises, bends, and falls—a combination of a quadratic curve and a slow wave—with a little noise added.

Because the data is synthetic, the true baseline is known. This allows each method to be evaluated against it (Table 1).

xy (measured)true baseline
02.00012.0000
102.48452.4600
255.87702.8864
402.93132.9064
507.73812.7000
602.38952.3600
753.75161.7172
901.05881.0600
990.59140.6932

Table 1. The example: 9 of its 100 points, with the true baseline under each

The bold row, $x = 50$, corresponds to the top of the tallest peak. Each method below is worked out by hand at that point. Chart 1 plots all 100 data points.

100 measured points with three peaks at x = 25, 50 and 75, and the true baseline dashed in red, rising to about 2.9 near x = 35 and falling to 0.7 at x = 99

Chart 1. The example: measured data and the true baseline under it

The task: find the dashed baseline using only the data points.

Methods

Four ways to estimate the baseline $b$, from the simplest to the method designed for signals with peaks. The first three use least-squares fitting: they fit a curve as closely as possible to all the data points, subject to the chosen model's shape. The fourth treats points above and below the baseline differently.

Mean Removal

In plain words. Calculate the average of all the measured values and subtract it from each one. The resulting data is centered around zero.

Basic idea. Assume the baseline is a single constant equal to the mean $\bar{y}$ (Math 2).

$$b(x) = \bar{y} = \frac{1}{N}\sum_{i=1}^{N} y_i$$

Math 2.

Example. At $x = 50$ (Math 3):

$$\bar{y} = 2.8045, \qquad d_{50} = 7.7381 - 2.8045 = 4.9336$$

Math 3.

Important characteristics. A flat line cannot follow a slope or a curve. Mean removal centers the data around zero, but does nothing beyond that.

Advantages

Disadvantages

Linear Detrending

In plain words. Draw the straight line that fits all the data points as closely as possible, then subtract it from the data.

Basic idea. Model the baseline as a straight line, $b(x) = \beta_0 + \beta_1 x$, where the slope $\beta_1$ and the intercept $\beta_0$ are chosen to minimize the sum of squared differences between the measured values and the fitted line. This is the least-squares line (Math 4).

$$\beta_1 = \frac{\sum_i (x_i - \bar{x})(y_i - \bar{y})}{\sum_i (x_i - \bar{x})^2}, \qquad \beta_0 = \bar{y} - \beta_1 \bar{x}$$

Math 4.

In the example. Calculate $\beta_1$ and $\beta_0$ using two sums over all 100 data points, with $\bar{x} = 49.5$ and $\bar{y} = 2.8045$ (Math 5):

$$\beta_1 = \frac{-1631.23}{83325} = -0.019577, \qquad \beta_0 = 2.8045 + 0.019577 \times 49.5 = 3.7735$$

Math 5.

Then calculate the baseline at $x = 50$ and subtract it from the measured value to obtain the detrended value (Math 6):

$$b(50) = 3.7735 - 0.019577 \times 50 = 2.7947, \qquad d_{50} = 7.7381 - 2.7947 = 4.9434$$

Math 6.

Important characteristics. Removes both a constant offset and a steady linear trend. This is what scipy.signal.detrend does by default.

Advantages

Disadvantages

Polynomial Baseline

In plain words. Use the same approach as linear detrending, but allow the baseline to bend: fit a smooth polynomial curve of a chosen order to the data, then subtract it.

Basic idea. Model the baseline as a polynomial of order $m$, fitted by least squares. First, scale $x$ to the interval $s \in [-1, 1]$ so that higher powers remain on a similar numerical scale and the fit is better conditioned (Math 7).

$$s_i = \frac{2(x_i - x_{\min})}{x_{\max} - x_{\min}} - 1, \qquad b(x_i) = \sum_{j=0}^{m} c_j\, s_i^{\,j}, \qquad \min_{c} \sum_i \big(y_i - b(x_i)\big)^2$$

Math 7.

An order-1 polynomial is equivalent to linear detrending. In the example, consider an order-3 polynomial evaluated at $x = 50$ (Math 8).

$$s_{50} = \frac{2 \times 50}{99} - 1 = 0.0101, \qquad b(50) = 3.8045, \qquad d_{50} = 7.7381 - 3.8045 = 3.9336$$

Math 8.

The true baseline at this point is 2.7000 (Table 1). The cubic fit lies 1.1 units above it: the three peaks have pulled the curve upward, causing the tallest peak to lose one-fifth of its height after detrending.

Important characteristics. A polynomial can follow a curved baseline, to the extent allowed by its order. Choosing the polynomial order is the main decision, and it matters (see Polynomial Order).

Advantages

Disadvantages

Asymmetric Least Squares (ALS)

In plain words. Fit a smooth curve, then identify which points lie above it and give them less weight because they are likely to be peaks. Fit the curve again using the updated weights, and repeat until the curve runs beneath the peaks rather than cutting through them.

Basic idea. Eilers and Boelens (2005) estimate the baseline $z$ by balancing two objectives: staying close to the data and maintaining smoothness. The method uses a Whittaker smoother with asymmetric weights. Closeness to the data is controlled by the weights $w_i$, while smoothness is enforced by penalizing the second differences $\Delta^2 z_i = z_{i+2} - 2 z_{i+1} + z_i$, scaled by $\lambda$ (Math 9).

$$\min_{z} \; \sum_i w_i (y_i - z_i)^2 + \lambda \sum_i (\Delta^2 z_i)^2$$

Math 9.

The asymmetry comes from the weights: a point above the current baseline receives the small weight $p$, while a point below it receives the larger weight $1 - p$ (Math 10).

$$w_i = \begin{cases} p, & y_i > z_i \\ 1 - p, & y_i \le z_i \end{cases}$$

Math 10.

Each iteration requires solving a linear system, where $W$ is the diagonal weight matrix and $D$ is the second-difference matrix (Math 11).

$$\left(W + \lambda D^{\mathsf T} D\right) z = W y$$

Math 11.

Start with all weights set to 1, solve for the baseline, update the weights, and solve again. In this example, use 10 iterations, $\lambda = 10^3$, and $p = 0.01$. At $x = 50$ (Math 12):

$$z_{50} = 2.7811, \qquad d_{50} = 7.7381 - 2.7811 = 4.9570$$

Math 12.

The estimated baseline is only 0.08 above the true baseline, and the peak retains its full height after correction.

Important characteristics. The system matrix is banded, making each solve efficient even for long records. The second-difference penalty assumes evenly spaced data points.

Advantages

Disadvantages

Which One Should You Choose?

Chart 2 overlays the four estimated baselines on the example signal. Table 2 compares each estimate with the true baseline and shows the height of each peak after correction.

The example data with four fitted baselines: mean flat at 2.8, linear sloping from 3.8 to 1.8, cubic arching up to 4 through the peaks, and ALS lying on the dotted true baseline under all three peaks

Chart 2. Four baselines on the example; the true one dotted

methoderror RMSerror maxpeak 25peak 50peak 75
true baseline002.9915.0382.034
mean0.9002.1113.0734.9340.947
linear0.7391.7742.5934.9431.446
polynomial, order 30.7701.1212.1133.9341.415
ALS0.0490.0993.0364.9572.053

Table 2. Baseline error against the truth, and the peak heights each method leaves

The least-squares methods have an average error of 0.7 to 0.9. Mean removal and linear detrending happen to give values close to the true baseline at $x = 50$, but their errors reach 2 elsewhere. With mean removal, the peak at $x = 75$ loses half its height.

Only ALS preserves all three peaks.

In summary:

The basic calculator runs mean removal, linear detrending, and polynomial fitting in your browser. The advanced calculator also supports ALS, which is computed on the server.

Key Ideas

Subtraction itself is exact. Everything depends on the baseline estimate—and there are four ways to get it wrong.

Polynomial Order: Too Low Leaves a Trend; Too High Eats the Peaks

Polynomial order $m$ is the highest power in Math 7. It determines how much the baseline can bend. Chart 3 fits the example using three different polynomial orders.

The example data with polynomial baselines of order 1, a straight sloping line; order 3, an arch through the peaks; and order 15, a wavy curve that climbs into each peak

Chart 3. Polynomial baselines of order 1, 3 and 15 on the example

ordererror RMSpeak 50 left
10.7394.943
30.7703.934
151.2141.532

Table 3. Error and the height left at the tallest peak (true 5.038), by order

Order 1 cannot bend, so the curvature of the true baseline remains in the detrended signal. Order 15 is flexible enough to follow the peaks themselves, removing much of the tallest peak. When peaks are present, no polynomial order is ideal: increasing the order reduces the residual trend but also removes more of the signal.

Use a low order—typically 2 to 4—when the peaks are small relative to the overall signal. Use ALS when the peaks are more prominent.

Why Least Squares Is Pulled Up by Peaks

Least squares minimizes the sum of squared residuals, $\sum_i (y_i - b(x_i))^2$, giving every point the same weight. A peak consists of consecutive points far above the baseline, so the fit cannot ignore them. It moves upward toward the peaks to reduce their squared residuals. In Chart 2, the cubic fit lies 1.1 units above the true baseline at $x = 50$, while the mean baseline lies above the true baseline at both ends.

ALS changes only the weights. With $p = 0.01$, a point above the estimated baseline receives one ninety-ninth of the weight of a point below it (Math 10). As a result, peaks exert less upward influence, allowing the baseline to settle closer to the points between them.

ALS Parameters: λ for Stiffness, p for Asymmetry

Smoothness parameter $\lambda$ controls the trade-off between baseline smoothness and closeness to the data (Math 9). A small $\lambda$ allows the baseline to bend freely and climb into the peaks. A large $\lambda$ makes it stiffer, tending toward a straight line that may fail to follow the true curved baseline (Chart 4).

ALS baselines on the example with lambda 10, climbing partway into each peak; lambda 1000, lying on the true baseline; lambda ten million, nearly straight and below the data in the middle

Chart 4. ALS with $\lambda = 10$, $10^3$, $10^7$; $p = 0.01$

Asymmetry parameter $p$ controls the weight of a point above the baseline (Math 10). At $p = 0.5$, points on both sides receive equal weights, producing an ordinary smooth fit that is pulled into the peaks. Smaller values of $p$ allow the fit to ignore the peaks more effectively (Chart 5).

ALS baselines on the example with p 0.5, bulging up through the peaks; p 0.1, a smaller bulge; p 0.01, lying on the true baseline

Chart 5. ALS with $p = 0.5$, 0.1, 0.01; $\lambda = 10^3$

settingerror RMSpeak 50 left
$\lambda = 10$0.3623.836
$\lambda = 10^3$, $p = 0.01$0.0494.957
$\lambda = 10^7$0.8086.205
$p = 0.5$0.8133.217
$p = 0.1$0.2314.456

Table 4. ALS settings against the truth; tallest peak true 5.038

An overly stiff baseline can overshoot, too. At $\lambda = 10^7$, the nearly straight baseline runs below the curved baseline, causing the corrected peak to appear 1.2 units too tall. Eilers and Boelens suggest using $p$ between 0.001 and 0.1 and $\lambda$ between $10^2$ and $10^9$. The appropriate value of $\lambda$ depends on the number of data points and the width of the peaks. Tune the parameters visually on one record, then keep the same settings for all records of the same type.

Trend and the FFT: Drift Fills the Low-Frequency Bins

Drift in a spectrum — a Fourier transform interprets any slow trend as low-frequency content. Take the two tones from the Fourier transform page: a 1 Hz tone with an amplitude of 1.0 and a 3 Hz tone with an amplitude of 0.5, recorded for 8 seconds at 16 samples per second. Now add a drift of $1 + 0.5\,t$ (Chart 6).

Amplitude spectrum up to 4 Hz: with drift, a bar of 3 at 0 Hz and a falling run of bars across all low bins, plus 0.84 at 1 Hz and 0.45 at 3 Hz; after linear detrending only 0.99 at 1 Hz and 0.50 at 3 Hz remain

Chart 6. Spectrum of two tones plus a linear drift, before and after a linear detrend

binwith driftafter linear detrend
0 Hz2.9840.000
0.125 Hz1.2730.086
1 Hz (true 1.0)0.8430.989
3 Hz (true 0.5)0.4540.497

Table 5. Amplitudes before and after detrending

The drift is not periodic. To the FFT, the end of the record must wrap back to its beginning, creating a discontinuity that causes spectral leakage across the frequency bins. The 0 Hz bin captures the offset, while the nearby bins show a decaying tail. The two tones' amplitudes are underestimated by 16% and 9%, respectively. A single linear detrend removes the tail and brings both amplitudes to within 2% of their true values.

Detrend first, then transform.

REFs

pybaselines, Whittaker baselines - asls, after P. H. C. Eilers, H. F. M. Boelens, Baseline Correction with Asymmetric Least Squares Smoothing, 2005 SciPy scipy.signal.detrend Wikipedia, Polynomial regression Fourier Transform (DFT, FFT): What Frequencies Are in My Data? Smoothing Methods: Moving Average, EWMA, Savitzky-Golay, Gaussian, Median