Finding local minimums/maximums for a set of data

451 views (last 30 days)
Hi, I have a set of data which oscillates between minimums and maximum values. The min and max values change slightly over time. I want to see the trend of changing of min and max values over time. In order to do that, it seems that I need to extract the local min and local maximums.
Is there any function to extract the local minimums and maximums of the following graph?
Steev Mathew
Steev Mathew on 4 Mar 2021
Thanks! This helped me a lot. I was looking for a way to filter out noise from my data.

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 22 Jul 2012
Edited: Star Strider on 22 Jul 2012
Another option is ‘findpeaks’ in the Signal Processing Toolbox. It will give you the maximum (and indirectly the minimum) values and their index locations. If ‘Data’ is the vector that produced the plot, to find the maxima and minima:
[Maxima,MaxIdx] = findpeaks(Data);
DataInv = 1.01*max(Data) - Data;
[Minima,MinIdx] = findpeaks(DataInv);
The true minima will then be:
Minima = Data(MinIdx);
The index values also allow you to determine the times the maxima and minima occurred.
Star Strider
Star Strider on 27 Feb 2018
For some reason, it was important that the inverted waveform be greater than zero. (That was years ago, so I don’t remember the details.)
That’s likely not necessary for every signal. If you want the minima, just negate the original signal and use the indices findpeaks returns in the second output to get the values of the original signal.
I you have R2017b, the islocalmin function is also an option to get the minima without inverting the signal.

Sign in to comment.

More Answers (5)

Steven Lord
Steven Lord on 4 Mar 2021
For more recent releases take a look at the islocalmin, islocalmax, and (perhaps) detrend functions.

Image Analyst
Image Analyst on 22 Jul 2012
Edited: Image Analyst on 22 Jul 2012
If you have the Image Processing Toolbox, you can use imregionmax() and imregionalmin(). Do you have that toolbox? If you do that would be the simplest because it's just simply one line of code to find either the maxs or the mins.
You can also do it by seeing where the morphological max or min (performed by imdilate() and imerode() respectively) equals the original array. But again, that requires the Image Processing Toolbox.

Si14 on 26 Jul 2012
Thank you guys for your responses. I am using the following and it works nice:
[Maxima,MaxIdx] = findpeaks(Data);
DataInv = 1.01*max(Data) - Data;
[Minima,MinIdx] = findpeaks(DataInv);
Minima = Data(MinIdx);

Randy82 on 13 Aug 2014
I have one question: Why do i have to multiply max(Data) with a factor 1.01?
  1 Comment
Star Strider
Star Strider on 13 Aug 2014
To keep all the values of the inverted vector > 0.

Sign in to comment.

Prithisha K
Prithisha K on 8 Sep 2021
Set the prominence window value to 25. Then increase the min.prominence value untill there are exactly 9 minima found.

Community Treasure Hunt

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

Start Hunting!