将高通滤波器应用于音频信号

3

我已经编写了一个程序来捕获音频信号,去除背景噪声,应用窗口函数并可视化该信号。目前为止,我的程序没有出现任何错误。现在,我正在尝试将高通滤波器实现到我的代码中。我已经找到了一个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(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 链接。

有人能帮我完成这个功能吗?非常感谢!:)

提前致谢!


1
你3天前问过同样的问题 - Jens
@Jens 是的。我再次发布了它,因为它没有帮助到我。无论如何,昨晚我成功实现了高通滤波器功能。那么我需要做什么?我需要关闭这个问题吗?因为我是stackoverflow的新手。 - Hash_S
通常情况下,您会在对第一个问题的回答中添加评论,澄清为什么该回答不能解答您的问题。也许可以重新表述(编辑)原始问题以提高可读性。请记住,StackOverflow用户在问题被提出很久之后仍会继续阅读这些问题。 - Jens
好的,谢谢 Jens......干杯!.... :) - Hash_S
1个回答

12

这是我从一个C#库中转换成Java的类。 我使用它,它工作得很好。 你也可以将这个类用作低通滤波器。

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时,你会发现它被过滤了。

希望这能有所帮助。

它可以工作,但我们如何将其适应心电图呢?有什么想法吗? - Sriram C G

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接