Normalise
Normalisation converts values from some scale to a consistent scalar 0..1 range. It is very helpful to have all your logic based on this same scale rather than working with absolute units like pixels, angles etc.
For simple normalisation, some sense of the input range of values is needed: a minimum and maximum. For example, although an analog input value might theoretically be in the range of 0..1023, perhaps we’ve empirically discovered that the usable range is actually 0..400. This would constitute the range of the value.
Arrays
Section titled “Arrays”If you have all the data in advance, it’s easy enough to ‘perfectly’ normalise, because the smallest and largest value can be determined. Normalise.array(source) returns a normalised copy of source, such that the smallest value becomes 0 and the largest value 1.
To use it, pass in the name of the normalisation stategy (minmax, zscore or [robust(https://en.wikipedia.org/wiki/Feature_scaling#Robust_Scaling)]) and the array:
import { Numbers } from 'https://unpkg.com/ixfx';Numbers.Normalise.array(`minmax`, [100,20,0,50]); // [1, 0.2, 0, 0.5]Depending on the normalisation strategy, options can be passed in when normalising. For example with the ‘minmax’ strategy, we can use a fixed range and clamp values outside of it:
import { Numbers } from 'https://unpkg.com/ixfx';Numbers.Normalise.array(`minmax`, [100,20,0,50], {clamp: true, minForced: 0, maxForced: 50}); // [1, 0.4, 0, 1]Stream
Section titled “Stream”We don’t always have a complete data set to normalise. Sometimes we have streaming values, and it might be that we never know the range of the data. Normalise.stream creates a normalise function which automatically adapts as values are processed.
import { Numbers } from 'https://unpkg.com/ixfx';
// Initialise a streaming normaliser, defaults to 'minmax' strategyconst n = Numbers.Normalise.stream();
// Yields 1, because 5 is the highest seenn(5);
// Yields 1, because now 10 is the highest seenn(10);
// Yields 0, because it's so far the lowest seenn(5);
// Yields 0.5, because it's in the middle of the range seen thus farn(7.5);
// Yields 1, because now it's the largest seenn(11);It should be clear from the examples that different input values may produce the same output value, depending on what has been seen before. For example, an input of 5 yields 1 if 5 is the highest value, but 5 yields 0.05 if 100 is the highest value. It’s good to remember then that normalised values aren’t necessarily comparable to each other.
Like Normalise.array, it’s possible to specify the normalisation strategy and pass options to the normaliser.