java – 使用频率从android获取超声波
作者:互联网
我希望从任何Android设备获得超声波,例如频率介于18KHz和19KHz之间的超声波.
我使用下面的代码来计算频率,但它似乎没有让我得到正确的频率.我的频率保持在11 KHz和13KHz之间.
private void calculateFrequency()
{
// 1 - Initialize audio
int channel_config = AudioFormat.CHANNEL_CONFIGURATION_MONO;
int format = AudioFormat.ENCODING_PCM_16BIT;
int sampleRate = 8000;
int bufferSize = 2048;
if (bufferSize < AudioRecord.getMinBufferSize(sampleRate, channel_config, format))
bufferSize = AudioRecord.getMinBufferSize(sampleRate, channel_config, format);
AudioRecord audioInput = new AudioRecord(AudioSource.MIC, sampleRate, channel_config, format, bufferSize);
// 2 - Get sound
byte[] audioBuffer = new byte[bufferSize];
audioInput.startRecording();
int nbRead = audioInput.read(audioBuffer, 0, bufferSize);
audioInput.stop();
audioInput.release();
// 3 - Transform to double array
double[] micBufferData = new double[bufferSize];
final int bytesPerSample = 2; // As it is 16bit PCM
final double amplification = 100.0; // choose a number as you like
for (int index = 0, floatIndex = 0; index < nbRead - bytesPerSample + 1; index += bytesPerSample, floatIndex++) {
double sample = 0;
for (int b = 0; b < bytesPerSample; b++) {
int v = audioBuffer[index + b];
if (b < bytesPerSample - 1 || bytesPerSample == 1) {
v &= 0xFF;
}
sample += v << (b * 8);
}
double sample32 = amplification * (sample / 32768.0);
micBufferData[floatIndex] = sample32;
}
// 4 - Create complex array
Complex[] fftTempArray = new Complex[bufferSize];
for (int i=0; i<bufferSize; i++)
{
fftTempArray[i] = new Complex(micBufferData[i], 0);
}
// 5 - Calculate FFT
Complex[] fftArray = FFT.fft(fftTempArray);
// 6 - Calculate magnitude
double[] magnitude = new double[bufferSize / 2];
for (int i = 0; i < (bufferSize / 2); i++)
{
magnitude[i] = Math.sqrt(fftArray[i*2].re() * fftArray[i*2].re() + fftArray[i*2].im() * fftArray[i*2].im());
}
// 7 - Get maximum magnitude
double max_magnitude = -1;
for (int i = 0; i < bufferSize / 2; i++)
{
if (magnitude[i] > max_magnitude)
{
max_magnitude = magnitude[i];
}
}
// 8 - Calculate frequency
int freq = (int)(max_magnitude * sampleRate / bufferSize);
((TextView) findViewById(R.id.textView1)).setText("FREQUENCY = " + freq + "Hz");
}
我正在使用两部手机:一部分用this app发送超声波,另一部用于超声波.
我用这个question作为起点,我用FFT和复杂类.
我的代码出了什么问题?
解决方法:
为了获得正确的非混叠频率估计,必须使用更多的采样率(可能比滤波器滚降更大10%到20%),而不是音频输入中最高频率的两倍,因此超过两倍你想要找到的最高频率.
这是由于采样定理所需的奈奎斯特速率.
因此,如果要查找19 kHz信号,则需要接近48000的采样率.
标签:android,java,fft,signal-processing,audiorecord 来源: https://codeday.me/bug/20190728/1560794.html