x-coordinate of peaks in 1D plot

4 views (last 30 days)
zozo
zozo on 21 Jun 2012
Hello,
I have the following 1D plot:
I want to find all the x-coordinate values of the peaks in a above plot. I have used the following lines of code and end up having only the y-coordinates(maginutude) values sorted in descending order:
out; %row vector containing the magnitude values(dependent variable)
omega %row vector containing the frequency axis of FFT (independent variable)
peaks=imregionalmax(out);
[label n]=bwlabel(peaks);
peak = imdilate(peaks,strel('disk',1));
G=regionprops(label,'PixelIdxList');
maxForThisBlob=zeros(1,n);
for blob = 1 : n
% Get pixel intensities of this particular blob.
thisBlob = out(G(blob).PixelIdxList);
% Find the max intensity out of those pixel intensities.
maxForThisBlob(blob) = max(thisBlob(:));
end
% Sort them in descending order.
sortedMaxes = sort(maxForThisBlob, 'descend');
%----plotting----%
N=size(omega,2);
figure;
plot(omega(1:N/2),out,'r') %plot one-sided spectrum
The above variable sortedMaxes contains the Magnitude (y-coordinate) values of the peaks(that is 0.02542,0.0191,......so on). How can I extract the corresponding x-coordinates at each of the peaks(that is 3.6,7.2,......so on) ?
please help

Answers (2)

Walter Roberson
Walter Roberson on 21 Jun 2012
[sortedMaxes, maxidx] = sort(maxForThisBlob, 'descend');
maxx = omega(maxidx);
  3 Comments
Walter Roberson
Walter Roberson on 21 Jun 2012
Hmmmm....
Looking again I see that maxidx would be a blob number. I do not have any feel for how blobs correspond to peak positions in your graph.
Change your G=regionprops(label,'PixelIdxList'); to
G=regionprops(label,'PixelIdxList', 'Centroid');
Then
for blob = 1 : n
thiscentroid = G(maxidx(blob)).Centroid;
blobx(blob) = thiscentroid(1);
end
Then blobx will be a vector of x coordinates of the centroids (for lack of anything better to use). The x coordinates would be pixel coordinates.
Then you have the problem of converting pixel coordinates to graph x coordinates. Doing that automatically (without user input) is a challenge. Programs can help, such as http://www.mathworks.co.uk/matlabcentral/fileexchange/7173-grabit
or http://www.mathworks.com/matlabcentral/fileexchange/4316-reverseplot
Though, you could probably automatically detect the pixel coordinates corresponding to the beginning and ending of the x axis without so much difficulty. I guess you could use interp1() to interpolate those to omega values.
XLen = Xaxisend - Xaxisstart + 1; %might need to tweaked as right end of plot does not touch axis
blobomega = interp1( Xaxisstart : Xaxisend, linspace( omega(1), omega(end), XLen ), blobx );
Or more simply,
XLen = Xaxisend - Xaxisstart + 1; %might need to tweaked as right end of plot does not touch axis
blobomega = omega(1) + (blobx - Xstart) / XLen * (omega(end) - omega(1));
zozo
zozo on 22 Jun 2012
@walter: that looks very challenging..!! Is there anyway I can combine the results from sortedMaxes (from my piece of code) and findpeaks() as illustrated below by other experts to achieve my goal?

Sign in to comment.


Honglei Chen
Honglei Chen on 21 Jun 2012
  10 Comments
Honglei Chen
Honglei Chen on 22 Jun 2012
It seems that you can just use 'SORTSTR' to 'descend' and then pick the first 3 peaks.
However, this only works if you are reasonably confident for your data set because as long as you have noise, there will be a lot of small peaks. Hence, if there are no big peaks, the small peaks will be chosen. To solve this issue, you will need some sort of adaptive minimum peak height. You will need to estimate the noise floor of your data and then use that to come up with a reasonable minimum height
zozo
zozo on 23 Jun 2012
okay..thank you @honglei and @walter

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!