Is there a way to get the local maxima/minima in a series? I’ve been thinking of a way to use the difference function to find out when the rate of change changes from positive to negative, something like this:

|> duplicate(column: “_value”, as: “n1”)

|> difference(columns: [“n1”])

|> map(fn: (r)=>({r with n2: if r[“n1”] > 0 then 1 else 0}))

|> difference(columns: [“n2”])

but I’m having some trouble because it flags the point after the minima and maxima, and it seems a bit too complex, anyway.

Hello @simon38,

You could try using the derivative function?

https://docs.influxdata.com/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/derivative/

And filtering for when the derivative close 0? Or are you worried about inflection points?

I discovered this is a complex question, because my data isn’t smooth, but rather has many spikes, and the question then is what is considered a peak? So finding peaks isn’t so important to me right now, but it might important to other people.

I haven’t figured out how to use the derivative function in a way that it will be zero at a peak. It seems it only shows me the rate of increase up to the peak.

I did something a bit complicated. I took the difference between consecutive values, set another column to -1 if the value was negative, 1 if it was positive, zero otherwise, sorted in descending order by time, summed the new column, and sorted it back in ascending order, so now every row with a +2 is a peak and -2 is a valley. This is the sequence of functions after the data is retrieved:

|> duplicate(column: “_value”, as: “diff”)

|> difference(columns: [“diff”])

|> map(fn: (r) => ({r with peak: if r.diff < 0 then -1 else if r.diff == 0 then 0 else 1}))

|> sort(columns: ["_time"], desc: true)

|> difference(columns: [“peak”])

|> sort(columns: ["_time"])

|> yield()

I don’t think most query languages can do this in one function, but I see that there is such a function in Matlab:

Find local maxima - MATLAB islocalmax (mathworks.com)

Right now I’m just exploring the capabilities of Flux, which is why I’m asking these questions.

On this issue, no, thanks. I am

curious how other people would formulate the solution, but I don’t know if this thread will generate much dialog. I will get back to my other questions soon.