arduino FFT scale down the results

hello

i 'm using the FFT audio analyser to get a spectrum analyser.
i use ESP32 .

if i use a audio sweep from 20 - 20.000hz ,
and i turn on the serial plotter in the IDE ,
then the visualisation works like a charm

BUT :
the acual values are between 0 and 750.000 !!
video : youtubevid code in description
how can i scale these values to a range of 0 - 255 ??

Decimal 750000 = 0x0B 71B0
So shift the results to the right 12 bits so the results become 0 to 0xB7.

Assuming the output values are positive, make a pass through the output and record the highest value. Call that "max".

Make a second pass through the output, multiplying all values by 255.0/float(max).

If the lowest value is greater than zero, you might want to subtract that off all the output values before determining "max" and rescaling.

It may not be the most computationally efficient approach, but there is the Arduino map() function: map() - Arduino Reference

You didn't ask, but audio spectral values are usually displayed on a logarithmic (decibel) scale which will greatly compress the range of values, but they'll still have to be mapped to the proper display range.

Assuming the output values are positive, make a pass through the output and record the highest value. Call that "max".

Make a second pass through the output, multiplying all values by 255.0/float(max).

I used a similar concept with a "VU meter effect". The "top" of the meter is the maximum from a 20-second circular buffer. The "bottom" of the meter is the moving average from the same array/buffer. That gives a nice meter effect with lots of "meter action" and it automatically adjusts to to volume changes. ...Of course, it's useless as a meter because the reference is constantly (slowly) changing and the "space" between LEDs is also constantly changing and it doesn't measure dB or anything meaningful.

ok thx

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.