【问题标题】:How to implement a high-pass filter for an audio signal?如何为音频信号实现高通滤波器?
【发布时间】:2015-01-31 14:24:35
【问题描述】:

我正在尝试做一个像 Shazam 这样的音乐识别应用程序。这是一个安卓应用程序。首先,我通过 MIC 捕获了音频信号。接下来我对音频信号实现了汉宁窗函数和FFT,如下代码所示:

private class RecordAudio extends AsyncTask<Void, double[], Void> {
    @Override
    protected Void doInBackground(Void... params) {
        started = true;
        try {
            DataOutputStream dos = new DataOutputStream(
                    new BufferedOutputStream(new FileOutputStream(
                            recordingFile)));
            int bufferSize = AudioRecord.getMinBufferSize(frequency,
                    channelConfiguration, audioEncoding);
            audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
                    frequency, channelConfiguration, audioEncoding,
                    bufferSize);

            short[] buffer = new short[blockSize];
            double[] toTransform = new double[blockSize];
            long t = System.currentTimeMillis();
            long end = t + 15000;
            audioRecord.startRecording();

            while (started) {
                //System.currentTimeMillis() < end
                int bufferReadResult = audioRecord.read(buffer, 0,
                        blockSize);
                for (int i = 0; i < blockSize && i < bufferReadResult; i++) {
                    toTransform[i] = (double) buffer[i] / 32768.0;
                    dos.writeShort(buffer[i]);
                }
                toTransform = hann(toTransform);
                transformer.ft(toTransform);
                publishProgress(toTransform);
            } 
            audioRecord.stop();
            dos.close();
        } catch (Throwable t) {
            Log.e("AudioRecord", "Recording Failed");
        }
        return null;
    }

现在我的问题是如何将高通滤波器应用于我的音频信号。有没有这方面的API?? 请有人帮我做这个功能。

代码修改部分

private class RecordAudio extends AsyncTask<Void, double[], Void> {
    @Override
    protected Void doInBackground(Void... params) {
        started = true;
        try {
            DataOutputStream dos = new DataOutputStream(
                    new BufferedOutputStream(new FileOutputStream(
                            recordingFile)));
            int bufferSize = AudioRecord.getMinBufferSize(sampleRate,
                    channelConfiguration, audioEncoding);
            audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
                    sampleRate, channelConfiguration, audioEncoding,
                    bufferSize);

            short[] buffer = new short[blockSize];
            double[] toTransform = new double[blockSize];
            long t = System.currentTimeMillis();
            long end = t + 15000;
            audioRecord.startRecording();

            while (started) {
                //System.currentTimeMillis() < end
                int bufferReadResult = audioRecord.read(buffer, 0,
                        blockSize);
                for (int i = 0; i < blockSize && i < bufferReadResult; i++) {
                    toTransform[i] = (double) buffer[i] / 32768.0;
                    dos.writeShort(buffer[i]);
                }
                toTransform = hann(toTransform);
                transformer.ft(toTransform);
                publishProgress(toTransform);
                //new part
                //sample rate = 8000
                highPassFilter(toTransform, sampleRate);
            } 
            audioRecord.stop();
            dos.close();
        } catch (Throwable t) {
            Log.e("AudioRecord", "Recording Failed");
        }
        return null;
    }

这是我的高通滤波方法:

public void highPassFilter(double []frequency, int samplerate){
    double [] f = new double[frequency.length];
    for (int n=1; n<frequency.length; n++){
    f[n] = (double)frequency[n]/samplerate;
    double x = (double)Math.exp(-2 * Math.PI * f[n]);
    double []a = new double[] { (1+x)/2, -(1+x)/2 };
    double []b = new double[] { x };
    }   
}

谢谢!!

【问题讨论】:

  • 您好,您是否检查过您的高通滤波器是否有效?谢谢

标签: java android signal-processing highpass-filter


【解决方案1】:

我认为信号处理只能在本机级别(C,C++)库中完成你可以尝试

this (TarsosDSP)

如果上述方法没有帮助,请尝试this SO Answer。

【讨论】:

  • @ Vigneshearan.m 感谢您的回答...根据您的第一个链接,我已经为我的程序尝试了一些代码。在 src 文件夹(TarsosDSP/src/be/tarsos/dsp/filters/)下有一个名为 HighPass 的类。但我不确定我的代码是否正确。请问可以查吗??检查我上面的代码修改部分。
猜你喜欢
  • 2015-04-02
  • 2011-06-02
  • 2016-03-23
  • 2016-02-20
  • 2017-05-09
  • 2014-03-20
  • 2011-05-05
  • 1970-01-01
  • 2019-12-08
相关资源
最近更新 更多