diff options
Diffstat (limited to 'apps/plugins/fft/fft.c')
-rw-r--r-- | apps/plugins/fft/fft.c | 35 |
1 files changed, 21 insertions, 14 deletions
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) |