The Tutorial: Visualise the frequencies of a signal in real time problem

Hi everyone,
In the statement auto level = jmap(jlimit(mindB, maxdB, Decibels::gainToDecibels(fftData[fftDataIndex]) - Decibels::gainToDecibels((float)fftSize)), mindB, maxdB, 0.0f, 1.0f);
Why minus Decibels::gainToDecibels((float)fftSize)? Why not directly auto level = jmap(jlimit(mindB, maxdB, Decibels::gainToDecibels(fftData[fftDataIndex])),mindB, maxdB, 0.0f, 1.0f);

The magnitude of the frequency bins need to be normalised by fftSize. And the minus of the two logarithms is like a division in scalar domain:

Decibels::gainToDecibels (fftData[fftDataIndex]) - Decibels::gainToDecibels ((float)fftSize)
// is the same as
Decibels::gainToDecibels (fftData[fftDataIndex] / (float)fftSize);

The first one might be slightly easier to calculate, since Decibels::gainToDecibels ((float)fftSize) is constant and will be taken out by the optimiser. And subtractions are easier to compute than divisions.

But who knows what really happens in the optimiser, this is speculation.

2 Likes