Home > NoiseTools > nt_detrnd_period.m

# nt_detrnd_period

## PURPOSE

y=nt_detrnd_period(x,period) - remove trend over period

## SYNOPSIS

function y=nt_detrnd_period(x,period)

## DESCRIPTION

    y=nt_detrnd_period(x,period) - remove trend over period

     A linear trend is removed so that the difference between samples
     separated by 'period' is zero on average (calculated over an interval
     of size 'size(x,1)-period').

     If 3D, the trend is removed from each trial and channel separately.

     NoiseTools

## CROSS-REFERENCE INFORMATION

This function calls:

- nt_demean [y,mn]=nt_demean(x,w) - remove weighted mean over cols
- nt_vecmult y=nt_vecmult(x,v) - multiply all rows or columns of matrix by vector

This function is called by:

## SOURCE CODE

    0001 function y=nt_detrnd_period(x,period)
    0002 %y=nt_detrnd_period(x,period) - remove trend over period
    0003 %
    0004 % A linear trend is removed so that the difference between samples
    0005 % separated by 'period' is zero on average (calculated over an interval
    0006 % of size 'size(x,1)-period').
    0007 %
    0008 % If 3D, the trend is removed from each trial and channel separately.
    0009 %
    0010 % NoiseTools
    0011 %
    0012
    0013 if nargin<2; error('!'); end
    0014 if period>size(x,1)-1; error('period too large'); end
    0015
    0016 period=round(period);
    0017
    0018 [m,n,o]=size(x);
    0019 x=reshape(x,[m,n*o]);
    0020
    0021 % averages over first and last intervals
    0022 interval_size=m-period;
    0023 a=mean(x(1:interval_size,:));
    0024 b=mean(x(end-interval_size+1:end,:));
    0025
    0026 % trend functions
    0027 t=cumsum(ones(size(x)));
    0028 t=nt_vecmult(t,(b-a)/period);
    0029 t=nt_demean(t);
    0030
    0031 y=x-t;  % remove trend
    0032 y=reshape(y,[m,n,o]);