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

**URL:** <https://discourse.julialang.org/t/how-to-identify-local-maxima-peaks-in-a-time-signal/6000>\
**Category:** Signal and Image Processing\
**Created:** [September 20, 2017, 2:19pm UTC](https://discourse.julialang.org/t/how-to-identify-local-maxima-peaks-in-a-time-signal/6000 "2017-09-20T14:19:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ceysa75](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ceysa75/32/19121_2.png) [@ceysa75](https://discourse.julialang.org/u/ceysa75)\
**Post date:** [September 20, 2017, 2:19pm UTC](https://discourse.julialang.org/t/how-to-identify-local-maxima-peaks-in-a-time-signal/6000/1 "2017-09-20T14:19:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ggggggggg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ggggggggg/32/265_2.png) [@ggggggggg](https://discourse.julialang.org/u/ggggggggg)\
**Post date:** [September 20, 2017, 3:06pm UTC](https://discourse.julialang.org/t/how-to-identify-local-maxima-peaks-in-a-time-signal/6000/2 "2017-09-20T15:06:56Z")

</div>

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

```julia
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

```

---

<div class="post-metadata">

**Author:** ![ssfrr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ssfrr/32/3736_2.png) [@ssfrr](https://discourse.julialang.org/u/ssfrr)\
**Post date:** [September 20, 2017, 3:59pm UTC](https://discourse.julialang.org/t/how-to-identify-local-maxima-peaks-in-a-time-signal/6000/3 "2017-09-20T15:59:07Z")

</div>

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

```julia
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]))

```

---

<div class="post-metadata">

**Author:** ![ggggggggg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ggggggggg/32/265_2.png) [@ggggggggg](https://discourse.julialang.org/u/ggggggggg)\
**Post date:** [September 20, 2017, 4:26pm UTC](https://discourse.julialang.org/t/how-to-identify-local-maxima-peaks-in-a-time-signal/6000/5 "2017-09-20T16:26:52Z")

</div>

> [@ssfrr](#):
>
> sig = cos.(linspace(0, 10pi, 2000))  
> maxs\_cart = findlocalmaxima(sig)

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

---

<div class="post-metadata">

**Author:** ![ssfrr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ssfrr/32/3736_2.png) [@ssfrr](https://discourse.julialang.org/u/ssfrr)\
**Post date:** [September 20, 2017, 4:55pm UTC](https://discourse.julialang.org/t/how-to-identify-local-maxima-peaks-in-a-time-signal/6000/6 "2017-09-20T16:55:50Z")

</div>

> [@ggggggggg](#):
>
> It looks like the documentation says findlocalmaxima returns a Vector{Tuple} but it actually returns a Array{CartesianIndex{1},1}.

Correct - [PR submitted](https://github.com/JuliaImages/Images.jl/pull/668)

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

---

<div class="post-metadata">

**Author:** ![ceysa75](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ceysa75/32/19121_2.png) [@ceysa75](https://discourse.julialang.org/u/ceysa75)\
**Post date:** [September 22, 2017, 2:20pm UTC](https://discourse.julialang.org/t/how-to-identify-local-maxima-peaks-in-a-time-signal/6000/7 "2017-09-22T14:20:10Z")

</div>

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