locdetrend.m 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. function data=locdetrend(data,Fs,movingwin)
  2. % Remove running line fit (using local linear regression)-continuous
  3. % processes
  4. % Usage: data=locdetrend(data,Fs,movingwin)
  5. % Inputs:
  6. % Note that units of Fs, movinwin have to be consistent.
  7. % data (data as a matrix times x channels or a single vector)
  8. % Fs (sampling frequency) - optional. Default 1
  9. % movingwin (length of moving window, and stepsize) [window winstep] - optional.
  10. % Default. window=full length of data (global detrend).
  11. % winstep=window -- global detrend
  12. %
  13. % Output:
  14. % data: (locally detrended data)
  15. data=change_row_to_column(data);
  16. [N,C]=size(data);
  17. if nargin < 2 || isempty(Fs); Fs=1; end;
  18. if nargin < 3 || isempty(movingwin); movingwin=[N/Fs N/Fs]; end;
  19. Tw=movingwin(1); Ts=movingwin(2);
  20. if Ts>Tw; error('Use step size shorter than window size'); end;
  21. n=round(Fs*Tw);
  22. dn=round(Fs*Ts);
  23. if ~isreal(data)
  24. yr=real(data);
  25. yi=imag(data);
  26. if n==N;
  27. yr=detrend(yr);
  28. yi=detrend(yi);
  29. data=yr+i*yi;
  30. else;
  31. for ch=1:C
  32. tmp=runline(yr(:,ch),n,dn);
  33. yr=yr-tmp;
  34. tmp=runline(yi(:,ch),n,dn);
  35. yi=yi-tmp;
  36. data(:,ch)=yr+i*yi;
  37. end;
  38. end;
  39. else
  40. if n==N;
  41. data=detrend(data);
  42. else;
  43. for ch=1:C;
  44. tmp=runline(data(:,ch),n,dn);
  45. data(:,ch)=data(:,ch)-tmp;
  46. end;
  47. end
  48. end