Specifically find the first substantial peak in a graph.

5 views (last 30 days)
  1 Comment
Anthony on 5 Jun 2023
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.

Sign in to comment.

Answers (2)

Diwakar Diwakar
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
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');
legend('Signal', 'Threshold');
ylabel('Signal Value');
title('Signal with First Substantial Peak');
hold off;

Star Strider
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)
pks = 252.8592
locs = 18
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.
Image Analyst
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".

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!