1

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!

Community
  • 1
  • 1
amin89
  • 558
  • 1
  • 8
  • 26

1 Answers1

1

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

Semicolons and Duct Tape
  • 2,927
  • 4
  • 20
  • 35
  • Ok thanks for explanations, but do you know why to calculate this power value we do : power = (sqsum - sum * sum / samples) / samples ? – amin89 May 20 '14 at 10:31
  • 1
    yeah that is the variance calculation I was talking about. the power measure being used is one that tries to capture the range over which samples are spread out. variance is a way to do this. the computation is calculating the difference between the average of sig squared and sig average squared. this value gives you a good idea of how spread out the range of values is. if the values sampled had a bell curve distribution it would show you how wide the bell curve is. Take a look here http://en.wikipedia.org/wiki/Variance and remember that E[x] means average of x when you do. – Semicolons and Duct Tape May 20 '14 at 11:04
  • Ok I understand now, but in this particulary example we have: return Math.log10(power) * 10f + FUDGE 1- Correct me if i'm wrong, but if power = P(value) / P(ref), why P(ref) in this code is equal to MAX_16_bit * MAX_16_bit? 2- I've seen your link about SignalToNoise ratio, but is there a difference here between P(value) - P(ref) and P(Signal) - P(Noise) ? 3- and Finally, isn't more accurate to write double variance = (sqsum - sum * sum / samples) / samples; instead of double power = (sqsum - sum * sum / samples) / samples;? thank you. – amin89 May 20 '14 at 11:27
  • 1) Mathematically P(ref) can be anything using the max value makes sure that when you take the log of the power the returned value maxes out at 0. it is just a scaling and has nothing to do with the underlying theory. 2) no difference but be careful the P values as you describe are already in decibels and thus already in a logarithmic scale. 3)those are the same equations. if you are wondering about the naming convention difference between variance and power than "more correct" is a weird way to phrase it. for signal processing people this is a normal convention for mathematicians it's not. – Semicolons and Duct Tape May 20 '14 at 11:37
  • 1- Ok so if I'm understanding this code, is it right to say: SNR(db) = 10 log(10)(P_signal/P_noise) = 10 log(10) ((sqsum-sum*sum/samples)/samples) / MAX_16_bit * MAX_16_bit ? 2- And if it's correct, my problem is still to understand how the power of the signal could be equal to the variance!? – amin89 May 20 '14 at 11:55
  • It is literally a definition thing. Take a look at this to get your bearings: http://en.wikipedia.org/wiki/Spectral_density – Semicolons and Duct Tape May 20 '14 at 12:07
  • Last question: if SNR(db) = 10 log(10)(P_signal/P_noise) = 10 log(10) ((sqsum-sum*sum/samples)/samples) / MAX_16_bit * MAX_16_bit, is it normal that the values that i have are all negatives ? (the developer of this code said that all values are between 0 and -95 db) 2- And how can I do to have a db(a) value? – amin89 May 20 '14 at 12:42
  • the negative values are a result of scaling to the MAX_16_bit. they are fine and expected. not sure what 2 is asking so I need you to clarify. also don't forget to mark the answer as accepted and up vote it if it helps. – Semicolons and Duct Tape May 20 '14 at 12:48
  • If the negative values are fine and expected due to the scaling to the MAX_16_bit * MAX_16_bit power reference, how can I convert these values to db(a) values (Like what a soundmeter shows the values) In other terms, if I have SNR(db) = -48 now, what's the value into decibel A ? – amin89 May 20 '14 at 13:00
  • in that case -48 is the value if you want to reference from a different point just calculate the minimum value you will encounter (-144 I believe) and show how many decibels above that you are. – Semicolons and Duct Tape May 20 '14 at 13:28
  • Let me explain to you what I need maybe you'll understand what I mean. In fact, I want to measure the Ambient noise level (exactly what we have with a sound level meter) I want to know how to use the power of signal measured until now (all the negative values) to determinate ambiant noise level? thanks – amin89 May 20 '14 at 13:46
  • the easiest thing to do is comment out the MAX_16_BIT line. this will be the same as dividing by 1 and therefore using 1 as your reference point. the upshot is that all the readings will be positive and they will get bigger as your noise level increases. – Semicolons and Duct Tape May 20 '14 at 16:00
  • Yes that's what I've done. It seems more realistic now, but are you sure that's a sound pressure value ? I'm confused when I've seen this comments http://stackoverflow.com/questions/14937023/sound-meter-android/14937665#14937665 – amin89 May 20 '14 at 16:30
  • it is NOT a sound pressure value. it is a measure of the power in the signal that your device recorded which is related to the pressure on the microphone as it is recording. if you need an exact calibrated measurement you really should purchase professional lab equipment. – Semicolons and Duct Tape May 20 '14 at 16:42
  • Ok I understand. There is a good app on the google store which can measure the sound pressure value .. I don't have any idea of how it works :/ https://play.google.com/store/apps/details?id=kr.sira.sound&hl=en It's very accurate I've tested it with a sound level meter ... Have you any idea? – amin89 May 20 '14 at 16:52
  • probably some sort of calibration / curve fitting with a traceable source. I would be very surprised to see it work across all devices because they physical dimensions of the devices will be different. – Semicolons and Duct Tape May 20 '14 at 17:22
  • It's calibrated for each device which was tested with a sound level meter ... each calibration add a db ratio manually. Thank you for all your responses ;-) – amin89 May 20 '14 at 17:57