Click here to Skip to main content
16,016,777 members
Articles / General Programming / Algorithms
Tip/Trick

A Butterworth Filter in C#

Rate me:
Please Sign up or sign in to vote.
5.00/5 (12 votes)
22 Apr 2016CPOL1 min read 70.7K   14   21
C# code for a low-pass Butterworth filter is presented

Image 1

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.

C#
//--------------------------------------------------------------------------
// 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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionQuery over line of code. Pin
Greg Russell21-Feb-24 0:25
professionalGreg Russell21-Feb-24 0:25 
AnswerRe: Query over line of code. Pin
Darryl Bryk21-Feb-24 10:09
Darryl Bryk21-Feb-24 10:09 
GeneralRe: Query over line of code. Pin
Greg Russell21-Feb-24 21:05
professionalGreg Russell21-Feb-24 21:05 
GeneralRe: Query over line of code. Pin
Darryl Bryk22-Feb-24 7:38
Darryl Bryk22-Feb-24 7:38 
Questionsecond order filter Pin
Member 1584680928-Nov-22 7:16
Member 1584680928-Nov-22 7:16 
AnswerRe: second order filter Pin
Darryl Bryk29-Nov-22 9:45
Darryl Bryk29-Nov-22 9:45 
AnswerRe: second order filter Pin
Darryl Bryk2-Dec-22 6:34
Darryl Bryk2-Dec-22 6:34 
QuestionThank You Pin
Member 113487907-Feb-20 1:28
Member 113487907-Feb-20 1:28 
PraiseButterworth Filter Pin
Member 795514028-Jul-17 14:46
Member 795514028-Jul-17 14:46 
GeneralRe: Butterworth Filter Pin
Darryl Bryk3-Aug-17 6:55
Darryl Bryk3-Aug-17 6:55 
QuestionGraph Pin
Nadirbek Nurlybekov5-May-17 10:45
Nadirbek Nurlybekov5-May-17 10:45 
AnswerRe: Graph Pin
Darryl Bryk3-Aug-17 6:51
Darryl Bryk3-Aug-17 6:51 
QuestionHigh pass Pin
Member 1303386128-Apr-17 0:18
Member 1303386128-Apr-17 0:18 
QuestionOrder of Butterworth Filter in C# Pin
Mariana Barros5-Apr-17 3:15
Mariana Barros5-Apr-17 3:15 
AnswerRe: Order of Butterworth Filter in C# Pin
Darryl Bryk3-Aug-17 6:46
Darryl Bryk3-Aug-17 6:46 
QuestionButterworth BandPass filter Pin
abhi nalage1-Aug-16 23:49
abhi nalage1-Aug-16 23:49 
AnswerRe: Butterworth BandPass filter Pin
Darryl Bryk2-Aug-16 5:33
Darryl Bryk2-Aug-16 5:33 
QuestionWorks perfect !! Can you also write code for Zerophase Bandpass filter Pin
Yasar Arfath16-May-16 17:47
Yasar 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
Yasar 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
Darryl 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.