function [highf,lowf] = lowhighsplit(x,cutoff)
%    LowHighSplit  Split array into low and high frequency components 
%    based on a cutoff, using lowpassmin
%    [highf,lowf] = lowhighsplit(x,cutoff) applies filter to matrix x
%    and produces high and low frequency components
%    Uses lowpassmin (lowpass and butter) 

lowlim=1./(cutoff./2);
frequency=1./cutoff;
[nrows,ncols]=size(x);

%  need to find where each series starts and stops. Series must be continuous
gotstart=zeros(1,ncols);
gotstop=zeros(1,ncols);
start=zeros(1,ncols);
stop=zeros(1,ncols);
stop(:)=nrows;

for i=1:nrows
  for j=1:ncols
    if ~isnan(x(i,j)) & gotstart(j)~=1
      start(j)=i;
      gotstart(j)=1;
    elseif isnan(x(i,j)) & gotstart(j)==1 & gotstop(j)~=1
      stop(j)=i-1;
      gotstop(j)=1;	
    end
  end
end

% Apply the zero-phase butterworth filter 

highf=NaN*ones(nrows,ncols);
lowf=NaN*ones(nrows,ncols); 
for k=1:ncols
  data=x(start(k):stop(k),k);
  lowf(start(k):stop(k),k)=lowpassmin(data,frequency);
  highf(start(k):stop(k),k)=data-lowf(start(k):stop(k),k);
end

% done
