Findpeak from the graph in specific range

14 次查看(过去 30 天)
Hello Everyone,
I am trying to use the findpeaks function in my code but in a specific range.In the code below you can see I have a plot of x2 and y2.From this plot, I want to find the peak at x2 values between 90 and 120.BUt my code doesnt work for the peaks and shows a blank graph.Any help would be appreciated.
Data = xlsread('test.xlsx');
%% Step 2 data
x2 =Data(655:8466,6); % Sample temprature
y2 =Data(655:8466,3); % Umsubstracted temprature
figure
plot(x2,y2);
set(gca,'ydir', 'reverse')
title('Step 2 Data')
hold on
[pks, locs]= findpeaks((x2(90:120)),'Npeaks',1)
title('Peak for step 2')

回答(1 个)

Allen
Allen 2021-5-26
编辑:Allen 2021-5-27
[pks, locs]= findpeaks((x2(90:120)),'Npeaks',1)
In your above line of code, you are indexing x2 from elements 90 through 120 and not from between the values of 90 and 120. To find the indices of x2 with values in your preferred range use the following instead.
% Between 90 and 120, but not equal to 90 or 120.
idx = x2>90 & x2<120;
% If you want to include values equal to 90 and 120, then use
idx = x2>=90 & x2<=120;
Using the correct index, use the following with findpeaks.
[pks, locs] = findpeaks((x2(idx)),'Npeaks',1);
  2 个评论
Harsimranjot Grewal
Thanks for your answer but it didnt work and the command line says ' Array indices must be positive integers or logical values." I just replaced the code with your answer.So, any other recommendations.
Allen
Allen 2021-5-27
@Harsimranjot Grewal looks like repeated the applying the index to x2 more than once. I have made corrections to my example.
Changed idx = x2(x2>90 & x2<120); to idx = x2>90 & x2<120; and idx = x2(x2>=90 & x2<=120); to idx = x2>=90 & x2<=120; which work better.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by