-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpeak_finding.go
More file actions
77 lines (66 loc) · 1.55 KB
/
peak_finding.go
File metadata and controls
77 lines (66 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package num
import (
"errors"
)
// adjustIndex 处理索引越界,根据mode返回有效索引
func adjustIndex(i, length int, mode string) int {
if length == 0 {
return 0
}
if i < 0 {
switch mode {
case "clip":
return 0
case "wrap":
return (i%length + length) % length
default:
return 0 // 默认为clip模式
}
} else if i >= length {
switch mode {
case "clip":
return length - 1
case "wrap":
return i % length
default:
return length - 1 // 默认为clip模式
}
}
return i
}
// ArgRelExtrema 检测数据中的相对极值点
func ArgRelExtrema(data []float64, comparator func(a, b float64) bool, order int, mode string) ([]bool, error) {
if order < 1 {
return nil, errors.New("order必须为大于等于1的整数")
}
dataLen := len(data)
results := make([]bool, dataLen)
for i := range results {
results[i] = true
}
for shift := 1; shift <= order; shift++ {
anyTrue := false
for i := 0; i < dataLen; i++ {
// 计算左右偏移后的索引
plusIndex := adjustIndex(i+shift, dataLen, mode)
minusIndex := adjustIndex(i-shift, dataLen, mode)
// 比较当前值与左右值
current := data[i]
plusVal := data[plusIndex]
minusVal := data[minusIndex]
// 应用比较函数
cmpPlus := comparator(current, plusVal)
cmpMinus := comparator(current, minusVal)
// 更新结果数组
results[i] = results[i] && cmpPlus && cmpMinus
if results[i] {
anyTrue = true
}
}
// 如果所有结果都为false,提前终止
if !anyTrue {
break
}
}
return results, nil
}