MCPcopy Create free account
hub / github.com/FastLED/FastLED / FL_TEST_FILE

Function FL_TEST_FILE

tests/fl/audio/fft/fft.cpp:21–1402  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

19#include "fl/stl/vector.h"
20
21FL_TEST_FILE(FL_FILEPATH) {
22
23// FFT tests adapt to the build-time FASTLED_FFT_PRECISION setting.
24// Test expectations are provided for all three precision modes:
25// FASTLED_FFT_FIXED16 (default) - 16-bit fixed point
26// FASTLED_FFT_FLOAT - 32-bit floating point
27// FASTLED_FFT_DOUBLE - 64-bit floating point
28
29
30
31FL_TEST_CASE("fft tester 512") {
32 int16_t buffer[512] = {0};
33 const int n = 512;
34 // fill in with a sine wave
35 for (int i = 0; i < n; ++i) {
36 float rot = fl::map_range<float, float>(i, 0, n - 1, 0, 2 * FL_PI * 10);
37 float sin_x = fl::sin(rot);
38 buffer[i] = int16_t(32767 * sin_x);
39 }
40 fl::audio::fft::Bins out(16);
41 // Explicitly use CQ_OCTAVE to match golden values (AUTO would select LOG_REBIN for 16 bins)
42 const int samples = n;
43 fl::audio::fft::Args args(samples, 16, fl::audio::fft::Args::DefaultMinFrequency(),
44 fl::audio::fft::Args::DefaultMaxFrequency(),
45 fl::audio::fft::Args::DefaultSampleRate(), fl::audio::fft::Mode::CQ_OCTAVE);
46 fl::audio::fft::Impl fft(args);
47 fft.run(buffer, &out);
48 // Test expectations for different precision modes
49 // Each mode has slightly different numerical results due to internal precision
50#if FASTLED_FFT_PRECISION == FASTLED_FFT_FIXED16
51 // Fixed16 precision (default) - CQ_OCTAVE octave-wise path
52 // With improved 7-tap halfband decimation filter (-45dB stopband)
53 // fastMag produces integer magnitudes (u16 → float)
54 // Golden values for 90-14080 Hz range
55 const float expected_output[16] = {
56 5.00, 0.00, 3.00, 6.00, 1.00, 21.00, 3.00, 46.00,
57 9.00, 12.00, 4.00, 6.00, 5.00, 2.00, 1.00, 1.00};
58 const float tolerance = 0.1; // Strict tolerance
59#elif FASTLED_FFT_PRECISION == FASTLED_FFT_FLOAT
60 // Float/double precision - needs regeneration with new frequency range.
61 const float expected_output[16] = {
62 5.00, 0.00, 3.00, 6.00, 1.00, 21.00, 3.00, 46.00,
63 9.00, 12.00, 4.00, 6.00, 5.00, 2.00, 1.00, 1.00};
64 const float tolerance = 5000.0; // Wide tolerance - needs regeneration in float mode
65#elif FASTLED_FFT_PRECISION == FASTLED_FFT_DOUBLE
66 const float expected_output[16] = {
67 5.00, 0.00, 3.00, 6.00, 1.00, 21.00, 3.00, 46.00,
68 9.00, 12.00, 4.00, 6.00, 5.00, 2.00, 1.00, 1.00};
69 const float tolerance = 5000.0; // Wide tolerance - needs regeneration in double mode
70#endif
71
72 for (int i = 0; i < 16; ++i) {
73 float a = out.raw()[i];
74 float b = expected_output[i];
75 bool almost_equal = fl::almost_equal(a, b, tolerance);
76 if (!almost_equal) {
77 FL_WARN("Impl output mismatch at index " << i << ": " << a
78 << " != " << b);

Callers

nothing calls this directly

Calls 15

almost_equalFunction · 0.85
logfFunction · 0.85
expfFunction · 0.85
makeAdversarialSineFunction · 0.85
findPeakBinFunction · 0.85
sqrtfFunction · 0.85
makeTwoToneFunction · 0.85
isNaNCheckFunction · 0.85
bandsMethod · 0.80
dbMethod · 0.80
freqToBinMethod · 0.80
binToFreqMethod · 0.80

Tested by

no test coverage detected