Debugging timing information is frequently hard to read. This is far from the best method, but tends to spew out more readable results than raw data. Of course usage isn’t limited to filtering timing information.
This example snippet implements a two-speed adaptive IIR (infinite impulse response filter). A base IIR simply lerps the current input toward current filtered value with a constant lerp constant. The ‘heavier’ the filter, the slow the output adapts to the input. Here we have a version with two compile time constant speed which are selected by the percent difference between the current filter and actual.
// constant to determine the choice between the two speeds
private static final float FILTER_P = 0.1f; // current: if filtered is within 10% of current then use slow
// constant for slow speed adaption
private static final float FILTER_S = 0x1p-6f;
// constant for fast speed adaption
private static final float FILTER_F = 0x1p-2f; // current: 75% filtered, 25%
/**
* 2-speed adaptive IIR.
*/
public static float twospeedIIR(float filtered, float current)
{
// difference between the current filtered and actual values
float diff = Math.abs(filtered-current);
// choose between the two speeds
if (diff < FILTER_P*filtered)
return (1-FILTER_S)*filtered + FILTER_S*current;
return(1-FILTER_F)*filtered + FILTER_F*current;
}