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.
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$