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

# Low Pass Filter Bessel C / C++ Implementation

Some time ago while working on a vehicle crash simulation system we needed to simulate a Bessel low pass filter.

These hardware Bessel filter are used to “smooth out” signals from a acceleration sensor. Some sensors output a pulse-width modulated signal. After letting it pass through a Bessel filter we get a pseudo integral type signal. You can use these filters with any sample rate you like. The cut off frequency is mentioned below as a factor of the actual sample rate you are using. For example, 0.04.

The below code implements a Bessel 4th order low pass filter in C / C++ with double precision. Note that the so-called cut off frequency is the factor 0.04 of the sampling frequency. For example, if sampled at 10kHz, the cut off is 0.04 * 10000 = 400 Hz

```// cuts off at 0.04 * sampling freq
void BesselLowpassFourthOrder004(const double source[], double dest[], int length)
{
double xv[4+1] = {0.0},
yv[4+1] = {0.0};

for (int i = 0; i < length; i++)
{
xv = xv; xv = xv; xv = xv; xv = xv;
xv = source[i] / double(1.330668083e+03);
yv = yv; yv = yv; yv = yv; yv = yv;
yv =   (xv + xv) + 4 * (xv + xv) + 6 * xv
+ ( -0.3041592568 * yv) + (  1.5960375869 * yv)
+ ( -3.1910200543 * yv) + (  2.8871176889 * yv);
dest[i] = yv;
}
}```

Below is the same Bessel low pass filter as 3rd order:

```// cuts off at 0.04 * sampling freq
void BesselLowpassThirdOrder004(const double src[], double dest[], int size)
{
double xv[3+1] = {0.0};
double yv[3+1] = {0.0};

for (int i=0; i<size; i++)
{
xv = xv; xv = xv; xv = xv;
xv = src[i] / double(2.711023309e+02);
yv = yv; yv = yv; yv = yv;
yv = (xv + xv) + 3 * (xv + xv)
+ (  0.4226750651 * yv) + ( -1.6550518354 * yv)
+ (  2.2028676179 * yv);
dest[i] = yv;
}
}```

The same cut off frequency again but 2nd order Bessel low pass:

```// cuts off at 0.04 * sampling freq
void BesselLowpassSecondOrder004(const double src[], double dest[], int size)
{
double xv[2+1] = {0.0}, yv[2+1] = {0.0};

for (int i = 0; i < size; i++)
{
xv = xv; xv = xv;
xv = src[i] / double(5.050469146e+01);
yv = yv; yv = yv;
yv = (xv + xv) + 2 * xv + ( -0.5731643146 * yv) + (  1.4939637515 * yv);
dest[i] = yv;
}
}```

And this code simulates a 0.036 cut off frequency (360Hz at 10kHz sample rate), 2nd order Bessel low pass filter:

```// cuts off at 0.036 * sampling freq
void BesselLowpassSecondOrder0036(const double src[], double dest[], int size)
{
double xv[2+1] = {0.0}, yv[2+1] = {0.0};

for (int i = 0; i < size; i++)
{
xv = xv; xv = xv;
xv = src[i] / double(6.089464560e+01);
yv = yv; yv = yv;
yv =   (xv + xv) + 2 * xv + ( -0.6062613139 * yv) + (  1.5405740936 * yv);
dest[i] = yv;
}
}```
1. • 