2016-03-18 54 views
1

我有一个数据数组Y,这样Y是一个独立变量X(另一个数组)的函数。Smooth circular data

X的值从0变化到360,与环绕。

Y的值而变化,从-180到180,也与环绕。

(即,这些值是在度的角度围绕一个圆。)

没有人知道在Python任何功能(在numpyscipy等)能够低通滤波我Y值作为X的功能?

在这种情况下,在所有混乱,这里的示例数据的一个情节:

enter image description here

+0

您想如何处理缠绕?如果X从359度环绕。如果Y值与001度附近的其他数据平滑,还是应该把Y值看作是在361度?同样,你想如何处理Y的环绕? – RootTwo

+0

问题说,有两个阵列的X和Y,其中Y [i]是X [I]的函数。我假设我代表时间。我的问题是,如果Y的,看起来像一个序列[...,179,180,-179,-178,177,...]它更有意义的字面上对待它,还是像它包含了[... ,179,180,181,182,183,...]。对于X也是如此。在我的回答中,我在平滑数据之前先“解开”数据。 – RootTwo

回答

1

假设您从

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, '+'); 

enter image description here

要执行循环卷积,你可以做到以下几点:

yy = np.concatenate((y, y)) 
smoothed = np.convolve(np.array([1] * 5), yy)[5: len(x) + 5] 

这使用,在每一个点上,与前5个点(含)的循环平均值。当然,还有其他的方式。

>>> plot(x, smoothed) 

enter image description here

+0

注意,这不涉及在'y'值的回转 - 不过没关系,因为在这里他们从来没有获得接近环绕点(+/- 180)值。 – dbliss

1

下面是一个使用大熊猫做均线的解决方案。第一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 
+0

我看不到平滑'X1'的感觉。这是我的自变量。那里不确定。在上面,你把“X”和“Y”看作是时间的函数,但这不是正确的思考方式。就像我说的那样,'Y'是'X'的函数。 'X'是这里的代表。 – dbliss

+0

p.s.感谢您指出'np.unwrap'。我没有意识到这一点。 – dbliss