## IIR数字滤波器实现 转

abcijkxyz

/*

* Filter Coefficients (C Source) generated by the Filter Design and Analysis Tool

*

* Generated by MATLAB(R) 7.6 and the Signal Processing Toolbox 6.9.

*

* Generated on: 03-Dec-2010 10:41:03

*

*/

/*

* Discrete-Time IIR Filter (real)

* -------------------------------

* Filter Structure    : Direct-Form II, Second-Order Sections

* Number of Sections  : 3

* Stable              : Yes

* Linear Phase        : No

*/

/* General type conversion for MATLAB generated C-code  */

#include "tmwtypes.h"

/*

* Expected path to tmwtypes.h

* D:/MATLAB/R2008a/extern/include/tmwtypes.h

*/

#define MWSPT_NSEC 7

const int NL[MWSPT_NSEC] = { 1,3,1,3,1,3,1 };

const real64_T NUM[MWSPT_NSEC][3] = {

{

0.09065504059673,                 0,                 0

},

{

1,  -0.1323122149853,   0.9999674089086

},

{

1,                 0,                 0

},

{

1,   0.1670351201308,   0.9999889247428

},

{

1,                 0,                 0

},

{

1,    1.417032671609,   0.9978019623105

},

{

1,                 0,                 0

}

};

const int DL[MWSPT_NSEC] = { 1,3,1,3,1,3,1 };

const real64_T DEN[MWSPT_NSEC][3] = {

{

1,                 0,                 0

},

{

1,    -0.40321085647,   0.7334254033056

},

{

1,                 0,                 0

},

{

1,  -0.6868636040216,   0.2670185171768

},

{

1,                 0,                 0

},

{

1,  -0.2880720042256,   0.9480010462991

},

{

1,                 0,                 0

}

};

double a[3][3] = {

{1,  -0.6868636040216,   0.2670185171768},

{1,    -0.40321085647,   0.7334254033056},

{1,  -0.2880720042256,   0.9480010462991}

};

double b[3][3] = {

{1,   1.417032671609,   0.9978019623105},

{1,   0.1670351201308,   0.9999889247428},

{1,  -0.1323122149853,   0.9999674089086},

};

double gain =  0.09065504059673

# 2阶节IIR滤波的直接实现

typedef struct tag_ IIR_State_2order

{

float y2;

float y1;

float x1;

float x0;

} IIR_State_2order;

#define  ONE_FRAME_SAMPLE_SIZE   1024

void cy_signal_filter_by_iir(signed short* pcmIn, IIR_State_2order* filter_state, float a[], float b[], signed short* pcmOut)

{

int i;

float x2;

float tmp;

for ( i = 0; i < ONE_FRAME_SAMPLE_SIZE; i++ )

{

x2 = filter_state->x1;

filter_state->x1 = filter_state->x0;

filter_state->x0 = pcmIn[i];

tmp = ( float )(b[0]* filter_state->x0 + b[1]* filter_state->x1 +

b[2] * x2 - a[1] * filter_state->y1 - a[2] * filter_state->y2);

if(tmp >= 32767)

{

tmp = 32767;

}

if(tmp <= -32768)

{

tmp = -32768;

}

pcmOut[i] = (signed short)tmp;

filter_state->y2 = filter_state->y1;

filter_state->y1 = tmp;

}

}

typedef struct tag_ IIR_State_2order

{

float st1;

float st2;

} IIR_State_2order;

void cy_signal_filter_by_iir(signed short* pcmIn, IIR_State_2order* filter_state, float a[], float b[], signed short* pcmOut)

{

int i;

float st;

float Tmp_fl;

for ( i = 0; i < ONE_FRAME_SAMPLE_SIZE; i++ )

{

st = (float)(pcmIn[i] -  a[1] * filter_state->st1  - a[2] * filter_state-> st2);

Tmp_fl = b[0] *  st + b[1] * filter_state->st1 + b[2] * filter_state->st2;

filter_state->st2= filter_state->st1;

filter_state->st1 = st;

if(Tmp_fl >= 32767.0)

{

Tmp_fl = 32767;

}

if(Tmp_fl <= -32768)

{

Tmp_fl = -32768;

}

pcmOut[i] = (signed short)Tmp_fl;

}

}

# 6阶节IIR滤波的实现

6阶节分解为32阶节级联实现。每个2阶节需要2个历史状态，总共需要6个历史状态。结构体定义如下：

typedef struct tag_IIR_State_3Order

{

double w01;

double w02;

double w11;

double w12;

double w21;

double w22;

}IIR_State_6order;

void cy_signal_filter_by_6th_iir(signed short* pcmIn, IIR_State_6order* filter_state, int sample_size)

{

double x1, x2, x3, Tmp_f00, Tmp_f10, Tmp_f20;

int i;

double Tmp_pcm;

for (i = 0; i < sample_size; i++)

{

Tmp_pcm = pcmIn[i];

Tmp_f00 = Tmp_pcm - a[0][1] * filter_state->w01 - a[0][2] * filter_state->w02;

x1 = Tmp_f00 + b[0][1] * filter_state->w01 + b[0][2] * filter_state->w02;

filter_state->w02 = filter_state->w01;

filter_state->w01 = Tmp_f00;

x1 = gain * x1;

Tmp_f10 = x1 - a[1][1] * filter_state->w11 - a[1][2] * filter_state->w12;

x2 = Tmp_f10 + b[1][1] * filter_state->w11 + b[1][2] * filter_state->w12;

filter_state->w12 = filter_state->w11;

filter_state->w11 = Tmp_f10;

Tmp_f20 = x2 - a[2][1] * filter_state->w21 - a[2][2] * filter_state->w22;

x3 = Tmp_f20 * b[2][0] + b[2][1] * filter_state->w21 + b[2][2] * filter_state->w22;

filter_state->w22 = filter_state->w21;

filter_state->w21 = Tmp_f20;

if (x3 >= 32767)

{

x3 = 32767;

}

if (x3 <= -32768)

{

x3 = -32768;

}

pcmIn[i] = (signed short)x3;

}

}

最后看下滤波的效果：

### abcijkxyz

【工具使用系列】关于 MATLAB 数字滤波器设计，你需要知道的事

AllenMoore
2018/02/03
0
0

linmin418
2017/05/16
0
0
IIR数字滤波器实现（mantlab+C语言）

syrchina
2013/06/08
0
0

2013/02/24
460
4
SP++3.0 发布，欢迎大家使用

2011/02/12
4.9K
4

C++ vector和list的区别

1.vector数据结构 vector和数组类似，拥有一段连续的内存空间，并且起始地址不变。 因此能高效的进行随机存取，时间复杂度为o(1); 但因为内存空间是连续的，所以在进行插入和删除操作时，会造...

shzwork
51分钟前
2
0
Spring之invokeBeanFactoryPostProcessors详解

Spring的refresh的invokeBeanFactoryPostProcessors，就是调用所有注册的、原始的BeanFactoryPostProcessor。 相关源码 public static void invokeBeanFactoryPostProcessors(Configu......

cregu

3
0
ibmcom/db2express-c_docker官方使用文档

(DEPRECIATED) Please check DB2 Developer-C Edition for the replacement. What is IBM DB2 Express-C ? ``IBM DB2 Express-C``` is the no-charge community edition of DB2 server, a si......

BG2KNT

2
0
Ubuntu 18.04.2 LTS nvidia-docker2 : 依赖: docker-ce (= 5:18.09.0~3-0~ubuntu-bionic)

Pulsar-V

4
0

4
0