Why doesn't findpeaks find the peaks?
6 views (last 30 days)
Show older comments
Colby Nicholson
on 25 May 2018
Answered: Star Strider
on 25 May 2018
I am trying to characterize peaks in non continuous and non normal distributed data using a spline interpolation on direction data which I then want to pass to findpeaks. Findpeaks doesn't give me the peaks it gives locations between the peaks for some reason.
I have directional data 1-180 degrees that I have used histcount with nbins to extract the count per bin
countNbin = histcounts(id1.dir,nbins);
y = countNbin
So I have
sample code starts here:
nbins = 45
y =[6 4 4 7 2 8 5 7 2 5 3 4 6 4 6 10 4 8 5 5 5 3 5 7 3 3 6 4 2 0 1 0 0 1 0 1 1 2 1 1 0 3 0 3 5]
%This is my data set corresponding to 45 bins in 180 degrees
edges = linspace(1,180,nbins);
%I want to try and group like direction together so
plot(edges,y) % produces a plot with too many spikes and peaks
% using a pchip to interpolate data and find peaks at edges
res = 8; %resolution to interpolate
xq1= 1:res:180; %where to query
len = length(xq1)
p = pchip(edges,y,xq1);
findpeaks(p) These are the places I want to have lines on
figure
plot(xq1,p) % produces a better fit of my data
% but I need the endpoints included
%therefore locs = locs-1 after padding p
p = [0, p, 0];
xq1 = [0, xq1, 181]; %locs become locs-1
plot(xq1,p)
[pks,locs] = findpeaks(p,'SortStr','descend','MinPeakDistance',2,'MinPeakProminence',2)
locs = locs-1
locs = locs*(180/len)
line([locs(1), locs(1)], ylim, 'LineWidth', 2)
line([locs(2), locs(2)], ylim, 'LineWidth', 2)
line([locs(3), locs(3)], ylim, 'LineWidth', 2)
line([locs(4), locs(4)], ylim, 'LineWidth', 2)
Why doesn't this plot lines

where the peaks are. It plots one at 180, that is correct but why is the line not at the center of the peak elsewhere?
Thanks for the help.
1 Comment
jonas
on 25 May 2018
I'm a bit confused, why don't you just write
[pks,locs] = findpeaks(p)
plot(xq1,p);hold on
plot(xq1(locs),p(locs),'x')
after your padding p
Accepted Answer
Star Strider
on 25 May 2018
I can’t figure out what you’re doing.
A straightforward ‘fix’ is to give the independent variable vector ‘xq1’ to findpeaks, and let it return the correct references.
Change the last lines of your code to:
[pks,locs] = findpeaks(p,xq1,'SortStr','descend','MinPeakDistance',2,'MinPeakProminence',2)
line([locs(1), locs(1)], ylim, 'LineWidth', 2)
line([locs(2), locs(2)], ylim, 'LineWidth', 2)
line([locs(3), locs(3)], ylim, 'LineWidth', 2)
line([locs(4), locs(4)], ylim, 'LineWidth', 2)
That appears to me to plot correctly.
0 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!