# Documentation of lin_remove

Global Index (all files) (short | long) | Local Index (files in subdir) (short | long)

## Function Synopsis

`[y, xpat] = lin_remove(x,xtim)`

## Help text

```lin_remove:  linearly remove a time series from data

Y = lin_remove(Xdat, Xtim) removes the best linear fit of Xtim to
each column of Xdat.  If Xdat is N-dimensional, then it is
assumed that the time series Xtim will be removed from the first
dimension of Xdat.

Y = lin_remove(Xdat) assumes Xtim is evenly spaced, so the linear
trend is removed.

```

## Listing of function lin_remove

```function [y, xpat] = lin_remove(x,xtim)

sz = size(x); ndim = length(sz);
if (ndim == 2) & (sz(1) == 1); x = x(:); end;
sz = size(x); ndim = length(sz);

if nargin < 2; xtim = [1:sz(1)]/sz(1); end;

if (size(xtim, 1))==1; xtim=xtim(:); end;

if size(xtim, 1)~=sz(1);
error('Xtim must have the same length as the first dimension of Xdat');
end

xnum = size(xtim, 2);

%  Reshape x if necessary, assuming the dimension to be
%  detrended is the first

if ndim > 2;
x = reshape(x, sz(1), prod(sz(2:ndim)));
end

N = size(x,1);

%  Remove means from data and time series
xtim = xtim - ones(N, 1)*mean(xtim);
x = x - ones(N, 1)*mean(x);

%  Remove regression

xpat = xtim\x;
y = x - xtim*xpat;

if sz(1) == 1
y = y.';
end

%  Reshape output so it is the same dimension as input

if ndim > 2;
y = reshape(y, sz);
xpat = reshape(xpat, [xnum sz(2:ndim)]);
end

```