function pico = detPico(x)
% DETPICO Detects a maximum peak within a sequence of samples.
%
% [PICO] = DETPICO(X) where the out (PICO) returns the position of the
% maximum peak detected in the input, the sequence of samples (X).
% Copyright (C) Iván López Espejo 2010
% Version: Id: detPico.m , v 1.0 2010
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You can obtain a copy of the GNU General Public License from
% ftp://prep.ai.mit.edu/pub/gnu/COPYING-2.0 or by writing to
% Free Software Foundation, Inc.,675 Mass Ave, Cambridge, MA 02139, USA.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
THRES = 3; % Default threshold to consider a peak.
sube = 0; % Positive slope counter.
baja = 0; % Negative slope counter.
pico = 0; % Maximum detected peak.
for n = 1:length(x)-1
if x(n+1) - x(n) > 0
sube = sube + 1;
prepico = n + 1;
baja = 0;
end
if x(n+1) - x(n) < 0
if sube > THRES
baja = baja + 1;
if baja > THRES
if pico ~= 0
if x(pico) < x(prepico)
pico = prepico;
end
sube = 0;
else
pico = prepico;
sube = 0;
end
end
else
sube = 0;
end
end
end
% Plot of the result.
plot(x)
grid on
hold on
plot(pico,x(pico),'*r')
hold off
xlabel('Muestras')
ylabel('Amplitud')
title('Pico máximo detectado')