x-coordinate of peaks in 1D plot
4 views (last 30 days)
Show older comments
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
0 Comments
Answers (2)
Walter Roberson
on 21 Jun 2012
[sortedMaxes, maxidx] = sort(maxForThisBlob, 'descend');
maxx = omega(maxidx);
3 Comments
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));
Honglei Chen
on 21 Jun 2012
You can use findpeaks
10 Comments
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
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!