噪声数据中的梯度,python

2024-05-29 10:35:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个宇宙射线探测器的能谱。光谱遵循指数曲线,但会有很宽(可能很小)的肿块。显然,这些数据包含了一个噪声元素。

我试着把数据平滑,然后画出它的梯度。 到目前为止,我一直在使用scipy sline函数来平滑它,然后使用np.gradient()。

从图中可以看出,梯度函数的方法是找出每个点之间的差异,它并不能很清楚地显示肿块。

我基本上需要一个平滑的梯度图。任何帮助都将是惊人的!

我尝试过两种样条曲线方法:

def smooth_data(y,x,factor):
    print "smoothing data by interpolation..."
    xnew=np.linspace(min(x),max(x),factor*len(x))
    smoothy=spline(x,y,xnew)
    return smoothy,xnew

def smooth2_data(y,x,factor):
    xnew=np.linspace(min(x),max(x),factor*len(x))
    f=interpolate.UnivariateSpline(x,y)
    g=interpolate.interp1d(x,y)
    return g(xnew),xnew

编辑:尝试数值微分:

def smooth_data(y,x,factor):
    print "smoothing data by interpolation..."
    xnew=np.linspace(min(x),max(x),factor*len(x))
    smoothy=spline(x,y,xnew)
    return smoothy,xnew

def minim(u,f,k):
    """"functional to be minimised to find optimum u. f is original, u is approx"""
    integral1=abs(np.gradient(u))
    part1=simps(integral1)
    part2=simps(u)
    integral2=abs(part2-f)**2.
    part3=simps(integral2)
    F=k*part1+part3
    return F


def fit(data_x,data_y,denoising,smooth_fac):
    smy,xnew=smooth_data(data_y,data_x,smooth_fac)
    y0,xnnew=smooth_data(smy,xnew,1./smooth_fac)
    y0=list(y0)
    data_y=list(data_y)
    data_fit=fmin(minim, y0, args=(data_y,denoising), maxiter=1000, maxfun=1000)
    return data_fit

但是,它只是返回相同的图形!

Data, smoothed data and gradients


Tags: datalenreturndefnpminmax梯度
3条回答

我不保证这篇文章的数学有效性;看起来EOL引用的LANL的论文值得一看。不管怎样,在使用splev时,我使用SciPy样条的内置微分得到了不错的结果。

%matplotlib inline
from matplotlib import pyplot as plt
import numpy as np
from scipy.interpolate import splrep, splev

x = np.arange(0,2,0.008)
data = np.polynomial.polynomial.polyval(x,[0,2,1,-2,-3,2.6,-0.4])
noise = np.random.normal(0,0.1,250)
noisy_data = data + noise

f = splrep(x,noisy_data,k=5,s=3)
#plt.plot(x, data, label="raw data")
#plt.plot(x, noise, label="noise")
plt.plot(x, noisy_data, label="noisy data")
plt.plot(x, splev(x,f), label="fitted")
plt.plot(x, splev(x,f,der=1)/10, label="1st derivative")
#plt.plot(x, splev(x,f,der=2)/100, label="2nd derivative")
plt.hlines(0,0,2)
plt.legend(loc=0)
plt.show()

matplotlib output

在这个:Numerical Differentiation of Noisy Data上发布了一个有趣的方法。它应该能给你一个解决问题的好办法。更多细节在另一个accompanying paper中给出。作者还提供了Matlab code that implements it;另一个implementation in Python也可用。

如果您想使用样条函数进行插值,我建议您调整scipy.interpolate.UnivariateSpline()的平滑因子s

另一个解决方案是通过卷积来平滑函数(比如用高斯函数)。

我在论文中联系了一些声明,以防止出现卷积方法(样条方法可能会遇到类似的困难)的一些伪影。

您也可以使用^{}

结果

enter image description here

示例

import matplotlib.pyplot as plt
import numpy as np
import scipy
from random import random

# generate data
x = np.array(range(100))/10
y = np.sin(x) + np.array([random()*0.25 for _ in x])
dydx = scipy.signal.savgol_filter(y, window_length=11, polyorder=2, deriv=1)

# Plot result
plt.plot(x, y, label='Original signal')
plt.plot(x, dydx*10, label='1st Derivative')
plt.plot(x, np.cos(x), label='Expected 1st Derivative')
plt.legend()
plt.show()

相关问题 更多 >

    热门问题