Implementing a High Pass filter to an audio signal

前端 未结 1 752
猫巷女王i
猫巷女王i 2020-12-18 11:22

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 thi

1条回答
  •  时光说笑
    2020-12-18 12:02

    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 {
    
    
    /// 
    /// rez amount, from sqrt(2) to ~ 0.1
    /// 
    private float resonance;
    
    private float frequency;
    private int sampleRate;
    private PassType passType;
    
    
    public float value;
    
    private float c, a1, a2, a3, b1, b2;
    
    /// 
    /// Array of input values, latest are in front
    /// 
    private float[] inputHistory = new float[2];
    
    /// 
    /// Array of output values, latest are in front
    /// 
    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];
    }
    
    
    }
    

    and this is how I use this

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

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

    0 讨论(0)
提交回复
热议问题