简体   繁体   English

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

[英]How to implement a high-pass filter for an audio signal?

I am trying to do a music identification application like shazam. 我正在尝试制作诸如shazam的音乐识别应用程序。 This is an android app. 这是一个Android应用。 First i have captured an audio signal through the MIC. 首先,我已经通过MIC捕获了音频信号。 Next I have implemented the hanning window function and FFT to the audio signal as shown as following code : 接下来,我对音频信号实施了hanning窗口功能和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;
    }

Now my question is how do I need to apply high pass filter to my audio signal. 现在我的问题是我该如何对音频信号应用高通滤波器。 Is there any API for this ?? 是否有任何API? Please some one help me to do this function. 请有人帮我做这个功能。

Code modification part 代码修改部分

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;
    }

Here is my high pass filter method: 这是我的高通滤波器方法:

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 };
    }   
}

Thanks !! 谢谢 !!

I think the signal processing could be done in the native level(C,C++) libs only You may try 我认为信号处理只能在本机(C,C ++)库中完成,您可以尝试

this (TarsosDSP) 这个(TarsosDSP)

If the above doesn't help then try this SO Answer. 如果上述方法无济于事,请尝试使用答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM