Reputation: 13
I'm using the code over at https://github.com/sommukhopadhyay/FFTBasedSpectrumAnalyzer. The problem i'm running into is the spectrum graph only shows low ranges.
How can I modify the code so that I can see a more complete frequency range?
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;
}
}
Upvotes: 0
Views: 697
Reputation: 212929
If your sample rate is 8 kHz then you will only be able to see frequencies from 0 to 4 kHz. For a greater frequency range you need to increase the sample rate, e.g. if you set it to 44.1 kHz then you will see frequencies from 0 to 22.05 kHz. I'm not sure what sample rates are supported on Android with your particular device, but try changing:
int frequency = 8000;
to:
int frequency = 44100;
Upvotes: 3