How do I pick the largest peaks in a region?

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),'*');

Réponses (1)

Michelle Hirsch
Michelle Hirsch le 21 Juin 2017

1 vote

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.

1 commentaire

Bharath Lohray
Bharath Lohray le 21 Juin 2017
Modifié(e) : Bharath Lohray le 21 Juin 2017
I just tried and discovered that findpeak() does not work on matrices. It needs a vector :-(

Connectez-vous pour commenter.

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by