我怎样才能摆脱速度测量过程中不可预测的速度峰值?

How can i get rid of unpredictable speed peaks during speed measurements?

我已经为用户速度编写了服务tracking.This服务正在使用Core Location框架来跟踪地理点并计算用户速度。

服务示例:

private func processLocation(_ current:CLLocation) {
    guard let location = lastLocation else {
        lastLocation = current
        speed.value = 0
        return
    }
    var speed = current.speed
    if (speed > 0) {
        speed.value = speed
    } else {
        speed = location.distance(from: current) / (current.timestamp.timeIntervalSince(location.timestamp))
        speed.value = speed
    }
    lastLocation = current
}

此服务的工作精度可以接受。但是有一种情况,当你静止不动时,这项服务会收到很多不可预测的峰值。

当我静止时记录。

Speed: 0.249200397696353
Speed: 0.778375328912623
Speed: 6.99212664940017 -> peak
Speed: 4.91809055561385 -> peak
Speed: 0.701735999364708
Speed: 0.025146066057472
Speed: 0.0233857682731226
Speed: 12.7814687721084 -> peak
Speed: 0.61632553168542
Speed: 7.37520678279276 -> peak
Speed: 0.023421072500409
Speed: 0.0343784939631481
Speed: 0.471982071125438
Speed: 0.0207671001927932
Speed: 0.0217459598583271
Speed: 0.0394697185852203
Speed: 0.439568634647097
Speed: 14.1348693612176 -> peak
Speed: 6.29588775714151 -> peak
Speed: 5.55254459904619 -> peak 
Speed: 0.218587071425142

如何摆脱此速度跟踪服务中不可预测的峰值?我应该更好地配置 Core Location 管理器吗?

我建议使用 中值滤波器 来减少数据峰值。定义检测峰值的阈值并对其应用过滤器。参考这篇文档https://homepages.inf.ed.ac.uk/rbf/HIPR2/median.htm