Android的音频FFT使用audiorecord检索特定的频率幅度 [英] Android audio FFT to retrieve specific frequency magnitude using audiorecord

查看:378
本文介绍了Android的音频FFT使用audiorecord检索特定的频率幅度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在实施采用Android检测时,一些特定的音频频率范围是通过手机的麦克风打了一些code。我已经设置了类使用 AudioRecord 类:

I am currently trying to implement some code using Android to detect when a number of specific audio frequency ranges are played through the phone's microphone. I have set up the class using the AudioRecord class:

int channel_config = AudioFormat.CHANNEL_CONFIGURATION_MONO;
int format = AudioFormat.ENCODING_PCM_16BIT;
int sampleSize = 8000;
int bufferSize = AudioRecord.getMinBufferSize(sampleSize, channel_config, format);
AudioRecord audioInput = new AudioRecord(AudioSource.MIC, sampleSize, channel_config, format, bufferSize);

音频,然后阅读:

The audio is then read in:

short[] audioBuffer = new short[bufferSize];
audioInput.startRecording();
audioInput.read(audioBuffer, 0, bufferSize);

执行FFT是我被卡住,因为我有这方面的经验非常少。我一直在尝试使用这个类:

Performing an FFT is where I become stuck, as I have very little experience in this area. I have been trying to use this class:

FFT在Java中和的Complex类去用它

我然后再送以下值:

Complex[] fftTempArray = new Complex[bufferSize];
for (int i=0; i<bufferSize; i++)
{
    fftTempArray[i] = new Complex(audio[i], 0);
}
Complex[] fftArray = fft(fftTempArray);

这很容易被误解了我怎么这个类是为了工作,但返回的值跳所有的地方,并没有重新统一频率的presentative即使是在沉默。是任何人都知道的一种方式来执行这个任务,还是我的问题过于复杂,试图抢只有少数的频率范围,而不是将其拉为图形重新presentation?

This could easily be me misunderstanding how this class is meant to work, but the values returned jump all over the place and aren't representative of a consistent frequency even in silence. Is anyone aware of a way to perform this task, or am I overcomplicating matters to try and grab only a small number of frequency ranges rather than to draw it as a graphical representation?

推荐答案

首先,你需要确保你得到的结果是正确转换为float /双。我不知道怎么短[]版本的作品,但byte []的版本只返回原始字节版本。这个字节数组然后需要被正确地转换为一个浮点数。在code为转换应该是这个样子:

First you need to ensure that the result you are getting is correctly converted to a float/double. I'm not sure how the short[] version works, but the byte[] version only returns the raw byte version. This byte array then needs to be properly converted to a floating point number. The code for the conversion should look something like this:

    double[] micBufferData = new double[<insert-proper-size>];
    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 < bytesRecorded - bytesPerSample + 1; index += bytesPerSample, floatIndex++) {
        double sample = 0;
        for (int b = 0; b < bytesPerSample; b++) {
            int v = bufferData[index + b];
            if (b < bytesPerSample - 1 || bytesPerSample == 1) {
                v &= 0xFF;
            }
            sample += v << (b * 8);
        }
        double sample32 = amplification * (sample / 32768.0);
        micBufferData[floatIndex] = sample32;
    }

然后你使用micBufferData []创建输入一系列复杂的。

Then you use micBufferData[] to create your input complex array.

一旦你得到的结果,使用复数的幅度在结果中。大部分的幅度应该是接近零,除了具有实际值的频率。​​

Once you get the results, use the magnitudes of the complex numbers in the results. Most of the magnitudes should be close to zero except the frequencies that have actual values.

您需要的采样频率的数组索引这样的大小转换为频率:

You need the sampling frequency to convert the array indices to such magnitudes to frequencies:

private double ComputeFrequency(int arrayIndex) {
    return ((1.0 * sampleRate) / (1.0 * fftOutWindowSize)) * arrayIndex;
}

这篇关于Android的音频FFT使用audiorecord检索特定的频率幅度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆