function [mM, Mm]=tp2mm(tp)
%TP2MM Calculates the min2Max and Max2min cycles from the sequence 
%       of turning points.
%
%  CALL: [mM,Mm] = tp2mm(TP);
%
%        mM  = a two column matrix with the min2Max count.
%        Mm  = a two column matrix with the Max2min count.
%        TP  = a two column matrix with the sequence of turning points.

% handles vectors
% Revised by jr July 6, 2000
% - Added line 38
  
[n m]= size(tp);
if n<m
  b=m;m=n;n=b; 
  tp=tp';
end

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

switch m
  case {1, 2},  % dimension OK!
  otherwise, 
    error('Wrong dimension of input! dim must be 2xN, 1xN, Nx2 or Nx1 ')
end

if tp(1,m)>tp(2,m)
  n=length(tp);
  tp = tp(2:n,:);
  if tp(1,m)>tp(2,m)
    error('tp  is not a sequence of turning points.')
  end
end

n=length(tp);
mM=[tp(1:2:n-1,m) tp(2:2:n,m)];
Mm=[tp(2:2:n-1,m) tp(3:2:n,m)];

