2016-03-18 52 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