基于AVR单片机-高阶椭圆数字低通滤波器设计

之前做毕业设计,较多的涉及了传感器的数据信号处理,主要是进行了各种滤波操作,这里记录一下椭圆低通滤波器的程序设计。这里讲的主要的数字低通滤波器,跑在avr单片机上的。我的低通滤波器选用了性能和效果都比较好的椭圆滤波器(Elliptic filter),用来对信号进行频域上的滤波操作。

个人原创,版权所有,转载请注明原文出处,并保留原文链接:

https://www.embbnux.com/2015/07/14/elliptic_filter_design_on_avr_mcu_c/

一 首先是滤波器的设计

需要选定你的滤波器的主要指标,比如我是低通滤波器,那要考虑截至频率、采样频率、阻带下降分贝、纹波系数以及滤波器阶数等。选定这些系数后可以直接使用matlab帮我们算出数字滤波器的模型,这里我使用椭圆滤波器模型,matlab的使用参考这里。假定使用3阶椭圆低通滤波器,1DB的通带纹波系数,40DB的的阻带下降,采样频率为100HZ,截至频率为20HZ,归一化频率则为0.4pi rad/sample。matlab程序设计为:

[b,a] = ellip(3,1,40,0.4)

其中得到的两组数a和b就是我们的三阶椭圆低通滤波器的参数。下面程序会用到。

二 使用c语言实现上述椭圆低通滤波器

这里的滤波器设计采用iir无限脉冲设计:

程序设计思想是使用递归运算来实现的

具体程序实现如下:

elliptic_filter.c:

#include "elliptic_filter.h"
double xn_fifo[3][4] = {{0.0,0.0,0.0,0.0},{0.0,0.0,0.0,0.0},{0.0,0.0,0.0,0.0}};
double yn_fifo[3][4] = {{0.0,0.0,0.0,0.0},{0.0,0.0,0.0,0.0},{0.0,0.0,0.0,0.0}};
double ellip_a[4] = {1.0,-1.97,1.44,-0.37}; //使用matlab 计算a得到这里瞎写的
double ellip_b[4] = {0.01,0.03,0.03,0.01}; //使用matlab 计算b得到

void elliptic_filter(double xnx,double xny,double xnz, double *yn) {
    unsigned char i=0;
    for(i=0;i<3;i++){
       xn_fifo[0][i] = xn_fifo[0][i+1];
       xn_fifo[1][i] = xn_fifo[1][i+1];
       xn_fifo[2][i] = xn_fifo[2][i+1];
       yn_fifo[0][i] = yn_fifo[0][i+1];
       yn_fifo[1][i] = yn_fifo[1][i+1];
       yn_fifo[2][i] = yn_fifo[2][i+1];
     }
    xn_fifo[0][3] = xnx;
    xn_fifo[1][3] = xny;
    xn_fifo[2][3] = xnz;

    yn_fifo[0][3] = xn_fifo[0][3]*ellip_b[0] + xn_fifo[0][2]*ellip_b[1] +
                    xn_fifo[0][1]*ellip_b[2] + xn_fifo[0][0]*ellip_b[3] -
                    yn_fifo[0][2]*ellip_a[1] - yn_fifo[0][1]*ellip_a[2] -
                    yn_fifo[0][0]*ellip_a[3];
    yn_fifo[1][3] = xn_fifo[1][3]*ellip_b[0] + xn_fifo[1][2]*ellip_b[1] +
                    xn_fifo[1][1]*ellip_b[2] + xn_fifo[1][0]*ellip_b[3] -
                    yn_fifo[1][2]*ellip_a[1] - yn_fifo[1][1]*ellip_a[2] -
                    yn_fifo[1][0]*ellip_a[3];
    yn_fifo[2][3] = xn_fifo[2][3]*ellip_b[0] + xn_fifo[2][2]*ellip_b[1] +
                    xn_fifo[2][1]*ellip_b[2] + xn_fifo[2][0]*ellip_b[3] -
                    yn_fifo[2][2]*ellip_a[1] - yn_fifo[2][1]*ellip_a[2] -
                    yn_fifo[2][0]*ellip_a[3];
    yn[0] = yn_fifo[0][3];
    yn[1] = yn_fifo[1][3];
    yn[2] = yn_fifo[2][3];
}

 

《基于AVR单片机-高阶椭圆数字低通滤波器设计》有3个想法

    1. matlab计算结果不是这个么?
      >> [b,a] = ellip(3,1,40,0.4)

      b =

      0.0982 0.2162 0.2162 0.0982

      a =

      1.0000 -0.9531 0.8714 -0.2895

发表评论

电子邮件地址不会被公开。 必填项已用*标注

Time limit is exhausted. Please reload the CAPTCHA.