快速地以波动的时间间隔捕获固定的时间间隔捕获的线性重采样数据点 [英] Linear resampling datapoints captured at fluctuating time intervals, to flxed time intervals, in swift

查看:107
本文介绍了快速地以波动的时间间隔捕获固定的时间间隔捕获的线性重采样数据点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将波动时捕获的一些指标线性内插到固定的时间间隔。

I want to linearly interpolate some metrics that are captured at times that fluctuate, to fixed timing intervals.

let original_times:[Double] = [0.0,1.3,2.2,3.4,4.2,5.5,6.6,7.2,8.4,9.5,10.0]
let metric_1:[Double] = [4,3,6,7,4,5,7,4,2,7,2]

let wanted_times:[Double] = [0,1,2,3,4,5,6,7,8,9,10]

//linearly resample metric_1 (with corresponding sampling times 'original_times') to fixed time interval times 'wanted_times'

加速提供 vDSP_vlint ,但我正在努力弄清楚如何为我的应用程序实现它。

Accelerate offers vDSP_vlint but I'm struggling to figure out how to implement it for my application.

func vDSP_vlint(_ __A: UnsafePointer<Float>, _ __B: UnsafePointer<Float>, _ __IB: vDSP_Stride, _ __C: UnsafeMutablePointer<Float>, _ __IC: vDSP_Stride, _ __N: vDSP_Length, _ __M: vDSP_Length)


推荐答案

我不我不了解您要100%完成的数学运算,但我确实了解如何使用Accelerate。我创建了一个函数,可以更轻松地调用此Accelerate函数,并向您展示其工作原理。

I don't understand the math you want to do 100%, but I do understand how to use Accelerate. I created a function which makes it easier to call this Accelerate function and shows you how it works.

/**
 Vector linear interpolation between neighboring elements

 - Parameter a: Input vector.
 - Parameter b: Input vector: integer parts are indices into a and fractional parts are interpolation constants.

 Performs the following operation:

 ```C
 for (n = 0; n < N; ++n) {
    double b = B[n];
    double index = trunc([b]); //int part of B value
    double alpha = b - index; //frac part of B value

    double a0 = A[(int)index];     //indexed A value
    double a1 = A[(int)index + 1]; //next A value

    C[n] = a0 + (alpha * (a1 -a0)); //interpolated value
 }
 ```
 Generates vector C by interpolating between neighboring values of vector A as controlled by vector B. The integer portion of each element in B is the zero-based index of the first element of a pair of adjacent values in vector A.

 The value of the corresponding element of C is derived from these two values by linear interpolation, using the fractional part of the value in B.
*/
func interpolate(inout a: [Double], inout b: [Double]) -> [Double] {
    var c = [Double](count: b.count, repeatedValue: 0)
    vDSP_vlintD(&a, &b, 1, &c, 1, UInt(b.count), UInt(a.count))
    return c
}

编辑:好的,我全神贯注于您的问题,现在我明白了您想做什么。这样做很有趣,我想到了:

Alright, I wrapped my head around your problem, I understand now what you want to do. Was pretty fun to do, I came up with this:

import Accelerate

func calculateB(sampleTimes: [Double], outputTimes: [Double]) -> [Double] {
    var i = 0
    return outputTimes.map { (time: Double) -> Double in
        defer {
            if time > sampleTimes[i] { i++ }
        }
        return Double(i) + (time - sampleTimes[i]) / (sampleTimes[i+1] - sampleTimes[i])
    }
}

func interpolate(inout b: [Double], inout data: [Double]) -> [Double] {
    var c = [Double](count: b.count, repeatedValue: 0)
    vDSP_vlintD(&data, &b, 1, &c, 1, UInt(b.count), UInt(data.count))
    return c
}


let sampleTimes : [Double] = [0.0, 1.3, 2.2, 3.4, 4.2, 5.5, 6.6, 7.2, 8.4, 9.5, 10.0]
let outputTimes : [Double] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

var metric_1 : [Double] = [4, 3, 6, 7, 4, 5, 7, 4, 2, 7, 2]
var metric_2 : [Double] = [5, 4, 7, 5, 6, 6, 1, 3, 1, 6, 7]
var metric_3 : [Double] = [9, 8, 5, 7, 4, 8, 5, 6, 8, 9, 5]

var b = calculateB(sampleTimes, outputTimes: outputTimes)

interpolate(&b, data: &metric_1)   // [4, 3.230769, 5.333333, 6.666667, 4.75, 4.615385, 5.909091, 5, 2.666667, 4.727273, 2]
interpolate(&b, data: &metric_2)   // [5, 4.230769, 6.333333, 5.666667, 5.75, 6, 3.727273, 2.333333, 1.666667, 3.727273, 7]
interpolate(&b, data: &metric_3)   // [9, 8.230769, 5.666667, 6.333333, 4.75, 6.461538, 6.636364, 5.666667, 7.333333, 8.545455, 5]

加速所需的var。我不知道如何使用Accelerate来完成 calculateB ,我的意思是可能,但是要寻找正确的 vDSP 函数...

The vars are necessary for Accelerate. I don't know how calculateB could be done with Accelerate, I mean it's possible I think, but it's a pain to search for the correct vDSP functions...

这篇关于快速地以波动的时间间隔捕获固定的时间间隔捕获的线性重采样数据点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆