package temperature

// EWMAFilter is an exponentially weighted moving average filter
// See https://hackaday.com/2019/09/06/sensor-filters-for-coders/
type EWMAFilter struct {
	weight        float64
	prevTemp      float64
	isInitialized bool
}

func NewEWMAFilter(weight float64) EWMAFilter {
	return EWMAFilter{
		weight: weight,
	}
}

func (p *EWMAFilter) Update(input float64) float64 {
	if !p.isInitialized {
		p.prevTemp = input
		p.isInitialized = true
		return input
	}

	ewmf := (1-p.weight)*p.prevTemp + p.weight*input
	p.prevTemp = ewmf

	return ewmf
}
