﻿ Butterworth Filter Source Code in C/C+, Low Pass | Café Papa

# Butterworth Low Pass Filter C / C++ Implementation

The code below defines a butterworth 6th order low pass filter with center freq. 0.1 * sampling frequency.
The processor code uses double precision but you can easily convert it to ‘float’ floating point arithmetic as needed.
One application of this type of Butterworth low pass filter is anti-aliasing.
It’s necessary to use a low pass filter whenever you want to reduce the sample rate, for example from 10kHz sampling rate down to 5kHz. Butterworth 6th order has a sharp transfer function and few ripples.

```void ButterworthLowpassFilter0100SixthOrder(const double src[], double dest[], int size)
{
const int NZEROS = 6;
const int NPOLES = 6;
const double GAIN = 2.936532839e+03;

double xv[NZEROS+1] = {0.0}, yv[NPOLES+1] = {0.0};

for (int i = 0; i < size; i++)
{
xv = xv; xv = xv; xv = xv; xv = xv; xv = xv; xv = xv;
xv = src[i] / GAIN;
yv = yv; yv = yv; yv = yv; yv = yv; yv = yv; yv = yv;
yv =   (xv + xv) + 6.0 * (xv + xv) + 15.0 * (xv + xv)
+ 20.0 * xv
+ ( -0.0837564796 * yv) + (  0.7052741145 * yv)
+ ( -2.5294949058 * yv) + (  4.9654152288 * yv)
+ ( -5.6586671659 * yv) + (  3.5794347983 * yv);
dest[i] = yv;
}
}```

The same Butterworth 6th order low pass filter again with a cut off frequency of 0.05 * sample rate:

```// Defines a butterworth low pass filter with center freq. 0.05 * sampling frequency
// Note: this processor only works with 'double'
// this filter is used as anti-aliasing filter from 20kHz sampling rate down to 5kHz
void ButterworthLowpassFilter0050SixthOrder(const double src[], double dest[], int size)
{
const int NZEROS = 6;
const int NPOLES = 6;
const double GAIN = 1.165969038e+05;

double xv[NZEROS+1] = {0.0}, yv[NPOLES+1] = {0.0};

for (int i = 0; i < size; i++)
{
xv = xv; xv = xv; xv = xv; xv = xv; xv = xv; xv = xv;
xv = src[i] / GAIN;
yv = yv; yv = yv; yv = yv; yv = yv; yv = yv; yv = yv;
yv =   (xv + xv) + 6.0 * (xv + xv) + 15.0 * (xv + xv)
+ 20.0 * xv
+ ( -0.2951724313 * yv) + (  2.1290387500 * yv)
+ ( -6.4411118810 * yv) + ( 10.4690788930 * yv)
+ ( -9.6495177287 * yv) + (  4.7871354989 * yv);
dest[i] = yv;
}
}
```

This time the Butterworth 6th order low pass filter was parameterized with a cut off frequency of 0.08 * sample rate:

```// Defines a butterworth lowpass filter with center freq. 0.08 * sampling frequency
// Note: this processor only works with 'double'
// this filter is used as anti-aliasing filter from 10kHz sampling rate down to 4kHz
void ButterworthLowpassFilter0080SixthOrder(const double src[], double dest[], int size)
{
const int NZEROS = 6;
const int NPOLES = 6;
const double GAIN = 9.339780497e+03;

double xv[NZEROS+1] = {0.0}, yv[NPOLES+1] = {0.0};

for (int i = 0; i < size; i++)
{
xv = xv; xv = xv; xv = xv; xv = xv; xv = xv; xv = xv;
xv = src[i] / GAIN;
yv = yv; yv = yv; yv = yv; yv = yv; yv = yv; yv = yv;
yv =   (xv + xv) + 6.0 * (xv + xv) + 15.0 * (xv + xv)
+ 20.0 * xv
+ ( -0.1396600417 * yv) + (  1.1086708553 * yv)
+ ( -3.7230194289 * yv) + (  6.7850160254 * yv)
+ ( -7.0995038188 * yv) + (  4.0616439992 * yv);
dest[i] = yv;
}
}
```

The last variant of this filter uses a cut off frequency of 0.04 * sample rate. For example if sample rate is 10 kHz, the cut off would be at 400 Hz.

```void ButterworthLowpassFilter0040SixthOrder(const double src[], double dest[], int size)
{
const int NZEROS = 6;
const int NPOLES = 6;
const double GAIN = 4.004448900e+05;
double xv[NZEROS+1] = {0.0}, yv[NPOLES+1] = {0.0};

for (int i = 0; i < size; i++)
{
xv = xv; xv = xv; xv = xv; xv = xv; xv = xv; xv = xv;
xv = src[i] / GAIN;
yv = yv; yv = yv; yv = yv; yv = yv; yv = yv; yv = yv;
yv =   (xv + xv) + 6.0 * (xv + xv) + 15.0 * (xv + xv)
+ 20.0 * xv
+ ( -0.3774523864 * yv) + (  2.6310551285 * yv)
+ ( -7.6754745482 * yv) + ( 11.9993158160 * yv)
+ (-10.6070421840 * yv) + (  5.0294383514 * yv);
dest[i] = yv;
}
}
```

Hope this helps!