


EXTREMA Gets the global extrema points from a time series.
[XMAX,IMAX,XMIN,IMIN] = EXTREMA(X) returns the global minima and maxima
points of the vector X ignoring NaN's, where
XMAX - maxima points in descending order
IMAX - indexes of the XMAX
XMIN - minima points in descending order
IMIN - indexes of the XMIN
DEFINITION (from http://en.wikipedia.org/wiki/Maxima_and_minima):
In mathematics, maxima and minima, also known as extrema, are points in
the domain of a function at which the function takes a largest value
(maximum) or smallest value (minimum), either within a given
neighbourhood (local extrema) or on the function domain in its entirety
(global extrema).
Example:
x = 2*pi*linspace(-1,1);
y = cos(x) - 0.5 + 0.5*rand(size(x)); y(40:45) = 1.85; y(50:53)=NaN;
[ymax,imax,ymin,imin] = extrema(y);
plot(x,y,x(imax),ymax,'g.',x(imin),ymin,'r.')
See also EXTREMA2, MAX, MIN

0001 function [xmax,imax,xmin,imin] = extrema(x) 0002 %EXTREMA Gets the global extrema points from a time series. 0003 % [XMAX,IMAX,XMIN,IMIN] = EXTREMA(X) returns the global minima and maxima 0004 % points of the vector X ignoring NaN's, where 0005 % XMAX - maxima points in descending order 0006 % IMAX - indexes of the XMAX 0007 % XMIN - minima points in descending order 0008 % IMIN - indexes of the XMIN 0009 % 0010 % DEFINITION (from http://en.wikipedia.org/wiki/Maxima_and_minima): 0011 % In mathematics, maxima and minima, also known as extrema, are points in 0012 % the domain of a function at which the function takes a largest value 0013 % (maximum) or smallest value (minimum), either within a given 0014 % neighbourhood (local extrema) or on the function domain in its entirety 0015 % (global extrema). 0016 % 0017 % Example: 0018 % x = 2*pi*linspace(-1,1); 0019 % y = cos(x) - 0.5 + 0.5*rand(size(x)); y(40:45) = 1.85; y(50:53)=NaN; 0020 % [ymax,imax,ymin,imin] = extrema(y); 0021 % plot(x,y,x(imax),ymax,'g.',x(imin),ymin,'r.') 0022 % 0023 % See also EXTREMA2, MAX, MIN 0024 0025 % Written by 0026 % Lic. on Physics Carlos Adrián Vargas Aguilera 0027 % Physical Oceanography MS candidate 0028 % UNIVERSIDAD DE GUADALAJARA 0029 % Mexico, 2004 0030 % 0031 % nubeobscura@hotmail.com 0032 0033 % From : http://www.mathworks.com/matlabcentral/fileexchange 0034 % File ID : 12275 0035 % Submited at: 2006-09-14 0036 % 2006-11-11 : English translation from spanish. 0037 % 2006-11-17 : Accept NaN's. 0038 % 2007-04-09 : Change name to MAXIMA, and definition added. 0039 0040 0041 xmax = []; 0042 imax = []; 0043 xmin = []; 0044 imin = []; 0045 0046 % Vector input? 0047 Nt = numel(x); 0048 if Nt ~= length(x) 0049 error('Entry must be a vector.') 0050 end 0051 0052 % NaN's: 0053 inan = find(isnan(x)); 0054 indx = 1:Nt; 0055 if ~isempty(inan) 0056 indx(inan) = []; 0057 x(inan) = []; 0058 Nt = length(x); 0059 end 0060 0061 % Difference between subsequent elements: 0062 dx = diff(x); 0063 0064 % Is an horizontal line? 0065 if ~any(dx) 0066 return 0067 end 0068 0069 % Flat peaks? Put the middle element: 0070 a = find(dx~=0); % Indexes where x changes 0071 lm = find(diff(a)~=1) + 1; % Indexes where a do not changes 0072 d = a(lm) - a(lm-1); % Number of elements in the flat peak 0073 a(lm) = a(lm) - floor(d/2); % Save middle elements 0074 a(end+1) = Nt; 0075 0076 % Peaks? 0077 xa = x(a); % Serie without flat peaks 0078 b = (diff(xa) > 0); % 1 => positive slopes (minima begin) 0079 % 0 => negative slopes (maxima begin) 0080 xb = diff(b); % -1 => maxima indexes (but one) 0081 % +1 => minima indexes (but one) 0082 imax = find(xb == -1) + 1; % maxima indexes 0083 imin = find(xb == +1) + 1; % minima indexes 0084 imax = a(imax); 0085 imin = a(imin); 0086 0087 nmaxi = length(imax); 0088 nmini = length(imin); 0089 0090 % Maximum or minumim on a flat peak at the ends? 0091 if (nmaxi==0) && (nmini==0) 0092 if x(1) > x(Nt) 0093 xmax = x(1); 0094 imax = indx(1); 0095 xmin = x(Nt); 0096 imin = indx(Nt); 0097 elseif x(1) < x(Nt) 0098 xmax = x(Nt); 0099 imax = indx(Nt); 0100 xmin = x(1); 0101 imin = indx(1); 0102 end 0103 return 0104 end 0105 0106 % Maximum or minumim at the ends? 0107 if (nmaxi==0) 0108 imax(1:2) = [1 Nt]; 0109 elseif (nmini==0) 0110 imin(1:2) = [1 Nt]; 0111 else 0112 if imax(1) < imin(1) 0113 imin(2:nmini+1) = imin; 0114 imin(1) = 1; 0115 else 0116 imax(2:nmaxi+1) = imax; 0117 imax(1) = 1; 0118 end 0119 if imax(end) > imin(end) 0120 imin(end+1) = Nt; 0121 else 0122 imax(end+1) = Nt; 0123 end 0124 end 0125 xmax = x(imax); 0126 xmin = x(imin); 0127 0128 % NaN's: 0129 if ~isempty(inan) 0130 imax = indx(imax); 0131 imin = indx(imin); 0132 end 0133 0134 % Same size as x: 0135 imax = reshape(imax,size(xmax)); 0136 imin = reshape(imin,size(xmin)); 0137 0138 % Descending order: 0139 [temp,inmax] = sort(-xmax); clear temp 0140 xmax = xmax(inmax); 0141 imax = imax(inmax); 0142 [xmin,inmin] = sort(xmin); 0143 imin = imin(inmin); 0144 0145 0146 % Carlos Adrián Vargas Aguilera. nubeobscura@hotmail.com