function pitch=getpitch(v)
%PITCH=GETPITCH(AC)    Obtain a pitch contour from an Autocorrelation matrix. 
%  Matrix must have each frame as a new column.
% 
%  Author: Stuart N Wrigley       
%  MAD - Matlab Auditory Demonstrations
%  (c) University of Sheffield 1998
%  Revision 0.01: 25 July 1998

winsize=size(v,1);
numcols=size(v,2);

for i=1:numcols
   p(:,i)=centreclip(v(:,i),70,'3level');
   pitch(i)=findFirstPeak(p(:,i),winsize);
end

function i=findFirstPeak(p,winsize)
i=1;
while i<length(p)
   if p(i)==0
      break;
   end
   i=i+1; 
end
while i<length(p)
   if p(i)==1
      break;
   end
   i=i+1;
end
if i==winsize
   i=0;
end
