From 4d8de47ad47141bbc070725a0a4359a126bbb962 Mon Sep 17 00:00:00 2001 From: Ching-Kang Yen Date: Tue, 6 Oct 2020 23:27:21 +0800 Subject: common: body_detection: modify variance updating formula Modify the variance formula to speed up body detection. BRANCH=None BUG=b:123434029 TEST=make buildall; TEST=make BOARD=ampton, re-flash the DUT, see if the body detection works Signed-off-by: Ching-Kang Yen Change-Id: Ib444abb6bdfbc637539103d563b515817865acc9 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2452133 Reviewed-by: Heng-ruey Hsu --- common/body_detection.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'common/body_detection.c') diff --git a/common/body_detection.c b/common/body_detection.c index 334009e320..375b4456d0 100644 --- a/common/body_detection.c +++ b/common/body_detection.c @@ -48,18 +48,18 @@ static struct body_detect_motion_data * x_0: oldest value in the window, will be replaced by x_n * x_n: new coming value * - * n^2 * var(x') = n^2 * var(x) + (sum(x') - sum(x))^2 + - * (n * x_n - sum(x'))^2 / n - (n * x_0 - sum(x'))^2 / n + * n^2 * var(x') = n^2 * var(x) + (x_n - x_0) * + * (n * (x_n + x_0) - sum(x') - sum(x)) */ static void update_motion_data(struct body_detect_motion_data *x, int x_n) { const int n = window_size; const int x_0 = x->history[history_idx]; - const int new_sum = x->sum + (x_n - x->history[history_idx]); + const int sum_diff = x_n - x_0; + const int new_sum = x->sum + sum_diff; - x->n2_variance = x->n2_variance + POW2((int64_t)new_sum - x->sum) + - (POW2((int64_t)x_n * n - new_sum) - - POW2((int64_t)x_0 * n - new_sum)) / n; + x->n2_variance += sum_diff * + ((int64_t)n * (x_n + x_0) - new_sum - x->sum); x->sum = new_sum; x->history[history_idx] = x_n; } -- cgit v1.2.1