Existing library to find peaks (local maxima) in data series?

I'm trying to find peaks in a series of data akin to how scipy.signal.find_peaks works (ideally by peak "prominence"): give it an array and it returns an array of indices into the data indicating where the peaks are.

Is there existing library code for arduino for this, or do I need to code my own?

(Edit to add: I'm not interested in real-time peak finding, this is a static data set the Arduino has already gathered, so it has plenty of time to go over the data to find peaks. I'm finding some stream-based peak libraries, but that's not what I'm seeking.)

Thanks!

Maybe qsort function can do what do you want.

Thank you for the response -- If I understand correctly what it's doing, it won't work; I need real peak detection (with indices and ideally with prominence values), not just finding maximum values in a set.

There is no Arduino library for peak detection, but it is a very important topic, so there is plenty of discussion and code "out there" for you to study. Search phrase "peak detection algorithm".

I don't know exactly the meaning of "prominence" but here another finding.

It will sort and give the index for each value.

#include <stdio.h>      
#include <stdlib.h>     

int A[] = {2,3,5,4,1};
#define N sizeof(A)/sizeof(A[0])

struct PlayerScore {
    int playerId;
    int score;
};
int compare (const void * a, const void * b)
{
    return ( (*(struct PlayerScore*)b).score - (*(struct PlayerScore*)a).score );
}

int main ()
{
  int n;
  struct PlayerScore ps[N];
  for(n=0;n<N; n++) {
      ps[n].playerId = n;
      ps[n].score = A[n];
  }
  qsort (ps, 5, sizeof(struct PlayerScore), compare);
  for (n=0; n<N; n++)
      printf ("%d (%d) ",ps[n].score, ps[n].playerId);
  return 0;
}

The output:

5 (2) 4 (3) 3 (1) 2 (0) 1 (4)

First, define a "real peak". A local maxima can be described as a datum where both the previous and following values are lesser.

It sounds obvious, but it's easier to code for than "local maxima".

1 Like

Thanks @jremington -- I'll get to work on my own version.

To be more clear -- I want peaks as found by the scipy library. That is, an array containing indices that point to peaks, with a configurable number of peaks found, where those peaks are ranked by topographic prominence and returned in descending order of prominence. Sounds like it doesn't exist in library form... too bad, I was hoping to save the time.

The peak finding code is open source, so just port it to C/C++.

Yeah, thanks -- I had thought to do that. I wonder if it might be easier to just DIY it rather than try to parse their code, given my simpler/more restricted needs, but I'll find out soon. :slight_smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.