I am trying to implement a butterworth bandpass filter
My filter requirement is Fs= 500Hz, Fc1=0.4Hz, Fc2=4Hz, Oreder=2.

Below is the code which is implemented on 16bit-mcu and is called for every new sample
The coefficients are calculated from http://www-users.cs.york.ac.uk/~fisher/mkfilter

static int32_t xv[4+1], yv[4+1];
int32_t my_filter(int32_t sample)
    xv[0] = xv[1]; xv[1] = xv[2]; xv[2] = xv[3]; xv[3] = xv[4];
    xv[4] = sample / 1.989695029e+03;
    yv[0] = yv[1]; yv[1] = yv[2]; yv[2] = yv[3]; yv[3] = yv[4];
    yv[4] = (xv[0] + xv[4]) - 2 * xv[2]
                 + ( -0.9380260977 * yv[0]) + (  3.8116138294 * yv[1])
                 + ( -5.8091337665 * yv[2]) + (  3.9355459731 * yv[3]);
    return yv[4];

This is the output of code Output What i want is output similar to scipy_filter.

  1. Change all data types to "float"
  2. Split the filter into two second order sections
  3. If you can't afford the floating point, you need to learn fixed point math, which will require significant effort

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.