python - 从 gpx 数据中滤除噪音

标签 python pandas scipy noise

我有一个带有速度列的 Pandas 数据框,其中偶尔有噪音(数据来自 Garmin,代表运行期间捕获的数据)。

我正在尝试找到一种对相邻点进行平均的方法,但是当我遇到这样的事情时

9.112273445
164.5779550738
84.4553498412
4.231089359
4.3740439706

我陷入了无限循环。

我的算法相当幼稚:

# Get list of indices in which value is great than 6:
idx = z[(z['speed']>=6)].index
while list(idx) != []:
    for i in idx:
        # check if out of bounds
        if i + 1 >= len(z):
            z.iloc[i, z.columns.get_indexer(['speed'])] = (z['speed'].ix[i-2] + z['speed'].ix[i-1])/2
        elif i - 1 < 0:
            z.iloc[i, z.columns.get_indexer(['speed'])] = (z['speed'].ix[i+1] + z['speed'].ix[i+2])/2
        else:
            z.iloc[i, z.columns.get_indexer(['speed'])] = (z['speed'].ix[i-1] + z['speed'].ix[i+1])/2
    idx = z[(z['speed']>=6)].index

当然,问题是当我有两个非常大的相邻值时,就会陷入无限循环。

我正在应用此过滤器(使用汉宁窗)来消除随机噪声:SciPy Cookbook SignalSmooth ,但它没有处理数据中的这些大峰值。

除了丢弃它们或将它们设置为恒定值之外,还有其他简单的方法来处理它们吗?

编辑

我正在测试的值是:

0           NaN
1      3.508394
2      5.097879
3      7.743824
4      9.138245
5     13.315918
6     12.836310
7     12.001393
8     15.815223
9      0.000000
10    16.622944
11     9.061864
12     2.089729
13     2.710874
Name: speed, dtype: float64

最佳答案

如果您想“桥接”大于六的值,您可以这样做:

import numpy as np

# locate outliers and adjacent values
outliers = np.r_[False, (~np.isfinite(data)) | (data > 6), False]
if np.any(outliers):
    boundaries = np.where(outliers[:-1] != outliers[1:])[0]
    lb = boundaries[::2]
    rb = boundaries[1::2]
    # special case if leftmost and/or rightmost values are outliers 
    lv = data[lb-1]
    if lb[0] == 0:
        lv[0] = data[rb[0]]
    rv = data[rb % len(data)]
    if rb[-1] == len(data):
        rv[-1] = data[lb[-1]-1]
    # create fill values; use a bit of trickery to keep it vectorised
    lengths = rb-lb
    fv = np.repeat((rv-lv)/(lengths+1), lengths)
    sw = np.cumsum(lengths[:-1])
    fv[sw] += fv[sw-1] - rv[:-1] + lv[1:]
    fv[0] += lv[0]
    fv = np.cumsum(fv)
    # place them
    out = data.copy()
    out[outliers[1:-1]] = fv
else:
    out = data.copy()

关于python - 从 gpx 数据中滤除噪音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43083342/

相关文章:

python - 从两个现有的字典创建一个字典

python - SQLAlchemy + pyTelegramBotAPI : SQLite objects created in a thread can only be used in that same thread

python - 比较 Pandas 中的相对开始日期

python - 使用 pandas resample/rolling_sum 计算秒时间间隔

python - 如何解释networkx.adjacency_matrix中的indptr?

Python+SQLAlchemy : Getting mapper class from InstrumentedList?

python - 有没有办法在引用原始列表的同时将列表元素转换为自变量?

python - 动态更改回顾期

python - Scipy:尝试写入 wav 文件,AttributeError: 'list' 对象没有属性 'dtype'

numpy - 在 Pandas 中获取所有分类列的优雅方法?