0

我在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;
    }
}
4

1 回答 1

3

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

int frequency = 8000;

至:

int frequency = 44100;
于 2013-07-23T12:48:30.013 回答