Decibels in android

2020-03-07 08:16发布

问题:

I have created a application that get the getMaxAmpitude and then converts it to decibels but i only get a range of 30dB.

Does an android phone only have a range of 30dB or is it a problem in my code?

public class getMaxAmpitude extends AsyncTask<Void, Float, Void>{
     String dB = "";    
     int ampitude;
     float db;
     @Override
    public Void doInBackground(Void... params) {
        while(rec == true){
            try{
                Thread.sleep(250);
            }catch(InterruptedException e){
                e.printStackTrace();
            }
            ampitude = recorder.getMaxAmplitude();
            db =(float) (20 * Math.log10(ampitude/700.0));
            publishProgress(db);
        }
        return null;
    }
    public void onProgressUpdate(Float... progress){

        //database.addSoundData(dB);
        dB = (progress[0].toString());

回答1:

The decibel scale is always a measure relative to something - and in the case of digital audio, that something is customarily the largest value that a sample is capable of holding. This is either 1.0 for floating point representations of samples and 32767 for signed 16-bit samples. Calculated dB values are negative, with 0dB being clip.

Without knowledge of what precisely what recorder is an instance of, I will assume it's Android's MediaRecorder - which uses signed 16-bit ints.

The correct equation in this case would therefore be:

db = 20.0 * log10(peakAmplitude/32767.0)

The equation you've used above however, merely biases the results as it's eqivelant to:

db = 20.0 * log10(peakAmplitude) - 20.0 * log10(700)

=20.0*log10(peakAmplitude) - ~56.9

The dynamic range figure of 30dB might not be surprising if you're recording the onboard microphone and automatic gain control is enabled - as it will be unless you've taken active steps to disable it.