mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-10-28 00:53:45 +08:00
ab81464bd1
Add navigation and map matching providers with the vendored map service sources. Co-authored-by: Daniel Koepping <elkoled@gmail.com> Co-authored-by: Danny <dannyirwin31@yahoo.com> Co-authored-by: Prabhaav Pillai <prabhaav@msu.edu> Co-authored-by: dirwin31 <83434411+dirwin31@users.noreply.github.com>
49 lines
880 B
Go
49 lines
880 B
Go
package math
|
|
|
|
type MovingAverage struct {
|
|
values []float64
|
|
index int
|
|
size int
|
|
initialized bool
|
|
Estimate float64
|
|
}
|
|
|
|
func (a *MovingAverage) Init(size int) {
|
|
a.size = size
|
|
a.values = make([]float64, size)
|
|
a.initialized = false
|
|
a.index = 0
|
|
}
|
|
|
|
func (a *MovingAverage) Reset() {
|
|
a.initialized = false
|
|
}
|
|
|
|
func (a *MovingAverage) Update(val float64) float64 {
|
|
corrected := val
|
|
if corrected == 0 { // prevent divide by zero issues
|
|
corrected = 0.01
|
|
}
|
|
if !a.initialized {
|
|
for i := range a.values {
|
|
a.values[i] = corrected
|
|
}
|
|
a.initialized = true
|
|
a.Estimate = corrected
|
|
return corrected
|
|
}
|
|
a.index += 1
|
|
a.index %= a.size
|
|
a.values[a.index] = corrected
|
|
total := 0.0
|
|
for _, v := range a.values {
|
|
total += v
|
|
}
|
|
a.Estimate = total / float64(a.size)
|
|
return a.Estimate
|
|
}
|
|
|
|
func (a *MovingAverage) Raw() float64 {
|
|
return a.values[a.index]
|
|
}
|