finding peaks when axis are non-uniform

I have been using “findpeaks” to locate and plot the peaks that have a prominence of at least 0.05.
findpeaks(data,position,'MinPeakProminence',0.00005,'Annotate','extents')
My current data “position” values are generally increasing but not strictly. As expected I get the following error:
“Error using findpeaks. Expected X to be strictly increasing.”
Is there another way to find the max values that have minimum peak prominence of 0.05?
I would appreciate any suggestion.
in the example below (data and position attached) findpeak is ignoring the position and it is using indeces instead. This is due to not strickly increasing / nonlinear position values (changing position vector to a linearly spaced vector is not an option). The general trend of position data is increasing.
figure(1);
plot(position,data)
findpeaks(data,position,'MinPeakProminence',0.00005,'Annotate','extents')

 Accepted Answer

I've never used a second input argument like that but if it wants it increasing, then try this:
[sortedX, sortOrder] = sort(x, 'ascend');
% Not sure if PeakSig also needs to be sorted the same way. But if it does:
%PeakSig = PeakSig(sortOrder);
[peakValues, indexesOfPeaks] = findpeaks(PeakSig, sortedX,'MinPeakProminence',0.05,'Annotate','extents')

6 Comments

Thanks for your comment. As I mentioned in previous comment both PeakSig and position data have already been sorted to be in increasing order. The collection of position data is intentionally none linear. sometimes a section needs denser sampling (closer position data), sometimes there are few sample at the same position, and rarely due to positional uncertainty x2>x1. However, the general trend is in increasing order. By resampling points of interest will not be in their actual location anymore. The shortcoming of findpeak is that it needs strictly increasing and equally spaced position data. Hence my effort in seeking help to find and alternative solution.
ok
do you have a example (data) on which we could work ?
I just updated the question with data and position as well as the plotting command.
This is what I have so far.
clc; % Clear the command window.
clear all;
close all;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
s1 = load('data.mat')
C_CoPol1 = s1.C_CoPol1;
s2 = load('position.mat')
x1 = s2.X1;
% Plot raw data vs. index.
subplot(2, 1, 1);
plot(C_CoPol1, 'b-', 'LineWidth', 2);
grid on;
title('C_CoPol1 vs. index', 'FontSize', fontSize, 'Interpreter', 'none');
xlabel('Index', 'FontSize', fontSize, 'Interpreter', 'none');
ylabel('C_CoPol1', 'FontSize', fontSize, 'Interpreter', 'none');
% Find peaks
[peakValues, indexesOfPeaks] = findpeaks(C_CoPol1,'MinPeakProminence',0.00005,'Annotate','extents')
% Plot peaks
hold on;
plot(indexesOfPeaks, peakValues, 'rv', 'MarkerSize', 14, 'LineWidth', 2);
% Plot raw data vs. x1.
subplot(2, 1, 2);
plot(x1, C_CoPol1, 'b-', 'LineWidth', 2);
grid on;
title('C_CoPol1 vs. X1', 'FontSize', fontSize, 'Interpreter', 'none');
xlabel('X1', 'FontSize', fontSize, 'Interpreter', 'none');
ylabel('C_CoPol1', 'FontSize', fontSize, 'Interpreter', 'none');
% Plot peaks
hold on;
plot(x1(indexesOfPeaks), peakValues, 'rv', 'MarkerSize', 14, 'LineWidth', 2);
There are no errors thrown, but I only put one vector into findpeaks().
What are you wanting beyond this?
Here is a plot of X1.
You can see that it does continually increase though the rate changes. However for most of the findpeaks parameters, it doesn't care about that, though some do. What's wrong with the peaks I found?
Thanks for your help. The second plot is what I was looking for ( .vs X). For some reason my plotting lines and how I used findpeak was totally ignoring the X when plotting (I was getting .vs index plot and the error with X). Would you by any chance know how to get prominence and width displayed on the plots same as the image below?
No - I've never done that before. I'd have to read the documentation just like you, but you can do that as well as I can.

Sign in to comment.

More Answers (1)

hi
my 2 cent suggestion if x is not strictly increasing, resample your data on a vector that is 100% sure strictly increasing
like this : create a new x data vector , linearly spaced, 100 values and interpolate the new y data on it
new_x_data = linspace(min(x),max(x),100); % my new x data
new_y_data = interp1(x,y,new_x_data) % my new y data

1 Comment

Thanks for your comment. Unfortunately, resampling is not the solution given that the collection of position data is intentionally none linear. sometimes a section needs denser sampling (closer position data), sometimes there are few sample at the same position, and rarely due to positional uncertainty x2>x1. However, the general trend is in increasing order. By resampling points of interest will not be in their actual location anymore.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!