How to find location of points used to calculate width of peaks using findpeaks function?

15 views (last 30 days)
In addition to the standard output of findpeaks, I would like to have the corresponding vectors for x1 and x2 (where the horizontal width line intersects the plotted line) that are used to calculate the width (x2-x1). While the location of the peak is given with locs, is there a way to get the two x values used to calculated width for each identified peak?
[pks,locs,widths,proms] = findpeaks(y,x,'Annotate','extents');
  4 Comments

Sign in to comment.

Answers (2)

Rick Rosson
Rick Rosson on 29 Feb 2016
Edited: Rick Rosson on 29 Feb 2016
Although not ideal, here is a workaround that might help:
ax = gca;
lines = ax.Children;
x = lines(1).XData';
x = x(~isnan(x));
x = transpose(reshape(x,2,[]));
HTH.
Rick

Habib Ali
Habib Ali on 21 Jun 2016
Hi, I found a work around to this, which includes manipulating the existing findpeaks.m I copied the findpeaks.m and made a new function findpeaksali.m, you can name it whatever you like. Make sure that this is in your current working directory. So if you call a function [pks,locs,widths,proms] = findpeaksali(y,x,'Annotate','extents'); You might get an error. "Undefined function 'chkinputdatatype' for input arguments of type 'double'." I solved it by copying the script 'chkinputdatatype' from , MATLAB>2015b>toolbox>signal>signal>private to the current directory. Now once I call [pks,locs,widths,proms] = findpeaksali(y,x,'Annotate','extents'); It runs perfectly.
In findpeaksali.m at around line 533, add "disp(wxPk);" and run the code. You will find the x2 and x1 you asked for in the commandline. But they are approximate values. You can work around by rounding them off to the nearest value. You can also modify the output parameters of the function and output wxPk so as to use it in your code. Hope this helps.

Community Treasure Hunt

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

Start Hunting!

Translated by