
Last chance! 50% off unlimited learning
Sale ends in
# ---- 1D convolution ------------------------------------
x <- cumsum(rnorm(100))
filter <- dnorm(-2:2)
# normalize
filter <- filter / sum(filter)
smoothed <- convolve_signal(x, filter)
plot(x, pch = 20)
lines(smoothed, col = 'red')
# ---- 2D convolution ------------------------------------
x <- array(0, c(100, 100))
x[
floor(runif(10, min = 1, max = 100)),
floor(runif(10, min = 1, max = 100))
] <- 1
# smooth
kernel <- outer(dnorm(-2:2), dnorm(-2:2), FUN = "*")
kernel <- kernel / sum(kernel)
y <- convolve_image(x, kernel)
oldpar <- par(mfrow = c(1,2))
image(x, asp = 1, axes = FALSE, main = "Origin")
image(y, asp = 1, axes = FALSE, main = "Smoothed")
par(oldpar)
Run the code above in your browser using DataLab