From e58f2d37a21b2fee8b0f39c2bf33b2b6bbe0bb35 Mon Sep 17 00:00:00 2001 From: Volker Fischer Date: Thu, 16 Jun 2011 11:25:38 +0000 Subject: [PATCH] added math functions for non-linear IIR filtering and hysteresis --- src/util.h | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/util.h b/src/util.h index 518b3138..e673ea40 100755 --- a/src/util.h +++ b/src/util.h @@ -691,6 +691,39 @@ public: { return (int) ( ( x - floor ( x ) ) >= 0.5 ) ? ceil(x) : floor(x); } + + static void UpDownIIR1 ( double& dOldValue, + const double& dNewValue, + const double& dWeightUp, + const double& dWeightDown ) + { + // different IIR weights for up and down direction + if ( dNewValue < dOldValue ) + { + dOldValue = + dOldValue * dWeightDown + (1.0 - dWeightDown) * dNewValue; + } + else + { + dOldValue = + dOldValue * dWeightUp + (1.0 - dWeightUp) * dNewValue; + } + } + + static int DecideWithHysteresis ( const double dValue, + const int iOldValue, + const double dHysteresis ) + { + // apply hysteresis + if ( dValue > static_cast ( iOldValue ) ) + { + return round ( dValue - dHysteresis ); + } + else + { + return round ( dValue + dHysteresis ); + } + } };