############### #1. filtfiltx (Roman Murelka) #this is an emulation of Matlab filtfilt (as R filtfilt executes a little differently. See http://www.climateaudit.org/?p=4038#comment-304170 filtfiltx = function( b, a, x = NULL ) { if (is.null(x)) { x = a a = b$a b = b$b } len = length(x) nfilt = max(length(b),length(a)) nfact = 3*(nfilt-1) # padding size if (len <= nfact) stop("Data must have length more than 3 times filter order.") # Not enough data b = c(b, rep(0,nfilt))[1:nfilt] # zero-pad shorter of b and a a = c(a, rep(0,nfilt))[1:nfilt] # ditto y = c(2*x[1]-rev(x[2:(nfact+1)]), x, 2*x[len]-rev(x[(len-nfact):(len-1)])) y = rev(y[1] + filter(b,a,y-y[1])) y = rev(y[1] + filter(b,a,y-y[1])) y[(nfact+1):(len+nfact)] }