diff options
-rw-r--r-- | apps/plugins/fft/SOURCES | 1 | ||||
-rw-r--r-- | apps/plugins/fft/fft.c | 35 | ||||
-rw-r--r-- | apps/plugins/fft/math.h | 8 |
3 files changed, 21 insertions, 23 deletions
diff --git a/apps/plugins/fft/SOURCES b/apps/plugins/fft/SOURCES index 07fb013eaa..ebe5f3067b 100644 --- a/apps/plugins/fft/SOURCES +++ b/apps/plugins/fft/SOURCES | |||
@@ -1,4 +1,3 @@ | |||
1 | kiss_fft.c | 1 | kiss_fft.c |
2 | kiss_fftr.c | 2 | kiss_fftr.c |
3 | fft.c | 3 | fft.c |
4 | math.c | ||
diff --git a/apps/plugins/fft/fft.c b/apps/plugins/fft/fft.c index ae07179557..acc19fabe5 100644 --- a/apps/plugins/fft/fft.c +++ b/apps/plugins/fft/fft.c | |||
@@ -307,8 +307,8 @@ struct { | |||
307 | /************************* End of globals *************************/ | 307 | /************************* End of globals *************************/ |
308 | 308 | ||
309 | /************************* Math functions *************************/ | 309 | /************************* Math functions *************************/ |
310 | #define QLOG_MAX 286286 | 310 | #define QLOG_MAX 0x00040000 |
311 | #define QLIN_MAX 1534588906 | 311 | #define QLIN_MAX 0x5B000000 |
312 | #define QLN_10 float_q16(2.302585093) | 312 | #define QLN_10 float_q16(2.302585093) |
313 | #define LIN_MAX (QLIN_MAX >> 16) | 313 | #define LIN_MAX (QLIN_MAX >> 16) |
314 | 314 | ||
@@ -350,24 +350,31 @@ void apply_window_func(char mode) | |||
350 | /* Calculates the magnitudes from complex numbers and returns the maximum */ | 350 | /* Calculates the magnitudes from complex numbers and returns the maximum */ |
351 | int32_t calc_magnitudes(bool logarithmic) | 351 | int32_t calc_magnitudes(bool logarithmic) |
352 | { | 352 | { |
353 | int64_t tmp; | 353 | /* A major assumption made when calculating the Q*MAX constants |
354 | * is that the maximum magnitude is 29 bits long. */ | ||
355 | |||
356 | uint32_t tmp; | ||
354 | size_t i; | 357 | size_t i; |
355 | 358 | ||
356 | int32_t max = -2147483647; | 359 | int32_t max = 0; |
357 | 360 | ||
358 | /* Calculate the magnitude, discarding the phase. | 361 | /* Calculate the magnitude, discarding the phase. */ |
359 | * The sum of the squares can easily overflow the 15-bit (s15.16) | ||
360 | * requirement for fsqrt, so we scale the data down */ | ||
361 | for (i = 0; i < ARRAYSIZE_PLOT; ++i) | 362 | for (i = 0; i < ARRAYSIZE_PLOT; ++i) |
362 | { | 363 | { |
363 | tmp = output[i].r * output[i].r + output[i].i * output[i].i; | 364 | tmp = output[i].r * output[i].r + output[i].i * output[i].i; |
364 | tmp <<= 16; | ||
365 | |||
366 | tmp = fsqrt64(tmp, 16); | ||
367 | |||
368 | if (logarithmic) | ||
369 | tmp = get_log_value(tmp & 0x7FFFFFFF); | ||
370 | 365 | ||
366 | if (tmp > 0x7FFFFFFF) tmp >>= 1; /* if our assumptions are correct, | ||
367 | this should never happen. It's just | ||
368 | a safeguard. */ | ||
369 | if (tmp > 0) | ||
370 | { | ||
371 | tmp = fp_sqrt(tmp, 0); /* linear scaling, nothing | ||
372 | bad should happen */ | ||
373 | tmp <<= 16; | ||
374 | if (logarithmic) | ||
375 | tmp = get_log_value(tmp);/* the log function | ||
376 | expects s15.16 values */ | ||
377 | } | ||
371 | plot[i] = tmp; | 378 | plot[i] = tmp; |
372 | 379 | ||
373 | if (plot[i] > max) | 380 | if (plot[i] > max) |
diff --git a/apps/plugins/fft/math.h b/apps/plugins/fft/math.h index 450b9aafcb..ffacf1eedb 100644 --- a/apps/plugins/fft/math.h +++ b/apps/plugins/fft/math.h | |||
@@ -17,12 +17,4 @@ | |||
17 | #define float_q15(a) float_q(a, 15) | 17 | #define float_q15(a) float_q(a, 15) |
18 | #define float_q16(a) float_q(a, 16) | 18 | #define float_q16(a) float_q(a, 16) |
19 | 19 | ||
20 | /** | ||
21 | * Fixed point square root via Newton-Raphson. | ||
22 | * @param a square root argument. | ||
23 | * @param fracbits specifies number of fractional bits in argument. | ||
24 | * @return Square root of argument in same fixed point format as input. | ||
25 | */ | ||
26 | int64_t fsqrt64(int64_t a, unsigned int fracbits); | ||
27 | |||
28 | #endif | 20 | #endif |