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.
Specifically find the first substantial peak in a graph.
25 views (last 30 days)
Show older comments
Answers (2)
Diwakar Diwakar
on 5 Jun 2023
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;
0 Comments
Star Strider
on 5 Jun 2023
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)
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 Comments
Star Strider
on 5 Jun 2023
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)
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
on 6 Jun 2023
@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".
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!