Skip to content

VectorAverageFilter

roosit edited this page Nov 15, 2026 · 1 revision

VectorAverageFilter

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 VectorAverageFilter.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.FilterStep;
import com.velitask.sdk.scenario.IStepControl;
import com.velitask.sdk.scenario.StepException;
import com.velitask.sdk.scenario.StepInput;

/* Smoothing by average for sensors with several components in the atom (axes, angles, quaternion). */
public abstract class VectorAverageFilter<A extends SensorAtom> extends FilterStep<A> {

    /* Parameter name; the same as AverageFilter.WINDOW_SEC. */
    public static final String WINDOW_SEC = "windowSec";

    /* The same strings as in AverageFilter: Plugin.ImportStep.average.*. */
    private static final String KEY = _KEY + ".average";

    /* Window width in seconds. */
    private DoubleSpinnerProperty mWindowSec;

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

    /* How many components the atom has. */
    protected abstract int components();

    /* Reads component index. */
    protected abstract double component(A atom, int index);

    /* Writes component index (the subclass rounds itself if the field is an integer). */
    protected abstract void setComponent(A atom, int index, double value);

    /* Period of an angular value in degrees; 0 - the value is not angular. */
    protected double period() {
        return 0;
    }

    /* One parameter windowSec, default 0. */
    @Override
    public IProperty[] defineParameters() {
        mWindowSec = decimal(WINDOW_SEC, localized(KEY + "." + WINDOW_SEC + ".title"),
                localizedOrEmpty(KEY + "." + WINDOW_SEC + ".description"), 0);
        return new IProperty[]{mWindowSec};
    }

    /* The whole step: window 0 - output unchanged; otherwise each component is smoothed separately. */
    @Override
    protected final A[] filter(StepInput<A> input, IStepControl control) throws StepException {
        /* Creates the parameters on the first call so that mWindowSec is filled. */
        parameters();
        A[] atoms = input.atoms();
        /* Half-window in milliseconds, as in AverageFilter. */
        long halfMs = (long) (mWindowSec.get() * 500);
        if (halfMs <= 0) {
            return atoms;
        }
        double period = period();
        int count = components();
        for (int c = 0; c < count; c++) {
            double[] original = new double[atoms.length];
            for (int i = 0; i < atoms.length; i++) {
                original[i] = component(atoms[i], c);
            }
            /* For ordinary values the original values are smoothed. */
            double[] values = original;
            /* For angles the values are first "unwrapped": a jump across the period boundary is not counted as a break. */
            if (period > 0) {
                values = new double[atoms.length];
                for (int i = 0; i < atoms.length; i++) {
                    if (ValueFilter.startsSegment(atoms, i)) {
                        values[i] = original[i];
                    } else {
                        values[i] = values[i - 1] + normalize(original[i] - original[i - 1], period);
                    }
                }
            }
            /* The average over the window is computed by WindowAverage. */
            double[] smoothed = WindowAverage.apply(atoms, values, halfMs, control);
            for (int i = 0; i < atoms.length; i++) {
                /* For angles the result is returned to the period range; only the changed values are written. */
                double value = period > 0 ? normalize(smoothed[i], period) : smoothed[i];
                if (value != original[i]) {
                    setComponent(atoms[i], c, value);
                }
            }
        }
        /* The increments of all atoms are recalculated from the new values. */
        ValueFilter.relink(atoms);
        return atoms;
    }

    /* Brings the value to the range (-period/2; period/2]. */
    private static double normalize(double value, double period) {
        return value - period * Math.ceil(value / period - 0.5);
    }
}

Purpose

VectorAverageFilter is the abstract base of the "Smoothing by average" filters for sensors with several numbers in the atom: acceleration and rotation on the axes, orientation as a quaternion, orientation angles. Every component is smoothed independently. Subclasses: AccelAverageFilter, GyroAverageFilter, OrientAverageFilter, AttitudeAverageFilter.

How it works

Data. The class extends FilterStep<A> directly (not ValueFilter, which has a single value). The subclass describes the atom: components() is the number of components, component(atom, index) and setComponent(atom, index, value) read and write, period() is the period for angles (0 by default).

Settings. One parameter windowSec ("Averaging window, s") with a default of 0 — as in AverageFilter, with the same strings Plugin.ImportStep.average.*.

Calculation in filter:

  1. halfMs = (long) (windowSec * 500). If it is not greater than zero, the method returns input.atoms() as it is: the values and increments are not touched.
  2. For every component c an array of the original values is collected.
  3. If period() > 0, the values are unwrapped along the recording segments: the first value of a segment is taken as it is, every next one is the previous unwrapped value plus the difference of the neighbouring original values brought by the normalize function to the range of half a period. So the angle 359° after 1° counts as a step of −2°, not a jump by 358°.
  4. WindowAverage averages the array over the window.
  5. For angles the result is brought back with normalize(value, period); the value is written to the atom only if it is not equal to the original.
  6. After all components ValueFilter.relink recalculates the atom increments.

The function normalize(value, period) is value − period * ceil(value / period − 0.5); for a period of 360 it gives the range (−180; 180].

Features and limitations

  • The components are smoothed separately. For a quaternion (OrientAverageFilter) the result is therefore not normalized back to unit length, and the equivalence of the signs of q and −q is not taken into account.
  • For angles: if the original values lie outside (−180; 180] (for example, 0…360), then with a window greater than 0 all of them will be rewritten into this range.
  • With a window of 0 the step changes nothing, including not calling relink (unlike AverageFilter, where relink is always executed).
  • The window does not cross recording pauses, the segments are smoothed separately.
  • Angle unwrapping goes along a segment from its first value; at the start of every segment the accumulation is reset.

Clone this wiki locally