3

How can I calculate A-weighted and C-weighted dB sound levels from the microphone on iOS?

Here is what I have tried, but the reading I get is far below the sound level meter I have next to my iPhone:

  1. Using the Novocain library, which I have slightly modified to set the audio session mode to Measurement.
  2. Using the Maximilian audio library to run the incoming audio frames through an FFT and converting the amplitudes into dB.
  3. Using the Maximilian audio libraries's Octave Analyser to place the FFT output into octave bins from 10hz to 20480hz.
  4. For each octave bin I am apply a db gain of the relevant dB-weighing (e.g. apply -70.f db gain to the db value stored in the 10hz bin to get an A-weighted dB gain).
  5. Added the db values of each bin together by reducing each dB bin to an amplitude, and the gain to an an amplitude, making the addition, and converting back to a dB value again.

Is this on the correct track, I have my doubts? Can someone outline an approach? Suggest a library and/or other example (I have looked).

To note – I would like approximate dB(A) and dB(C) values, this does not need to be scientific. Not sure how to compensate for the frequency response of the microphone, could the above technique be correct if it were compensating for the response of the microphone?

Ross
  • 14,266
  • 12
  • 60
  • 91

1 Answers1

6

I don't think you can measure physical Sound Pressure Levels from a device. In step 2 you "convert the amplitudes into dB." However the amplitude that you record from the device has arbitrary units. When recording 16-bit audio, the audio is represented as numbers in the range -32768 to +32767. If you are working with floating point data then this is normalised by 32768 so that it has a range of (approximately) -1 to +1.

The device's microphone has to cope with a wide variety of sound levels. Generally devices will have some form of Automatic Gain Control which adapts to the current average sound level. This means that if you measure a peak value of 1.0 then you have no way of knowing the actual SPL it corresponded to. You can convert a recording to a series of dBs, but this uses a different definition of dB: as a power ratio. This has no correlation with SPL measurements such as dB(A).

It may be possible to produce an approximate dB(A) measurement if you are able to turn off the AGC and calibrate your device against your sound meter

EDIT: JASA have published a paper with a detailed comparison of existing SPL measurement apps with stats for the comparison of different generations of iPhone: Evaluation of smartphone sound measurement applications

the_mandrill
  • 29,792
  • 6
  • 64
  • 93
  • I believe it is possible to approximate dV(A) and dB(C) - which will be good enough - as there are already apps on the App Store which do this. I've compared them with a sound level meter and they are quite close. In step 1., on iOS 6 I am disabling automatic gain control. – Ross Oct 26 '12 at 08:53
  • It's probable that those applications have been calibrated against a particular model. If you ran on a different generation of device then I imagine you wouldn't get the same results. – the_mandrill Oct 26 '12 at 11:21
  • Another consideration is that you need to take account of the frequency response of the microphone. The mic in a sound meter will be chosen for a flat response (or will be carefully calibrated to compensate for the actual response). The mic in your iPhone is very unlikely to have a flat response. How are you comparing the results? Are you using a sine wave generator at particular frequencies? – the_mandrill Oct 26 '12 at 11:39
  • I am comparing the results by taking both into different sound environments which will be used by the app. It would have been great to get the technique for calculating the dB values (the recipe for calculating the values is same surely even if the microphone is calibrated or not, adjusting the input for the response of the microphone is another problem/pre-step). Thanks for your answer, although I feel it does not answer my question of *how* this can be done, just that you believe it can't be. Therefore I won't award the bounty myself, although it may be awarded automatically. – Ross Nov 01 '12 at 17:00