Finding 2d impulse peaks in MATLAB
-
27-09-2019 - |
Question
What is the best method for finding impulse peaks (dirac delta) in a 2d matrix.
More specifically, I would like to find the harmonic frequencies of a given image and so I need to find impulse peaks in the image absolute value DFT.
I thought of using findpeaks but there's no 2d version. I also saw earlier posts regarding finding ordinary peaks using imdilate and/or imextendedmax but those find all the peaks in a 2d matrix whereas I am only interested in impulse peaks. I am sure DSP people have a common recipe for this...
Please Help,
Thanks
Solution
What you want to do is find peaks with high contrast. Thus, you need a way to identify local maxima, plus a way to measure the difference between the peak and the surrounding values. Thresholding on this difference will identify the impulse peaks for you.
Assuming your input signal is called signal
%# dilate to find, for every pixel, the maximum of its neighbors
dilationMask = ones(3);
dilationMask(5) = 0;
dilSignal = imdilate(signal, dilationMask);
%# find all peaks
%# peaks = signal > dilSignal;
%# find large peaks peaks by thresholding, i.e. you accept a peak only
%# if it's more than 'threshold' higher than its neighbors
peaks = (signal - dilSignal) > threshold;
peaks
is a logical array with 1's wherever there is a good peak. You can use it to read peak heights from signal with signal(peaks)
, and to find coordinates using find(peaks)
.
OTHER TIPS
This paper I wrote contains Matlab source code for fast local peak detection in 2D. It works similar to imregionalmax()
in Mathworks Image Processing Toolbox but allows you to specify a local neighborhood radius: bigger radius -> sparser peaks.
Since you expect sparse impulses, the nonmaxsupp_scanline()
function may be suitable for you.
The findpeaks
algorithm is pretty trivial; if an element is bigger than both its neighbours, then it is a peak. Writing a 2D version of this should be pretty simple.