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

我的梦境 提交于 2019-12-23 05:47:12

问题


I am trying to do a music identification application like shazam. This is an android app. First i have captured an audio signal through the MIC. Next I have implemented the hanning window function and FFT to the audio signal as shown as following code :

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 ?? 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 !!


回答1:


I think the signal processing could be done in the native level(C,C++) libs only You may try

this (TarsosDSP)

If the above doesn't help then try this SO Answer.



来源:https://stackoverflow.com/questions/28252665/how-to-implement-a-high-pass-filter-for-an-audio-signal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!