detrend.RdRemoves the mean value or (piecewise) linear trend from a vector or from each column of a matrix.
detrend(x, tt = 'linear', bp = c())detrend computes the least-squares fit of a straight line (or
composite line for piecewise linear trends) to the data and subtracts the
resulting function from the data.
To obtain the equation of the straight-line fit, use polyfit.
removes the mean or (piecewise) linear trend from x and returns it
in y=detrend(x), that is x-y is the linear trend.
Detrending is often used for FFT processing.
t <- 1:9
x <- c(0, 2, 0, 4, 4, 4, 0, 2, 0)
x - detrend(x, 'constant')
#> [,1]
#> [1,] 1.777778
#> [2,] 1.777778
#> [3,] 1.777778
#> [4,] 1.777778
#> [5,] 1.777778
#> [6,] 1.777778
#> [7,] 1.777778
#> [8,] 1.777778
#> [9,] 1.777778
x - detrend(x, 'linear')
#> [,1]
#> [1,] 1.777778
#> [2,] 1.777778
#> [3,] 1.777778
#> [4,] 1.777778
#> [5,] 1.777778
#> [6,] 1.777778
#> [7,] 1.777778
#> [8,] 1.777778
#> [9,] 1.777778
y <- detrend(x, 'linear', 5)
if (FALSE) { # \dontrun{
plot(t, x, col="blue")
lines(t, x - y, col="red")
grid()} # }