# Compare a 5 sample averager, an order-5 butterworth lowpass
# filter (cutoff 1/3) and sgolayfilt(x, 3, 5), the best cubic
# estimated from 5 points.
bf <- butter(5, 1/3)
x <- c(rep(0, 15), rep(10, 10), rep(0, 15))
sg <- sgolayfilt(x)
plot(sg, type="l", xlab = "", ylab = "")
lines(filtfilt(rep(1, 5) / 5, 1, x), col = "red") # averaging filter
lines(filtfilt(bf, x), col = "blue") # butterworth
points(x, pch = "x") # original data
legend("topleft", c("sgolay (3,5)", "5 sample average", "order 5
Butterworth", "original data"), lty=c(1, 1, 1, NA),
pch = c(NA, NA, NA, "x"), col = c(1, "red", "blue", 1))
Run the code above in your browser using DataLab