How do I select a specific peak in a repeating pattern?

조회 수: 3 (최근 30일)
Spencer Smith
Spencer Smith 2017년 9월 5일
댓글: Image Analyst 2017년 9월 5일
y is my data and I am trying to find the first peak of each repeating part, but in the third part, the last peak is greater than the first peak so the last peak is the peak that gets selected. This problem only occurs when the last peak is greater than the first peak. Nothing I have tried has been working.
pks = findpeaks(y,'MinPeakHeight', 200,'MinPeakDistance',4000)

채택된 답변

Image Analyst
Image Analyst 2017년 9월 5일
편집: Image Analyst 2017년 9월 5일
Try this:
clc; % Clear command window.
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = 24;
s = load('dataa.mat')
z = s.z;
plot(z, 'b-', 'LineWidth', 2);
grid on;
hold on;
title('z vs. Index', 'FontSize', fontSize);
xlabel('Index', 'FontSize', fontSize);
ylabel('z', 'FontSize', fontSize);
% Label different squarish regions.
[labeledSignal, numRegions] = bwlabel(z > 200);
% Find indexes of each group
props = regionprops(labeledSignal, 'PixelIdxList');
% Examine each group to find out where the signal decreases
for k = 1 : numRegions
% Get indexes of only this group.
theseIndexes = props(k).PixelIdxList;
% Plot a red line where the group starts.
line([theseIndexes(1), theseIndexes(1)], ylim, 'Color', 'r', 'LineWidth', 2);
% Get differences. If it's going up, differences will be positive.
% When it turns around, the difference values will be zero or negative.
diffs = diff(z(theseIndexes));
% Find out where diffs <= 0
peakIndex(k) = find(diffs <= 0, 1, 'first') + theseIndexes(1) - 1;
% Tell user what the index is.
fprintf('The peak for group #%d is at index %d.\n', k, peakIndex(k));
% Plot a magenta line where the first peak in the group is.
line([peakIndex(k), peakIndex(k)], ylim, 'Color', 'm', 'LineWidth', 2);
end
%------------------------------------------------------------------------------
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
Each group starts at the red line, and the very first peak has its highest point at the magenta line.
  댓글 수: 2
Spencer Smith
Spencer Smith 2017년 9월 5일
If you saw my last comment, ignore it please. It was a very simple thing to do that I just blanked on for a second. I figured out what I needed to do. Thank you so much for this; it's perfect!
Image Analyst
Image Analyst 2017년 9월 5일
No problem (I didn't). Glad it worked for you. Thanks for Accepting the answer. For the last peak, which is really tiny or even a shoulder for this set of sample data, you can change the code to only find differences more than a certain amount if you want, so that it would then move on the next higher peak.

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

추가 답변 (1개)

Image Analyst
Image Analyst 2017년 9월 5일
편집: Image Analyst 2017년 9월 5일
OK, not too hard (a variety of ways to do it probably), but you forgot to attach your data, so I'll wait for that, as will probably most people. I'd probably threshold and find each group, then scan within the group to find out when the data stops increasing and starts decreasing. You can use diff() for that.
  댓글 수: 1
Spencer Smith
Spencer Smith 2017년 9월 5일
편집: Spencer Smith 2017년 9월 5일
This is the data associated with the image. This is only part of my whole data as my whole data is over 1000 of these repeating parts, but I was hoping i could extrapolate a code from this that would apply to the rest of the very similar data.

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

카테고리

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