function [ind , Nc]= dat2crossind(x,l,wdef)
%DAT2CROSSIND Returns indices of down and/or upcrossings
%              of level l by a signal.
%
%  CALL:  [ind  Nc]= dat2crossind(x,l,wdef/cdef);
%
%	ind 	= indices to the level l crossings of the original 
%		  sequence x.
%
%	Nc    	= number of crossings (i.e.length of ind). 
%
%       x	= the surface elevation.
%
%       l    	= the reference level (default  l = mean of  x).
%
%	wdef  	= defines the type of wave. Possible options are
%		'dw', 'uw', 'cw', 'tw' or 'none'. Default is 'none'.
%		If wdef='none' all crossings will be returned,
%		otherwise only the crossings which defines a 
%		wave according to the wave definition will be returned.
%
%	cdef  	= defines the type crossings returned. Possible options are
%		'd' 'u' or 'all'. Default is 'all'.
%		If def='d' all down-crossings will be returned.
%		Similarly if def='u' only the up-crossings will be returned
%		otherwise 'all' the crossings will be returned.
%
% Example: 
%   t = linspace(0,7*pi,250); x = sin(t);
%   [ind, Nc] = dat2crossind(x,0.75,'u')
%
% See also: findcross

%tested on: Matlab 6.0, 5.3, 5.2, 5.1
% History:
% revised by pab 12.06.2001
%  -added check on ind returned from findcross.
% Revised by jr 02.04.2001
% - Added example, updated help 
% By Per A. Brodtkorb 07.07.1998,  27.07.1998,  


xn=x;

[n m]= size(xn);
if n<m
 b=m;m=n;n=b; 
 xn=xn';
end

if n<2, 
  error('The vector must have more than 2 elements!')
end

istime=1;

switch m
 case 1, istime=0;
 case 2, xn= xn(:,2);% dimension OK!
 otherwise, error('Wrong dimension of input! dim must be 2xN, 1xN, Nx2 or Nx1 ')          
end

if ((nargin<3) | isempty(wdef)),
  wdef='none';
end

if ((nargin<2) | isempty(l)),
  l=mean(xn);
  disp(['   The level l is set to: ', num2str(l)])
end


% find level l down-crossings and/or up-crossings
% according to wdef or cdef
ind = findcross(xn,l); % faster than find

if isempty(ind), %added pab 12.06.2001
   Nc = 0; 
   txt = sprintf('No level v = %0.5g crossings found in x',l)   
   warning(txt)
   return,
end


switch wdef   % switch wdef/cdef    
  case 'd', %downcrossings only
    if xn(ind(1)+1)>l,
      ind =ind(2:2:end);
    else
      ind =ind(1:2:end);
    end
    
 case 'u',%upcrossings  only
   if xn(ind(1)+1)<l,
      ind =ind(2:2:end);
    else
      ind =ind(1:2:end);
    end
    
  case {'dw','uw'},
    % make sure that the first is a level l down-crossing if wdef == 'dw'
    % or make sure that the first is a level u up-crossing if wdef == 'uw'

    if xor(((xn(ind(1))>xn(ind(1)+1))),strcmp(wdef,'dw')),
      ind(1)=[];
    end
    Nc=length(ind); % number of level l crossings
    % make sure the number of troughs and crests are according to the wavedef.
    % ie. make sure length(ind) is odd
    if ~(mod(Nc,2)), % if Nc is even do
      ind(end)=[];
    end	
  case {'tw','cw'},
    % make sure that the first is a level l down-crossing if wdef == 'tw'
    % or make sure that the first is a level u up-crossing if wdef == 'cw'

    if xor(((xn(ind(1))>xn(ind(1)+1))),strcmp(wdef,'tw')),
      ind(1)=[];
    end
    Nc=length(ind); % number of level l crossings
    % make sure the number of troughs and crests are according to the wavedef.
    % ie. make sure length(ind) is even
    if (mod(Nc,2)), % if Nc is odd do
      ind(end)=[];
    end	
  case {'du','all','none'},
    % do nothing
 otherwise,  error('Unknown wave/crossing definition!')
end
if nargout>1,
  Nc=length(ind); % number of level l crossings
end




