How to identify local maxima (peaks) in a time signal?

Hi folks,

making my first steps in Julia with the basic aim to finally get rid of Matlab. I am searching for a package which helps me identiying local extremes, i.e. peak values, from a monitored time signal (decaying vibration). I found a package named Images.jl which offers that functionality. However, I am not able to make it run. Let’s assume the signal amplitudes are stored in the array ‘signal’. Then, the expression ‘locmax = findlocalmaxima(signal)’ leads to strange results made up of Cartesian Indexes…

Can someone help me how to use this package correctly ? Or can you recommend any other package which identifies the local extremes ?

Regards

Ceysa

If you just want local maxima in 1d, you could use something like

function findlocalmaxima(signal::Vector)
   inds = Int[]
   if length(signal)>1
       if signal[1]>signal[2]
           push!(inds,1)
       end
       for i=2:length(signal)-1
           if signal[i-1]<signal[i]>signal[i+1]
               push!(inds,i)
           end
       end
       if signal[end]>signal[end-1]
           push!(inds,length(signal))
       end
   end
   inds
 end
1 Like

CartesianIndexs can be used for indexing into arrays, but they’re not a subtype of Number so you need to convert them to normal Ints to pass on to many other functions. Here’s an example that shows plotting the local maxima using findlocalmaxima:

using PlotlyJS
using Images

sig = cos.(linspace(0, 10pi, 2000))
maxs_cart = findlocalmaxima(sig)
maxs = [idx[1] for idx in maxs_cart]
plot(sig,
     Layout(shapes=[vline(max) for max in maxs]))
2 Likes

It looks like the documentation says findlocalmaxima returns a Vector{Tuple} but it actually returns a Array{CartesianIndex{1},1}.

Correct - PR submitted

(Vector{T} is a synonym for Array{T, 1}, so it’s just the TupleCartesianIndex part that’s wrong)

Thank you guys for the instant and immediate support… Feeling good feeling here… Surely, this was not my last issue…

1 Like