diff --git a/example/hsp_ag_demo.c b/example/hsp_ag_demo.c index da1fb078f..a395e8eda 100644 --- a/example/hsp_ag_demo.c +++ b/example/hsp_ag_demo.c @@ -55,7 +55,6 @@ #include #include #include -#include #include #include "btstack.h" diff --git a/example/hsp_hs_demo.c b/example/hsp_hs_demo.c index 4d870f4d0..6b01a4c24 100644 --- a/example/hsp_hs_demo.c +++ b/example/hsp_hs_demo.c @@ -55,7 +55,6 @@ #include #include #include -#include #include #include "btstack.h" diff --git a/src/classic/btstack_cvsd_plc.c b/src/classic/btstack_cvsd_plc.c index b59e52287..8a6f18d02 100644 --- a/src/classic/btstack_cvsd_plc.c +++ b/src/classic/btstack_cvsd_plc.c @@ -45,7 +45,6 @@ #include #include #include -#include #include "btstack_cvsd_plc.h" #include "btstack_debug.h" @@ -56,6 +55,30 @@ static float rcos[CVSD_OLAL] = { 0.45386582,0.36316850,0.27713082,0.19868268, 0.13049554,0.07489143,0.03376389,0.00851345}; +// taken from http://www.codeproject.com/Articles/69941/Best-Square-Root-Method-Algorithm-Function-Precisi +// Algorithm: Babylonian Method + some manipulations on IEEE 32 bit floating point representation +static float sqrt3(const float x){ + union { + int i; + float x; + } u; + u.x = x; + u.i = (1<<29) + (u.i >> 1) - (1<<22); + + // Two Babylonian Steps (simplified from:) + // u.x = 0.5f * (u.x + x/u.x); + // u.x = 0.5f * (u.x + x/u.x); + u.x = u.x + x/u.x; + u.x = 0.25f*u.x + x/u.x; + + return u.x; +} + +static float absolute(float x){ + if (x < 0) x = -x; + return x; +} + static float CrossCorrelation(int8_t *x, int8_t *y){ float num = 0; float den = 0; @@ -67,7 +90,7 @@ static float CrossCorrelation(int8_t *x, int8_t *y){ x2+=((float)x[m])*x[m]; y2+=((float)y[m])*y[m]; } - den = (float)sqrt(x2*y2); + den = (float)sqrt3(x2*y2); return num/den; } @@ -93,8 +116,8 @@ static float AmplitudeMatch(int8_t *y, int8_t bestmatch) { float sf; for (i=0;i #include #include -#include #include "btstack_sbc_plc.h" @@ -61,6 +60,30 @@ static float rcos[SBC_OLAL] = { 0.45386582f,0.36316850f,0.27713082f,0.19868268f, 0.13049554f,0.07489143f,0.03376389f,0.00851345f}; +// taken from http://www.codeproject.com/Articles/69941/Best-Square-Root-Method-Algorithm-Function-Precisi +// Algorithm: Babylonian Method + some manipulations on IEEE 32 bit floating point representation +static float sqrt3(const float x){ + union { + int i; + float x; + } u; + u.x = x; + u.i = (1<<29) + (u.i >> 1) - (1<<22); + + // Two Babylonian Steps (simplified from:) + // u.x = 0.5f * (u.x + x/u.x); + // u.x = 0.5f * (u.x + x/u.x); + u.x = u.x + x/u.x; + u.x = 0.25f*u.x + x/u.x; + + return u.x; +} + +static float absolute(float x){ + if (x < 0) x = -x; + return x; +} + static float CrossCorrelation(int16_t *x, int16_t *y){ float num = 0; float den = 0; @@ -72,7 +95,7 @@ static float CrossCorrelation(int16_t *x, int16_t *y){ x2+=((float)x[m])*x[m]; y2+=((float)y[m])*y[m]; } - den = (float)sqrt(x2*y2); + den = (float)sqrt3(x2*y2); return num/den; } @@ -99,8 +122,8 @@ static float AmplitudeMatch(int16_t *y, int16_t bestmatch) { float sf; for (i=0;i +#include +#include +#include + +#include "CppUTest/TestHarness.h" +#include "CppUTest/CommandLineTestRunner.h" + +static struct timeval init_tv; + +union { + int i; + float x; +} u; + +// taken from http://www.codeproject.com/Articles/69941/Best-Square-Root-Method-Algorithm-Function-Precisi +// Algorithm: Babylonian Method + some manipulations on IEEE 32 bit floating point representation +float sqrt1(const float x){ + u.x = x; + u.i = (1<<29) + (u.i >> 1) - (1<<22); + + // Two Babylonian Steps (simplified from:) + // u.x = 0.5f * (u.x + x/u.x); + // u.x = 0.5f * (u.x + x/u.x); + u.x = u.x + x/u.x; + u.x = 0.25f*u.x + x/u.x; + + return u.x; +} + +// Algorithm: Log base 2 approximation and Newton's Method +float sqrt3(const float x){ + u.x = x; + u.i = (1<<29) + (u.i >> 1) - (1<<22); + return u.x; +} + +float sqrt2(const float n) { + /*using n itself as initial approximation => improve */ + float x = n; + float y = 1; + float e = 0.001; /* e decides the accuracy level*/ + while(x - y > e){ + x = (x + y)/2; + y = n/x; + } + return x; +} + +static uint32_t get_time_ms(void){ + struct timeval tv; + gettimeofday(&tv, NULL); + uint32_t time_ms = (uint32_t)((tv.tv_sec - init_tv.tv_sec) * 1000) + (tv.tv_usec / 1000); + return time_ms; +} + +static int values_len = 100000; + +TEST_GROUP(SqrtTest){ + void setup(void){ + } + + void test_method(float (*my_sqrt)(const float x)){ + int i, j; + float precision = 0; + int ta = 0; + int te = 0; + + for (j=0; j<100; j++){ + for (i=0; i #include -#include #include #include #include