lnacquaroli / SavitzkyGolay.jl

Implementation of the 1D Savitzky-Golay filter in JuliaLang
MIT License
21 stars 4 forks source link

Improvements to Savitzky Golay filter #29

Closed langestefan closed 1 month ago

langestefan commented 1 month ago

Hello, thank you for this great implementation, I have been using it and it is very useful.

Significant improvements to the filter can be done by applying a weighting function to the residuals when doing the polynomial fit, see for example this paper: An Optimal Weighting Function for the Savitzky-Golay Filter

See also the section on Wikipedia that describes this result: Weighting the data

It seems like this could be easily added to the package and get a much better result. Probably in this section of the code:

function _coefficients(V::Matrix{T1}, order_range::UnitRange{T2}, p::SGolay) where {T1 <: Float64, T2 <: Int64}
    Vqr = qr(V')
    c = Vqr.R \ (Vqr.Q' * _onehot(p.deriv + 1, length(order_range)))
    c .*= (p.rate)^(p.deriv) * factorial(p.deriv)
    return reverse(c)
end

Do you think this would be feasible to add as a feature?

lnacquaroli commented 1 month ago

Hello there, thanks for reaching out. Do you think you can come up with a PR for it? At this time, I can't promise any prompt implementation, it'll remain on my TODO list.

lnacquaroli commented 1 month ago

23e2153aa3a394c83ed028e467ceed9e15bc5b7d