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

最佳答案

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

int frequency = 8000;


至:

int frequency = 44100;

09-13 09:56