python学到哪知道baseline_Python baseline correction library

可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):

问题:

I am currently working with some Raman Spectra data, and I am trying to correct my data caused by florescence skewing. Take a look at the graph below:

I am pretty close to achieving what I want. As you can see, I am trying to fit a polynomial in all my data whereas I should really just be fitting a polynomial at the local minimas.

Ideally I would want to have a polynomial fitting which when subtracted from my original data would result in something like this:

Are there any built in libs that does this already?

If not, any simple algorithm one can recommend for me?

回答1:

I found an answer to my question, just sharing for everyone who stumbles upon this.

There is an algorithm called "Asymmetric Least Squares Smoothing" by P. Eilers and H. Boelens in 2005. The paper is free and you can find it on google. def baseline_als(y, lam, p, niter=10): L = len(y) D = sparse.csc_matrix(np.diff(np.eye(L), 2)) w = np.ones(L) for i in xrange(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

回答2:

I know this is an old question, but I stumpled upon it a few months ago and implemented the equivalent answer using spicy.sparse routines. # Baseline removal def baseline_als(y, lam, p, niter=10): s = len(y) # assemble difference matrix D0 = sparse.eye( s ) d1 = [numpy.ones( s-1 ) * -2] D1 = sparse.diags( d1, [-1] ) d2 = [ numpy.ones( s-2 ) * 1] D2 = sparse.diags( d2, [-2] ) D = D0 + D2 + D1 w = np.ones( s ) for i in range( niter ): W = sparse.diags( [w], [0] ) Z = W + lam*D.dot( D.transpose() ) z = spsolve( Z, w*y ) w = p * (y > z) + (1-p) * (y < z) return z

Cheers,

Pedro.

回答3:

The following code works on Python 3.6.

This is adapted from the accepted correct answer to avoid the dense matrix diff computation (which can easily cause memory issues) and uses range (not xrange) import numpy as np from scipy import sparse from scipy.sparse.linalg import spsolve def baseline_als(y, lam, p, niter=10): 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值