comparison Orchestland/Assets/LeapMotion/Widgets/Scripts/Utils/ExponentialSmoothing.cs @ 1:f7675884f2a1

Add Orchestland project
author Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
date Fri, 17 Jul 2015 23:09:20 +0900
parents
children
comparison
equal deleted inserted replaced
0:347d21cdfc22 1:f7675884f2a1
1 /// <summary>
2 /// Time-step independent exponential smoothing.
3 /// </summary>
4 /// <remarks>
5 /// When moving at a constant speed: speed * delay = Value - ExponentialSmoothing.value.
6 /// </remarks>
7 public class ExponentialSmoothing
8 {
9 public float value = 0f; // Filtered value
10 public float delay = 0f; // Mean delay
11 public bool reset = true; // Reset on Next Update
12
13 public void SetAlpha(float alpha, float deltaTime = 1f)
14 {
15 this.delay = deltaTime * alpha / (1f - alpha);
16 }
17
18 public float Update(float value, float deltaTime = 1f)
19 {
20 if (deltaTime > 0f &&
21 !reset) {
22 float gamma = delay / deltaTime;
23 float alpha = gamma / (1f + gamma);
24 // NOTE: If deltaTime -> 0 then alpha -> 1,
25 // reducing the filter to this.value = value.
26 this.value *= 1f - alpha;
27 this.value += alpha * value;
28 } else {
29 this.value = value;
30 reset = false;
31 }
32 return this.value;
33 }
34 }