Skip to content

Instantly share code, notes, and snippets.

@lnacquaroli
Last active May 15, 2024 02:12
Show Gist options
  • Save lnacquaroli/e251ab8309da23f328f8bd350dc246c4 to your computer and use it in GitHub Desktop.
Save lnacquaroli/e251ab8309da23f328f8bd350dc246c4 to your computer and use it in GitHub Desktop.
Baseline correction on data
"""
Asymmetric Least Squares Smoothing by P. Eilers and H. Boelens in 2005
For a more options and detailed implementation, see https://github.com/charlesll/Spectra.jl/blob/master/src/baseline.jl
Requires using LinearAlgebra and SparseArrays
"""
function baselineCorrection(y, lam, p, ninter=10)
L = length(y)
eyeL = Matrix(1.0I, L, L)
D = sparse(diff(diff(eyeL, dims=2), dims=2))
w = ones(L)
q = 0.0
for _ in 1 : ninter
W = spdiagm(0 => w)
q = \(W + lam * D * D', w.*y)
w = p * (y .> q) + (1 - p) * (y .< q)
end
return q
end
@mithgil
Copy link

mithgil commented May 14, 2024

Thanks for your Julia version of ALS smoothing. I notice that the niter should agree with the parameter ninter in your code.

@lnacquaroli
Copy link
Author

Ups, thanks for catching that up. Corrected!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment