Skip to content

Commit

Permalink
Move decay computation to new method
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Jan 7, 2022
1 parent 73b07fc commit ac735a5
Showing 1 changed file with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,7 @@ private void update() {
// xₜ
float newValue = unsmoothed.getAsFloat();

// Compute the decay constant from the half life
// https://en.wikipedia.org/wiki/Exponential_decay#Measuring_rates_of_decay
// https://en.wikipedia.org/wiki/Exponential_smoothing#Time_constant
double timeConstant = (newValue > this.accumulator ? halfLifeUp : halfLifeDown) / LN_OF_2;
// k = 1 / τ
this.decayConstant = (float) (1.0f / timeConstant);
computeDecay(newValue > this.accumulator ? halfLifeUp : halfLifeDown);

// Implements the basic variant of exponential smoothing
// https://en.wikipedia.org/wiki/Exponential_smoothing#Basic_(simple)_exponential_smoothing_(Holt_linear)
Expand All @@ -101,6 +96,14 @@ private void update() {
accumulator = lerp(accumulator, newValue, smoothingFactor);
}

private void computeDecay(float halfLife) {
// Compute the decay constant from the half life
// https://en.wikipedia.org/wiki/Exponential_decay#Measuring_rates_of_decay
// https://en.wikipedia.org/wiki/Exponential_smoothing#Time_constant
// k = 1 / τ
this.decayConstant = (float) (1.0f / (halfLife / LN_OF_2));
}

/**
* @return the current smoothed value
*/
Expand Down

0 comments on commit ac735a5

Please sign in to comment.