打开MATLAB,选择应用程序,点击Filter Design & Analysis

选择Butterworth,设置阶数,采样频率FS,截止频率FC,最后点击Design Filter确定

选择Analysis菜单,点击Filter Coefficients

选择Edit菜单,点击Convert to single section

选择Targets菜单,点击Generate C Header...

选择Export as,选择single-precision float,点击Generate

生成的文件内容如下
/*
* Filter Coefficients (C Source) generated by the Filter Design and Analysis Tool
* Generated by MATLAB(R) 9.0 and the Signal Processing Toolbox 7.2.
* Generated on: 27-Nov-2019 07:42:47
*/
/*
* Discrete-Time IIR Filter (real)
* -------------------------------
* Filter Structure : Direct-Form II
* Numerator Length : 7
* Denominator Length : 7
* Stable : Yes
* Linear Phase : No
*/
/* General type conversion for MATLAB generated C-code */
#include "tmwtypes.h"
/*
* Expected path to tmwtypes.h
* C:\Program Files\MATLAB\R2016a\extern\include\tmwtypes.h
*/
/*
* Warning - Filter coefficients were truncated to fit specified data type.
* The resulting response may not match generated theoretical response.
* Use the Filter Design & Analysis Tool to design accurate
* single-precision filter coefficients.
*/
const int NL = 7;
const real32_T NUM[7] = {
0.004106325097, 0.02463795058, 0.06159487367, 0.08212649822, 0.06159487367,
0.02463795058, 0.004106325097
};
const int DL = 7;
const real32_T DEN[7] = {
1, -2.021385193, 2.312567234, -1.521253347, 0.6202822328,
-0.1416414529, 0.01423505601
};
C代码实现Butterworlth滤波系数数组定义
float FACTOR_B[] = {0.004106325097, 0.02463795058, 0.06159487367, 0.08212649822, 0.06159487367, 0.02463795058, 0.004106325097};
float FACTOR_A[] = {1, -2.021385193, 2.312567234, -1.521253347, 0.6202822328, -0.1416414529, 0.01423505601};
Butterworlth数据结构
typedef struct
{
float xBuffer[FACTOR_NUMBER];
float yBuffer[FACTOR_NUMBER];
}BUTTER_WORTH_BUFFER;
Butterworth算法实现
static BUTTER_WORTH_BUFFER s_ecgFilter;
#define FACTOR_NUMBER 7
uint16_t buterworlthFilter(uint16_t x)
{
uint32_t i;
//运算之前Buf向前移动一个位置,以保存之前Buf的数据;
for(i=FACTOR_NUMBER-1; i>0;i--)
{
s_ecgFilter.yBuffer[i] = s_ecgFilter.yBuffer[i-1];
s_ecgFilter.xBuffer[i] = s_ecgFilter.xBuffer[i-1];
}
s_ecgFilter.xBuffer[0] = x;
s_ecgFilter.yBuffer[0] = 0;
for(i=1;i
1658017818
查看更多评论