Finding local minima about either side of a maxima in a matrix in matlab

459 Views Asked by At

For some "big data" processing. When plotted my data is sign wave like but with random peaks. (So imagine plotting the value of each matrix position against it's position)

I know how to find the peaks, but I need a way of then finding the value of local minima either side of the peaks and the position in the matrix. For example, if the data were:

3 2 1 0 1 2 3 7 -4 -5 -6 -5 -4 0

The function I need would return something like: min,loc = [0, -6; 4, 11]

2

There are 2 best solutions below

0
On

If you have access to R2017b or later, check out the islocalmax and islocalmin functions.

3
On

MATLAB R2007a and newer have a function called findpeaks (which requires the Signal Processing Toolbox). The syntax that you're looking for is

[pks,locs] = findpeaks(data)

Specifically,

>> [pks,locs] = findpeaks(-[3 2 1 0 1 2 3 7 -4 -5 -6 -5 -4 0]) % note it's "-[data]"

pks =
     0     6
locs =
     4    11

The minus is because we want the "valleys" and not "peaks", so make sure you don't forget to negate pks afterwards.