Java 在android中实现带通滤波器

Java 在android中实现带通滤波器,java,android,audio,audio-recording,bandpass-filter,Java,Android,Audio,Audio Recording,Bandpass Filter,我所做的: 我正在使用录音机从蓝牙录制音频,将其保存为PCM,然后将其更改为.wav文件。这是我的最终输出 我需要20-400Hz的音频。我需要应用我正在使用的带通滤波器 为了解决我的问题,我按照问题的解决办法 这是我的录音课: private class myThread extends AsyncTask<String, String, String>{ @Override protected void onPreExecute() { supe

我所做的:

我正在使用录音机从蓝牙录制音频,将其保存为PCM,然后将其更改为.wav文件。这是我的最终输出

我需要20-400Hz的音频。我需要应用我正在使用的带通滤波器

为了解决我的问题,我按照问题的解决办法

这是我的录音课:

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);
    }
}
私有类myThread扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
}
@RequiresApi(api=Build.VERSION\u code.LOLLIPOP)
@凌驾
受保护的字符串背景(字符串…字符串){
while(isRecording){
试一试{
int-cAmplitude=0;
int read=audioRecord.read(数据,0,缓冲区大小);
float[]信号=byteToFloat(数据);
信号=带通滤波器(信号);
audioTrack.write(信号,0,信号.length(),写入非阻塞);
byte[]bytes=新字节[Data.length];//每个音频两个字节
//帧,16位
for(inti=0,bufferIndex=0;i>>8);//高位字节
}
写操作(字节);
}捕获(例外情况除外){
Log.e(“错误”,“读写失败:”+ex.getMessage());
}
}
返回null;
}
@凌驾
受保护的void onPostExecute(字符串s){
super.onPostExecute(s);
}
}
以下是带通滤波器类:

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;
}
public float[]带通滤波器(float[]振幅){
对于(int i=0;i<振幅.长度;i++){
int-highCutoff=20;
int低截止=400;
双中心FREQ=(高截止+低截止)/2.0;
双宽度=数学绝对值(高截止-低截止);
巴特沃斯巴特沃斯=新巴特沃斯();
带通(4,采样器,中心频率,宽度);
振幅[i]=(浮点)巴特沃斯滤波器(振幅[i]);
}
回波幅度;
}
问题

在audacity中检查时,我获得的音频文件的频率仍在0到4k之间:

  • 这是否意味着音频未被过滤
  • 带通滤波器函数或其库是否存在任何问题
  • 字节到短字节和浮点转换有问题吗
  • 或者rawToWav()函数中存在问题
  • 错在哪里?
    可能是我在过滤原始音频,在转换后,我对你的问题投了更高的票,因为这确实显示了我的努力,再好不过了。也就是说,你的问题非常专业,我希望你能找到能帮忙的人:)过滤器的设置不在for循环之外吗?过滤器需要知道几个样本才能进行过滤。在当前设置中,它只看到一个示例。@Henry filter已超出for循环。在for循环内部,我只是将float[]转换回byte[]进行写入。