MATLAB: Finding local minimums/maximums for a set of data

local maxlocal mintrend

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?
Thanks.

Best Answer

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.