2
$\begingroup$

I am struggling to get the correct answer for the simple calculation of convolution in R.

The convolution of $f(t) = e^{-t}$ and $g(t) = \sin(t)$ is:

$$ (f * g)(t) = 1/2 \left( e^{-t} + \sin(t) - \cos(t) \right) $$

Source: page 12 of https://users.math.msu.edu/users/gnagy/teaching/12-spring/mth235/w09-235-p.pdf

In R, convolution can be performed like so (source: https://www.rdocumentation.org/packages/stats/versions/3.6.2/topics/convolve):

t = seq(0, 0.1, 0.01)
f = exp(-t)
g = sin(t)

res = zapsmall(convolve(g, rev(f), type='open'))
expected = zapsmall(0.5*(exp(-t) + sin(t) - cos(t)))

However, expected and res are completely different. I don't understand why I'm getting such different results.

$\endgroup$
1
  • $\begingroup$ There shouldn't be any correspondence between your code and that formula, because that formula concerns functions defined on the positive reals and the code represents functions defined only on the interval $[0,0.1].$ Of course the results differ! However, you can make it work, as in this example: t = seq(0, (2^10-1)) * (d <- 0.01); res = zapsmall(convolve(sin(t), rev(exp(-t)), type='open')); expected = zapsmall(0.5*(exp(-t) + sin(t) - cos(t))); plot(expected); lines(d * res[seq_along(expected)], col = "Red", lwd = 2) $\endgroup$
    – whuber
    Commented Jun 15, 2023 at 13:40

0