Implementing BandPass filter in android

626 views Asked by At

WHAT I DID:

I am recording audio from Bluetooth using AudioRecorder, saving it as PCM and then changing it into .wav file. That's my final output.

I need audio in frequency range 20-400Hz. I need to apply BandPass Filter for which I am using this.

To solve my problem I followed solution for question already posted.

This is my recording class:

private class myThread extends AsyncTask<String, String, String>{

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected String doInBackground(String... strings) {

        while (isRecording) {
            try {
                int cAmplitude = 0;

                int read = audioRecord.read(Data, 0, bufferSize);

                float[] signals = byteToFloat(Data);
                signals = BandPassFilter(signals);
                audioTrack.write(signals, 0, signals.length(), WRITE_NON_BLOCKING);

                byte[] bytes = new byte[Data.length]; // two bytes per audio
                // frame, 16 bits

                for (int i = 0, bufferIndex = 0; i < bytes.length; i++) {
                    short x = (short) (signals[bufferIndex++] * 32767.0); // [2^16 - 1]/2 =
                    // 32767.0
                    bytes[i] = (byte) x; // low byte
                    bytes[++i] = (byte) (x >>> 8); // high byte
                }

                os.write(bytes);

            } catch (Exception ex) {
                Log.e("Error", "Read write failed: " + ex.getMessage());
            }
        }

        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
    }
}

Here is BandPass filter class:

public float[] BandPassFilter(float[] amplitude){

    for (int i = 0; i< amplitude.length; i++) {

        int highCutoff = 20;
        int lowCutoff = 400;

        double centreFreq = (highCutoff + lowCutoff) / 2.0;
        double width = Math.abs(highCutoff - lowCutoff);

        Butterworth butterworth = new Butterworth();
        butterworth.bandPass(4, SampleRate, centreFreq, width);
        amplitude[i] = (float) butterworth.filter(amplitude[i]);

    }
    return amplitude;
}

PROBLEM

The audio file I get still has frequencies between 0 and 4k when i check it in audacity:

enter image description here

  1. Does this mean audio is not filtered?
  2. Is there any problem in BandPass filter function or its library?
  3. Is there any problem in byte to short and to float conversion?
  4. or there is problem in rawToWav() function.

Where is the mistake? May be I am filtering raw audio and after conversion it

0

There are 0 answers