Files
firestar5683 ab81464bd1 Maps
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>
2026-10-14 19:56:17 -05:00

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]
}