噪声数据中的渐变,python [英] Gradient in noisy data, python

查看:20
本文介绍了噪声数据中的渐变,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

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

推荐答案

上面发布了一个有趣的方法:Numerical Differentiation of Noisy Data。它应该会为你的问题提供一个很好的解决方案。更多细节见另一篇accompanying paper。作者还提供了Matlab code that implements it;也可以选择implementation in Python

如果您想使用样条法,我建议调整scipy.interpolate.UnivariateSpline()的平滑系数s

另一种解决方案是通过卷积使函数平滑(例如使用高斯函数)。

我链接到的论文声称要防止卷积方法产生的一些伪影(样条法可能会遇到类似的困难)。

这篇关于噪声数据中的渐变,python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆