简介:我正在尝试使用Android的AudioRecord来实现带通滤波器,并将经过滤波的音频写入PCM文件。
到目前为止,我的库尝试了什么: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
结果:当查看过滤的PCM文件时,两者都将导致过滤的音频输出失真。当前使用jDSP Butterworth带通滤波器的实现。我认为我没有从ByteArray正确转换为Double,反之亦然。以下是用于此实现的主要信息。
/**
* 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()
}
我真的想通了。使用https://github.com/JorenSix/TarsosDSP库,过滤后无需进行转换。它还包括在FFT库中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句