The functions both take an optional third argument to determine the way borders
are processed. The default value, CONSTANT, will consider that the values out
of the bounds are all 0. If it is set to CUT, borders will be ignored and the
result will be smaller than te input by kernel.length - 1:
If you need to execute the convolution many times with the same kernel and input
length, you should consider instead to use the class-based API:
import { DirectConvolution, FFTConvolution } from 'ml-convolution';// const input = [0, 255, 255, 255, 255, 0, 0, 0];const kernel = [0.1, 0.2, 0.3];// First parameter is the size of the inputs and allows to pre-allocate an array with the correct sizeconst direct = new DirectConvolution(8, kernel, 'CUT');// The convolve function mutates the same array at each executiondirect.convolve([0, 255, 255, 255, 255, 0, 0, 0]); // [ 127.5, 153, 153, 76.5, 25.5, 0 ]direct.convolve([255, 0, 0
Benchmark
With small kernels, direct convolution is usually faster:
Current results suggest that from a kernel size around 64, FFT convolution should be used.