BaseLine (remove baseline drift):
Modpoly modified polynomial fitting
ModPoly improved by IModPoly , which solves the noise problem in ModPoly
ZhangFit Zhang fit [3], which does not require any user intervention and prior information such as detected peaks.
A python library for processing spectral data by baseline subtraction via ModPoly, IModPoly or Zhang-fit algorithms. The function will return the baseline-subtracted spectrum.
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
From: Python BaselineRemoval Package_Program Modules - PyPI - Python Chinese Network (cnpython.com)
In the figure above, all the data are pulled to the same coordinate above 0.
Find peak: findpeak
The accuracy rate is good, but the efficiency is very slow. It is not recommended to use the data volume.
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]
Fast efficiency, from scipy.signal import find_peaks
#进行寻峰
peaks,value=signal.find_peaks(ldata,height=2000,distance=117)
ldata: data sample, height: the minimum height allowed to be set for peak finding, distance: the minimum distance between abscissas