Extract peaks and troughs from a curve
93 views (last 30 days)
Show older comments
Mustafa Sobhy
on 29 Jul 2019
Commented: Star Strider
on 29 Jul 2019
How to extract only peaks and troughs from a curve
![2.jpg](https://www.mathworks.com/matlabcentral/answers/uploaded_files/231673/2.jpeg)
0 Comments
Accepted Answer
Star Strider
on 29 Jul 2019
If you want to identify the individual peaks and troughs, use islocalmax and islocalmin, or findpeaks on the positive and negative versions of your signal respectively:
[pks, pklocs] = findpeaks(s);
[troughs,trlocs] = findpeaks(-s);
where ‘s’ is youir signal vector.
2 Comments
Star Strider
on 29 Jul 2019
As always, my pleasure!
[pks, pklocs] = findpeaks(s); % Original Peaks
[troughs,trlocs] = findpeaks(-s); % Original Troughs
pktr = [pklocs(:), pks(:); trlocs(:) -troughs(:)]; % Location, Value Matrix
pktrs = sortrows(pktr); % Location, Value Matrix Sorted By Location
Since with findpeaks the troughs are detected using the negative of the original vector, their amplitudes in the ‘pktr’ matrix are negated to correct for that, presenting the actual values.
The sortrows function sorts by default on the first column. If you use a different configuration for ‘pktr’ you need to specify the column for the locations so that sortrows know which one to sort on.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!