function y = tremfilt(x,deriv)
%TREMFILT   Filters the specified derivative of a tremor time series.

%    Y = TREMFILT(X,DERIV) takes the derivative specified by DERIV
%    (0=displacement, 1=velocity or 2=acceleration) of a tremor time
%    series X and filters it appropriately to create the filtered
%    data Y.  
%
%    For the 0th derivative (displacement), the signal is detrended
%    and high-pass filtered below 3 Hz.  
%   
%    For the 1st and 2nd derivatives (velocity and acceleration), the
%    signal is low-pass filtered above 15 Hz. 
%
%    See also MAKEFILT, DATAFILT.
%

%   Copyright 2003
%   Centre for Nonlinear Dynamics in Physiology and Medicine

  if (deriv >= 1) x = diff(x) * 200; end;
  if (deriv >= 2) x = diff(x) * 200; end;
  if (deriv >= 1)
    xfilt = makefilt(.075, length(x), .025);
  else
    p = polyfit((1:length(x))', x, 1);
    x = x - polyval(p, (1:length(x))');
    xfilt = 1-makefilt(.01, length(x), .005);
  end
  y = datafilt(x,xfilt);
  
return


