【问题标题】:How to smooth a curve as function of distance如何平滑曲线作为距离的函数
【发布时间】:2021-07-03 06:35:43
【问题描述】:

我有一个模拟结果曲线,它是 y = f(x),它在某个距离间隔之间有一些振荡。我喜欢通过沿 x 应用一些移动平均线来平滑振荡。怎么做?

我知道的移动平均是时间平均。距离平均呢?谢谢

【问题讨论】:

  • 您需要将沿 f(x) 的每个 x 的累积距离计算到一个单独的数组中,并使用它们来获取滚动索引。您应该在问题中提供 x 和 y 的示例数组以及您已经尝试过的内容

标签: python curve-fitting smoothing


【解决方案1】:

如果您使用的是 python,那么有很多选择:

Savitzky-Golay Filter:

import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import savgol_filter

x = np.linspace(0,2*np.pi,100)
y = np.sin(x) + np.random.random(100) * 0.2
yhat = savitzky_golay(y, 51, 3) # window size 51, polynomial order 3

plt.plot(x,y)
plt.plot(x,yhat, color='red')
plt.show()

B-spline:

from scipy.interpolate import make_interp_spline, BSpline

#create data
x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
y = np.array([4, 9, 12, 30, 45, 88, 140, 230])

#define x as 200 equally spaced values between the min and max of original x 
xnew = np.linspace(x.min(), x.max(), 200) 

#define spline
spl = make_interp_spline(x, y, k=3)
y_smooth = spl(xnew)

#create smooth line chart 
plt.plot(xnew, y_smooth)
plt.show()

polynomial approximation:

import numpy as np
import matplotlib.pyplot as plt

plt.figure()
poly = np.polyfit(list_x,list_y,5)
poly_y = np.poly1d(poly)(list_x)
plt.plot(list_x,poly_y)
plt.plot(list_x,list_y)
plt.show()

【讨论】:

  • 哇,例如先生,这太棒了,这正是我要找的。对此,我真的非常感激。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-13
  • 2021-05-25
  • 1970-01-01
  • 1970-01-01
  • 2021-11-28
  • 2022-10-12
  • 1970-01-01
相关资源
最近更新 更多