我有一个数据数组Y
,这样Y
是一个独立变量X
(另一个数组)的函数。Smooth circular data
在X
的值从0变化到360,与环绕。
在Y
的值而变化,从-180到180,也与环绕。
(即,这些值是在度的角度围绕一个圆。)
没有人知道在Python任何功能(在numpy
,scipy
等)能够低通滤波我Y
值作为X
的功能?
在这种情况下,在所有混乱,这里的示例数据的一个情节:
我有一个数据数组Y
,这样Y
是一个独立变量X
(另一个数组)的函数。Smooth circular data
在X
的值从0变化到360,与环绕。
在Y
的值而变化,从-180到180,也与环绕。
(即,这些值是在度的角度围绕一个圆。)
没有人知道在Python任何功能(在numpy
,scipy
等)能够低通滤波我Y
值作为X
的功能?
在这种情况下,在所有混乱,这里的示例数据的一个情节:
假设您从
import numpy as np
x = np.linspace(0, 360, 360)
y = 5 * np.sin(x/90. * 3.14) + np.random.randn(360)
plot(x, y, '+');
要执行循环卷积,你可以做到以下几点:
yy = np.concatenate((y, y))
smoothed = np.convolve(np.array([1] * 5), yy)[5: len(x) + 5]
这使用,在每一个点上,与前5个点(含)的循环平均值。当然,还有其他的方式。
>>> plot(x, smoothed)
注意,这不涉及在'y'值的回转 - 不过没关系,因为在这里他们从来没有获得接近环绕点(+/- 180)值。 – dbliss
下面是一个使用大熊猫做均线的解决方案。第一unwrap
的数据(需要转换为弧度和背面),因此没有不连续性(例如,跳从180到-179)。然后计算移动平均值,并根据需要最终转换回包装数据。此外,请使用np.convolve()
查看此numpy cookbook recipe。
import numpy as np
import pandas as pd
# generate random data
X = pd.Series([(x + 5*np.random.random())%360 for x in range(-100, 600, 15)])
Y = pd.Series([(y + 5*np.random.random())%360 - 180 for y in range(-200, 500, 15)])
# 'unwrap' the angles so there is no wrap around
X1 = pd.Series(np.rad2deg(np.unwrap(np.deg2rad(Y))))
Y1 = pd.Series(np.rad2deg(np.unwrap(np.deg2rad(Y))))
# smooth the data with a moving average
# note: this is pandas 17.1, the api changed for version 18
X2 = pd.rolling_mean(X1, window=3)
Y2 = pd.rolling_mean(Y1, window=3)
# convert back to wrapped data if desired
X3 = X2 % 360
Y3 = (Y2 + 180)%360 - 180
您想如何处理缠绕?如果X从359度环绕。如果Y值与001度附近的其他数据平滑,还是应该把Y值看作是在361度?同样,你想如何处理Y的环绕? – RootTwo
问题说,有两个阵列的X和Y,其中Y [i]是X [I]的函数。我假设我代表时间。我的问题是,如果Y的,看起来像一个序列[...,179,180,-179,-178,177,...]它更有意义的字面上对待它,还是像它包含了[... ,179,180,181,182,183,...]。对于X也是如此。在我的回答中,我在平滑数据之前先“解开”数据。 – RootTwo