The previous implementation first took square root of the energy,
then divided that by number of samples to get "average", whereas
the correct procedure is first to take the average of the energy
(to get "average square") and then take the square root.
Thus, the previous implementation divided the RMS by additional
sqrt(buffer.length) term, causing the sound pressure to go
to zero with increasing buffer size (and also, not actually computing
sound pressure).
For instance:
buffer = [2, -2, 2, -2, 2, ...] of len N
energy = N * 4
rms = sqrt(energy / N) = 2
# now, correctly
sound_pressure_correct = linearToDecibel(2)
# previously, wrong (e.g. L207 @ AudioEvent)
value = sqrt(energy) = 2 * sqrt(N)
avg = value / N = 2 / sqrt(N)
sound_pressure = linearToDecibel(avg) = linearToDecibel(2 / sqrt(N))
Apart from that, the PR adds a few static keywords & removes (now unused) localEnergy function.
The previous implementation first took square root of the energy, then divided that by number of samples to get "average", whereas the correct procedure is first to take the average of the energy (to get "average square") and then take the square root.
Thus, the previous implementation divided the RMS by additional
sqrt(buffer.length)
term, causing the sound pressure to go to zero with increasing buffer size (and also, not actually computing sound pressure).For instance:
Apart from that, the PR adds a few
static
keywords & removes (now unused)localEnergy
function.