Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
538 views
in Technique[技术] by (71.8m points)

signal processing - Low pass filter software?

I'm looking for digital low pass filter code/library/class for a .net windows forms project, preferably written in c, c++ or c#. I probably need to set the number of poles, coefficients, windowing, that sort of thing. I can't use any of the gpl'd code that's available, and don't know what else is out there. Any suggestions appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is a Butterworth Low Pass filter I wrote for a recent project.

It has some magic numbers as constants that was given to me. If you can figure out how to create the magic numbers with your poles, coefficients, etc, then this might be helpful.

using System;
using System.Collections.Generic;
using System.Text;

namespace Filter
{
public class ButterworthLowPassFilter
{

    //filter fc = 2hz, fs = 10hz

    private const int LowPassOrder = 4;

    private double[] inputValueModifier;
    private double[] outputValueModifier;
    private double[] inputValue;
    private double[] outputValue;
    private int valuePosition;

    public ButterworthLowPassFilter()
    {
        inputValueModifier = new double[LowPassOrder];
        inputValueModifier[0] = 0.098531160923927;
        inputValueModifier[1] = 0.295593482771781;
        inputValueModifier[2] = 0.295593482771781;
        inputValueModifier[3] = 0.098531160923927;

        outputValueModifier = new double[LowPassOrder];
        outputValueModifier[0] = 1.0;
        outputValueModifier[1] = -0.577240524806303;
        outputValueModifier[2] = 0.421787048689562;
        outputValueModifier[3] = -0.0562972364918427;
    }

    public double Filter(double inputValue)
    {
        if (this.inputValue == null && this.outputValue == null)
        {
            this.inputValue = new double[LowPassOrder];
            this.outputValue = new double[LowPassOrder];

            valuePosition = -1;

            for (int i=0; i < LowPassOrder; i++)
            {
                this.inputValue[i] = inputValue;
                this.outputValue[i] = inputValue;
            }

            return inputValue;
        }
        else if (this.inputValue != null && this.outputValue != null)
        {
            valuePosition = IncrementLowOrderPosition(valuePosition);

            this.inputValue[valuePosition] = inputValue;
            this.outputValue[valuePosition] = 0;

            int j = valuePosition;

            for (int i = 0; i < LowPassOrder; i++)
            {
                this.outputValue[valuePosition] += inputValueModifier[i] * this.inputValue[j] -
                    outputValueModifier[i] * this.outputValue[j];

                j = DecrementLowOrderPosition(j);
            }

            return this.outputValue[valuePosition];
        }
        else
        {
            throw new Exception("Both inputValue and outputValue should either be null or not null.  This should never be thrown.");
        }
    }

    private int DecrementLowOrderPosition(int j)
    {
        if (--j < 0)
        {
            j += LowPassOrder;
        }
        return j;
    }

    private int IncrementLowOrderPosition(int position)
    {
        return ((position + 1) % LowPassOrder);
    }

}
}

Keith


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...