Search code examples
androidaudiorecorddecibel

Audio Record class : calculate noise level


I'm trying to develop an android app (soundmeter) using the audio record class instead of the getMaxAmplitude() function.

I followed this link to do it:

Is there any Sound filtering library in android

I have a few questions about this code:

  1. how is calculated the power in calculatePowerDb() function? why we do the sqsum of the samples? in other terms, what does this equation "power = (sqsum - sum * sum / samples) / samples" represents?
  2. is the output value "return Math.log10(power) * 10f + FUDGE;" is the difference between two power (power leve/ref_power)?

In fact, I don't understand this function:

public final static double calculatePowerDb(short[] sdata, int off, int samples){

// Calculate the sum of the values, and the sum of the squared values.
// We need longs to avoid running out of bits.
   double sum = 0;
   double sqsum = 0;
   for (int i = 0; i < samples; i++) {
   final long v = sdata[off + i];
   sum += v; // Ok until here I understand
   sqsum += v * v; //why this??
   }

       double power = (sqsum - sum * sum / samples) / samples;//what's this equation?

   // Scale to the range 0 - 1.
   power /= MAX_16_BIT * MAX_16_BIT;

   // Convert to dB, with 0 being max power.  Add a fudge factor to make
   // a "real" fully saturated input come to 0 dB.
   return Math.log10(power) * 10f + FUDGE; //the value is at decibel? 
                                    //if it is, is it a db(a) value, 
                                    //a power level value 
                                    //or a pressure level value ?
   }

This code return a negative value (hope the sound power) and works fine after doing some calibration on devices (add/withdraw ratio)

Now, I want to understand some parts of this code (commented above).

Thanks for your responses and all details that can be done!


Solution

  • the power value is calculating the variance of the signal, and it is being normalized to the number of samples being used to calculate it. essentially it shows you how much spread there is between the highest recorded sample value's intensity and the lowest, or more precisely - it is a measure of the spread of the distribution of samples captured by the device. This is a commonly used measure of the power in a signal.

    the return value is just a standard definition of decibels when you have a signal's power given that your reference is power is MAX_16_BIT * MAX_16_BIT take a look at the wiki page for decibels here: http://en.wikipedia.org/wiki/Decibel

    you may also want to check out: http://en.wikipedia.org/wiki/Signal-to-noise_ratio