From c9004a6f3f129778fcab7dfcf9060f0f168c06b8 Mon Sep 17 00:00:00 2001 From: Simon Lin Date: Fri, 11 Sep 2020 03:22:32 +0800 Subject: [PATCH] [link-quality] optimize RSS calculation (#5508) --- src/core/thread/link_quality.cpp | 27 ++++----------------------- 1 file changed, 4 insertions(+), 23 deletions(-) diff --git a/src/core/thread/link_quality.cpp b/src/core/thread/link_quality.cpp index e1e34476c..ca1632381 100644 --- a/src/core/thread/link_quality.cpp +++ b/src/core/thread/link_quality.cpp @@ -68,7 +68,6 @@ otError RssAverager::Add(int8_t aRss) { otError error = OT_ERROR_NONE; uint16_t newValue; - uint16_t oldAverage; VerifyOrExit(aRss != OT_RADIO_RSSI_INVALID, error = OT_ERROR_INVALID_ARGS); @@ -83,28 +82,10 @@ otError RssAverager::Add(int8_t aRss) newValue = static_cast(-aRss); newValue <<= kPrecisionBitShift; - oldAverage = mAverage; - - if (mCount == 0) - { - mCount++; - mAverage = newValue; - } - else if (mCount < (1 << kCoeffBitShift) - 1) - { - mCount++; - - // Maintain arithmetic mean. - // newAverage = newValue * (1/mCount) + oldAverage * ((mCount -1)/mCount) - mAverage = static_cast(((oldAverage * (mCount - 1)) + newValue) / mCount); - } - else - { - // Maintain exponentially weighted moving average using coefficient of (1/2^kCoeffBitShift). - // newAverage = + newValue * 1/2^j + oldAverage * (1 - 1/2^j), for j = kCoeffBitShift. - - mAverage = static_cast(((oldAverage << kCoeffBitShift) - oldAverage + newValue) >> kCoeffBitShift); - } + mCount += (mCount < (1 << kCoeffBitShift)); + // Maintain arithmetic mean. + // newAverage = newValue * (1/mCount) + oldAverage * ((mCount -1)/mCount) + mAverage = static_cast(((mAverage * (mCount - 1)) + newValue) / mCount); exit: return error;