Android fft获得更高的频率范围

时间:2013-07-23 11:57:19

标签: java android fft microphone

我在https://github.com/sommukhopadhyay/FFTBasedSpectrumAnalyzer处使用了代码。我遇到的问题是频谱图只显示低范围。

如何修改代码以便我可以看到更完整的频率范围?

int frequency = 8000;
int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;

private class RecordAudio extends AsyncTask<Void, double[], Void> {
    @Override
    protected Void doInBackground(Void... params) {

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

        short[] buffer = new short[blockSize];
        double[] toTransform = new double[blockSize];
        try{
            audioRecord.startRecording();
        }
        catch(IllegalStateException e){

        }
        while (started) {
            int 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);
        }
        try{
            audioRecord.stop();
        }
        catch(IllegalStateException e){

        }

        return null;
    }
}

1 个答案:

答案 0 :(得分:3)

如果您的采样率为8 kHz,那么您将只能看到0到4 kHz的频率。对于更大的频率范围,您需要提高采样率,例如:如果将其设置为44.1 kHz,则会看到0到22.05 kHz的频率。我不确定您的特定设备在Android上支持哪些采样率,但请尝试更改:

int frequency = 8000;

为:

int frequency = 44100;