zeehio / MassSpecWavelet

MassSpecWavelet Bioconductor package
8 stars 2 forks source link

sav.gol filters incorrectly the first and last fl/2 points #2

Closed zeehio closed 2 years ago

zeehio commented 2 years ago

The short reprex below shows how the Savitzky-Golay implementation that we have on MassSpecWavelet does not filter properly the signal boundaries. We can see in the figure that the implementation from the signal package works well. It's not a critical issue since it just affects few points at the border, and it is just used to optionally skip some peak detection computations... but it would be nice to get it done right.

data("exampleMS", package = "MassSpecWavelet")
# Filter the signal:
filtered_with_sav_gol <- MassSpecWavelet:::sav.gol(exampleMS, fl =13, forder=2, dorder=0)
filtered_with_sgolayfilt <- signal::sgolayfilt(exampleMS, p = 2, n = 13)
# Plot
plot(exampleMS, type = "l", xlim = c(1, 24), ylim = c(300, 1300), main = "sav.gol() is wrong close to the end points")
abline(v = 7, col = "blue")
lines(filtered_with_sav_gol, col = "red", lty = 2)
lines(filtered_with_sgolayfilt, col = "darkgreen", lty = 3)
legend(
  x = "topright",
  c("Raw signal", "MassSpecWavelet:::sav.gol()", "signal::sgolayfilt()", "half of filter length"),
  col = c("black", "red", "darkgreen", "blue"), 
  lty = c(1,2,3, 1)
)

Created on 2022-04-04 by the reprex package (v2.0.1)

My plan to address this is:

The xcms package seems to be the main user of this function and these filtering options were added by Steffen Neumann, so I'm mentioning you (@sneumann) to get feedback and see if I may be missing anything.

zeehio commented 2 years ago