18KHz에서 19KHz 사이의 주파수를 가진 초음파를 모든 안드로이드 장치에서 얻고 싶습니다.주파수를 사용하여 안드로이드에서 초음파를 받으십시오.
주파수를 계산하기 위해 아래 코드를 사용하지만 정확한 주파수를 얻지 못하는 것 같습니다. 주파수는 11KHz와 13KHz 사이에 머물러 있습니다. 하나 개 this app로 보내기 초음파이 초음파를 얻기 위해 다른 하나를
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");
}
나는 두 개의 휴대 전화를 사용하고 있습니다. question을 시작 지점으로 사용했습니다. FFT 및 복합체 클래스입니다.
내 코드에 어떤 문제가 있습니까?
좋은 아이디어. 설명해 주시겠습니까 :'int freq = (int) (max_magnitude * sampleRate/bufferSize);'. – FelixMarcus
나는이 대답을 사용했다. (http://stackoverflow.com/a/7675171/1770833) 코드의 마지막 줄을 보자. – Drakkin
실수는 7 단계와 8 단계에있다. 최대 크기의 * index *가 필요하다. 강도 자체가 아닌 주파수를 결정합니다. 링크 된 답변의 의사 코드를 좀 더주의 깊게보십시오. –