我的数据包括到地面的径向距离,每d_theta
均匀采样。我想对它进行高斯平滑处理,但是使平滑窗口的大小在x中保持不变,而不是恒定的点数。有什么好办法呢?
我做了一个功能,但它很慢,我甚至没有放入计算边缘的部分。
如果它有助于更快地做到这一点,我猜你可以假设地板是平的,并用它来计算采样点数,而不是使用实际的x值。
以下是我到目前为止的尝试:
bs = [gaussian(2*n-1,n/2) for n in range (1,500)] #bring the computation of the
bs = [b/b.sum() for b in bs] #gaussian outside to speed it up
def uneven_gauss_smoothing(xvals,yvals,sigma):
newy = []
for i, xval in enumerate (xvals):
#find how big the window should be to have the chosen sigma
#(or .5*sigma, whatever):
wheres = np.where(xvals> xval + sigma )[0]
iright = wheres[0] -i if len(wheres) else 100
if i - iright < 0 :
newy.append(0) #not implemented yet
continue
if i + iright >= len(xvals):
newy.append(0) #not implemented
continue
else:
#weighted average with gaussian curve:
newy.append((yvals[i-iright:i+iright+1]*bs[iright]).sum())
return np.array(newy)
对不起,这有点乱 - 调试时非常令人难以置信,我最终使用了第一个解决方案(通常是难以阅读的解决方案),这些解决方案突然出现在一些问题中。但它确实以有限的方式发挥作用。