1function convolve(array, weights) {
2 if (weights.length % 2 !== 1)
3 throw new Error('weights array must have an odd length');
4
5 var al = array.length;
6 var wl = weights.length;
7 var offset = ~~(wl / 2);
8 var output = new Array(al);
9
10 for (var i = 0; i < al; i++) {
11 var kmin = (i >= offset) ? 0 : offset - i;
12 var kmax = (i + offset < al) ? wl - 1 : al - 1 - i + offset;
13
14 output[i] = 0;
15 for (var k = kmin; k <= kmax; k++)
16 output[i] += array[i - offset + k] * weights[k];
17 }
18
19 return output;
20}