如何平滑数组中的值(没有多项式方程)?

How do you smoothen out values in an array (without polynomial equations)?

所以基本上我有一些数据,我需要找到一种方法来平滑它(以便从中产生的线条平滑而不抖动)。当绘制出数据时,现在看起来像这样:

我希望它看起来像这样:

我尝试使用 this numpy 方法来获取直线方程,但它对我不起作用,因为图形会重复(有多个读数,因此图形上升、饱和、然后下降然后重复多次)所以没有真正的方程式可以表示。

我也试过但是还是不行,原因和上面一样。

图表定义如下:

gx = [] #x is already taken so gx -> graphx
gy = [] #same as above

#Put in data

#Get nice data #[this is what I need help with]

#Plot nice data and original data

plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()

我认为最适用于我的解决方案的方法是获取每 2 个点的平均值并将其设置为两个点的值,但这个想法不适合我 - 潜在值可能会丢失.

您可以使用无限水平过滤器

import numpy as np
import matplotlib.pyplot as plt

x = 0.85 # adjust x to use more or less of the previous value
k = np.sin(np.linspace(0.5,1.5,100))+np.random.normal(0,0.05,100)
filtered = np.zeros_like(k)
#filtered = newvalue*x+oldvalue*(1-x)
filtered[0]=k[0]
for i in range(1,len(k)):
# uses x% of the previous filtered value and 1-x % of the new value
    filtered[i] = filtered[i-1]*x+k[i]*(1-x) 

plt.plot(k)
plt.plot(filtered)
plt.show()

我想通了,通过平均 4 个结果,我能够显着平滑图表。这是一个演示:

希望对需要的人有所帮助