gjmvanboxtel / gsignal

R implementation of the Octave signal package
GNU General Public License v3.0
22 stars 6 forks source link

R/filtfilt.R: Better method for reflecting signal before filtering #12

Closed rlaboiss closed 1 year ago

rlaboiss commented 1 year ago

The reflection on both sides of the signal prior to filtering can be improved by negating the signal reversed in time. This ensures the continuity of the first derivative of the resulting expanded signal, reducing the border effects.

This technique is currently used in the filtfilt function of Octave.

The improvement can be shown in the following image, for a ramp signal:

Screenshot from 2022-12-16 17-55-06

This figure was obtained with the following code:

library (gsignal)
x <- seq (0, 50)
f <- butter (3, 0.1)
plot (x, pch = 19, col = "gray")
cols = c ("#ff000080", "#0000ff80")
lines (filtfilt(f,x), col = cols [1], lwd = 3)
source ("R/filtfilt.R")
lines (filtfilt(f,x), col = cols [2], lwd = 3)
legend ("topleft", inset = 0.05, col = cols, lwd = 3,
        legend = c ("original", "improved"))