Skip to content

Commit

Permalink
Update utils.py
Browse files Browse the repository at this point in the history
  • Loading branch information
knc6 authored Sep 23, 2024
1 parent 723a6bd commit 5ea0991
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion jarvis/core/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Set of useful utility functions."""

from collections import OrderedDict
from collections import defaultdict
from scipy import sparse
from scipy.sparse.linalg import spsolve
import random
import numpy as np
import math
Expand Down Expand Up @@ -334,6 +335,34 @@ def cos_formula(a, b, c):
return np.arccos(res)


def baseline_als(y, lam, p, niter=10):
"""
Adaptive Least Squares fitting for baseline correction
Parameters:
y: array_like
Input signal
lam: float
Lambda (smoothness)
p: float
Asymmetry
niter: int, optional
Number of iterations
Returns:
array_like
The estimated baseline
"""
L = len(y)
D = sparse.diags([1,-2,1],[0,-1,-2], shape=(L,L-2))
w = np.ones(L)
for i in range(niter):
W = sparse.spdiags(w, 0, L, L)
Z = W + lam * D.dot(D.transpose())
z = spsolve(Z, w*y)
w = p * (y > z) + (1-p) * (y < z)
return z

# def is_xml_valid(xsd="jarvisdft.xsd", xml="JVASP-1002.xml"):
# """Check if XML is valid."""
# xml_file = etree.parse(xml)
Expand Down

0 comments on commit 5ea0991

Please sign in to comment.