# Visual example ----
set.seed(42)
n <- 600
t <- seq_len(n)
p1 <- 30
p2 <- 240
xy <- (1 + 0.6 * sin(t*2*pi/p2)) * sin(t*2*pi/p1) + 2 * sin(t*2*pi/p2) +
rnorm(n, sd = 0.5) + t * 0.01
inter_dt <- round(runif(length(xy), min = 0.5, max = 1.5),1)
dt <- cumsum(inter_dt)
keep <- runif(length(dt)) < 0.5
xy <- xy[keep]
dt <- dt[keep]
window <- in.window(dt, w = 30, 1:590, xy = xy)
par(mfrow = c(1,2))
plot(xy, dt, type = "o", pch = 19,
ylim = c(0,600), main = "Moving Average")
lines(rowMeans(window$xy, na.rm = TRUE), window$xout,
col = "red", lwd = 2)
plot(window$n.size, window$xout, pch = 19,
ylim = c(0,600), xlim = c(0,20), ylab = "dt",
main = "Amount of Points in Average")
# Test the boundary rule ----
x <- c(1,1,2,3,4,6,8,10,15,16)
xout <- -6:22
output <- in.window(x = x, w = 10, xout = xout, b = "]]")
test <- output$x - output$xout
see <- cbind(output$xout, output$x)
colnames(see) <- c("xout", paste0("x", seq_len(ncol(see)-1)))
test # difference between x and xout: it is contained in ]-5,5]
see
Run the code above in your browser using DataLab