Rephrase smoothing window as weight half-life

This commit is contained in:
Jacob Palecki 2023-09-28 12:56:46 -07:00
parent e6b2ea667d
commit d3397f82cc
4 changed files with 21 additions and 24 deletions

View file

@ -69,7 +69,7 @@ namespace rawaccel {
bool whole = true;
double lp_norm = 2;
bool should_smooth = false;
double smooth_window = 70;
double smooth_halflife = 25;
bool use_linear = false;
};

View file

@ -60,7 +60,7 @@ namespace rawaccel {
input_speed_args speed_args = {};
distance_mode dist_mode = {};
const double trendAge = 2.5;
const double trendHalflife = 1.25;
double windowCoefficient = 0;
double cutoffCoefficient = 0;
@ -84,9 +84,8 @@ namespace rawaccel {
dist_mode = distance_mode::euclidean;
}
double averageAge = fabs(speed_args.smooth_window / 2.0);
windowCoefficient = averageAge > 0 ? exp(-1.0 / averageAge) : 0;
windowTrendCoefficient = trendAge > 0 ? exp(-1.0 / trendAge) : 0;
windowCoefficient = args.smooth_halflife > 0 ? pow(0.5, 1 / args.smooth_halflife) : 0;
windowTrendCoefficient = trendHalflife > 0 ? pow(0.5, 1 / trendHalflife) : 0;
cutoffCoefficient = 1.0 - sqrt(1.0 - windowCoefficient);
cutoffTrendCoefficient = 1.0 - sqrt(1.0 - windowTrendCoefficient);
}
@ -106,7 +105,7 @@ namespace rawaccel {
}
if (speed_args.should_smooth &&
speed_args.smooth_window > 0)
speed_args.smooth_halflife > 0)
{
if (speed_args.use_linear)
{

View file

@ -45,20 +45,19 @@ namespace wrapper_tests
[TestMethod]
public void Given_InputForSimpleExponentialSmoothing_SmoothCalculator_Smooths()
{
double smoothWindow = 100;
double averageAge = smoothWindow / 2.0;
double smoothHalfLife = 50;
double pollTime = 1;
var speedArgs = new SpeedCalculatorArgs(
lp_norm: 2,
should_smooth: true,
smooth_window: smoothWindow,
smooth_halflife: smoothHalfLife,
use_linear: false);
var speedCalc = new SpeedCalculator();
speedCalc.Init(speedArgs);
var modelSmoother = new SimpleExponentialSmoother(averageAge);
var modelSmoother = new SimpleExponentialSmoother(smoothHalfLife);
var inputs = new[]
{
@ -86,20 +85,19 @@ namespace wrapper_tests
[TestMethod]
public void Given_InputForLinearExponentialSmoothing_SmoothCalculator_Smooths()
{
double smoothWindow = 100;
double averageAge = smoothWindow / 2.0;
double smoothHalflife = 50;
double pollTime = 1;
var speedArgs = new SpeedCalculatorArgs(
lp_norm: 2,
should_smooth: true,
smooth_window: smoothWindow,
smooth_halflife: smoothHalflife,
use_linear: true);
var speedCalc = new SpeedCalculator();
speedCalc.Init(speedArgs);
var modelSmoother = new LinearExponentialSmoother(averageAge);
var modelSmoother = new LinearExponentialSmoother(smoothHalflife);
var inputs = new[]
{
@ -136,9 +134,9 @@ namespace wrapper_tests
protected class SimpleExponentialSmoother : IMouseSmoother
{
public SimpleExponentialSmoother(double averageAge)
public SimpleExponentialSmoother(double halfLife)
{
WindowCoefficient = Math.Pow(Math.E, (-1 / averageAge));
WindowCoefficient = Math.Pow(0.5, (1 / halfLife));
CutoffCoefficient = 1 - Math.Sqrt(1 - WindowCoefficient);
SmoothedSpeeds = new List<double>();
WindowTotal = 0;
@ -168,12 +166,12 @@ namespace wrapper_tests
protected class LinearExponentialSmoother : IMouseSmoother
{
public const double TrendAverageAge = 2.5;
public const double TrendHalfLife = 1.25;
public LinearExponentialSmoother(double averageAge)
public LinearExponentialSmoother(double halfLife)
{
WindowCoefficient = Math.Pow(Math.E, -1 / averageAge);
WindowTrendCoefficient = Math.Pow(Math.E, -1 / TrendAverageAge);
WindowCoefficient = Math.Pow(0.5, 1 / halfLife);
WindowTrendCoefficient = Math.Pow(0.5, 1 / TrendHalfLife);
CutoffCoefficient = 1 - Math.Sqrt(1 - WindowCoefficient);
CutoffTrendCoefficient = 1 - Math.Sqrt(1 - WindowTrendCoefficient);
SmoothedSpeeds = new List<double>();

View file

@ -111,8 +111,8 @@ public value struct InputSpeedArgs
[MarshalAs(UnmanagedType::U1)]
bool shouldSmooth;
[JsonProperty("Time window in ms over which input should be smoothed")]
double smoothWindow;
[JsonProperty("Time in ms after which an input is weighted at half its original value.")]
double smoothHalfLife;
[JsonProperty("Whether smoothed input speeds should use linear (true) or simple (false) exponential smoothing")]
[MarshalAs(UnmanagedType::U1)]
@ -498,11 +498,11 @@ public:
SpeedCalculatorArgs(
double lp_norm,
bool should_smooth,
double smooth_window,
double smooth_halflife,
bool use_linear)
{
speed_args->lp_norm = lp_norm;
speed_args->smooth_window = smooth_window;
speed_args->smooth_halflife = smooth_halflife;
speed_args->should_smooth = should_smooth;
speed_args->use_linear = use_linear;
}