Repository navigation
ExponentialFilter
Up | Previous | Next | Русский
Below is the full source of the class in documentation form. The comments inside the block explain the purpose of the parts of the class, but they are not stored in the working file ExponentialFilter.java: the production code stays clean and the explanations live in the wiki.
package com.velitask.plugin.official.processing.filters;
import static com.velitask.plugin.official.processing.FormulaParameters.decimal;
import com.velitask.sdk.data.SensorAtom;
import com.velitask.sdk.properties.DoubleSpinnerProperty;
import com.velitask.sdk.properties.IProperty;
import com.velitask.sdk.scenario.IStepControl;
/* Exponential smoothing of a value: every new value is blended with the previous smoothed one. */
public abstract class ExponentialFilter<A extends SensorAtom> extends ValueFilter<A> {
/* Parameter name: key in the import plans and in the step settings. */
public static final String ALPHA = "alpha";
/* Localization key prefix: Plugin.ImportStep.exponential. */
private static final String KEY = _KEY + ".exponential";
private DoubleSpinnerProperty mAlpha;
protected ExponentialFilter(String name, int version, Class<A> atomClass) {
super(name, version, atomClass);
}
@Override
/* One parameter alpha; default 1 - no smoothing. */
public IProperty[] defineParameters() {
mAlpha = decimal(ALPHA, localized(KEY + "." + ALPHA + ".title"),
localizedOrEmpty(KEY + "." + ALPHA + ".description"), 1);
return new IProperty[]{mAlpha};
}
@Override
/* Recurrent formula over the recording segments; the first value of a segment stays as it is. */
protected double[] smooth(A[] atoms, double[] values, IStepControl control) {
double alpha = mAlpha.get();
double[] result = new double[values.length];
for (int i = 0; i < values.length; i++) {
/* The start of a segment (the recording or the first point after a pause) is not smoothed and resets the chain. */
if (startsSegment(atoms, i)) {
result[i] = values[i];
} else {
/* New = alpha * current + (1 - alpha) * previous smoothed. */
result[i] = alpha * values[i] + (1 - alpha) * result[i - 1];
}
}
return result;
}
}ExponentialFilter<A> is the base of the "Gradual smoothing" filters. Every value is replaced with a blend of itself and the previous smoothed value: the smaller the factor, the smoother the curve and the more it lags behind. The subclass SpeedExponentialFilter defines the speed. The base is ValueFilter: the class implements only smooth and the parameter.
Parameter. One: alpha, "Smoothing factor" — decimal(...) (DoubleSpinnerProperty, a number not less than zero), default 1. The description from the strings: the share of the new value in the result from 0 to 1, unitless; 1 — no smoothing, 0.4 — noticeably smoother.
Formula. For every value i of the values array:
- if
startsSegment(atoms, i)(the first atom of the recording or the first one after a pause) —result[i] = values[i]; - otherwise
result[i] = alpha * values[i] + (1 - alpha) * result[i - 1].
Smoothing goes forward in time and does not cross a pause: the chain is reset at the start of every segment. The time between atoms is not taken into account — the formula counts points, not seconds.
Result. An array of smoothed values; writing to the atoms and recalculating the increments are done by ValueFilter.
Where it is used. The speedExponential step is registered in BuiltinScenarios. In GoproImportPlan it is in the speed scenario after "Speed drift at a stop": alpha = 0.4; the speed sensor in the plan is marked disabled().
- With
alpha = 1the result equals the input (no smoothing), the atoms do not change. - With
alpha = 0the curve freezes at the first value of the segment (the parameter description says so too): do not use this value. - The parameter has no upper bound (the range goes up to
Double.MAX_VALUE): values above 1 are allowed, but they give "overshoot" — the result goes beyond the input values. - The formula counts points, not time, so the same
alphagives a different smoothness at a different recording rate.