Consider the following (derived from signal::filter
help page):
require(signal)
bf <- butter(5, 0.1)
t <- seq(0, 1, len = 100)
x <- sin(2*pi*t*2.3) + 0.25*rnorm(length(t)) + 5
y <- filtfilt(bf, x)
z <- filter(bf, x)
plot(t, x, ylim = c(0, max(x)))
lines(t, y, col="red")
lines(t, z, col="blue")
lines(t, stats::filter(x, rep(1/10, 10)), col = "green")
legend("bottomright", legend = c("data", "filtfilt", "filter", "SMA"),
pch = 1, col = c("black", "red", "blue", "green"), bty = "n")
It can be seen that both red and blue (filter and filtfilt, that is) originate in (0,0). However, I would like them to start at a given initial value, such as init = mean(x[1:10])
. No matter how I provide said constrain in filter(bf, x)
, be it integer or vector, I either get no result or length of 'init' must equal length of 'filter'
. As a comparison for desired output, a simple moving average (green) is provided. Thank you.