1

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]

user2587726
  • 169
  • 2
  • 11

2 Answers2

2

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

CKT
  • 781
  • 5
  • 6
2

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.

Dev-iL
  • 23,742
  • 7
  • 57
  • 99
  • Whilst findpeaks works for this example, my actual data is much longer and this doesn't work I'm afraid (My apologies for not making this clear in my original question). Imagine the data were: A = [-9 3 2 1 0 1 2 3 7 -4 -5 -6 -5 -4 0 -9]; Then findpeaks(-A) would find the -9's, not the 0 and -6. – user2587726 Nov 15 '18 at 10:23
  • @user2587726 you can configure the "prominence" of the extrema that `findpeaks` returns. See [here](https://www.mathworks.com/help/signal/ref/findpeaks.html#buff2uu). Also, you can use `sign(diff(data))`, then look for `[-1 1]` in the resulting vector. – Dev-iL Nov 15 '18 at 16:01
  • I'm confused about why you say findpeaks wouldn't find the 0 and -6. By default, it will (actually in the example you gave it won't find the -9 because they are on the ends). When I try this, I get peaks of 0 and 6 at locations 5 and 12 (which you'd then negate). If you also want the endpoints, just add -inf to both ends of the input. – CKT Nov 15 '18 at 17:53