I need to convolve two one dimensional signals, one has on average 500 points (This one is a Hanning window function), the other 125000. Per run, I need to apply three times the convolution operation. I already have an implementation running based on the documentation of scipy. You can see the code here if you want to (Delphi code ahead):
function Convolve(const signal_1, signal_2 : ExtArray) : ExtArray;
var
capital_k : Integer;
capital_m : Integer;
smallest : Integer;
y : ExtArray;
n : Integer;
k : Integer;
lower, upper : Integer;
begin
capital_k := Length(signal_1) + Length(signal_2) - 1;
capital_m := Math.Max(Length(signal_1), Length(signal_2));
smallest := Math.Min(Length(signal_1), Length(signal_2));
SetLength(y, capital_k);
for n := 0 to Length(y) - 1 do begin
y[n] := 0;
lower := Math.Max(n - capital_m, 0);
upper := Math.Min(n, capital_k);
for k := lower to upper do begin
if (k >= Length(signal_1)) or (n - k >= Length(signal_2)) then
Continue;
y[n] := y[n] + signal_1[k] * signal_2[n - k];
end;
end;
Result := Slice(y,
Floor(smallest / 2) - 1,
Floor(smallest / 2) - 1 + capital_m);
end;
The problem is, this implementation is too slow. The whole procedure takes about five minutes. I was wondering if I can find a faster way of computing that.
Fast convolution can be carried out using FFTs. Take the FFT of both input signals (with appropriate zero padding), multiply in the frequency domain, then do an inverse FFT. For large N (typically N > 100) this is faster than the direct method.