Skip to content

Commit

Permalink
Fix #30; equal elements for a plateau must occur after the candidate …
Browse files Browse the repository at this point in the history
…extrema
  • Loading branch information
halleysfifthinc committed Jul 31, 2023
1 parent 2d526c1 commit 077381a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/minmax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,21 @@ function findnextextrema(cmp, x::AbstractVector, i::Int, w::Int, strict::Bool)
peak &= false
i = j > i ? j : i + 1
break # No reason to continue checking if the rest of the elements qualify
elseif xi === xj # plateau
elseif xi === xj # potential plateau
k = findnext(p -> xi !== p, x, j+1)
# @show k i j
if strict
if !isnothing(k) && i-1 firstindex(x)
xi1 = x[i-1]
xk = x[k]
# @show xi1 xi xk i j k
if !ismissing(xi1) && !isnan(xi1) && cmp(xi1, xi) &&
if k < i
# elements of equal value are allowed within x[i-w:i+w] when
# `strict == true` (as long as the immediately previous
# element is less than `x[i]`, etc as tested on the next
# line)
continue
elseif !ismissing(xi1) && !isnan(xi1) && cmp(xi1, xi) &&
!ismissing(xk) && !isnan(xk) && cmp(xk, xi)
# plateau begins and ends with elements less than the current
return i
Expand All @@ -44,7 +50,7 @@ function findnextextrema(cmp, x::AbstractVector, i::Int, w::Int, strict::Bool)
else
xk = isnothing(k) ? NaN : x[k]
xi1 = i-1 firstindex(x) ? x[i-1] : NaN
if something(k, lastindex(x)+1) i+w &&
if i+w something(k, lastindex(x)+1) &&
(ismissing(xi1) || isnan(xi1) || cmp(xi1, xi)) &&
(ismissing(xk) || isnan(xk) || cmp(xk, xi))
return i
Expand Down
4 changes: 4 additions & 0 deletions test/minmax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ x1 = a*sin.(2*pi*f1*T*t)+b*sin.(2*pi*f2*T*t)+c*sin.(2*pi*f3*T*t);
# issue #4
@test isempty(argmaxima(zeros(10)))
@test argmaxima(zeros(10), 1; strict=false) == [1]

# issue #30
y = [12.452637, 12.389122, 12.452637, 12.512817, 48.756142, 48.410103, 45.00222]
@test findnextmaxima(y, 3, 2) === 5
end

# A missing or NaN should not occur within the `w` of the peak
Expand Down

0 comments on commit 077381a

Please sign in to comment.