# How to cut out specific segment from findpeaks function?

12 views (last 30 days)
Matlaber on 7 Feb 2019
Commented: Giacomo Echevers on 18 Jan 2022
Hi All,
I am trying to figure out how to cut out the specific segment of the signal where I draw the border using findpeaks function.
I am using example of findpeaks link below:
x = linspace(0,1,1000);
Pos = [1 2 3 5 7 8]/10;
Hgt = [4 4 2 2 2 3];
Wdt = [3 8 4 3 4 6]/100;
for n = 1:length(Pos)
Gauss(n,:) = Hgt(n)*exp(-((x - Pos(n))/Wdt(n)).^2);
end
PeakSig = sum(Gauss);
plot(x,Gauss,'--',x,PeakSig)
grid
Then, using findpeaks function,
findpeaks(PeakSig,x,'Annotate','extents','WidthReference','halfheight')
title('Signal Peak Widths')
I want to cut the segment of the border into 3 segment.
The method I am thinking is find the X value corresponding the Y value, which is not an effective way to do it.

Star Strider on 8 Feb 2019
You can get the ‘Borders’ x-coordinates using:
(Previous Code)
findpeaks(PeakSig,x,'Annotate','extents','WidthReference','halfheight')
title('Signal Peak Widths')
Ax = gca;
Kids = Ax.Children;
Borders = Kids(1:2);
Line = Borders.XData;
You have to search the ‘Axes.Children’ result manually to find the ‘(Border)’ elements. There appears to be no way to get them automatically, or even to specifically search for a ‘Line (Border)’ or ‘Border’ entry in the ‘Kids’ variable. (There may be a way, but it would take more patience than I have tonight to find it. I’ve tried every cell and structure addressing approach I can think of or can find in the documentation, including using strcmp and related functions, and couldn’t get a good result.) It would be very nice if this was more straightforward.
Star Strider on 14 Feb 2019
I am not sure can I plot only specific range and then find the pks in that specific range?
You can. Starting with:
[pks,locs] = findpeaks(-avSpots,year)
(or something similar) then calculate the indices as as:
idx = find((year >= yr1) & (year <= yr2));
where ‘yr1’ is the beginning year and ‘yr2’ is the end year, then analyse your data as:
spots_sub = avSpots(idx);
year_sub = year(idx);
That should work (I did not test it).

João on 7 Feb 2019
Try this,
[pks,locs] = findpeaks(PeakSig,x,'Annotate','extents','WidthReference','halfheight')
The variable locs have the indexes of the peaks found. Use them like this to remove a specific peak (your case the third):
PeakSig(locs(3)) = [];
Matlaber on 9 Feb 2019
I managed to find the peak point, which is below:
pks =
4.8799 4.0040 3.0328 1.9994 2.2135 3.0039
locs =
0.1031 0.2002 0.2833 0.4995 0.7057 0.7998
Thus,
peakToFind = 1; % the peak you want to delete (in this case the first one)
% where you get the postion in x corresponding to locs
idx_x = find(x==locs(peakToFind));
PeakSig(idx_x)
ans =
4.8765
which is the first peak answer.
However, how can I plot of first segment of the peak?

Image Analyst on 9 Feb 2019
The borders appear to be available in findpeaks() at line 558 (in R2018b version):
% get the x-coordinates of the half-height width borders of each peak
[wxPk,iLBh,iRBh] = getPeakWidth(yFinite,x,iPk,bPk,iLB,iRB,refW);
See it with edit, then search for border
>> edit findpeaks.m
Make a copy of findpeaks.m and call it findpeaksandborders() in some utilities folder on your path with all your other m-files. BE SURE NOT TO ALTER THE ORIGINAL ONE!!!
Then have findpeaksandborders() return the borders (iLB and iRB) in the list of output arguments.
Matlaber on 14 Feb 2019
Edited: Matlaber on 14 Feb 2019
I used the above option. I still unable to figure out the error:
Error using findpeaksandborders
Too many output arguments.
Error in Find_All_Peak (line 315)
[pks,locs,Wpk,Ppk, iLB, iRB] = findpeaksandborders(PeakSig,x)
However, it cannot find the first border.
I think it is due to invert of peak.
year = sunspot(:,1);
avSpots = sunspot(:,2);
findpeaks(avSpots,year,'Annotate','extents','WidthReference','halfheight')
title('Signal Peak Widths')
hold on
[pks,locs] = findpeaks(-avSpots,year)
plot([locs; locs], [zeros(size(pks)); -pks], '+r', 'MarkerSize',10)
hold off

Giacomo Echevers on 18 Jan 2022
Well, I kind of needed exactly this feature so I ventured and fought with the findpeaks code, luckily I found what I needed. I attached the modified version of the findpeaks function (now called Findpeaks.m).
Note: It seems that because MATLAB recognized that this is a custom function, the plotting features are not working, but that's no issue, because you can simply use the orginial function and that'll do, this mod is just to extract the x position for the borders.
I'm going to include here a small example using the previous presented case.
x = linspace(0,1,1000);
Pos = [1 2 3 5 7 8]/10;
Hgt = [4 4 2 2 2 3];
Wdt = [3 8 4 3 4 6]/100;
for n = 1:length(Pos)
Gauss(n,:) = Hgt(n)*exp(-((x - Pos(n))/Wdt(n)).^2);
end
PeakSig = sum(Gauss);
findpeaks(PeakSig,x,'Annotate','extents','WidthReference','halfheight');
Now, let's use the modified version of the findpeaks function to get the borders x values. Please note the use of the pair parameters 'BorderExtraction' and logical 1. Basically, logical 1 to get the x values for borders in the loc output variable position and logical 0 to tell it to behave exactly the same as the original findpeaks function.
[~,X] = Findpeaks(PeakSig,x,'Annotate','extents','WidthReference','halfheight','BorderExtraction',1)
X = 6×2
0 0.1451 0.1451 0.2663 0.2663 0.4184 0.4184 0.5866 0.5866 0.7397 0.7397 1.0000
And now let's use this to get the second peak just for fun.
plot(PeakSig(x>=X(2) & x<=X(3)))
Hope this helps
A94 out.
##### 2 CommentsShow 1 older commentHide 1 older comment
Giacomo Echevers on 18 Jan 2022
Glad to hear that! Just noticed those borders, literally yesterday and got quite triggered that it didn't have a native way to extract those x coordinates.