From eb4ff50cfab9fe229f85236c9bca76951dd2893d Mon Sep 17 00:00:00 2001 From: Donovan Baarda Date: Wed, 15 Oct 2014 17:22:48 +1100 Subject: Remove unnecessary and faulty new_frame detection. This would only detect a non-new-frame when the camera input equalled the filtered output, which would only happen after a long period of no movement or noise. It also is not recommended for this type of filter. --- ftnoir_filter_ewma2/ftnoir_filter_ewma2.cpp | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/ftnoir_filter_ewma2/ftnoir_filter_ewma2.cpp b/ftnoir_filter_ewma2/ftnoir_filter_ewma2.cpp index c7169faa..d4aa62b0 100644 --- a/ftnoir_filter_ewma2/ftnoir_filter_ewma2.cpp +++ b/ftnoir_filter_ewma2/ftnoir_filter_ewma2.cpp @@ -54,24 +54,6 @@ void FTNoIR_Filter::FilterHeadPoseData(const double *target_camera_position, return; } - bool new_frame = false; - - for (int i = 0; i < 6; i++) - { - if (target_camera_position[i] != current_camera_position[i]) - { - new_frame = true; - break; - } - } - - if (!new_frame) - { - for (int i = 0; i < 6; i++) - new_camera_position[i] = current_camera_position[i]; - return; - } - // Calculate the new camera position. for (int i=0;i<6;i++) { // Calculate the current and smoothed delta. -- cgit v1.2.3 From 42212ec9124bec8d489e649d005d991ce611fd2b Mon Sep 17 00:00:00 2001 From: Donovan Baarda Date: Wed, 15 Oct 2014 23:01:01 +1100 Subject: Simplify first_run handling to only initialize filter states. Make reset() set first_run=true; --- ftnoir_filter_ewma2/ftnoir_filter_ewma2.cpp | 4 +--- ftnoir_filter_ewma2/ftnoir_filter_ewma2.h | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/ftnoir_filter_ewma2/ftnoir_filter_ewma2.cpp b/ftnoir_filter_ewma2/ftnoir_filter_ewma2.cpp index d4aa62b0..a29aa9e5 100644 --- a/ftnoir_filter_ewma2/ftnoir_filter_ewma2.cpp +++ b/ftnoir_filter_ewma2/ftnoir_filter_ewma2.cpp @@ -42,16 +42,14 @@ void FTNoIR_Filter::FilterHeadPoseData(const double *target_camera_position, double new_delta, new_noise, norm_noise; double alpha; - //On the first run, initialize to output=target and return. + //On the first run, initialize filter states to target intput. if (first_run==true) { for (int i=0;i<6;i++) { - new_camera_position[i] = target_camera_position[i]; current_camera_position[i] = target_camera_position[i]; delta[i] = 0.0; noise[i] = 0.0; } first_run=false; - return; } // Calculate the new camera position. diff --git a/ftnoir_filter_ewma2/ftnoir_filter_ewma2.h b/ftnoir_filter_ewma2/ftnoir_filter_ewma2.h index 8863348c..ab01781b 100644 --- a/ftnoir_filter_ewma2/ftnoir_filter_ewma2.h +++ b/ftnoir_filter_ewma2/ftnoir_filter_ewma2.h @@ -24,7 +24,7 @@ class FTNoIR_Filter : public IFilter { public: FTNoIR_Filter(); - void reset() {} + void reset() { first_run=true; } void FilterHeadPoseData(const double *target_camera_position, double *new_camera_position); void receiveSettings(); -- cgit v1.2.3 From 43d30a7bdd83e2b589b7ea1b2fe8372103c72df8 Mon Sep 17 00:00:00 2001 From: Donovan Baarda Date: Thu, 16 Oct 2014 01:35:54 +1100 Subject: Update filter constants for dt=3ms as used in tracker.cpp. Change expressions and comments to make it easy to change dt. --- ftnoir_filter_ewma2/ftnoir_filter_ewma2.cpp | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/ftnoir_filter_ewma2/ftnoir_filter_ewma2.cpp b/ftnoir_filter_ewma2/ftnoir_filter_ewma2.cpp index a29aa9e5..58c6c6ac 100644 --- a/ftnoir_filter_ewma2/ftnoir_filter_ewma2.cpp +++ b/ftnoir_filter_ewma2/ftnoir_filter_ewma2.cpp @@ -1,5 +1,5 @@ /*** Written by Donovan Baarda -* +* * This program is free software; you can redistribute it and/or modify it * * under the terms of the GNU General Public License as published by the * * Free Software Foundation; either version 3 of the License, or (at your * @@ -24,10 +24,14 @@ FTNoIR_Filter::FTNoIR_Filter() : first_run(true), - // Deltas are smoothed over the last 3 frames (0.1sec at 30fps). - delta_smoothing(1.0/3.0), - // Noise is smoothed over the last 3600 frames (~2mins at 30fps). - noise_smoothing(1.0/3600.0) + // Currently facetracknoir/tracker.cpp updates every dt=3ms. All + // filter alpha values are calculated as alpha=dt/(dt + RC) and + // need to be updated when tracker.cpp changes. + // TODO(abo): Change this to use a dynamic dt using a timer. + // Deltas are smoothed over the last 1/60sec (16ms). + delta_smoothing(0.003/(0.003 + 0.016)), + // Noise is smoothed over the last 2mins. + noise_smoothing(0.003/(0.003 + 120.0)) { } @@ -40,7 +44,7 @@ void FTNoIR_Filter::FilterHeadPoseData(const double *target_camera_position, double *new_camera_position) { double new_delta, new_noise, norm_noise; - double alpha; + double scale, RC, alpha, pos; //On the first run, initialize filter states to target intput. if (first_run==true) { @@ -62,11 +66,16 @@ void FTNoIR_Filter::FilterHeadPoseData(const double *target_camera_position, noise[i] = noise_smoothing*new_noise + (1.0-noise_smoothing)*noise[i]; // Normalise the noise between 0->1 for 0->9 variances (0->3 stddevs). norm_noise = std::min(new_noise/(9.0*noise[i]), 1.0); - // Calculate the alpha from the normalized noise. + // Calculate the smoothing scale 0.0->1.0 from the normalized noise. // TODO(abo): change kSmoothingScaleCurve to a float where 1.0 is sqrt(norm_noise). - alpha = 1.0/(s.kMinSmoothing+(1.0-pow(norm_noise,s.kSmoothingScaleCurve/20.0))*(s.kMaxSmoothing-s.kMinSmoothing)); + scale = 1.0 - pow(norm_noise, s.kSmoothingScaleCurve/20.0); + // Currently min/max smoothing are ints 0->100. We want 0.0->3.0 seconds. + // TODO(abo): Change kMinSmoothing, kMaxSmoothing to floats 0.0->3.0 seconds RC. + RC = 3.0*(s.kMinSmoothing + scale*(s.kMaxSmoothing - s.kMinSmoothing))/100.0; + // TODO(abo): Change this to use a dynamic dt using a timer. + alpha = 0.003/(0.003 + RC); // Update the current camera position to the new position. - double pos = alpha*target_camera_position[i] + (1.0-alpha)*current_camera_position[i]; + pos = alpha*target_camera_position[i] + (1.0-alpha)*current_camera_position[i]; new_camera_position[i] = current_camera_position[i] = pos; } } -- cgit v1.2.3 From 107d98e39a0ef0f00f311b123e20dfda34532217 Mon Sep 17 00:00:00 2001 From: Donovan Baarda Date: Thu, 16 Oct 2014 02:00:05 +1100 Subject: Change noise filtering from 2mins to 60secs. --- ftnoir_filter_ewma2/ftnoir_filter_ewma2.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ftnoir_filter_ewma2/ftnoir_filter_ewma2.cpp b/ftnoir_filter_ewma2/ftnoir_filter_ewma2.cpp index 58c6c6ac..08f339a3 100644 --- a/ftnoir_filter_ewma2/ftnoir_filter_ewma2.cpp +++ b/ftnoir_filter_ewma2/ftnoir_filter_ewma2.cpp @@ -30,8 +30,8 @@ FTNoIR_Filter::FTNoIR_Filter() : // TODO(abo): Change this to use a dynamic dt using a timer. // Deltas are smoothed over the last 1/60sec (16ms). delta_smoothing(0.003/(0.003 + 0.016)), - // Noise is smoothed over the last 2mins. - noise_smoothing(0.003/(0.003 + 120.0)) + // Noise is smoothed over the last 60sec. + noise_smoothing(0.003/(0.003 + 60.0)) { } -- cgit v1.2.3 From 890c94a6e7449ee21cdbb2c27345ab8291e4f2f3 Mon Sep 17 00:00:00 2001 From: Donovan Baarda Date: Thu, 16 Oct 2014 11:50:32 +1100 Subject: Rename alpha/smoothing/output vars/attrs with better names. --- ftnoir_filter_ewma2/ftnoir_filter_ewma2.cpp | 26 +++++++++++++------------- ftnoir_filter_ewma2/ftnoir_filter_ewma2.h | 6 +++--- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/ftnoir_filter_ewma2/ftnoir_filter_ewma2.cpp b/ftnoir_filter_ewma2/ftnoir_filter_ewma2.cpp index 08f339a3..6dd1cee2 100644 --- a/ftnoir_filter_ewma2/ftnoir_filter_ewma2.cpp +++ b/ftnoir_filter_ewma2/ftnoir_filter_ewma2.cpp @@ -29,9 +29,9 @@ FTNoIR_Filter::FTNoIR_Filter() : // need to be updated when tracker.cpp changes. // TODO(abo): Change this to use a dynamic dt using a timer. // Deltas are smoothed over the last 1/60sec (16ms). - delta_smoothing(0.003/(0.003 + 0.016)), + delta_alpha(0.003/(0.003 + 0.016)), // Noise is smoothed over the last 60sec. - noise_smoothing(0.003/(0.003 + 60.0)) + noise_alpha(0.003/(0.003 + 60.0)) { } @@ -44,12 +44,12 @@ void FTNoIR_Filter::FilterHeadPoseData(const double *target_camera_position, double *new_camera_position) { double new_delta, new_noise, norm_noise; - double scale, RC, alpha, pos; + double smoothing, RC, alpha; //On the first run, initialize filter states to target intput. if (first_run==true) { for (int i=0;i<6;i++) { - current_camera_position[i] = target_camera_position[i]; + output[i] = target_camera_position[i]; delta[i] = 0.0; noise[i] = 0.0; } @@ -59,24 +59,24 @@ void FTNoIR_Filter::FilterHeadPoseData(const double *target_camera_position, // Calculate the new camera position. for (int i=0;i<6;i++) { // Calculate the current and smoothed delta. - new_delta = target_camera_position[i]-current_camera_position[i]; - delta[i] = delta_smoothing*new_delta + (1.0-delta_smoothing)*delta[i]; + new_delta = target_camera_position[i]-output[i]; + delta[i] = delta_alpha*new_delta + (1.0-delta_alpha)*delta[i]; // Calculate the current and smoothed noise variance. new_noise = delta[i]*delta[i]; - noise[i] = noise_smoothing*new_noise + (1.0-noise_smoothing)*noise[i]; + noise[i] = noise_alpha*new_noise + (1.0-noise_alpha)*noise[i]; // Normalise the noise between 0->1 for 0->9 variances (0->3 stddevs). norm_noise = std::min(new_noise/(9.0*noise[i]), 1.0); - // Calculate the smoothing scale 0.0->1.0 from the normalized noise. + // Calculate the smoothing 0.0->1.0 from the normalized noise. // TODO(abo): change kSmoothingScaleCurve to a float where 1.0 is sqrt(norm_noise). - scale = 1.0 - pow(norm_noise, s.kSmoothingScaleCurve/20.0); + smoothing = 1.0 - pow(norm_noise, s.kSmoothingScaleCurve/20.0); // Currently min/max smoothing are ints 0->100. We want 0.0->3.0 seconds. // TODO(abo): Change kMinSmoothing, kMaxSmoothing to floats 0.0->3.0 seconds RC. - RC = 3.0*(s.kMinSmoothing + scale*(s.kMaxSmoothing - s.kMinSmoothing))/100.0; + RC = 3.0*(s.kMinSmoothing + smoothing*(s.kMaxSmoothing - s.kMinSmoothing))/100.0; // TODO(abo): Change this to use a dynamic dt using a timer. alpha = 0.003/(0.003 + RC); - // Update the current camera position to the new position. - pos = alpha*target_camera_position[i] + (1.0-alpha)*current_camera_position[i]; - new_camera_position[i] = current_camera_position[i] = pos; + // Calculate the new output position. + output[i] = alpha*target_camera_position[i] + (1.0-alpha)*output[i]; + new_camera_position[i] = output[i]; } } diff --git a/ftnoir_filter_ewma2/ftnoir_filter_ewma2.h b/ftnoir_filter_ewma2/ftnoir_filter_ewma2.h index ab01781b..aacf4916 100644 --- a/ftnoir_filter_ewma2/ftnoir_filter_ewma2.h +++ b/ftnoir_filter_ewma2/ftnoir_filter_ewma2.h @@ -30,11 +30,11 @@ public: void receiveSettings(); private: bool first_run; - double delta_smoothing; - double noise_smoothing; + double delta_alpha; + double noise_alpha; double delta[6]; double noise[6]; - double current_camera_position[6]; + double output[6]; settings s; }; -- cgit v1.2.3 From ec6e15fc60fd910b31989f160596c169c6b7b515 Mon Sep 17 00:00:00 2001 From: Donovan Baarda Date: Thu, 16 Oct 2014 22:58:52 +1100 Subject: Simplify EWMA2 licence and add filter description comment. --- ftnoir_filter_ewma2/ftnoir_filter_ewma2.cpp | 34 ++++++++++++++--------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/ftnoir_filter_ewma2/ftnoir_filter_ewma2.cpp b/ftnoir_filter_ewma2/ftnoir_filter_ewma2.cpp index 6dd1cee2..320b95ad 100644 --- a/ftnoir_filter_ewma2/ftnoir_filter_ewma2.cpp +++ b/ftnoir_filter_ewma2/ftnoir_filter_ewma2.cpp @@ -1,19 +1,9 @@ -/*** Written by Donovan Baarda -* -* This program is free software; you can redistribute it and/or modify it * -* under the terms of the GNU General Public License as published by the * -* Free Software Foundation; either version 3 of the License, or (at your * -* option) any later version. * -* * -* This program is distributed in the hope that it will be useful, but * -* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * -* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * -* more details. * -* * -* You should have received a copy of the GNU General Public License along * -* with this program; if not, see . * -* * -********************************************************************************/ +/* Copyright (c) 2014 Donovan Baarda + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + */ #include "ftnoir_filter_ewma2.h" #include #include @@ -22,10 +12,20 @@ #include #include +// Exponentially Weighted Moving Average (EWMA) Filter with dynamic smoothing. +// +// This filter tries to adjust the amount of filtering to minimize lag when +// moving, and minimize noise when still. It uses the delta filtered over the +// last 1/60sec (16ms) compared to the delta's average noise variance over +// the last 60sec to try and detect movement vs noise. As the delta increases +// from 0 to 3 stdevs of the noise, the filtering scales down from maxSmooth +// to minSmooth at a rate controlled by the powCurve setting. + + FTNoIR_Filter::FTNoIR_Filter() : first_run(true), // Currently facetracknoir/tracker.cpp updates every dt=3ms. All - // filter alpha values are calculated as alpha=dt/(dt + RC) and + // filter alpha values are calculated as alpha=dt/(dt+RC) and // need to be updated when tracker.cpp changes. // TODO(abo): Change this to use a dynamic dt using a timer. // Deltas are smoothed over the last 1/60sec (16ms). -- cgit v1.2.3