detrend

Removes a mean or a straight-line trend from data.

Category: Language Fundamentals > Data Types

Syntax

y = detrend(x)

Subtracts the best-fit straight line from each column.

y = detrend(x, n)

Removes a polynomial of degree n, with 0 removing the mean.

y = detrend(x, 'constant')

Removes only the mean.

y = detrend(x, 'linear')

Removes the linear trend (default).

Description

The function fits a polynomial to the data in the least-squares sense, taking the sample index as the independent variable, and subtracts it. The result oscillates around zero, which is useful before spectral analysis, where a trend would add energy at low frequencies.

A row vector is treated as a single signal, not as many.

Input arguments

x
Vector, or matrix whose columns are treated as separate signals.
n / type
Degree n of the polynomial, or 'constant' or 'linear'.

Output arguments

y
Detrended data, same size as x.

Example

Mean and linear trend

x = [1 2 4 7];
detrend(x, 'constant')
detrend(x, 'linear')
Output
ans = 1×4

   -2.5000   -1.5000    0.5000    3.5000
ans = 1×4

    0.5000   -0.5000   -0.5000    0.5000

See also

polyfit polyval mean fft filter