ixfx
    Preparing search index...

    Function averageWeighted

    • Computes an average of an array with a set of weights applied.

      Weights can be provided as an array, expected to be on 0..1 scale, with indexes matched up to input data. Ie. data at index 2 will be weighed by index 2 in the weightings array.

      import { averageWeighted } from 'https://unpkg.com/ixfx/dist/numbers.js';
      // All items weighted evenly
      averageWeighted([1,2,3], [1,1,1]); // 2

      // First item has full weight, second half, third quarter
      averageWeighted([1,2,3], [1, 0.5, 0.25]); // 1.57

      // With reversed weighting of [0.25,0.5,1] value is 2.42

      A function can alternatively be provided to compute the weighting based on array index, via weight.

      import { weight,averageWeighted } from 'https://unpkg.com/ixfx/dist/numbers.js';
      import { gaussian } from 'https://unpkg.com/ixfx/dist/modulation.js';
      averageWeighted[1,2,3], gaussian()); // 2.0

      This is the same as:

      import { weight,averageWeighted } from 'https://unpkg.com/ixfx/dist/numbers.js';
      import { gaussian } from 'https://unpkg.com/ixfx/dist/modulation.js';

      const data = [1,2,3];
      const w = weight(data, gaussian());
      const avg = averageWeighted(data, w); // 2.0

      Parameters

      • data: number[] | readonly number[]

        Data to average

      • weightings: number[] | readonly number[] | (value: number) => number

        Array of weightings that match up to data array, or an easing function

      Returns number

      average Compute averages without weighting.