How do I pick the largest peaks in a region?

10 views (last 30 days)
Bharath Lohray on 21 Jun 2017
Edited: Bharath Lohray on 21 Jun 2017
The following MATLAB code takes in multiple peak coordinates and heights and eliminates lesser peaks that are within a certain distance threshold of the highest peak of the vicinity. Is there a better way to implement this code? Does a more efficient algorithm exist?
Code
function [ the_peaks ] = max_peaks( peak_list, vicinity_threshold )
%MAX_PEAKS(peak_list, vicinity_threshold) returns the highest peaks within
%the vicinity specified by the vicinity_threshold.
%
% peak_list is an array with rows defined by [x,y, amplitude]
%
% vicinity_threshold is the distance within which all lesser peaks are
% killed.
sorted_peaks=sortrows(peak_list,3,'desc');
the_peaks=zeros(size(sorted_peaks)); % Preallocate
peak_idx=1;
while(~isempty(sorted_peaks))
the_peaks(peak_idx,:)=sorted_peaks(1,:); %The greatest peak.
peak_idx=peak_idx+1;
D=pdist2(sorted_peaks(1:end,1:2),sorted_peaks(1,1:2)); %Distance to other peaks
np=D<vicinity_threshold; % Peaks in the vicinity
sorted_peaks(np,:)=[]; % Kill peaks in the vicinity
end
%
the_peaks(~any(the_peaks,2),:)=[]; % clear preallocated extras
end
example
test_peaks=[1,1,0.5; 5,5,0.9; 5,1,0.6; 300,300,0.2; 303,303,0.7; 1,100,0.9; 1,104,0.95; 1,250,.7; 1,200,.75];
mP=max_peaks(test_peaks,10)
scatter (test_peaks(:,1),test_peaks(:,2),'o');
hold on
scatter (mP(:,1),mP(:,2),'*');

Michelle Hirsch on 21 Jun 2017
If you are up for replacing the peak finding, and have Signal Processing Toolbox, you could consider trying findpeaks. The MinPeaksDistance option keeps just the highest peak in a neighborhood of peaks.
Bharath Lohray on 21 Jun 2017
I just tried and discovered that findpeak() does not work on matrices. It needs a vector :-(