diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Orchestland/Assets/LeapMotion/Widgets/Scripts/Utils/ExponentialSmoothing.cs	Fri Jul 17 23:09:20 2015 +0900
@@ -0,0 +1,34 @@
+/// <summary>
+/// Time-step independent exponential smoothing.
+/// </summary>
+/// <remarks>
+/// When moving at a constant speed: speed * delay = Value - ExponentialSmoothing.value.
+/// </remarks>
+public class ExponentialSmoothing
+{
+  public float value = 0f; // Filtered value
+  public float delay = 0f; // Mean delay
+  public bool reset = true; // Reset on Next Update
+
+  public void SetAlpha(float alpha, float deltaTime = 1f)
+  {
+    this.delay = deltaTime * alpha / (1f - alpha);
+  }
+
+  public float Update(float value, float deltaTime = 1f)
+  {
+    if (deltaTime > 0f &&
+        !reset) {
+      float gamma = delay / deltaTime;
+      float alpha = gamma / (1f + gamma);
+      // NOTE: If deltaTime -> 0 then alpha -> 1,
+      // reducing the filter to this.value = value.
+      this.value *= 1f - alpha;
+      this.value += alpha * value;
+    } else {
+      this.value = value;
+      reset = false;
+    }
+    return this.value;
+  }
+}