如何测试声级rms算法

时间:2013-04-21 08:53:31

标签: android signal-processing frequency audio-processing

我的应用。正在计算输入声音的噪音水平和频率峰值。 我用FFT来获取short []缓冲区数组,这就是代码: bufferSize = 1024,sampleRate = 44100

 int bufferSize = AudioRecord.getMinBufferSize(sapleRate,
                channelConfiguration, audioEncoding);
        AudioRecord audioRecord = new AudioRecord(
                MediaRecorder.AudioSource.DEFAULT, sapleRate,
                channelConfiguration, audioEncoding, bufferSize);

这是转换代码:

short[] buffer = new short[blockSize];
        try {
            audioRecord.startRecording();
        } catch (IllegalStateException e) {
            Log.e("Recording failed", e.toString());
        }
        while (started) {
            int bufferReadResult = audioRecord.read(buffer, 0, blockSize);

            /*
             * Noise level meter begins here
             */
            // Compute the RMS value. (Note that this does not remove DC).
            double rms = 0;
            for (int i = 0; i < buffer.length; i++) {
                rms += buffer[i] * buffer[i];
            }
            rms = Math.sqrt(rms / buffer.length);
            mAlpha = 0.9;   mGain = 0.0044;
            /*Compute a smoothed version for less flickering of the
            // display.*/
            mRmsSmoothed = mRmsSmoothed * mAlpha + (1 - mAlpha) * rms;
            double rmsdB = 20.0 * Math.log10(mGain * mRmsSmoothed);

现在我想知道这个算法是否正常工作还是我错过了什么? 我想知道它是否正确,我在手机上显示的声音是dB,如何测试? 我需要任何帮助,提前致谢:)

1 个答案:

答案 0 :(得分:1)

代码看起来正确但你应该处理缓冲区最初包含零的情况,这可能导致Math.log10失败,例如改变:

        double rmsdB = 20.0 * Math.log10(mGain * mRmsSmoothed);

为:

        double rmsdB = mGain * mRmsSmoothed >.0 0 ?
                           20.0 * Math.log10(mGain * mRmsSmoothed) :
                           -999.99;  // choose some appropriate large negative value here for case where you have no input signal