Click here to Skip to main content
13,146,742 members (79,955 online)
Click here to Skip to main content
Add your own
alternative version

Stats

14.8K views
11 bookmarked
Posted 22 Apr 2016

A Butterworth Filter in C#

, 22 Apr 2016
Rate this:
Please Sign up or sign in to vote.
C# code for a low-pass Butterworth filter is presented

Introduction

This post presents C# code for a fourth order zero-phase shift low-pass Butterworth filter function. The code was converted to C# from code originally written in Visual Basic for Applications (VBA) by Sam Van Wassenbergh (University of Antwerp, 2007). I was using the VBA code for an application, and when I decided to convert the application to C#, I found there were not many resources for a Butterworth filter written in C#, so I converted the VBA code to C#, and hence I thought an article could be useful to others.

Using the Code

The function is shown below. The unfiltered data is passed to the function as a double[] array, along with the time step in seconds, and the desired cutoff frequency in Hz. The filtered data is returned, unless the input data indata is null, in which case, null is returned, or if the cutoff frequency is 0 then the original data is returned unfiltered. The time step variable deltaTimeinsec, which is the time between one data point and the next in seconds, is needed to calculate the sampling rate (inverse of the time step). The input data is prepared in a slightly larger array (4 extra points - two extra points at each end of the data), so that the real endpoints will have neighbors for the calculations. After the variables are assigned, the two loops perform the backward and forward traversal of the filter calculations. Then the output data is put back into an array the same size as the original for return.

//--------------------------------------------------------------------------
// This function returns the data filtered. Converted to C# 2 July 2014.
// Original source written in VBA for Microsoft Excel, 2000 by Sam Van
// Wassenbergh (University of Antwerp), 6 june 2007.
//--------------------------------------------------------------------------
public static double[] Butterworth(double[] indata, double deltaTimeinsec, double CutOff) {
    if (indata == null) return null;
    if (CutOff == 0) return indata;

    double Samplingrate = 1 / deltaTimeinsec;
    long dF2 = indata.Length - 1;        // The data range is set with dF2
    double[] Dat2 = new double[dF2 + 4]; // Array with 4 extra points front and back
    double[] data = indata; // Ptr., changes passed data

    // Copy indata to Dat2
    for (long r = 0; r < dF2; r++) {
        Dat2[2 + r] = indata[r];
    }
    Dat2[1] = Dat2[0] = indata[0];
    Dat2[dF2 + 3] = Dat2[dF2 + 2] = indata[dF2];

    const double pi = 3.14159265358979;
    double wc = Math.Tan(CutOff * pi / Samplingrate);
    double k1 = 1.414213562 * wc; // Sqrt(2) * wc
    double k2 = wc * wc;
    double a = k2 / (1 + k1 + k2);
    double b = 2 * a;
    double c = a;
    double k3 = b / k2;
    double d = -2 * a + k3;
    double e = 1 - (2 * a) - k3;

    // RECURSIVE TRIGGERS - ENABLE filter is performed (first, last points constant)
    double[] DatYt = new double[dF2 + 4];
    DatYt[1] = DatYt[0] = indata[0];
    for (long s = 2; s < dF2 + 2; s++) {
        DatYt[s] = a * Dat2[s] + b * Dat2[s - 1] + c * Dat2[s - 2]
                   + d * DatYt[s - 1] + e * DatYt[s - 2];
    }
    DatYt[dF2 + 3] = DatYt[dF2 + 2] = DatYt[dF2 + 1];

    // FORWARD filter
    double[] DatZt = new double[dF2 + 2];
    DatZt[dF2] = DatYt[dF2 + 2];
    DatZt[dF2 + 1] = DatYt[dF2 + 3];
    for (long t = -dF2 + 1; t <= 0; t++) {
        DatZt[-t] = a * DatYt[-t + 2] + b * DatYt[-t + 3] + c * DatYt[-t + 4]
                    + d * DatZt[-t + 1] + e * DatZt[-t + 2];
    }

    // Calculated points copied for return
    for (long p = 0; p < dF2; p++) {
        data[p] = DatZt[p];
    }

    return data;
}

Conclusion

A fourth order zero-phase shift low-pass Butterworth filter function was presented in the C# language as converted from Sam Van Wassenbergh’s original VBA code.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Darryl Bryk
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
PraiseButterworth Filter Pin
Member 795514028-Jul-17 14:46
memberMember 795514028-Jul-17 14:46 
GeneralRe: Butterworth Filter Pin
Darryl Bryk3-Aug-17 6:55
memberDarryl Bryk3-Aug-17 6:55 
QuestionGraph Pin
Nadirbek Nurlybekov5-May-17 10:45
memberNadirbek Nurlybekov5-May-17 10:45 
AnswerRe: Graph Pin
Darryl Bryk3-Aug-17 6:51
memberDarryl Bryk3-Aug-17 6:51 
QuestionHigh pass Pin
Member 1303386128-Apr-17 0:18
memberMember 1303386128-Apr-17 0:18 
QuestionOrder of Butterworth Filter in C# Pin
Mariana Barros5-Apr-17 3:15
memberMariana Barros5-Apr-17 3:15 
AnswerRe: Order of Butterworth Filter in C# Pin
Darryl Bryk3-Aug-17 6:46
memberDarryl Bryk3-Aug-17 6:46 
QuestionButterworth BandPass filter Pin
abhi nalage1-Aug-16 23:49
memberabhi nalage1-Aug-16 23:49 
AnswerRe: Butterworth BandPass filter Pin
Darryl Bryk2-Aug-16 5:33
memberDarryl Bryk2-Aug-16 5:33 
QuestionWorks perfect !! Can you also write code for Zerophase Bandpass filter Pin
Yasar Arfath16-May-16 17:47
memberYasar Arfath16-May-16 17:47 
QuestionWhen implementing zero-phase filtering using a Fourth-order section filter, the length of the input, x, must be more than 4 times the filter order ? Pin
Yasar Arfath9-May-16 22:56
memberYasar Arfath9-May-16 22:56 
AnswerRe: When implementing zero-phase filtering using a Fourth-order section filter, the length of the input, x, must be more than 4 times the filter order ? Pin
Darryl Bryk10-May-16 10:57
memberDarryl Bryk10-May-16 10:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170915.1 | Last Updated 22 Apr 2016
Article Copyright 2016 by Darryl Bryk
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid