Audio spectrum analyzer in Unity C#: how to average bin amplitudes into bands

74 Views Asked by At

Watching this YouTube series on how to create a simple audio analyzer, and in Part 4, they take the output of GetSpectrumData() function and groups them into 7 frequency bands. However the calculation to combine / average the bins is decidedly not correct, and I would like to know what's the right way to generate meaningful frequency band values?

Example: Say we provide a spectrum[1024] array for GetSpectrumData() to store the values. With a sample rate of 48 000Hz, the Nyquist is 24 000Hz. The 1024 indexes are spread over 24 000Hz, giving ≈23.4Hz bin size.

If we do a standard audio frequency band distribution (again 1024 samples & ≈23.4Hz):

band0 (Sub-bass)        —   16Hz to    60Hz     —  2  samples
band1 (Bass)            —   60Hz to   250Hz     -  8  samples
band2 (Lower midrange)  —  250Hz to   500Hz     - 10  samples
band3 (Midrange)        —  500Hz to  2000Hz     - 64  samples
band4 (Higher midrange) — 2000Hz to  4000Hz     - 85  samples
band5 (Presence)        — 4000Hz to  6000Hz     - 85  samples
band6 (Brilliance)      — 6000Hz to 20000Hz     - 598 samples

Then for each band

for(int bandIndex = 0; bandIndex < 7; bandIndex++)
{
    // Calculate startIndex and endIndex in each frequence band

    float bandSum = 0;
    for(int i = startIndex; i < endIndex; i++)
    {
        bandSum += spectrum[i];
    }
    bandValue[bandIndex] = bandSum;
}

What's the meaningful way to calculate what goes into bandValue[]?

I have tried to take an average but don't think it makes much sense, like this:

float bandAverage = bandSum / (endIndex - startIndex);

I read somewhere that log should be involved but not sure how to do that.

0

There are 0 best solutions below