簡體   English   中英

使用 Python 計算多個峰下的面積

[英]Calculating the area under multiple Peaks using Python

我的問題是計算 FT-IR 分析中峰下的面積。 我通常使用 Origin,但我想看看使用 Python 是否能獲得更好的結果。 我正在使用的數據在此處鏈接,代碼如下。 我面臨的問題是,我不知道如何找到峰的起點和終點來計算面積以及如何設置基線。

我發現了這個關於如何計算多個峰下面積的回答問題,但我不知道如何在我的代碼中實現它: How to get value of area under multiple peaks

import numpy as np
from numpy import trapz
import matplotlib.pyplot as plt
import pandas as pd

df = pd.read_csv(r'CuCO3.csv', skiprows=5)
print(df)
Wavenumber = df.iloc[:,0]
Absorbance = df.iloc[:,1]
Wavenumber_Peak = Wavenumber.iloc[700:916] #Where the peaks start/end that i want to calculate the area
Absorbance_Peak = Absorbance.iloc[700:916] #Where the peaks start/end that i want to calculate the area

plt.figure()
plt.plot(Wavenumber_Peak, Absorbance_Peak)
plt.show()

繪制峰以計算面積:

在此處輸入圖片說明

好的,我已經快速將另一篇文章中的代碼添加到您的開頭並檢查它是否有效。 不幸的是,您鏈接的文件不適用於您的代碼,因此我不得不在開始時更改一些內容以使其工作(以一種非常不優雅的方式,因為我真的不知道如何使用數據幀)。 如果你的本地文件不同,這樣處理文件不起作用,那么就用你的來交換我的開頭。

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import peakutils

df = pd.read_csv(r'CuCO3.csv', skiprows=5)

data = np.asarray([[float(y) for y in x[0].split(",")] for x in df.to_numpy()])
Wavenumber = np.arange(700, 916)
Absorbance = data[700:916,1]

indices = peakutils.indexes(Absorbance, thres=0.35, min_dist=0.1)
peak_values = [Absorbance[i] for i in indices]
peak_Wavenumbers = [Wavenumber[i] for i in indices]

plt.figure()
plt.scatter(peak_Wavenumbers, peak_values)
plt.plot(Wavenumber, Absorbance)
plt.show()

ixpeak = Wavenumber.searchsorted(peak_Wavenumbers)
ixmin = np.array([np.argmin(i) for i in np.split(Absorbance, ixpeak)])
ixmin[1:] += ixpeak
mins = Wavenumber[ixmin]

# split up the x and y values based on those minima
xsplit = np.split(Wavenumber, ixmin[1:-1])
ysplit = np.split(Absorbance, ixmin[1:-1])

# find the areas under each peak
areas = [np.trapz(ys, xs) for xs, ys in zip(xsplit, ysplit)]

# plotting stuff
plt.figure(figsize=(5, 7))
plt.subplots_adjust(hspace=.33)
plt.subplot(211)
plt.plot(Wavenumber, Absorbance, label='trace 0')
plt.plot(peak_Wavenumbers, Absorbance[ixpeak], '+', c='red', ms=10, label='peaks')
plt.plot(mins, Absorbance[ixmin], 'x', c='green', ms=10, label='mins')
plt.xlabel('dep')
plt.ylabel('indep')
plt.title('Example data')
plt.ylim(-.1, 1.6)
plt.legend()

plt.subplot(212)
plt.bar(np.arange(len(areas)), areas)
plt.xlabel('Peak number')
plt.ylabel('Area under peak')
plt.title('Area under the peaks of trace 0')
plt.show()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM