如何实现音频信号的高通滤波器?

时间:2015-01-31 14:24:35

标签: java android signal-processing highpass-filter

我正在尝试像shazam这样的音乐识别应用程序。这是一个Android应用程序。首先,我通过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 };
    }   
}

谢谢!

1 个答案:

答案 0 :(得分:0)

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

this (TarsosDSP)

如果上述方法无效,请尝试this SO答案。