如何测试声级RMS算法声级、算法、测试、RMS

2023-09-06 16:25:37 作者:戾气i

我的应用程序。是计算噪声水平和输入声音的频率的峰值。 我用FFT得到的短裤[]缓冲区数组,这就是code: BUFFERSIZE = 1024,采样率= 44100

My app. is calculating noise level and peak of frequency of input sound. I used FFT to get array of shorts[] buffer , and this is the code : bufferSize = 1024, sampleRate = 44100

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

,这是将code:

and this is converting code :

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);

现在我想知道,如果这个算法正常工作或我失去了一些东西? 我想知道这是否是正确的,我有声音分贝显示在移动,如何测试呢? 我需要什么帮助请在此先感谢:)

Now I want to know if this algorithm works correctly or i'm missing something ? And I want to know if it was correct and i have sound in dB displayed on mobile , how to test it ? I need any help please , Thanks in advance :)

推荐答案

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

The code looks correct but you should probably handle the case where the buffer initially contains zeroes, which could cause Math.log10 to fail, e.g. change:

        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