detrend
Detrending of time series.
p=detrend(y)
p=detrend(y,m)
p=detrend(y,m,x)
Inputs
y A real vector.
m An optional integer, with default value of 1 .
x An optional vector of time instances at which the input y is defined. The default value is [0 1 2 3 ....] .
Outputs
p The detrended time sries obtained by removing the polynomial fit of order m from the input.


Description
If m=1 , the function detrend subtracts from y the least-square fitted linear aproximation to y . That is, it first identifies the parameters a and in the linear fit
    z = a + b*x
by minimizing the sum
    (z[1]-y[1])^2 + (z[2]-y[2])^2 + (z[3]-y[3])^2 ...
and then returns y-z .

If the second input is an integer m , instead of a linear series, best fit polynomial series of order m is subtracted from y to create the output.




Example
>>x=[0:10];
>>y=10x+2+5rand(size(x))
>>z=detrend(y)
>>plot(x,y,x,z)