python用在化学上_在Python上使用savgol_过滤器平滑在线数据化学信号图书馆

谢谢你更新问题!在

问题是,对于您的online_res方法,您缺少部分数据。边缘值由scipy的savgol_filter处理,但不适用于手工编码的版本。在

对于您的示例,请看两个结果:

“联机分辨率”:数组([3.93,3.17,0.73,0.2,1.11,5.87,6.37]))

“脱机分辨率”:数组([1.84,3.52,3.93,3.17,0.73,0.2,1.11,5.87,6.37,5.3,1.84]))

它们是相同的,但是offline res处理了data[0:2]和{}的值。在您的例子中,如果指定了不特定的mode,则将其设置为默认值interpolate:When the ‘interp’ mode is selected (the default), no extension is

used. Instead, a degree polyorder polynomial is fit to the last

window_length values of the edges, and this polynomial is used to

evaluate the last window_length // 2 output values.

这不是你为你的online res所做的。在

我为两边实现了一个简单的polynomial fit,得到了完全相同的结果:from queue import Queue, Empty

import numpy as np

from scipy.signal import savgol_filter

window_size = 5

data = list()

q = Queue()

d = [2.22, 2.22, 5.55, 2.22, 1.11, 0.01, 1.11, 4.44, 9.99, 1.11, 3.33]

for i in d:

q.put(i)

res = list()

while not q.empty():

element = q.get()

data.append(element)

length = len(data)

npd = np.array(data[length - window_size:])

if length >= window_size:

res.append(savgol_filter(npd, window_size, 2)[window_size//2])

# calculate the polynomial fit for elements 0,1,2,3,4

poly = np.polyfit(range(window_size), d[0:window_size], deg=2)

p = np.poly1d(poly)

res.insert(0, p(0)) # insert the polynomial fits at index 0 and 1

res.insert(1, p(1))

# calculate the polynomial fit for the 5 last elements (range runs like [4,3,2,1,0])

poly = np.polyfit(range(window_size-1, -1, -1), d[-window_size:], deg=2)

p = np.poly1d(poly)

res.append(p(1))

res.append(p(0))

npd = np.array(data)

res2 = savgol_filter(npd, window_size, 2)

diff = res - res2 # in your example you were calculating the wrong diff btw

np.set_printoptions(precision=2)

print('source data ', npd)

print('online res ', np.array(res))

print('offline res ', res2)

print('error ', diff.sum())

结果:

^{pr2}$

编辑:

这个版本独立于d-list,这意味着它可以消化从源代码获取的任何数据。在window_size = 5

half_window_size = window_size // 2 # this variable is used often

data = list()

q = Queue()

d = [2.22, 2.22, 5.55, 2.22, 1.11, 0.01, 1.11, 4.44, 9.99, 1.11, 3.33]

for i in d:

q.put(i)

res = [None]*window_size # create list of correct size instead of appending

while not q.empty():

element = q.get()

data.append(element)

length = len(data)

npd = np.array(data[length - window_size:])

if length == window_size: # this is called only once, when reaching the filter-center

# calculate the polynomial fit for elements 0,1,2,3,4

poly = np.polyfit(range(window_size), data, deg=2)

p = np.poly1d(poly)

for poly_i in range(half_window_size): # independent from window_size

res[poly_i] = p(poly_i)

# insert the sav_gol-value at index 2

res[(length-1)-half_window_size] = savgol_filter(npd, window_size, 2)[half_window_size]

poly = np.polyfit(range(window_size - 1, -1, -1), data[-window_size:], deg=2)

p = np.poly1d(poly)

for poly_i_end in range(half_window_size):

res[(window_size-1)-poly_i_end] = p(poly_i_end)

elif length > window_size:

res.append(None) # add another slot in the res-list

# overwrite poly-value with savgol

res[(length-1)-half_window_size] = savgol_filter(npd, window_size, 2)[half_window_size]

# extrapolate again into the future

poly = np.polyfit(range(window_size - 1, -1, -1), data[-window_size:], deg=2)

p = np.poly1d(poly)

for poly_i_end in range(half_window_size):

res[-poly_i_end-1] = p(poly_i_end)


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部