首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何测试声级rms算法

如何测试声级rms算法
EN

Stack Overflow用户
提问于 2013-04-21 16:53:31
回答 1查看 4.3K关注 0票数 3

我的应用。正在计算输入声音的噪声水平和频率峰值。我使用快速傅立叶变换得到shorts[]缓冲区的数组,代码如下: bufferSize = 1024,sampleRate = 44100

代码语言:javascript
复制
 int bufferSize = AudioRecord.getMinBufferSize(sapleRate,
                channelConfiguration, audioEncoding);
        AudioRecord audioRecord = new AudioRecord(
                MediaRecorder.AudioSource.DEFAULT, sapleRate,
                channelConfiguration, audioEncoding, bufferSize);

这是转换代码:

代码语言:javascript
复制
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格式的声音,如何测试它?我需要任何帮助,请提前谢谢:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-21 17:05:40

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

代码语言:javascript
复制
        double rmsdB = 20.0 * Math.log10(mGain * mRmsSmoothed);

至:

代码语言:javascript
复制
        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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16129480

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档