-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbaseline.py
80 lines (67 loc) · 2.82 KB
/
baseline.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 30 16:05:47 2015
@author: M Wieser
LICENCE
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
Original source:
https://irfpy.irf.se/projects/ica/_modules/irfpy/ica/baseline.html
"""
import numpy as np
from scipy.sparse.linalg import spsolve
from scipy import sparse
def als(y, lam=1e5, p=0.01, itermax=10):
r"""
Implements an Asymmetric Least Squares Smoothing
baseline correction algorithm (P. Eilers, H. Boelens 2005)
Baseline Correction with Asymmetric Least Squares Smoothing
based on https://github.com/vicngtor/BaySpecPlots
Baseline Correction with Asymmetric Least Squares Smoothing
Paul H. C. Eilers and Hans F.M. Boelens
October 21, 2005
Description from the original documentation:
Most baseline problems in instrumental methods are characterized by a
smooth baseline and a superimposed signal that carries the analytical
information: a series of peaks that are either all positive or all
negative. We combine a smoother with asymmetric weighting of deviations
from the (smooth) trend get an effective baseline estimator.
It is easy to use, fast and keeps the analytical peak signal intact.
No prior information about peak shapes or baseline (polynomial) is needed
by the method. The performance is illustrated by simulation and
applications to real data.
Inputs:
y:
input data (i.e. chromatogram of spectrum)
lam:
parameter that can be adjusted by user. The larger lambda is,
the smoother the resulting background, z
p:
wheighting deviations. 0.5 = symmetric, <0.5: negative
deviations are stronger suppressed
itermax:
number of iterations to perform
Output:
the fitted background vector
"""
L = len(y)
#D = sparse.csc_matrix(np.diff(np.eye(L), 2))
D = sparse.eye(L, format='csc')
D = D[1:] - D[:-1] #numpy.diff( ,2) does not work with sparse matrix. This is a workaround.
D = D[1:] - D[:-1]
D = D.T
w = np.ones(L)
for i in range(itermax):
W = sparse.diags(w, 0, shape=(L, L))
Z = W + lam * D.dot(D.T)
z = spsolve(Z, w * y)
w = p * (y > z) + (1 - p) * (y < z)
return z