function convolve(signal, filter) {
let outputLen = signal.length * 2;
let newSig = new Array(outputLen);
let sum = 0;
let signalMax = Math.max(Math.max(...signal), 1);
let filterMax = Math.max(Math.max(...filter), 1);
let normSignal = signal.map((e) => e / signalMax);
let normFilter = filter.map((e) => e / filterMax);
for (let i = 0; i < outputLen; i++) {
for (let j = Math.max(0, i - signal.length); j <= i; j++) {
let jInBounds = j < signal.length && i - j < filter.length;
if (jInBounds) {
sum += signal[j] * normFilter[i - j];
}
}
newSig[i] = sum;
sum = 0;
}
let begin = Math.floor(outputLen * 0.25);
let end = Math.floor(outputLen * 0.75);
return newSig.slice(begin, end);
}