Remove running line fit (using local linear regression)-continuous processes Usage: data=locdetrend(data,Fs,movingwin) Inputs: Note that units of Fs, movinwin have to be consistent. data (data as a matrix times x channels or a single vector) Fs (sampling frequency) - optional. Default 1 movingwin (length of moving window, and stepsize) [window winstep] - optional. Default. window=full length of data (global detrend). winstep=window -- global detrend Output: data: (locally detrended data)
0001 function data=locdetrend(data,Fs,movingwin) 0002 % Remove running line fit (using local linear regression)-continuous 0003 % processes 0004 % Usage: data=locdetrend(data,Fs,movingwin) 0005 % Inputs: 0006 % Note that units of Fs, movinwin have to be consistent. 0007 % data (data as a matrix times x channels or a single vector) 0008 % Fs (sampling frequency) - optional. Default 1 0009 % movingwin (length of moving window, and stepsize) [window winstep] - optional. 0010 % Default. window=full length of data (global detrend). 0011 % winstep=window -- global detrend 0012 % 0013 % Output: 0014 % data: (locally detrended data) 0015 data=change_row_to_column(data); 0016 [N,C]=size(data); 0017 if nargin < 2 || isempty(Fs); Fs=1; end; 0018 if nargin < 3 || isempty(movingwin); movingwin=[N/Fs N/Fs]; end; 0019 Tw=movingwin(1); Ts=movingwin(2); 0020 if Ts>Tw; error('Use step size shorter than window size'); end; 0021 n=round(Fs*Tw); 0022 dn=round(Fs*Ts); 0023 if ~isreal(data) 0024 yr=real(data); 0025 yi=imag(data); 0026 if n==N; 0027 yr=detrend(yr); 0028 yi=detrend(yi); 0029 data=yr+i*yi; 0030 else; 0031 for ch=1:C 0032 tmp=runline(yr(:,ch),n,dn); 0033 yr=yr-tmp; 0034 tmp=runline(yi(:,ch),n,dn); 0035 yi=yi-tmp; 0036 data(:,ch)=yr+i*yi; 0037 end; 0038 end; 0039 else 0040 if n==N; 0041 data=detrend(data); 0042 else; 0043 for ch=1:C; 0044 tmp=runline(data(:,ch),n,dn); 0045 data(:,ch)=data(:,ch)-tmp; 0046 end; 0047 end 0048 end