# Get dB full scale value of a group of wave samples

I am trying to compute the dBFS value of a group of samples (stereo wave file), according to this formula:

$$p_{RMS} = \sqrt{\frac{ x_1^2 + x_2^2 + \ldots}n }$$ $$dbFS = 20\log_{10}\frac{ p_{RMS}}{p_{max}}$$

The value I get is wrong, and not stable (I am using a stereo constant -18dbFS 1000H sinewave audio file). I know the input/output of the program is working, because I did an fft on a music file and the output is fine.

float get_db(Header *header, unsigned int *buffer_size, int32_t left[], int32_t right[])
{
unsigned int i = 0;
float dbfs = 0;  //From -144 to 0dbfs
double pmoy = 0;
double pmax = 0;

pmax = maxint(header->bits_per_sample); //Max positive range of sample, here it is 8388608

for (i=0; i < *buffer_size; i++){ //Buffer size of 4096 here
pmoy =+ pow(left[i], 2);  //Just the left channel for now
}

printf("sum= %f\n", pmoy);  //temporary sum
pmoy = sqrt( pmoy / *buffer_size);

printf("pmoy= %f\n", pmoy);   //pmoy rms of 4096 samples
dbfs = 20 * log10( pmoy / pmax);

return dbfs;
}


I really don't understand why I don't get a constant value of -18dBFS. I am still learning C, there should be some obvious reasons and I hope you could help me. Is there a normalized definition of a dBFS scale for discrete values, and a particular integration time that should be used ?

• wow! is the syntax: "=+" the same as +=? i thought i knew C pretty well, but i didn't know that. and how big is *buffer_size? – robert bristow-johnson Jun 4 '17 at 3:17
• and are you sure of the value of pmax? – robert bristow-johnson Jun 4 '17 at 3:24
• I think you nailed it ! Only in ancient C versions =+ was considered to be equal to +=, now it appears to be just similar to =(+number) so I wasn't adding any value to the previous one ... I can't test the new code right now but I will update the result in the comment section. Thank you ! – WaspCo Jun 5 '17 at 11:37

Have you tried printing out the value of buffer_size? Depending on how you defined the variable that is passed as buffer_size, it could contain the address of buffer_size and NOT the value of 4096 which you're looking for.