From 3cce24f2f8219ae5366516c0fef861cbb5d82311 Mon Sep 17 00:00:00 2001 From: "Robin P. Clark" Date: Mon, 29 Jul 2013 13:04:39 +0100 Subject: [PATCH] round up vale calculated according to binary scaling depth --- embedded_c_book/fo_filter/fo_filter.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/embedded_c_book/fo_filter/fo_filter.c b/embedded_c_book/fo_filter/fo_filter.c index 01b752c..c7b9521 100644 --- a/embedded_c_book/fo_filter/fo_filter.c +++ b/embedded_c_book/fo_filter/fo_filter.c @@ -20,7 +20,7 @@ main() { int i; - INT16 var,fvar; + INT16 var,fvar, threeup_result; var = ADRES; @@ -31,10 +31,11 @@ main() naive_filter = (naive_filter * 7 + ADRES) / 8; // old filter threeup_filter = (((threeup_filter << 3) - threeup_filter) >> 3) + ADRES; + threeup_result = (threeup_filter + 4)>>3; fvar = filter16(var,3); - printf(" iteration: %d naive_filter = %x threeup_filter = %x fvar = %x fp = %lf\n", i, naive_filter, threeup_filter, fvar, fp); + printf(" iteration: %d naive_filter = %d threeup_filter = %d fvar = %d fp = %lf\n", i, naive_filter, threeup_result, fvar, fp); } } @@ -43,6 +44,8 @@ main() #define BINARY_FILTER_SCALE 8 +#define ROUND_UP ((1)<<(BINARY_FILTER_SCALE)) + /* with BINARY_FILTER_SCALE set to 8, depth valid between 2 and 7 */ /* with a depth of 2 what we are saying is * * result = (old_result * 3.0 + new_val ) / 4.0) @@ -59,6 +62,8 @@ INT16 filter16(INT16 var, int depth) v <<= BINARY_FILTER_SCALE; // scale up printf(" v = %lx : ", v); v2 = (oldv << depth) - oldv + v; + // now round-up + v2 += ROUND_UP; v2 >>= depth; oldv = v2; printf(" v = %lx oldv = %lx ", v, oldv);