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:
- 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?
- 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!