Home > FMAToolbox > General > IsExtremum.m

IsExtremum

PURPOSE ^

IsExtremum - Identify local maxima or minima.

SYNOPSIS ^

function l = IsExtremum(samples,varargin)

DESCRIPTION ^

IsExtremum - Identify local maxima or minima.

 Find the maxima (resp. minima) in a signal by smoothing the signal and then
 finding the points where the derivative goes from positive to negative (resp.
 from negative to positive).

  USAGE

    status = IsExtremum(samples,<options>)

    samples        <a href="matlab:help samples">samples</a> to process
    <options>      optional list of property-value pairs (see table below)

    =========================================================================
     Properties    Values
    -------------------------------------------------------------------------
     'mode'        'maxima' or 'minima' (default = 'maxima')
     'type'        'linear' if samples are linear values (default),
                   'circular' otherwise
     'smooth'      standard deviation for Gaussian kernel (0 = no smoothing,
                   default)
    =========================================================================

  OUTPUT

    status         an NxM logical matrix, i.e. status(i,j)==1 if samples(i,j)
                   is an extremum

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function l = IsExtremum(samples,varargin)
0002 
0003 %IsExtremum - Identify local maxima or minima.
0004 %
0005 % Find the maxima (resp. minima) in a signal by smoothing the signal and then
0006 % finding the points where the derivative goes from positive to negative (resp.
0007 % from negative to positive).
0008 %
0009 %  USAGE
0010 %
0011 %    status = IsExtremum(samples,<options>)
0012 %
0013 %    samples        <a href="matlab:help samples">samples</a> to process
0014 %    <options>      optional list of property-value pairs (see table below)
0015 %
0016 %    =========================================================================
0017 %     Properties    Values
0018 %    -------------------------------------------------------------------------
0019 %     'mode'        'maxima' or 'minima' (default = 'maxima')
0020 %     'type'        'linear' if samples are linear values (default),
0021 %                   'circular' otherwise
0022 %     'smooth'      standard deviation for Gaussian kernel (0 = no smoothing,
0023 %                   default)
0024 %    =========================================================================
0025 %
0026 %  OUTPUT
0027 %
0028 %    status         an NxM logical matrix, i.e. status(i,j)==1 if samples(i,j)
0029 %                   is an extremum
0030 %
0031 
0032 % Copyright (C) 2004-2011 by Michaƫl Zugaro
0033 %
0034 % This program is free software; you can redistribute it and/or modify
0035 % it under the terms of the GNU General Public License as published by
0036 % the Free Software Foundation; either version 3 of the License, or
0037 % (at your option) any later version.
0038 
0039 % Default values
0040 mode = 'maxima';
0041 smooth = 0;
0042 type = 'linear';
0043 
0044 if nargin < 1 | mod(length(varargin),2) ~= 0,
0045   error('Incorrect number of parameters (type ''help <a href="matlab:help IsExtremum">IsExtremum</a>'' for details).');
0046 end
0047 
0048 % Parse parameter list
0049 for j = 1:2:length(varargin),
0050     if ~ischar(varargin{j}),
0051         error(['Parameter ' num2str(j+7) ' is not a property (type ''help <a href="matlab:help IsExtremum">IsExtremum</a>'' for details).']);
0052     end
0053     switch(lower(varargin{j})),
0054         case 'mode',
0055             mode = lower(varargin{j+1});
0056             if ~isastring(mode,'minima','maxima'),
0057                 error('Incorrect value for ''mode'' (type ''help <a href="matlab:help IsExtremum">IsExtremum</a>'' for details).');
0058             end
0059         case 'type',
0060             type = lower(varargin{j+1});
0061             if ~isastring(type,'linear','circular'),
0062                 error('Incorrect value for ''type'' (type ''help <a href="matlab:help Diff">Diff</a>'' for details).');
0063             end
0064         case 'smooth',
0065             smooth = varargin{j+1};
0066             if ~isdvector(smooth,'>=0') || length(smooth) > 2,
0067                 error('Incorrect value for property ''smooth'' (type ''help <a href="matlab:help IsExtremum">IsExtremum</a>'' for details).');
0068             end
0069         otherwise,
0070             error(['Unknown property ''' num2str(varargin{j}) ''' (type ''help <a href="matlab:help IsExtremum">IsExtremum</a>'' for details).']);
0071     end
0072 end
0073 
0074 if size(samples,2) ~= 2,
0075     smooth = [smooth 0];
0076 end
0077 
0078 derivative = Diff(samples,'smooth',smooth,'type',type);
0079 
0080 previous = derivative(1:end-1,2:end);
0081 next = derivative(2:end,2:end);
0082 
0083 if strcmp(mode,'maxima'),
0084     l = previous > 0 & next < 0;
0085 else
0086     l = previous < 0 & next > 0;
0087 end
0088 l = [l;zeros(1,size(l,2))];
0089 l = logical(l);
0090

Generated on Fri 16-Mar-2018 13:00:20 by m2html © 2005