Jtransforms,输出频率不准确。

3
我正在开发一款用于Google Glass的应用程序,它可以实时(大约)显示自录音以来的峰值电流峰值频率。我的当前问题是频率报告变化非常快,因此很难确定频率,而且我不确定我的NumberFormat输出格式是否正确,因为它只能达到“00.000”。我可能需要一些关于窗口的帮助,但我对它的理解还可以。谢谢!
public class RTAactivity extends Activity {

private static final int SAMPLING_RATE = 44100;

private TextView tvfreq;
private TextView tvdb;

private RecordingThread mRecordingThread;
private int mBufferSize;
private short[] mAudioBuffer;
private String mDecibelFormat;
private double  mFreqFormat = 0.0;
private int blockSize = 1024;  //4096
private DoubleFFT_1D fft;
private int[] bufferDouble, bufferDouble2;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.rta_view);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    tvfreq = (TextView) findViewById(R.id.tv_freq);
    tvdb = (TextView) findViewById(R.id.tv_decibels);

    // Compute the minimum required audio buffer size and allocate the buffer.
    mBufferSize = AudioRecord.getMinBufferSize(SAMPLING_RATE, AudioFormat.CHANNEL_IN_MONO,
            AudioFormat.ENCODING_PCM_16BIT);
    mAudioBuffer = new short[mBufferSize / 2];
    bufferDouble2 = new int[mBufferSize /2];
    bufferDouble = new int[(blockSize-1) * 2 ];

    mDecibelFormat = getResources().getString(R.string.decibel_format);
}

@Override
protected void onResume() {
    super.onResume();

    mRecordingThread = new RecordingThread();
    mRecordingThread.start();
}

@Override
protected void onPause() {
    super.onPause();

    if (mRecordingThread != null) {
        mRecordingThread.stopRunning();
        mRecordingThread = null;
    }
}
private class RecordingThread extends Thread{

    private boolean mShallContinue = true;

    @Override
    public void run() {
        android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_AUDIO);

        AudioRecord record = new AudioRecord(AudioSource.MIC, SAMPLING_RATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, mBufferSize);

        short[] buffer = new short[blockSize];
        double[] audioDataDoubles = new double[(blockSize * 2)];
        double[] re = new double[blockSize];
        double[] im = new double[blockSize];
        double[] magnitude = new double[blockSize];

        //start collecting data
        record.startRecording();



        DoubleFFT_1D fft = new DoubleFFT_1D(blockSize);

        while (shallContinue()) {

            /**decibels */
            record.read(mAudioBuffer, 0, mBufferSize / 2);
            updateDecibelLevel();

            /**frequency */
                ///windowing!?
            for(int i=0;i<mAudioBuffer.length;i++) {
                bufferDouble2[i] = (int) mAudioBuffer[i];
            }

            for(int i=0;i<blockSize-1;i++){
                double x=-Math.PI+2*i*(Math.PI/blockSize);
                double winValue=(1+Math.cos(x))/2.0;
                bufferDouble[i]= (int) (bufferDouble2[i]*winValue); }

               // bufferDouble[2*i]=bufferDouble2[i];
               // bufferDouble[2*i+1] = (int) 0.0;}


            int bufferReadResult = record.read(buffer, 0, blockSize);

            // Read in the data from the mic to the array
            for (int i = 0; i < blockSize && i < bufferReadResult; i++) {
                audioDataDoubles[2 * i] = (double) buffer[i] / 32768.0; // signed 16 bit
                audioDataDoubles[(2 * i) + 1] = 0.0;
            }

        //audiodataDoubles now holds data to work with
        fft.complexForward(audioDataDoubles);   //complexForward


        // Calculate the Real and imaginary and Magnitude.

        for (int i = 0; i < blockSize; i++) {
            double real = audioDataDoubles[2 * i];
            double imag = audioDataDoubles[2 * i + 1];
            magnitude[i] = Math.sqrt((real * real) + (imag * imag));
        }
        for (int i = 0; i < blockSize; i++) {
            // real is stored in first part of array
            re[i] = audioDataDoubles[i * 2];
            // imaginary is stored in the sequential part
            im[i] = audioDataDoubles[(i * 2) + 1];
            // magnitude is calculated by the square root of (imaginary^2 + real^2)
            magnitude[i] = Math.sqrt((re[i] * re[i]) + (im[i] * im[i]));
        }

        double peak = -1.0;
        // Get the largest magnitude peak
        for (int i = 0; i < blockSize; i++) {
            peak = magnitude[i];
        }

        // calculated the frequency
        mFreqFormat = (SAMPLING_RATE * peak) / blockSize;
        updateFrequency();

    }

        record.stop();   //stop recording please.
        record.release();  // Deystroy the recording, PLEASE!
    }

    /**true if the thread should continue running or false if it should stop
    */
    private synchronized boolean shallContinue() {return mShallContinue; }

    /** Notifies the thread that it should stop running at the next opportunity. */
    private synchronized void stopRunning() { mShallContinue = false; }


    private void updateDecibelLevel() {
        // Compute the root-mean-squared of the sound buffer and then apply the formula for
        // computing the decibel level, 20 * log_10(rms). This is an uncalibrated calculation
        // that assumes no noise in the samples; with 16-bit recording, it can range from
        // -90 dB to 0 dB.
        double sum = 0;

        for (short rawSample : mAudioBuffer) {
            double sample = rawSample / 32768.0;
            sum += sample * sample;
        }

        double rms = Math.sqrt(sum / mAudioBuffer.length);
        final double db = 20 * Math.log10(rms);

        // Update the text view on the main thread.
        tvdb.post(new Runnable() {
            @Override
            public void run() {
                tvdb.setText(String.format(mDecibelFormat, db));
            }
        });
    }

  }
           /// post the output frequency to TextView
private void updateFrequency() {
    tvfreq.post(new Runnable() {
        @Override
        public void run() {
            NumberFormat nM = NumberFormat.getNumberInstance();
            tvfreq.setText(nM.format(mFreqFormat) + " hz");
        }
    });


}

}


你需要检查一下你的代码 - 由于某些原因,你在计算幅度时重复了两次(无害但毫无意义),但更重要的是,你的峰值查找循环完全出错了。 - Paul R
2个回答

1

您的代码存在一些问题,但最重要的问题是您的峰值查找循环完全出错了——请更改:

    double peak = -1.0;
    // Get the largest magnitude peak
    for (int i = 0; i < blockSize; i++) {
        peak = magnitude[i];
    }

至:

    double peak_val = magnitude[0];   // init magnitude of peak
    peak = 0;                         // init index of peak
    for (int i = 1; i < blockSize; i++) {
        double val = magnitude[i];
        if (val > peak_val) {
            peak_val = val;           // update magnitude of peak
            peak = i;                 // update index of peak
        }
    }

1
谢谢PAUL R!在构建过程中,我必须说我变成了你的粉丝,并从你在Stack Overflow上的帖子中学到了很多。我已经按照上面所述实施了你的修复方法,似乎解决了我的问题,现在我播放时通过Klipsch音箱可以读取440赫兹!但我注意到一个奇怪的事情是有时候读数会跳到43281赫兹?此外,如果您能指出其他几个给自己带来的问题,那将不胜感激。再次感谢您的回复。 - Kjacksonmusic
很抱歉,当我回放440hz时读取到的是430hz,实际上偏差约为10hz。我会重新检查我的计算,并明天尝试在另一组扬声器上测试。如果有更多想法,请告诉我。谢谢! - Kjacksonmusic
1
很高兴现在至少部分工作正常了。请注意,您的FFT分辨率仅为44100/1024 = 43 Hz,因此您可能会在第10个bin中看到440 Hz的峰值,这给出了一个估计频率为430 Hz。至于代码的其他问题,我已经在评论中提到了冗余的幅度计算,但稍后我会再次检查是否还有其他问题。 - Paul R
1
我也删除了第一个量级的计算,感谢提醒,不知道怎么会错过那个... - Kjacksonmusic
我不是Java或Android专业人士,所以我无法给你具体的信息,但我相信有图形/图表/绘图库或API可用于显示频谱。 - Paul R
显示剩余4条评论

1
已添加:仅使用FFT的峰值幅度频率分辨率将被设置(量化)为采样率除以FFT长度(对于您的参数,为44100/1024 Hz)。 对于短FFT,430 Hz可能是最接近440的FFT结果容器。 要获得更好的效果,需要进行插值,使用更长的FFT或使用另一种频率估计算法。
如果您要显示音高频率(音乐音高或语音音高),则这通常与从FFT结果获得的峰值频谱频率不同。 查找音高检测/估计方法(许多学术论文涉及此问题),因为这通常需要比计算FFT幅度峰值更复杂和强大的算法。

虽然这是正确的,而且OP可能需要做更多的研究,但它并没有解决直接问题(代码中的错误),所以它应该只是一个评论而不是答案。 - Paul R
我的目标只是找到共振频率。我是一名音频工程师,每天都要设置音响系统并进行测试。我已经花了很多时间做研究,以便达到这个水平,如果您有任何论文可以推荐给我,以帮助我进一步理解,那将不胜感激! - Kjacksonmusic

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接