低通和高通滤波器在C# [英] lowpass and high pass filter in C#

查看:5123
本文介绍了低通和高通滤波器在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要低通和C#编写的高通滤波器。我对这个过滤过程的双阵列。我想,如果我尝试MATLAB巴特沃思和切比雪夫算法转换为C#中,它会更容易。但我无法找到在互联网上butter.m和切比雪夫算法的代码,我不想MATLAB和信号处理工具箱设置成我的电脑。你能提供代码讨好?谢谢..

I need low pass and high pass filter written in c#. I have double arrays for this filter process. I think if I try to convert matlab Butterworth and Chebyshev algorithms to c#, it would be easier. But I couldn't find the code of butter.m and Chebyshev algorithms on the internet and I don't want to set up matlab and signal processing toolbox into my computer. Could you provide that codes please? Thanks..

推荐答案

http://www.musicdsp.org/archive.php?classid=3#38

我在执行上面semicode作为过滤器遵循我们的表面肌电分析仪软件,它的伟大工程。

I implemented the filter in semicode above as follows in our sEMG analyzer software and it works great.

public class FilterButterworth
{
    /// <summary>
    /// rez amount, from sqrt(2) to ~ 0.1
    /// </summary>
    private readonly float resonance;

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

    private readonly 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 FilterButterworth(float frequency, int sampleRate, PassType passType, float resonance)
    {
        this.resonance = resonance;
        this.frequency = frequency;
        this.sampleRate = sampleRate;
        this.passType = passType;

        switch (passType)
        {
            case PassType.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 PassType.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 Value
    {
        get { return this.outputHistory[0]; }
    }
}

这篇关于低通和高通滤波器在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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