更改块大小会导致FFT分析失败

时间:2016-02-08 17:52:57

标签: android audio fft

我正在尝试录制音频并获取频率。我可以成功地采用44100的采样率和2048的块大小。我相信,容器大小约为20。但是,如果我尝试将块大小增加到4096,那么我不会获得准确的频率,而是获得相同的不准确频率,没有幅度/分贝。

我的录音任务如下:

 private class RecordAudio extends AsyncTask<Void, double[], Boolean> {

    @Override
    protected Boolean doInBackground(Void... params) {

        int bufferSize = AudioRecord.getMinBufferSize(frequency,
                channelConfiguration, audioEncoding);
        audioRecord = new AudioRecord(
                MediaRecorder.AudioSource.DEFAULT, frequency,
                channelConfiguration, audioEncoding, bufferSize);
        int bufferReadResult;
        short[] buffer = new short[blockSize];
        double[] toTransform = new double[blockSize];
        try {
            audioRecord.startRecording();
        } catch (IllegalStateException e) {
            Log.e("Recording failed", e.toString());

        }
        while (started) {
            if (isCancelled() || (CANCELLED_FLAG == true)) {

                started = false;
                //publishProgress(cancelledResult);
                Log.d("doInBackground", "Cancelling the RecordTask");
                break;
            } else {
                bufferReadResult = audioRecord.read(buffer, 0, blockSize);

                for (int i = 0; i < blockSize && i < bufferReadResult; i++) {
                    toTransform[i] = (double) buffer[i] / 32768.0; // signed 16 bit
                }

                transformer.ft(toTransform);

                publishProgress(toTransform);

            }

        }
        return true;
    }
    @Override
    protected void onProgressUpdate(double[]...progress) {

        int mPeakPos = 0;
        double mMaxFFTSample = 150.0;
        for (int i = 100; i < progress[0].length; i++) {
            int x = i;
            int downy = (int) (150 - (progress[0][i] * 10));
            int upy = 150;
            //Log.i("SETTT", "X: " + i + " downy: " + downy + " upy: " + upy);

            if(downy < mMaxFFTSample)
            {
                mMaxFFTSample = downy;
                mMag = mMaxFFTSample;
                mPeakPos = i;
            }
        }

        mFreq = (((1.0 * frequency) / (1.0 * blockSize)) * mPeakPos)/2;
        //Log.i("SSS", "F: " + mFreq + " / " + "M: " + mMag);

        Log.i("SETTT", "FREQ: " + mFreq + " MAG: " + mMaxFFTSample);

    }
    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        try{
            audioRecord.stop();
        }
        catch(IllegalStateException e){
            Log.e("Stop failed", e.toString());

        }
    }
}

希望有一个快速解决方法,我很想念。感谢。

1 个答案:

答案 0 :(得分:1)

您需要仔细查看RealDoubleFft.ft函数的文档。进入函数的值是实数,但出来的值是复数FFT系数,因此toTransform[0]是第一个系数的实部,toTransform[1]是第一个系数的虚部,依此类推。最终的数组大小是相同的,但由于复数每个占2个双倍,所以总共有N / 2个系数,其中最后一个是sampleRate / 2的系数。

接下来,因为您对计算复数的大小所需的幅度感兴趣。对于复数x = a + bj,幅度|x| = sqrt(a*a + b*b)

    double maxMag = 0;
    int peakIndex = 0;
    for (int i = 0; i < progress[0].length/2; i++)
    {
        double re = progress[i*2];
        double im = progress[i*2+1];
        double mag = Math.sqrt(re*re + im*im);
        if (mag > maxMag)
        {
            peakIndex = i;
            maxMag = mag;
        }
    }

    double peakFreq = sampleRate/fftLen * i/2; // might need a bit of tweaking.
    double magInDb  = 20*Math.log10(mag);