%
% remove the trend from a time series
%
%     yt = detrend( y )
%
% where  y  = initial time series
%        yt = detrended time series
%
% Bob Newell, April 1966
%
%-------------------------------------------------------
function yt = detrend( y )
%
% calculate a regression line
%
n = length( y );
yy = y(:) - mean(y);
xx = [ ones(n,1) (1:n)' ];
p = xx\yy;
%
% recalculate the data
%
yt = yy - xx * p + mean(y);
%
return;
%-------------------------------------------------------
% That's all
