【问题标题】:how to apply continuum removal in spectral graph如何在光谱图中应用连续去除
【发布时间】:2022-08-18 22:56:10
【问题描述】:

我必须在图上应用连续去除,并且我使用 scipy 凸包函数来找到凸包,现在我必须应用连续去除。

这是代码-

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-\')

在绘制图表时我得到convex hull graph

  1. 我不想要底线,只想要上半部分
  2. 我希望图形像这张图片,图形应该在同一轴上after continuum removal

    如何才能做到这一点?

标签: scipy convex-hull


【解决方案1】:

如果连续体移除简单地等同于移除(插值)凸包,那么下面的代码应该可以解决问题。基本思想是我们添加两个点,一个在线的每一端,它们的 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[np.sort([v for v in hull.vertices if v < len(points)])]
    approximation = interp1d(*continuum.T)
    yprime = y - approximation(x)

    if show:
        fig, axes = plt.subplots(2, 1, sharex=True)
        axes[0].plot(x, y, label='Data')
        axes[0].plot(*continuum.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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-06
    • 1970-01-01
    • 2012-07-15
    • 1970-01-01
    • 2018-12-09
    相关资源
    最近更新 更多