JorenSix / TarsosDSP

A Real-Time Audio Processing Framework in Java
http://0110.be/tag/TarsosDSP
GNU General Public License v3.0
1.97k stars 472 forks source link

soundPressureLevel(): fix bug in computation of sound pressure #177

Closed jmoudrik closed 4 years ago

jmoudrik commented 4 years ago

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.

JorenSix commented 4 years ago

Thanks for the fix!