|
| 1 | +/** |
| 2 | + * Copyright 2024 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#define LOG_TAG "OneEuroFilter" |
| 18 | + |
| 19 | +#include <chrono> |
| 20 | +#include <cmath> |
| 21 | + |
| 22 | +#include <android-base/logging.h> |
| 23 | +#include <input/CoordinateFilter.h> |
| 24 | + |
| 25 | +namespace android { |
| 26 | +namespace { |
| 27 | + |
| 28 | +inline float cutoffFreq(float minCutoffFreq, float beta, float filteredSpeed) { |
| 29 | + return minCutoffFreq + beta * std::abs(filteredSpeed); |
| 30 | +} |
| 31 | + |
| 32 | +inline float smoothingFactor(std::chrono::duration<float> samplingPeriod, float cutoffFreq) { |
| 33 | + return samplingPeriod.count() / (samplingPeriod.count() + (1.0 / (2.0 * M_PI * cutoffFreq))); |
| 34 | +} |
| 35 | + |
| 36 | +inline float lowPassFilter(float rawPosition, float prevFilteredPosition, float smoothingFactor) { |
| 37 | + return smoothingFactor * rawPosition + (1 - smoothingFactor) * prevFilteredPosition; |
| 38 | +} |
| 39 | + |
| 40 | +} // namespace |
| 41 | + |
| 42 | +OneEuroFilter::OneEuroFilter(float minCutoffFreq, float beta, float speedCutoffFreq) |
| 43 | + : mMinCutoffFreq{minCutoffFreq}, mBeta{beta}, mSpeedCutoffFreq{speedCutoffFreq} {} |
| 44 | + |
| 45 | +float OneEuroFilter::filter(std::chrono::duration<float> timestamp, float rawPosition) { |
| 46 | + LOG_IF(FATAL, mPrevFilteredPosition.has_value() && (timestamp <= *mPrevTimestamp)) |
| 47 | + << "Timestamp must be greater than mPrevTimestamp"; |
| 48 | + |
| 49 | + const std::chrono::duration<float> samplingPeriod = (mPrevTimestamp.has_value()) |
| 50 | + ? (timestamp - *mPrevTimestamp) |
| 51 | + : std::chrono::duration<float>{1.0}; |
| 52 | + |
| 53 | + const float rawVelocity = (mPrevFilteredPosition.has_value()) |
| 54 | + ? ((rawPosition - *mPrevFilteredPosition) / samplingPeriod.count()) |
| 55 | + : 0.0; |
| 56 | + |
| 57 | + const float speedSmoothingFactor = smoothingFactor(samplingPeriod, mSpeedCutoffFreq); |
| 58 | + |
| 59 | + const float filteredVelocity = (mPrevFilteredVelocity.has_value()) |
| 60 | + ? lowPassFilter(rawVelocity, *mPrevFilteredVelocity, speedSmoothingFactor) |
| 61 | + : rawVelocity; |
| 62 | + |
| 63 | + const float positionCutoffFreq = cutoffFreq(mMinCutoffFreq, mBeta, filteredVelocity); |
| 64 | + |
| 65 | + const float positionSmoothingFactor = smoothingFactor(samplingPeriod, positionCutoffFreq); |
| 66 | + |
| 67 | + const float filteredPosition = (mPrevFilteredPosition.has_value()) |
| 68 | + ? lowPassFilter(rawPosition, *mPrevFilteredPosition, positionSmoothingFactor) |
| 69 | + : rawPosition; |
| 70 | + |
| 71 | + mPrevTimestamp = timestamp; |
| 72 | + mPrevRawPosition = rawPosition; |
| 73 | + mPrevFilteredVelocity = filteredVelocity; |
| 74 | + mPrevFilteredPosition = filteredPosition; |
| 75 | + |
| 76 | + return filteredPosition; |
| 77 | +} |
| 78 | + |
| 79 | +} // namespace android |
0 commit comments