Android中計算座標變換速度的原理
採用最小二乘法對獲取的座標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即可。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.