Getting decibel from an Android microphone
Asked Answered
D

3

5

I have been searching for quite some time now, and I haven't been able to find a way to actually get an SPL/db value from a microphone on Android.

The few threads and articles that I have found that discussed it treated it like something so obvious that anyone should be able to do it, and were more concerned with getting their conversion algorithms correct.

Does anyone know how to do this?

Decretory answered 24/11, 2010 at 22:11 Comment(3)
Whoa I was just hoping for an API and a number. Do you really need to record to get the info. I was hoping just to listen.Foreman
As far as I have read through researching this, yes you do. The samples can be very short, like just a few milliseconds, but there does need to be a sample. However, you can delete the file as soon as you have made your calculation. I may be wrong, and there may be some other way to do it that I haven't learned yet, but I haven't read anything about it yet.Decretory
Yeah, I was just looking around and hoping it would be easy. I essentially what to just listen and play a sound based on the volume of the sound around it.Fleecy
S
6

You would have to first disable any automatic level control. I assume you either know, or know where to look to find out how to record samples from the microphone without ALC.

From then on, its basically all about the calibration and log magnitude. Essentially you will multiply or convolve by a frequency-dependent calibration and then take 10 * the log magnitude over all or some of the frequency range.

Then you'd have to take some calibration measurements, for example frequency sweeping a lab source of controlled, known spl and recording the resulting amplitude vs frequency (which if you sweep slowly is essentially the same as amplitude vs. time) or you'd hit it with a wideband pseudo impulse (such as an electric spark, starter pistol, whatever) that you are also measuring across frequency with good lab equipment in a radiation-symmetric position, or as a last resort you use some known physical property of the source to estimate the SPL and just hope it's flat enough across frequencies of interest.

You then have two choices for how to apply the correction.

One option is to invert the transfer function, calculate the time domain impulse response, and then use that as a digital filter by convolving it with an input signal to apply a frequency-flatness-correction. You can then take the log magnitude of the result per unit time to determine wideband spl.

Or you can do an FFT on the input data to generate a frequency spectrum, and multiply that by the inverse of your frequency response, and take the log magnitude of whatever (sum of) bin(s) is interesting.

Unfortunately, there's little reason to assume more than crude consistency from device to device, even of the same model.

Stoic answered 24/11, 2010 at 23:11 Comment(2)
Thanks for that! Sadly after all that work, I found out that I didn't even need to get the actual decibel value, though. That's programming for you I guess. Thanks anyway!Decretory
You're VERY right on that last statement you made. My app is now "finished" and I've been testing it on a few different devices, and the readings they give back can have values 10X that of other devices. Therefore, I had to incorporate a calibration function that would compare values of relative silence to our base line for silence. Thanks for including that!Decretory
D
14

While working on other related apps, and doing research, I have come across some information that would be useful to anyone who is looking into something such as this.

You see, the microphone on an Android phone will return a value between 0 and +- 32,400. For the most part, anything over 2,000 is really loud, such as a concert, club, or mechanical noise. "Relative Silence" is around 30. However, these values' meanings are relative, and will differ from phone to phone. Several devices sitting right next to each other will return different values, because of the microphones in them. The values can differ a little, or a lot.

Just testing several different android powered phones in my office, I have found that some devices would return values 10× that of other devices in the same situation. This is why there hasn't been anyone who has just said "Yeah, you can get the decibel value, here's some sample code:" because there is NOT really a reliable (and easy) way TO do it, realistically.

If you uploaded it on one device, you could get, say, 30 dB, but another would return 300dB. Clearly, no human would be in an evironment with 300 dB without massive ear protection. The values have to be taken as relative values; how they relate to each other. "Calibration", whether dynamic or otherwise, is a must if you want to get values that you can use. There has to be an established baseline, that cannot come from you, which has to be compared to a value from you. For example, relative silence, as stated before, should be around 30.

If the phone returns 456 in a relative silence environment, then simply divide yours (30) by theirs (456), and you have a factor that will serve as adjusting the values returned by the phone so that they match what you are expecting to see, and those values can be used for some degree of reliability.

This is just what I have found over the past few months, I hope it helps someone.

Decretory answered 23/2, 2011 at 22:12 Comment(1)
hi , your work is really helping me out. but I have a question. do you calibrate the decibel values calculated or the short datas got from read function?? RegardsClaudineclaudio
S
6

You would have to first disable any automatic level control. I assume you either know, or know where to look to find out how to record samples from the microphone without ALC.

From then on, its basically all about the calibration and log magnitude. Essentially you will multiply or convolve by a frequency-dependent calibration and then take 10 * the log magnitude over all or some of the frequency range.

Then you'd have to take some calibration measurements, for example frequency sweeping a lab source of controlled, known spl and recording the resulting amplitude vs frequency (which if you sweep slowly is essentially the same as amplitude vs. time) or you'd hit it with a wideband pseudo impulse (such as an electric spark, starter pistol, whatever) that you are also measuring across frequency with good lab equipment in a radiation-symmetric position, or as a last resort you use some known physical property of the source to estimate the SPL and just hope it's flat enough across frequencies of interest.

You then have two choices for how to apply the correction.

One option is to invert the transfer function, calculate the time domain impulse response, and then use that as a digital filter by convolving it with an input signal to apply a frequency-flatness-correction. You can then take the log magnitude of the result per unit time to determine wideband spl.

Or you can do an FFT on the input data to generate a frequency spectrum, and multiply that by the inverse of your frequency response, and take the log magnitude of whatever (sum of) bin(s) is interesting.

Unfortunately, there's little reason to assume more than crude consistency from device to device, even of the same model.

Stoic answered 24/11, 2010 at 23:11 Comment(2)
Thanks for that! Sadly after all that work, I found out that I didn't even need to get the actual decibel value, though. That's programming for you I guess. Thanks anyway!Decretory
You're VERY right on that last statement you made. My app is now "finished" and I've been testing it on a few different devices, and the readings they give back can have values 10X that of other devices. Therefore, I had to incorporate a calibration function that would compare values of relative silence to our base line for silence. Thanks for including that!Decretory
N
4

The open source app NoiseTube does this, including support for device specific calibration. Source code is available here. If you can access it I strongly recommend reading their article Participatory noise mapping works! An evaluation of participatory sensing as an alternative to standard techniques for environmental monitoring (available for pay).

Some highlights from the source:

Needs answered 2/9, 2014 at 13:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.