[英]how to apply continuum removal in spectral graph
我必須在圖表上應用連續去除,並且我使用了 scipy 凸包 function 來找到凸包,現在我必須應用連續去除。
這是代碼-
import pandas as pd
import numpy as np
from scipy.spatial import ConvexHull
import matplotlib.pyplot as plt
data=open('15C80D4_00002.txt')
d=pd.read_table(data, sep=r'\t',header=None, names=['Wvl', 'Reflectance'],skiprows=1,
engine='python')
x=d.iloc[:,:1]
a1=np.array(x)
y=d.iloc[:,1:]
b1=np.array(y)
points=np.concatenate((a1,b1), axis=1)
fig = plt.figure()
ax = fig.subplots()
hull = ConvexHull(points)
for simplex in hull.simplices:
ax.plot(points[simplex,0], points[simplex,1], 'k-')
在繪制圖表時,我得到凸包圖
如何才能做到這一點?
如果連續體移除簡單地等同於移除(插值)凸包,那么下面的代碼應該可以解決問題。 基本思想是我們添加兩個點,一個在線的每一端,它們的 y 值比任何其他點都低,因此可以保證形成凸包的“下”部分。
在凸包計算之后,我們簡單地再次刪除它們,並留下凸包的“上部”部分。 從那里您只需在給定的 x 坐標處插入船體以獲得相應的 y' 值,然后從原始 y 值中減去該值。
import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import ConvexHull
from scipy.interpolate import interp1d
def continuum_removal(points, show=False):
x, y = points.T
augmented = np.concatenate([points, [(x[0], np.min(y)-1), (x[-1], np.min(y)-1)]], axis=0)
hull = ConvexHull(augmented)
continuum_points = points[np.sort([v for v in hull.vertices if v < len(points)])]
continuum_function = interp1d(*continuum_points.T)
yprime = y - continuum_function(x)
if show:
fig, axes = plt.subplots(2, 1, sharex=True)
axes[0].plot(x, y, label='Data')
axes[0].plot(*continuum_points.T, label='Continuum')
axes[0].legend()
axes[1].plot(x, yprime, label='Data - Continuum')
axes[1].legend()
return np.c_[x, yprime]
x = np.linspace(0, 1, 100)
y = np.random.randn(len(x))
points = np.c_[x, y]
new_points = continuum_removal(points, show=True)
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.