![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/326345/image.png)
How to number the peaks and troughs of signal ?
22 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Suvvi Kuppur Narayana Swamy
am 3 Jul. 2020
Kommentiert: Suvvi Kuppur Narayana Swamy
am 3 Jul. 2020
Here is my signal where i need to number the peaks and troughs in numbers as 1,2,3,4...
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 3 Jul. 2020
Try findpeaks() in the Signal Processing Toolbox. Invert the signal to find valleys.
numPoints = 50;
x = sort(rand(1, numPoints));
y = rand(1, numPoints);
plot(x, y, 'k-', 'LineWidth', 2);
grid on;
[peakValues, indexesOfPeaks] = findpeaks(y);
hold on;
peakx = x(indexesOfPeaks);
peaky = y(indexesOfPeaks);
plot(peakx, peaky, 'r*', 'LineWidth', 2, 'MarkerSize', 10);
% Find valleys on inverted signal
[valleyValues, indexesOfValleys] = findpeaks(-y);
valleyx = x(indexesOfValleys);
valleyy = y(indexesOfValleys);
hold on;
plot(valleyx, valleyy, 'b*', 'LineWidth', 2, 'MarkerSize', 10);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/326345/image.png)
Be careful with the first and last point of your data since it doesn't find those. You should look at the adjacent index (2 or end-1) if you want to classify them as either a peak or valley.
3 Kommentare
Image Analyst
am 3 Jul. 2020
Do you know about the text() function?
numPoints = 50;
x = sort(rand(1, numPoints));
y = rand(1, numPoints);
plot(x, y, 'k-', 'LineWidth', 2);
grid on;
[peakValues, indexesOfPeaks] = findpeaks(y);
hold on;
peakx = x(indexesOfPeaks);
peaky = y(indexesOfPeaks);
plot(peakx, peaky, 'r*', 'LineWidth', 2, 'MarkerSize', 10);
% Find valleys on inverted signal
[valleyValues, indexesOfValleys] = findpeaks(-y);
valleyx = x(indexesOfValleys);
valleyy = y(indexesOfValleys);
hold on;
plot(valleyx, valleyy, 'b*', 'LineWidth', 2, 'MarkerSize', 10);
% Plot peak labels
for k = 1 : length(peakx)
textLabel = sprintf(' %d', k);
text(peakx(k), peaky(k), textLabel, 'HorizontalAlignment', 'left', 'VerticalAlignment', 'bottom', 'Color', 'r', 'FontWeight', 'bold');
end
% Plot valley labels
for k = 1 : length(valleyx)
textLabel = sprintf(' %d', k);
text(valleyx(k), valleyy(k), textLabel, 'HorizontalAlignment', 'left', 'VerticalAlignment', 'top', 'Color', 'b', 'FontWeight', 'bold');
end
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/326354/image.png)
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Spectral Estimation finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!