Skip to content
DeepForgeby svx
All problems
HardCalculusca-090

Convolution Numeric

Problem

Approximate the convolution (f * g)(t) = integral of f(tau) * g(t - tau) dtau at the grid point t = k*dt.

f[i] = f(i*dt) and g[j] = g(j*dt). Sum f[i] * g[k-i] over all i where 0 <= k - i < len(g), then multiply by dt to approximate the integral.

Hint

Only index pairs that stay inside both arrays contribute to the sum.

Test cases

case 1
in: [[1,2,3],[1,1,1],1,2]
expected: 6
case 2
in: [[1,0,0],[1,2,3],1,1]
expected: 2
case 3
in: [[1,2],[3,4],0.5,1]
expected: 5
case 4
in: [[1,1,1,1],[1,1],0.25,3]
expected: 0.5
case 5
in: [[0,0],[1,1],1,0]
expected: 0
Starter codePython
def convolution_sample(f, g, dt, k):
    # Your code here
    pass

5 test cases · run your code against them in the editor