实施高通滤波音频信号 [英] Implementing a High Pass filter to an audio signal

查看:320
本文介绍了实施高通滤波音频信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是能够捕获音频信号,去除背景噪声,应用窗口的功能和可视化信号写了一个程序。我的计划是工作到这一点没有一个错误。现在,我想实现一个高通滤波器,以我的code。我已经发现了这一部分的API。但我无法根据我的code应用它。这是我的code:

I was able to wrote a program for capturing an audio signal, remove background noise, applying window function and visualisation that signal. My program is working up to this point without an error. Now I am trying to implement a High Pass filter to my code. I have already found a API for this part. But I was unable to apply it according to my code. Here is my 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);

            NoiseSuppressor.create(audioRecord.getAudioSessionId());
            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链接。

谁能帮我做这个功能?我真的AP preciate吧! :)

Can anyone help me to do this function? I would really appreciate it! :)

在此先感谢!

推荐答案

这里是我转换成Java从库中,我发现在C#中的类。我用它和它的工作很大。你可以使用这个类的低通滤波器太

here is the class I convert to java from a library I found in c#. I use it and it work great. you can use this class for low pass filter too

public class Filter {


/// <summary>
/// rez amount, from sqrt(2) to ~ 0.1
/// </summary>
private float resonance;

private float frequency;
private int sampleRate;
private PassType passType;


public float value;

private float c, a1, a2, a3, b1, b2;

/// <summary>
/// Array of input values, latest are in front
/// </summary>
private float[] inputHistory = new float[2];

/// <summary>
/// Array of output values, latest are in front
/// </summary>
private float[] outputHistory = new float[3];

public Filter(float frequency, int sampleRate, PassType passType, float resonance)
{
    this.resonance = resonance;
    this.frequency = frequency;
    this.sampleRate = sampleRate;
    this.passType = passType;

    switch (passType)
    {
        case Lowpass:
            c = 1.0f / (float)Math.tan(Math.PI * frequency / sampleRate);
            a1 = 1.0f / (1.0f + resonance * c + c * c);
            a2 = 2f * a1;
            a3 = a1;
            b1 = 2.0f * (1.0f - c * c) * a1;
            b2 = (1.0f - resonance * c + c * c) * a1;
            break;
        case Highpass:
            c = (float)Math.tan(Math.PI * frequency / sampleRate);
            a1 = 1.0f / (1.0f + resonance * c + c * c);
            a2 = -2f * a1;
            a3 = a1;
            b1 = 2.0f * (c * c - 1.0f) * a1;
            b2 = (1.0f - resonance * c + c * c) * a1;
            break;
    }
}

public enum PassType
{
    Highpass,
    Lowpass,
}

public void Update(float newInput)
{
    float newOutput = a1 * newInput + a2 * this.inputHistory[0] + a3 * this.inputHistory[1] - b1 * this.outputHistory[0] - b2 * this.outputHistory[1];

    this.inputHistory[1] = this.inputHistory[0];
    this.inputHistory[0] = newInput;

    this.outputHistory[2] = this.outputHistory[1];
    this.outputHistory[1] = this.outputHistory[0];
    this.outputHistory[0] = newOutput;
}


public float getValue()
{
    return this.outputHistory[0];
}


}

和我这是怎么用这个

    Filter filter = new Filter(15000,44100, Filter.PassType.Highpass,1);
    for (int i = 0; i < numSamples; i++)
    {
        filter.Update(floatArray[i]);
        floatArray[i] = filter.getValue();
    }

你有floatArray的FFT之后,你看到它的过滤。 希望它可以帮助

after you got floatArray's fft, you see it is filtered. Hope it helps

这篇关于实施高通滤波音频信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆