Skip to content

ValueFilter

roosit edited this page Nov 15, 2026 · 1 revision

ValueFilter

Up | Previous | Next | Русский

Full source with short explanations

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 ValueFilter.java: the production code stays clean and the explanations live in the wiki.

package com.velitask.plugin.official.processing.filters;

import com.velitask.sdk.data.SensorAtom;
import com.velitask.sdk.scenario.FilterStep;
import com.velitask.sdk.scenario.IStepControl;
import com.velitask.sdk.scenario.StepException;
import com.velitask.sdk.scenario.StepInput;

/* Base of the filters that replace one numeric value of an atom without removing atoms. */
public abstract class ValueFilter<A extends SensorAtom> extends FilterStep<A> {

    protected ValueFilter(String name, int version, Class<A> atomClass) {
        super(name, version, atomClass);
    }

    /* Reads a numeric value from the atom in convenient units (for example, km/h). */
    protected abstract double value(A atom);

    /* Writes the smoothed value back into the atom in its units. */
    protected abstract void setValue(A atom, double value);

    /* The subclass algorithm: takes an array of values and returns an array of the same length. */
    protected abstract double[] smooth(A[] atoms, double[] values, IStepControl control) throws StepException;

    @Override
    /* The working template: read the values, smooth, write the changed ones, recompute the deltas. */
    protected final A[] filter(StepInput<A> input, IStepControl control) throws StepException {
        parameters();
        A[] atoms = input.atoms();
        double[] values = new double[atoms.length];
        for (int i = 0; i < atoms.length; i++) {
            values[i] = value(atoms[i]);
        }
        double[] smoothed = smooth(atoms, values, control);
        for (int i = 0; i < atoms.length; i++) {
            /* An atom is edited only where the value really changed. */
            if (smoothed[i] != values[i]) {
                setValue(atoms[i], smoothed[i]);
            }
        }
        relink(atoms);
        return atoms;
    }

    /* Recomputes the atom deltas inside recording segments; shared by ValueFilter and StayFilter. */
    static void relink(SensorAtom[] atoms) {
        for (int i = 0; i < atoms.length; i++) {
            if (i < atoms.length - 1 && atoms[i].continuesWith(atoms[i + 1])) {
                atoms[i].setDeltaTo(atoms[i + 1]);
            } else {
                atoms[i].clearDelta();
            }
        }
    }

    /* True for the first atom of a recording and for the first atom after a pause. */
    static boolean startsSegment(SensorAtom[] atoms, int index) {
        return index == 0 || !atoms[index - 1].continuesWith(atoms[index]);
    }
}

Purpose

ValueFilter<A> is the common base of the filters that change one numeric value of the sensor atoms and do not remove atoms: ExponentialFilter (smooth smoothing) and DriftFilter (zeroing the drift). The class extends FilterStep<A> from the SDK and takes over the routine: reading the values into an array, writing the result and recomputing the deltas. The subclass knows only its own atom (value, setValue) and its own algorithm (smooth).

The class is abstract and is not registered as a step: the concrete subclasses (SpeedExponentialFilter, SpeedDriftFilter) get into BuiltinScenarios.

How it works

Data. The filter method is final, subclasses do not override it:

  1. it calls parameters() so that the step parameters are created (the subclass defineParameters() is called once, the result is remembered);
  2. it takes the array of atoms from input.atoms() and copies the value of each atom into double[] values through value(atom);
  3. it calls smooth(atoms, values, control) and gets an array of smoothed values of the same length;
  4. it writes the value into the atom through setValue only where it differs from the original (smoothed[i] != values[i]);
  5. it calls relink(atoms) and returns the same array of atoms.

The atoms are edited in place, their number and order do not change, so FilterStep does not rebuild the layout over recording segments (it is needed only when there are fewer atoms).

Parameters. ValueFilter itself declares no parameters: they are set by the subclass. The requirements for the order of steps remain the same as for FilterStep: a pause step (ImportStepKind.GAPS) must come before the filter, so by the time of the filter the atoms are already laid out over recording segments and startsSegment tells the start of a segment.

Deltas (relink). An atom has a value and a delta "to the next atom" (SensorAtom.setDeltaTo). After the values are edited, the deltas become stale, so for every atom:

  • if it is followed by a continuation of the same segment (continuesWith), setDeltaTo(next) is called;
  • otherwise (the last atom of a segment, a pause boundary) clearDelta() is called.

startsSegment(atoms, i). True for i == 0 and for an atom that does not continue the previous one (!atoms[i - 1].continuesWith(atoms[i])), that is, for the first atom after a pause. Subclasses use it so as not to carry values across a pause.

Features and limitations

  • relink and startsSegment are package-private (static without an access modifier), they are used only by the filters of the package; relink is also called by StayFilter.
  • The comparison smoothed[i] != values[i] is an exact double comparison. For speed, setValue rounds the result to an integer in thousandths of km/h, so a tiny change may not change the atom itself.
  • The length of the array returned by smooth must equal the number of atoms: there are no checks, and a mismatch gives ArrayIndexOutOfBoundsException.
  • The class works only with values; taking time and atom durations into account (for example, in DriftFilter) is the subclass's concern.

Clone this wiki locally