Skip to content

WindowAverage

roosit edited this page Nov 15, 2026 · 1 revision

WindowAverage

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

/* Moving average over a time window. A helper class of the smoothing filters, creates no objects. */
final class WindowAverage {

    /* Private constructor: the class has only static methods. */
    private WindowAverage() {
    }

    /* Returns a new array of smoothed values. atoms and values are parallel arrays, */
    /* halfMs - half of the window in milliseconds. The control parameter is not used. */
    static double[] apply(SensorAtom[] atoms, double[] values, long halfMs, IStepControl control)
            throws StepException {
        if (halfMs <= 0) {
            return values;
        }
        /* Prefix sums: the sum of any range of values is computed with one subtraction. */
        double[] sums = new double[values.length + 1];
        for (int i = 0; i < values.length; i++) {
            sums[i + 1] = sums[i] + values[i];
        }
        double[] result = new double[values.length];
        /* segmentTo - the end of the current recording segment (exclusive); the window is the range [low; high). */
        int segmentTo = 0;
        int low = 0;
        int high = 0;
        for (int i = 0; i < atoms.length; i++) {
            /* At the start of a recording segment the window is reset: values are not averaged across a pause. */
            if (ValueFilter.startsSegment(atoms, i)) {
                segmentTo = segmentEnd(atoms, i);
                low = i;
                high = i;
            }
            /* The window - the atoms whose time differs from the current one by no more than halfMs. */
            long time = atoms[i].timeRaw;
            while (atoms[low].timeRaw < time - halfMs) {
                low++;
            }
            while (high < segmentTo && atoms[high].timeRaw <= time + halfMs) {
                high++;
            }
            /* A plain average by the number of atoms in the window, without weights by atom duration. */
            result[i] = (sums[high] - sums[low]) / (high - low);
        }
        return result;
    }

    /* Index right after the last atom of the segment that starts at atom from. */
    private static int segmentEnd(SensorAtom[] atoms, int from) {
        int end = from + 1;
        while (end < atoms.length && atoms[end - 1].continuesWith(atoms[end])) {
            end++;
        }
        return end;
    }
}

Purpose

WindowAverage is the common calculation block of smoothing by average. It is not an import step: it is neither a FilterStep nor has parameters. It is called by the filters AverageFilter (scalar values) and VectorAverageFilter (multi-component values). The class is visible only to the filters of its package (final class without an access modifier).

How it works

Data. The method apply(atoms, values, halfMs, control) receives the sensor atoms (SensorAtom[], the time timeRaw and the sign of belonging to a recording segment are needed) and a parallel values array — the values of one quantity. It returns a new array of the same length; the original values does not change.

Settings. The only parameter is halfMs, half of the window in milliseconds. If it is not greater than zero, the method immediately returns the original values array (without a copy). The window width and its default are set by the calling filters.

Calculation.

  1. Prefix sums of the values are built: sums[i + 1] = sums[i] + values[i].
  2. For every atom i the window is the atoms of the same recording segment whose timeRaw falls into [time(i) − halfMs; time(i) + halfMs]. The lower bound low and the upper bound high only move forward, so the pass is linear.
  3. The result is (sums[high] − sums[low]) / (high − low).

The recording segment is determined by ValueFilter.startsSegment: a new segment begins where the atom does not continue the previous one (continuesWith), that is, after a pause. At the start of a segment low and high are reset to the current atom, and segmentEnd finds the end of the segment. So the window at a pause boundary is cut off rather than capturing data from the other side.

Features and limitations

  • The average is arithmetic by the number of atoms in the window, not by time: atoms of different duration enter with the same weight. The window always contains the atom i itself, so there is no division by zero.
  • The atom time is its start (timeRaw), the duration is not taken into account.
  • The window is symmetric and gets shorter near the start or end of a segment, so the edges of the curve are smoothed less than the middle.
  • The control parameter (IStepControl) is not used in the method: there is no cancellation check or progress output inside the calculation.
  • The values are computed by subtracting prefix sums in double: for quantities with a large spread the result may differ from the exact average in the last digits, and for a series of identical fractional values the average sometimes does not match them bit for bit.
  • See also: ValueFilter — the base class that AverageFilter calls.

Clone this wiki locally