Yesterday I released Smooth.js version 0.1.5 . In addition to adding a lot of tests and reworking some internals, we’ve got a couple spiffy new interpolation methods, scale-to-interval, and deep input validation.
New Interpolation Methods
We have two new interpolation methods: 'sinc'
and 'lanczos'
. The latter is actually a special case of the
former, as we will see.
Windowed Sinc Filters
This is actually a whole class of filters based on the
idealized construct known as the sinc filter. The true sinc filter
cannot be applied because the sinc function extends to infinity. But we can apply a window to the sinc function
to get a filter kernel that we can use for interpolation. To do this we use the 'sinc'
interpolation method.
There are two config parameters that control the sinc filter window: sincFilterSize
and sincWindow
.
sincFilterSize
determines the interval in which the filter kernel is non-zero. The default is 2,
corresponding to the interval (-2, 2).
sincWindow
is a window function that you define. It must take one argument and return a number.
Let’s see some graphs!
Here are some points in two dimensions interpolated with a sinc filter with the default interval, and a Gaussian window function:
function(x) {
return Math.exp(-x*x);
}
Here are the same points interpolated using this circular window function:
function(x) {
return Math.sqrt(1 - x*x/4);
}
Lanczos Resampling
A common and useful case of the sinc filter is
Lanczos resampling, which is a sinc filter with a Lanczos
window. Rather than writing the Lanczos window yourself, you can just use the interpolation method
'lanczos'
. The Lanczos window function will be created automatically based on the sincFilterSize
. (You
can also use the alias lanczosFilterSize
).
Here are the same points as above, interpolated with the Lanczos resampling interpolation method:
Scale to interval
The scaleTo
config parameter scales the domain of function. For example scaleTo:1
will make it so that no
matter how long your input array is, all of the values are squished into the interval [0,1].
With version 0.1.5 you can pass in a whole interval like scaleTo:[1,3]
. You can even flip the function
around by passing a flipped interval like scaleTo:[3,1]
.
Deep input validation
Previously, Smooth.js would only sanity check the first element of the input array. For vector arrays, it would only check the length of the first element. Starting in version 0.1.5, the entire input array is checked to make sure that there are no non-numeric elements, that vectors are the same size, etc.
You can switch back to minimal validation globally by setting Smooth.deepValidation = false
.