BaseLine(去除基线漂移):
Modpoly修正多多项式拟合
IModPoly改进的ModPoly,它解决了ModPoly中的噪声问题
ZhangFitZhang fit[3],它不需要任何用户干预和先验信息,例如检测到的峰值。
python库通过ModPoly、IModPoly或Zhang-fit算法进行基线减法来处理光谱数据。函数将返回基线减去的频谱。
from BaselineRemoval import BaselineRemoval
def baseline_tools(input_array):
# input_array = [10, 20, 1.5, 5, 2, 9, 99, 25, 47]
polynomial_degree = 8 # only needed for Modpoly and IModPoly algorithm
baseObj = BaselineRemoval(input_array)
Modpoly_output = baseObj.ModPoly(polynomial_degree)
Imodpoly_output = baseObj.IModPoly(polynomial_degree)
Zhangfit_output = baseObj.ZhangFit()
plt.plot(Modpoly_output)
plt.show()
return Modpoly_output
来自:Python BaselineRemoval包_程序模块 - PyPI - Python中文网 (cnpython.com)
上图,所有数据都拉到0以上的同一个坐标下。
寻峰:findpeak
准确率可以,效率很慢,数据量打了不建议使用
import numpy as np
import matplotlib.pyplot as plt
def readtxt(path):
with open(path,'r') as f:
str=f.readline()
list = str.split(' ')
list1=[];
for i,x in enumerate(list):
if ((i%5 == 0) or (i%5 == 1) or (i%5 == 2)) and x !='':
list1.append(float(x))
return list1
def AMPD(data):
"""
实现AMPD算法
:param data: 1-D numpy.ndarray
:return: 波峰所在索引值的列表
"""
p_data = np.zeros_like(data, dtype=np.int32)
count = data.shape[0]
arr_rowsum = []
for k in range(1, count // 2 + 1):
row_sum = 0
for i in range(k, count - k):
if data[i] > data[i - k] and data[i] > data[i + k]:
row_sum -= 1
arr_rowsum.append(row_sum)
min_index = np.argmin(arr_rowsum)
max_window_length = min_index
for k in range(1, max_window_length + 1):
for i in range(k, count - k):
if data[i] > data[i - k] and data[i] > data[i + k]:
p_data[i] += 1
return np.where(p_data == max_window_length)[0]
效率快,from scipy.signal import find_peaks
#进行寻峰
peaks,value=signal.find_peaks(ldata,height=2000,distance=117)
ldata:数据样本、height:寻峰允许设置的最低高度、distance:横坐标之间最小距离