Android AudioRecord filter range of frequency

浪尽此生 提交于 2019-12-03 14:09:15

问题


I am using android platform, from the following reference question I come to know that using AudioRecord class which returns raw data I can filter range of audio frequency depends upon my need but for that I will need algorithm, can somebody please help me out to find algorithm to filter range b/w 14,400 bph and 16,200 bph.

I tried "JTransform" but i don't know can I achieve this with JTransform or not ? Currently I am using "jfftpack" to display visual effects which works very well but i can't achieve audio filter using this.

Reference here

help appreciated Thanks in advance. Following is my code as i mentioned above i am using "jfftpack" library to display you may find this library reference in the code please don't get confuse with that

private class RecordAudio extends AsyncTask<Void, double[], Void> {
        @Override
        protected Void doInBackground(Void... params) {
try {
    final AudioRecord audioRecord = findAudioRecord();
                    if(audioRecord == null){
                        return null;
                    }

                    final short[] buffer = new short[blockSize];
                    final double[] toTransform = new double[blockSize];

                    audioRecord.startRecording();


    while (started) {
                        final 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);

                    }
audioRecord.stop();
                audioRecord.release();
} catch (Throwable t) {
                Log.e("AudioRecord", "Recording Failed");
            }
            return null;

/**
         * @param toTransform
         */
        protected void onProgressUpdate(double[]... toTransform) {
            canvas.drawColor(Color.BLACK);
            for (int i = 0; i < toTransform[0].length; i++) {
                int x = i;
                int downy = (int) (100 - (toTransform[0][i] * 10));
                int upy = 100;
                canvas.drawLine(x, downy, x, upy, paint);
            }
            imageView.invalidate();
        }

回答1:


There are a lot of tiny details in this process that can potentially hang you up here. This code isn't tested and I don't do audio filtering very often so you should be extremely suspicious here. This is the basic process you would take for filtering audio:

  1. Get audio buffer
  2. Possible audio buffer conversion (byte to float)
  3. (optional) Apply windowing function i.e. Hanning
  4. Take the FFT
  5. Filter frequencies
  6. Take inverse FFT

I'm assuming you have some basic knowledge of Android and audio recording so will cover steps 4-6 here.

//it is assumed that a float array audioBuffer exists with even length = to 
//the capture size of your audio buffer

//The size of the FFT will be the size of your audioBuffer / 2
int FFT_SIZE = bufferSize / 2;
FloatFFT_1D mFFT = new FloatFFT_1D(FFT_SIZE); //this is a jTransforms type

//Take the FFT
mFFT.realForward(audioBuffer);

//The first 1/2 of audioBuffer now contains bins that represent the frequency
//of your wave, in a way.  To get the actual frequency from the bin:
//frequency_of_bin = bin_index * sample_rate / FFT_SIZE

//assuming the length of audioBuffer is even, the real and imaginary parts will be
//stored as follows
//audioBuffer[2*k] = Re[k], 0<=k<n/2
//audioBuffer[2*k+1] = Im[k], 0<k<n/2

//Define the frequencies of interest
float freqMin = 14400;
float freqMax = 16200;

//Loop through the fft bins and filter frequencies
for(int fftBin = 0; fftBin < FFT_SIZE; fftBin++){        
    //Calculate the frequency of this bin assuming a sampling rate of 44,100 Hz
    float frequency = (float)fftBin * 44100F / (float)FFT_SIZE;

    //Now filter the audio, I'm assuming you wanted to keep the
    //frequencies of interest rather than discard them.
    if(frequency  < freqMin || frequency > freqMax){
        //Calculate the index where the real and imaginary parts are stored
        int real = 2 * fftBin;
        int imaginary = 2 * fftBin + 1;

        //zero out this frequency
        audioBuffer[real] = 0;
        audioBuffer[imaginary] = 0;
    }
}

//Take the inverse FFT to convert signal from frequency to time domain
mFFT.realInverse(audioBuffer, false);


来源:https://stackoverflow.com/questions/10911189/android-audiorecord-filter-range-of-frequency

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!