diff --git a/apps/plugins/fft/SOURCES b/apps/plugins/fft/SOURCES index 07fb013..ebe5f30 100644 --- a/apps/plugins/fft/SOURCES +++ b/apps/plugins/fft/SOURCES @@ -1,4 +1,3 @@ kiss_fft.c kiss_fftr.c fft.c -math.c diff --git a/apps/plugins/fft/fft.c b/apps/plugins/fft/fft.c index dc05594..09bdb69 100644 --- a/apps/plugins/fft/fft.c +++ b/apps/plugins/fft/fft.c @@ -366,8 +366,8 @@ struct { /************************* End of globals *************************/ /************************* Math functions *************************/ -#define QLOG_MAX 286286 -#define QLIN_MAX 1534588906 +#define QLOG_MAX 0x00040000 +#define QLIN_MAX 0x5B000000 #define QLN_10 float_q16(2.302585093) #define LIN_MAX (QLIN_MAX >> 16) @@ -409,24 +409,31 @@ void apply_window_func(char mode) /* Calculates the magnitudes from complex numbers and returns the maximum */ int32_t calc_magnitudes(bool logarithmic) { - int64_t tmp; + /* A major assumption made when calculating the Q*MAX constants + * is that the maximum magnitude is 29 bits long. */ + + uint32_t tmp; size_t i; - int32_t max = -2147483647; + int32_t max = 0; - /* Calculate the magnitude, discarding the phase. - * The sum of the squares can easily overflow the 15-bit (s15.16) - * requirement for fsqrt, so we scale the data down */ + /* Calculate the magnitude, discarding the phase. */ for (i = 0; i < ARRAYSIZE_PLOT; ++i) { tmp = output[i].r * output[i].r + output[i].i * output[i].i; - tmp <<= 16; - - tmp = fsqrt64(tmp, 16); - - if (logarithmic) - tmp = get_log_value(tmp & 0x7FFFFFFF); + if (tmp > 0x7FFFFFFF) tmp >>= 1; /* if our assumptions are correct, + this should never happen. It's just + a safeguard. */ + if (tmp > 0) + { + tmp = fp_sqrt(tmp, 0); /* linear scaling, nothing + bad should happen */ + tmp <<= 16; + if (logarithmic) + tmp = get_log_value(tmp);/* the log function + expects s15.16 values */ + } plot[i] = tmp; if (plot[i] > max) diff --git a/apps/plugins/fft/math.c b/apps/plugins/fft/math.c deleted file mode 100644 index c00804f..0000000 --- a/apps/plugins/fft/math.c +++ /dev/null @@ -1,13 +0,0 @@ -#include "math.h" - -int64_t fsqrt64(int64_t a, unsigned int fracbits) -{ - int64_t b = a/2 + (1 << fracbits); /* initial approximation */ - unsigned int n; - const unsigned int iterations = 3; /* very rough approximation */ - - for (n = 0; n < iterations; ++n) - b = (b + (((int64_t)(a) << fracbits)/b))/2; - - return b; -} diff --git a/apps/plugins/fft/math.h b/apps/plugins/fft/math.h index 450b9aa..ffacf1e 100644 --- a/apps/plugins/fft/math.h +++ b/apps/plugins/fft/math.h @@ -17,12 +17,4 @@ #define float_q15(a) float_q(a, 15) #define float_q16(a) float_q(a, 16) -/** - * Fixed point square root via Newton-Raphson. - * @param a square root argument. - * @param fracbits specifies number of fractional bits in argument. - * @return Square root of argument in same fixed point format as input. - */ -int64_t fsqrt64(int64_t a, unsigned int fracbits); - #endif