Summary: Using Android's AudioRecord, I am trying to implement a bandpass filter and write the filtered audio to a PCM file.
What I libraries have tried so far: https://github.com/berndporr/iirj/blob/master/src/test/java/uk/me/berndporr/iirj/ButterworthTest.java https://github.com/psambit9791/jDSP/blob/master/src/main/java/com/github/psambit9791/jdsp/filter/Butterworth.java
Results: Both will result in distorted filtered audio output when viewing the filtered PCM file. Current Implementation using jDSP Butterworth bandpass filter. I think i am not converting correctly from ByteArray to Double and vice versa. Below are the main information used for this implementation.
/**
* This constructor initialises the prerequisites
* required to use Butterworth filter.
* @param s Signal to be filtered
* @param Fs Sampling frequency of input signal
*/
public Butterworth(double[] s, double Fs) {
this.signal = s;
this.samplingFreq = Fs;
}
/**
* This method implements a band pass filter with given parameters, filters the signal and returns it.
* @param order Order of the filter
* @param lowCutoff The lower cutoff frequency for the filter in Hz
* @param highCutoff The upper cutoff frequency for the filter in Hz
* @throws java.lang.IllegalArgumentException The lower cutoff frequency is greater than the higher cutoff frequency
* @return double[] Filtered signal
*/
public double[] bandPassFilter(int order, double lowCutoff, double highCutoff) throws IllegalArgumentException{
if (lowCutoff >= highCutoff) {
throw new IllegalArgumentException("Lower Cutoff Frequency cannot be more than the Higher Cutoff Frequency");
}
double centreFreq = (highCutoff + lowCutoff)/2.0;
double width = Math.abs(highCutoff - lowCutoff);
this.output = new double[this.signal.length];
uk.me.berndporr.iirj.Butterworth bp = new uk.me.berndporr.iirj.Butterworth();
bp.bandPass(order, this.samplingFreq, centreFreq, width);
for (int i=0; i<this.output.length; i++) {
this.output[i] = bp.filter(this.signal[i]);
}
return this.output;
}
private val AudioSource = MediaRecorder.AudioSource.MIC
private val SampleRate = 44100
private val Channel = AudioFormat.CHANNEL_IN_MONO
private val EncodingType = AudioFormat.ENCODING_PCM_16BIT
private var bufferSizeInByte: Int = 0
private fun writeDatatoFile() {
var audioData = ByteArray(bufferSizeInByte)
val file1 = UNFILTEREDPCM
val file2 = FILTEREDPCM
file1.createNewFile()
file2.createNewFile()
val out1 = BufferedOutputStream(FileOutputStream(file1))
val out2 = BufferedOutputStream(FileOutputStream(file2))
var length = 0
while (isRecord && audioRecorder != null) {
length = audioRecorder!!.read(audioData, 0, bufferSizeInByte) // get audio data
val butterworth = Butterworth(doubleArrayOf(ByteBuffer.wrap(audioData).getDouble()),44100.0)
val result = butterworth.bandPassFilter(2, 1525.0, 1625.0)
if (AudioRecord.ERROR_INVALID_OPERATION != length) {
out1.write(audioData, 0, length) // write file
out1.flush()
for (i in 0 until result.size) {
val newBuffer = ByteBuffer.allocate(bufferSizeInByte).putDouble(result[i]).array()
out2.write(newBuffer, 0, length)
}
out2.flush()
}
}
out1.close()
out2.close()
}
I actually figured it out. Using https://github.com/JorenSix/TarsosDSP, library there is no need for conversion after filtering. It also includes in FFT library.