采用最小二乘法对获取的坐标X,Y时间序列进行回归拟合。
对于X时间序列(xi,ti):xi=b0+b1*ti+b2*ti^2+...+bm*ti^m,可得如下等式
A*B=Y----------(1)
其中,(带t的表示矩阵的转置)
矩阵B为拟合后要求解的系数矩阵(b0 b1 b2 ... bn)t
矩阵A为
1 t0 t0^2 .... t0^m
1 t1 t1^2 .... t1^m
.
.
.
1 tn tn^2 ... tn^m
矩阵Y为(x0 x1 x2 ... xn)
对A进行QR分解,Q是正交矩阵,R是上三角矩阵:具体见http://zh.wikipedia.org/wiki/QR%E5%88%86%E8%A7%A3
则等式(1)变为
Q*R*B=Y=======》R*B = (Qt)*Y 与Android中input.c中函数solveLeastSquares的实现对应。
对X和Y的拟合与Android中函数VelocityTracker::getEstimator中的下列代码对应
// Calculate a least squares polynomial fit.
if (degree > Estimator::MAX_DEGREE) {
degree = Estimator::MAX_DEGREE;
}
if (degree > m - 1) {
degree = m - 1;
}
if (degree >= 1) {
float xdet, ydet;
uint32_t n = degree + 1;
if (solveLeastSquares(time, x, m, n, outEstimator->xCoeff, &xdet)
&& solveLeastSquares(time, y, m, n, outEstimator->yCoeff, &ydet)) {
outEstimator->degree = degree;
outEstimator->confidence = xdet * ydet;
最后要预测的X和Y的的坐标取b1作为对应的速度(忽略掉其他项为xi=b1*ti,正好是位移、速度、时间的表达式),与函数VelocityTracker::getVelocity中的如下代码对应
if (getEstimator(id, DEFAULT_DEGREE, DEFAULT_HORIZON, &estimator)) {
if (estimator.degree >= 1) {
*outVx = estimator.xCoeff[1];
*outVy = estimator.yCoeff[1];
return true;
}
}
根据上面的步骤得到了X方向和Y方向的速度
再应用时,通过VelocityTracker::addMovement添加MotionEvent事件
再需要获取速度信息的时候调用VelocityTracker::computeCurrentVelocity进行计算,
然后调用VelocityTracker::getXVelocity和VelocityTracker::getYVelocity即可。
对于X时间序列(xi,ti):xi=b0+b1*ti+b2*ti^2+...+bm*ti^m,可得如下等式
A*B=Y----------(1)
其中,(带t的表示矩阵的转置)
矩阵B为拟合后要求解的系数矩阵(b0 b1 b2 ... bn)t
矩阵A为
1 t0 t0^2 .... t0^m
1 t1 t1^2 .... t1^m
.
.
.
1 tn tn^2 ... tn^m
矩阵Y为(x0 x1 x2 ... xn)
对A进行QR分解,Q是正交矩阵,R是上三角矩阵:具体见http://zh.wikipedia.org/wiki/QR%E5%88%86%E8%A7%A3
则等式(1)变为
Q*R*B=Y=======》R*B = (Qt)*Y 与Android中input.c中函数solveLeastSquares的实现对应。
对X和Y的拟合与Android中函数VelocityTracker::getEstimator中的下列代码对应
// Calculate a least squares polynomial fit.
if (degree > Estimator::MAX_DEGREE) {
degree = Estimator::MAX_DEGREE;
}
if (degree > m - 1) {
degree = m - 1;
}
if (degree >= 1) {
float xdet, ydet;
uint32_t n = degree + 1;
if (solveLeastSquares(time, x, m, n, outEstimator->xCoeff, &xdet)
&& solveLeastSquares(time, y, m, n, outEstimator->yCoeff, &ydet)) {
outEstimator->degree = degree;
outEstimator->confidence = xdet * ydet;
最后要预测的X和Y的的坐标取b1作为对应的速度(忽略掉其他项为xi=b1*ti,正好是位移、速度、时间的表达式),与函数VelocityTracker::getVelocity中的如下代码对应
if (getEstimator(id, DEFAULT_DEGREE, DEFAULT_HORIZON, &estimator)) {
if (estimator.degree >= 1) {
*outVx = estimator.xCoeff[1];
*outVy = estimator.yCoeff[1];
return true;
}
}
根据上面的步骤得到了X方向和Y方向的速度
再应用时,通过VelocityTracker::addMovement添加MotionEvent事件
再需要获取速度信息的时候调用VelocityTracker::computeCurrentVelocity进行计算,
然后调用VelocityTracker::getXVelocity和VelocityTracker::getYVelocity即可。