7
$\begingroup$

I'm receiving PCM samples through stream which is one channel, 8000 samples per second. I figured out way of finding out loudness by calculating 'force' of signal by adding abs value of every next 1000 samples and then dividing by samples count which is 1000. It works somewhat good but sometimes my calculations are going crazy, my sum of absolute values of samples seems to constantly build up and reach ridiculous values... so I think that I'm doing something wrong there. Funny thing is that when calculations are going crazy everything will go back to normal when I hit 'sound input device' with my hand ;)

Anyway. My question is: how to properly calculate loudness of set of PCM samples?

$\endgroup$
  • $\begingroup$ What numeric format are you using when calculating the sum? Sounds like you might be experiencing overflow. $\endgroup$ – Jason R Jul 24 '12 at 3:11
  • 1
    $\begingroup$ I'm using 2 bytes, signed number. I don't have overflow. I did printing of my every samples to file in txt form and those samples in spite of silence were higher and higher over time. What is even stranger values didn't go below zero when it started to "build up". $\endgroup$ – solgar Jul 26 '12 at 9:00
9
$\begingroup$

To measure the energy (which is closely related to, but not the same as, "loudness"), calculate the RMS (Root-Mean-Square).

$ E = \sqrt{\frac{\displaystyle\sum\limits_{n=0}^{N-1} s[n]^2}{N}}$

$N$ is the number of samples and $s[n]$ is the sample at time $n$.

You can do this in any block size of samples. You can do it on all the samples at once to get an average energy, or you can break your sample set into chunks to see how the energy changes with time.

There are also more sophisticated methods that take into account the body and brain's quirks. You can find more information about that here.

$\endgroup$
  • 1
    $\begingroup$ Here is code in multiple languages for finding the root mean square. $\endgroup$ – Xeoncross Jan 7 '17 at 20:10
1
$\begingroup$

I'm no expert and not sure what you are trying to achieve, but it sounds like you've made a programming error somewhere, could you show us the code?

When you say loudness do mean it as defined here? In that case you need to account for the listener.

If you're just interested in energy, maybe RMS is a better choice, or averaged squares.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.