Repository navigation
AverageFilter
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 AverageFilter.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;
import com.velitask.sdk.scenario.StepException;
/* Smoothing by average for a sensor with one value per atom. The subclass says how to read and write the value. */
public abstract class AverageFilter<A extends SensorAtom> extends ValueFilter<A> {
/* Parameter name in the import plan and in the step settings JSON. */
public static final String WINDOW_SEC = "windowSec";
/* Common prefix of the string keys: Plugin.ImportStep.average - the same labels for all averaging filters. */
private static final String KEY = _KEY + ".average";
/* Window width in seconds. Created on the first call of parameters(). */
private DoubleSpinnerProperty mWindowSec;
/* name and version are passed to FilterStep; atomClass sets the type of atoms the step works with. */
protected AverageFilter(String name, int version, Class<A> atomClass) {
super(name, version, atomClass);
}
/* One parameter: "Averaging window, s", default 0 (smoothing is off). */
@Override
public IProperty[] defineParameters() {
mWindowSec = decimal(WINDOW_SEC, localized(KEY + "." + WINDOW_SEC + ".title"),
localizedOrEmpty(KEY + "." + WINDOW_SEC + ".description"), 0);
return new IProperty[]{mWindowSec};
}
/* Half-window in milliseconds: seconds * 1000 / 2, with the fractional part dropped. */
@Override
protected double[] smooth(A[] atoms, double[] values, IStepControl control) throws StepException {
return WindowAverage.apply(atoms, values, (long) (mWindowSec.get() * 500), control);
}
}AverageFilter is the abstract base of the "Smoothing by average" filters for sensors that have one number in the atom. It replaces every value with the average over a window around it: it removes jitter, but also softens sharp changes. The class itself is not registered as a step; its subclass SpeedAverageFilter is registered.
Data. The class extends ValueFilter<A>, and all the plumbing is in it: filter reads the values of all atoms through value(atom), calls smooth(...), writes only the changed values through setValue(...) and then restores the atom increments (relink). The details are on the ValueFilter page. AverageFilter implements only smooth: it passes the values to WindowAverage.
Settings. One parameter windowSec — a DoubleSpinnerProperty with an initial value of 0 (the spinner minimum is 0). The title and description are taken from the strings Plugin.ImportStep.average.windowSec.title and .description: "Averaging window, s" and "How many seconds around every point are averaged. The wider the window, the smoother the curve. 0 - do not smooth." The title and description of the step itself are taken from the subclass by its name.
Calculation. The half-window in milliseconds is (long) (windowSec * 500). The value 0 and anything below 0.002 s give a half-window of 0, and WindowAverage returns the values unchanged.
- The default 0 means "do not smooth": until the plan author or the user sets a window, the step does not change the values.
- Even with a window of 0 the base
ValueFilter.filtercallsrelinkafter the calculation: the increments of all atoms are recalculated from the neighbouring values (within a recording segment), and at the last atom of a segment they are zeroed. The values do not change. - The window does not cross recording pauses: every segment is smoothed separately (see WindowAverage).
- The parameter labels are shared by all averaging filters, the window unit is always seconds.