FastLED 3.10.8
Loading...
Searching...
No Matches

◆ fastDb()

static float fl::audio::fft::Context::fastDb ( u32 x)
inlinestaticprivate

Definition at line 360 of file fft_impl.cpp.hpp.

360 {
361 if (x == 0) return 0.0f;
362
363 // Find highest set bit (integer part of log2)
364 int msb = 0;
365 {
366 u32 v = x;
367 if (v >= 0x10000u) { v >>= 16; msb += 16; }
368 if (v >= 0x100u) { v >>= 8; msb += 8; }
369 if (v >= 0x10u) { v >>= 4; msb += 4; }
370 if (v >= 0x4u) { v >>= 2; msb += 2; }
371 if (v >= 0x2u) { msb += 1; }
372 }
373
374 // Normalized mantissa in [0, 1) as u16x16
375 u32 t_raw;
376 if (msb >= 16) {
377 t_raw = (x >> (msb - 16)) - 65536u;
378 } else {
379 t_raw = (x << (16 - msb)) - 65536u;
380 }
381 u16x16 t = u16x16::from_raw(t_raw);
382
383 // log2(1+t) ~= t + 0.345 * t * (1-t) [max error ~0.008]
384 // 22610 = 0.345 * 65536 (from_raw preserves exact original constant)
385 static constexpr u16x16 one(1.0f);
386 static constexpr u16x16 kCorrection = u16x16::from_raw(22610u);
387 u16x16 complement = one - t;
388 u16x16 prod = t * complement;
389 u16x16 correction = prod * kCorrection;
390 u16x16 frac = t + correction;
391
392 // log2(x) = msb + frac, assembled as u16x16
393 u16x16 log2_val = u16x16::from_raw(
394 (static_cast<u32>(msb) << 16) + frac.raw());
395
396 // 20*log10(x) = 6.02060 * log2(x)
397#if FASTLED_FFT_PRECISION == FASTLED_FFT_FIXED16
398 // 394593 = 6.02060 * 65536 (from_raw preserves exact original constant)
399 static constexpr u16x16 kDbScale = u16x16::from_raw(394593u);
400 u16x16 db = log2_val * kDbScale;
401 return db.to_float();
402#else
403 return 6.02060f * log2_val.to_float();
404#endif
405 }
static constexpr FASTLED_FORCE_INLINE u16x16 from_raw(u32 raw) FL_NO_EXCEPT
Definition u16x16.h:54

References fl::FL_NO_EXCEPT, fl::u16x16::from_raw(), fl::u16x16::raw(), fl::t, fl::u16x16::to_float(), and fl::x.

+ Here is the call graph for this function: