[转载]matlab 局部极值点搜索

Description This is a very simple function to find the local maximum in any dimensional array. As simple as it is it still gives nice results.
I use the imdilate() function as a maximum operation and then compare the data to the result.
The function receives three parameters: 
the data, a vector defining the minimum distance between peaks in each of the data dimensions. and a flag either to exclude equal points or not.
use examples: 
a = cumsum(randn(1000,1)); 
peaks = localMaximum(a,[100]); 
figure; plot(a); hold on; plot(peaks,a(peaks),'ro');
[x y] = meshgrid(-6:0.1:6,-6:0.1:6); 
a = sinc(x).*sinc(y); 
lMaxInd = localmaximum(a,[20 20]); 
lMinInd = localMaximum(-a,[20 20]); 
figure; mesh(x,y,a); hold on; 
plot3(x(lMaxInd),y(lMaxInd),a(lMaxInd),'k*','markersize',10,'linewidth',1.5); 
plot3(x(lMinInd),y(lMinInd),a(lMinInd),'g*','markersize',10','linewidth',1.5); 
legend('sinc(x)sinc(y)','peaks','valleys','location','best')
P.S 
- It is recommended to run (if possible) a LPF on the data before searching for the peaks
Required Products  Image Processing Toolbox
MATLAB release MATLAB 7.0.4 (R14SP2) 



function varargout = localMaximum(x,minDist, exculdeEqualPoints)
% function varargout = localMaximum(x,minDist, exculdeEqualPoints)
%
% This function returns the indexessubscripts of local maximum in the data x.
% x can be a vector or a matrix of any dimension
%
% minDist is the minimum distance between two peaks (local maxima)
% minDist should be a vector in which each argument corresponds to it's
% relevant dimension OR a number which is the minimum distance for all
% dimensions
%
% exculdeEqualPoints - is a boolean definning either to recognize points with the same value as peaks or not
% x = [1     2     3     4     4     4     4     4     4     3     3     3     2     1];  
%  will the program return all the '4' as peaks or not -  defined by the 'exculdeEqualPoints'
% localMaximum(x,3)
% ans = 
%      4     5     6     7     8     9    11    12
%
%  localMaximum(x,3,true)
% ans =
%      4     7    12
%      
%
% Example:
% a = randn(100,30,10);
% minDist = [10 3 5];
% peaks = localMaximum(a,minDist);
% To recieve the subscript instead of the index use:
% [xIn yIn zIn] = localMaximum(a,minDist);
%
% To find local minimum call the function with minus the variable:
% valleys = localMaximum(-a,minDist);

    if nargin < 3
        exculdeEqualPoints = false;
        if nargin < 2
            minDist = size(x)/10;
        end       
    end
    
    if isempty(minDist)
        minDist = size(x)/10;
    end
    
    dimX = length ( size(x) );
    if length(minDist) ~= dimX
        % In case minimum distance isn't defined for all of x dimensions
        % I use the first value as the default for all of the dimensions
        minDist = minDist( ones(dimX,1) );
    end
    
    % validity checks
    minDist = ceil(minDist);
    minDist = max( [minDist(:)' ; ones(1,length(minDist))] );
    minDist = min( [minDist ; size(x)] );

    % ---------------------------------------------------------------------
    if exculdeEqualPoints
        % this section comes to solve the problem of a plato
        % without this code, points with the same hight will be recognized as peaks
        y = sort(x(:));
        dY = diff(y);
        % finding the minimum step in the data
        minimumDiff = min( dY(dY ~= 0) );   
         adding noise which won't affect the peaks
        x = x + rand(size(x))*minimumDiff;
    end
    % ---------------------------------------------------------------------
    
    
    se = ones(minDist);
    X = imdilate(x,se);
    f = find(x == X);
     

    if nargout
        [varargout{1:nargout}] = ind2sub( size(x), f );
    else
        varargout{1} = f;

    end


http://www.mathworks.com/matlabcentral/fileexchange/14498-local-maxima-minima
posted @ 2015-08-27 10:56  alameda  阅读(866)  评论(0编辑  收藏  举报