Makeing a Butterworth filter of order 2 for Lowpass to process 50 samples per second with the corner at 8 Hertz

Code

mkfilter.exe -Bu -Lp -o 2 -a 0.16 -l | gencode.exe
/* Digital filter designed by mkfilter/mkshape/gencode   A.J. Fisher
   Command line: mkfilter.exe -Bu -Lp -o 2 -a 0.16 -l */

#define NZEROS 2
#define NPOLES 2
#define GAIN   6.881181354e+000

static float xv[NZEROS+1], yv[NPOLES+1];

static void filterloop()
  { for (;;)
      { xv[0] = xv[1]; xv[1] = xv[2]; 
        xv[2] = `next input value' / GAIN;
        yv[0] = yv[1]; yv[1] = yv[2]; 
        yv[2] =   (xv[0] + xv[2]) + 2 * xv[1]
                     + ( -0.2523246263 * yv[0]) + (  0.6710290908 * yv[1]);
        `next output value' = yv[2];
      }
  }

Plot



Magnitude (red) and phase (blue) vs. frequency
  • x axis: frequency, as a fraction of the sampling rate (i.e. 0.5 represents the Nyquist frequency, which is 5000 Hz)
  • y axis (red): magnitude (linear, normalized)
  • y axis (blue): phase

Parameters


mkfilter.exe -Bu -Lp -o 2 -a 0.16 -l
G  = 6.8811813538e+000
NZ = 2
 1.0000000000e+000
 2.0000000000e+000
 1.0000000000e+000
NP = 2
-2.5232462628e-001
 6.7102909077e-001
-1.0000000000e+000