光滑圆d

2024-05-01 22:12:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据数组Y,这样Y是自变量{}(另一个数组)的函数。在

X中的值从0到360不等,并有环绕。在

Y中的值从-180到180不等,也有环绕。在

(也就是说,这些值是围绕一个圆的角度(以度为单位)

有没有人知道Python(在numpyscipy等)中有任何函数能够将我的Y值作为X的函数进行低通过滤?在

如果这有点令人困惑,下面是一个示例数据图:

enter image description here


Tags: 数据函数numpy示例单位scipy数组角度
2条回答

假设你从

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

要执行循环卷积,可以执行以下操作:

^{pr2}$

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

>>> plot(x, smoothed)

enter image description here

这里有一个用熊猫做移动平均线的解决方案。首先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

相关问题 更多 >