Finding peak with two nulls around in a 1d plot

1 次查看(过去 30 天)
How can we find the presence of a peak (global maxima) with two nulls (or crossing lower threshold) on each side of the peak in a 1d plot.
And is there a way to find the width of the peak at a particular threshold level

回答(1 个)

Sai Pavan
Sai Pavan 2024-2-16
Hello Vinay,
I see that you want to find the presence of a peak with two nulls on each side of the peak in a 1d plot and find the width of the peak at a particular threshold level.
Please refer to the below workflow to find the presence of a peak with two nulls (or crossing a lower threshold) on each side of the peak in a 1D plot, and to find the width of the peak at a particular threshold level:
  • Identify the peaks using the 'findpeaks' function.
  • For each detected peak, find the points where the signal crosses the lower threshold level on both sides of the peak.
  • Measure the width of the peak at the threshold level by calculating the distance between the two crossing points.
Please refer to the below code that illustrates the above mentioned workflow:
% Sample data
x = linspace(0, 10, 1000);
y = exp(-(x-5).^2) + 0.1*randn(size(x)); % Gaussian peak with some noise
plot(x, y);
hold on
threshold = 0.1; % Set to consider as null. Adjust the threshold variable to the level at which you want to measure the peak widths.
[peakValues, peakLocs] = findpeaks(y, x, 'MinPeakHeight', threshold); % Find the peaks
plot(peakLocs, peakValues, 'r*', 'MarkerSize', 10);
% For each peak, find the width at the threshold level
for i = 1:length(peakLocs)
peakX = peakLocs(i);
peakY = peakValues(i);
% Find where the signal crosses the threshold on the left side of the peak
leftSide = y(1:find(x==peakX));
leftCross = find(leftSide < threshold, 1, 'last');
if isempty(leftCross)
leftCross = 1; % If it never crosses, use the start of the data
end
% Find where the signal crosses the threshold on the right side of the peak
rightSide = y(find(x==peakX):end);
rightCross = find(rightSide < threshold, 1, 'first');
if isempty(rightCross)
rightCross = length(rightSide); % If it never crosses, use the end of the data
else
rightCross = rightCross + find(x==peakX) - 1; % Adjust index
end
% Calculate the width at the threshold
peakWidth = x(rightCross) - x(leftCross);
% Plot the width
plot(x([leftCross rightCross]), [threshold threshold], 'k-', 'LineWidth', 2);
% Display the width
disp(['Peak ' num2str(i) ' width at threshold ' num2str(threshold) ': ' num2str(peakWidth)]);
end
hold off
Please refer to the below documentation to learn more about 'findpeaks' function: https://www.mathworks.com/help/signal/ref/findpeaks.html
Hope it helps!

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by