Specifically find the first substantial peak in a graph.

조회 수: 22 (최근 30일)
Anthony
Anthony 2023년 6월 5일
댓글: Image Analyst 2023년 6월 6일
  댓글 수: 1
Anthony
Anthony 2023년 6월 5일
I want to find the value of the first occuring peak in this graph. How can I write a code that can specifcally do that for this graph or any graph I input into the code.

댓글을 달려면 로그인하십시오.

답변 (2개)

Diwakar Diwakar
Diwakar Diwakar 2023년 6월 5일
Try this example. may be this code will help you.
% Example usage
signal = [0, 0, 0, 1, 2, 3, 2, 1, 0, 0, 0, 0, 0, 0, 1, 2, 3, 2, 1, 0, 0, 0];
threshold = 0.5;
% Find the first peak in the signal above the threshold
localMaxima = islocalmax(signal);
peakIndex = find(localMaxima & signal > threshold, 1);
% Plot the signal
figure;
plot(signal, 'b-', 'LineWidth', 2);
hold on;
% Plot the threshold line
plot([1, numel(signal)], [threshold, threshold], 'r--', 'LineWidth', 1);
% Plot the first peak, if found
if ~isempty(peakIndex)
plot(peakIndex, signal(peakIndex), 'go', 'MarkerSize', 10, 'MarkerFaceColor', 'g');
legend('Signal', 'Threshold', 'First Peak');
else
legend('Signal', 'Threshold');
end
xlabel('Index');
ylabel('Signal Value');
title('Signal with First Substantial Peak');
hold off;

Star Strider
Star Strider 2023년 6월 5일
Perhaps something like this —
x = linspace(1.3, 3.3)*1E+4;
yfcn = @(x) x.*exp(-0.00025*x);
y = yfcn(x-x(1))/6 + [zeros(1,13) randn(1,87)*10];
[pks,locs] = findpeaks(y, 'NPeaks',1)
pks = 252.8592
locs = 18
figure
plot(x, y)
hold on
plot(x(locs), pks, 'rs')
hold off
legend('Data','First Peak','Location','best')
That should be robust to all your data sets.
.
  댓글 수: 3
Star Strider
Star Strider 2023년 6월 5일
It’s not stupid at all. The ‘yfcn’ anonymous function is my attempt to approximate the data you displayed. The relation essentially approximates, it, however I wanted to be as accurate as I could, so I wrote the function, allowing me to easily shift it to accommodate your independent variable values.
Using ‘NPeaks’,1 is the key here since it returns the first peak.
I noticed that you mentioned ‘substantial’ in the title. I would let 'Prominence' define ‘substantial’. You can return the prominence values for all the identified peaks (see the findpeaks documentation for that informaiton), and then choose the peak with the highest prominence, using the max functon with two outputs, the second output returning the index that you could then use to return the corresponding ‘locs’ value.
That would go something like this —
x = linspace(1.3, 3.3)*1E+4;
yfcn = @(x) x.*exp(-0.00025*x);
y = yfcn(x-x(1))/6 + [zeros(1,13) randn(1,87)*10];
[pks,locs,~,prm] = findpeaks(y);
[maxprom,idx] = max(prm)
maxprom = 262.3909
idx = 5
figure
plot(x, y)
hold on
plot(x(locs(idx)), pks(idx), 'rs')
hold off
legend('Data','Most Prominant Peak','Location','best')
.
To return only the first peak, us my original code.
Image Analyst
Image Analyst 2023년 6월 6일
@Anthony what is your definition of the first peak, out of all those peaks in the image. Is it the very first one, like islocalmax would return in the first element it returns? Or is it the peak of the noisy signal, as it you had denoised the signal? Or is it the overall max of the entire signal, like the red square in Star's last plot? Do you even want to denoise your signal, or do you want to use the original, noisy signal?
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
and please indicate which (x,y) point on the curve you want to find as the "first peak".

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by