How to use findpeaks in a while loop?

12 vues (au cours des 30 derniers jours)
Brandyn
Brandyn le 15 Nov 2022
Modifié(e) : Image Analyst le 15 Nov 2022
I want to create a while loop that will increase the threshold value from 0 until a certain amount of peaks is reached using findpeaks. How do I go about doing this? I’ve tried doing it myself, but I keep getting errors.

Réponses (1)

Image Analyst
Image Analyst le 15 Nov 2022
Modifié(e) : Image Analyst le 15 Nov 2022
Try this
allThresholds = linspace(0, max(signal), 1000);
for k = 1 : length(allThresholds)
thisThreshold = allThresholds(k);
fprintf('Trying a threshold of %.3f.', thisThreshold);
[peakValue, indexesOfPeakValues] = findpeaks(signal, 'threshold', thisThreshold);
numPeaks = numel(peakValue);
fprintf(' It has %d peaks.\n', numPeaks);
if numPeaks == desiredNumberOfPeaks
break;
end
end
Adapt as needed.

Community Treasure Hunt

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

Start Hunting!

Translated by