我正在android中做声音分析仪应用程序。我可以使用AudioTrack Api生成18 khz到20 khz的超声波。我可以使用AudioRecord Api录制音频。但我不知道如何计算麦克风输入数据的频率。我看到了多个问题如何从 fft 结果中获取频率?并使用 FFT 和复杂类获取频率 wav 音频,它没有给出正确的频率。请帮助我。对不起我的交流。
这是我的频率计算代码
int bufferSizeInBytes = 1024;
short[] buffer = new short[bufferSizeInBytes];
class Recording extends Thread {
@Override
public void run() {
while (true) {
bufferReadResult = audioInput.read(buffer, 0, bufferSizeInBytes); // record data from mic into buffer
if(bufferReadResult > 0){
calculate();
}
}
}
public void calculate() {
DoubleFFT_1D fft1d = new DoubleFFT_1D(bufferSizeInBytes);//using JTransforms lib
double[] fftBuffer = new double[bufferSizeInBytes * 2];
double[] magnitude = new double[bufferSizeInBytes / 2];
// copy real input data to complex FFT buffer
for (int i = 0; i < bufferSizeInBytes - 1; ++i) {
fftBuffer[2 * i] = buffer[i];
fftBuffer[2 * i + 1] = 0;
}
//perform FFT on fft[] buffer
fft1d.realForward(fftBuffer);
// calculate power spectrum (magnitude) values from fft[]
for (int i = 0; i < (bufferSizeInBytes / 2) - 1; ++i) {
double real = fftBuffer[2 * i];
double imaginary = fftBuffer[2 * i + 1];
magnitude[i] = Math.sqrt(real * real + imaginary * imaginary);
}
// find largest peak in power spectrum
double max_magnitude = magnitude[0];
int max_index = 0;
for (int i = 0; i < magnitude.length; ++i) {
if (magnitude[i] > max_magnitude) {
max_magnitude = (int) magnitude[i];
max_index = i;
}
}
double freq = max_index * 44100 / bufferSizeInBytes;
Log.e("AudioBEacon", "" + freq);
}
}
这是我的输出。请让我知道我在哪里做错了。
02-10 12:33:04.450 17013-17063/com.org.sohamsaa.audiobeacontransmitter E/AudioBEacon: 21662.0
02-10 12:33:04.451 17013-17063/com.org.sohamsaa.audiobeacontransmitter E/AudioBEacon: 21317.0
02-10 12:33:04.453 17013-17063/com.org.sohamsaa.audiobeacontransmitter E/AudioBEacon: 21791.0
02-10 12:33:04.471 17013-17063/com.org.sohamsaa.audiobeacontransmitter E/AudioBEacon: 21748.0
02-10 12:33:04.472 17013-17063/com.org.sohamsaa.audiobeacontransmitter E/AudioBEacon: 21533.0
02-10 12:33:04.474 17013-17063/com.org.sohamsaa.audiobeacontransmitter E/AudioBEacon: 21834.0
02-10 12:33:04.491 17013-17063/com.org.sohamsaa.audiobeacontransmitter E/AudioBEacon: 21533.0
02-10 12:33:04.493 17013-17063/com.org.sohamsaa.audiobeacontransmitter E/AudioBEacon: 21705.0
02-10 12:33:04.511 17013-17063/com.org.sohamsaa.audiobeacontransmitter E/AudioBEacon: 21533.0
02-10 12:33:04.512 17013-17063/com.org.sohamsaa.audiobeacontransmitter E/AudioBEacon: 21447.0
02-10 12:33:04.513 17013-17063/com.org.sohamsaa.audiobeacontransmitter E/AudioBEacon: 21490.0
02-10 12:33:04.531 17013-17063/com.org.sohamsaa.audiobeacontransmitter E/AudioBEacon: 21576.0
02-10 12:33:04.551 17013-17063/com.org.sohamsaa.audiobeacontransmitter E/AudioBEacon: 21619.0
02-10 12:33:04.591 17013-17063/com.org.sohamsaa.audiobeacontransmitter E/AudioBEacon: 21877.0
02-10 12:33:04.613 17013-17063/com.org.sohamsaa.audiobeacontransmitter E/AudioBEacon: 21576.0
02-10 12:33:04.633 17013-17063/com.org.sohamsaa.audiobeacontransmitter E/AudioBEacon: 21920.0
02-10 12:33:04.653 17013-17063/com.org.sohamsaa.audiobeacontransmitter E/AudioBEacon: 21791.0
我的频率范围是 18 khz 到 20 khz。但我没有得到我的频率。如何过滤我的频率。谢谢。