您的位置:首页 > 编程语言 > C语言/C++

滤波器设计--C语言实现

2017-05-14 21:37 288 查看

本文简单介绍,在差分方程已知或滤波器框图给出的情况下,使用C语言实现滤波器的方法。

一. 对一个p阶FIR滤波器(无反馈):

差分方程:



滤波器框图:



float fir_filt(float x, const float *b, float *s, int p)
{
y = b[0] * x;   //对输出y赋初值

for(int i = 0; i <= p; i++)   //输出
y += b[i+1] * s[i];

for(int i = p; i > 0; i++)   //更新寄存器
s[i] = s[i-1];

s[0] = x;

return y;
}


二. 对于一个IIR滤波器:

差分方程:



滤波器框图:



float IIR_filt(float x, const float *c, float *s)
{
float y = c[0] * x + s[0];   //对y赋初值(输出)

s[0] = s[1] + c[1]*x - c[3] * y;    //更新

s[1] = c[2] * x - c[4] * y;

return y;
}


三. MATLAB中,Filter函数的C语言实现

在MATLAB中,有这样一个函数dsp.ColoredNoise,从名字看出,该函数用来生成不同颜色的噪声,使用方法:

H = dsp.ColoredNoise returns a colored noise generator, H, with default settings. Calling step with the default property settings generates a pink noise signal.


系数取不同值,生成不同色噪:

An InverseFrequencyPower value of 1 generates a pink noise. This is the default value.
An InverseFrequencyPower value of 2 generates a Brownian process.
An InverseFrequencyPower value of 0 generates a white noise process with a flat PSD.
An InverseFrequencyPower value of -1 generates a blue noise process.
An InverseFrequencyPower value of -2 generates a violet (purple) noise process.


当value = 1,生成粉噪,根据粉噪生成的原理,将白噪声通过一个全极点滤波器,即可生成粉噪,这里我们讨论如何设计这个IIR全极点(All-poles)滤波器:

差分方程:



滤波器框图:



float all-pole(float *x, float *y, float *s, const float *a, int n, int p)
{
y = x - a[0] * s[0];   //对y赋初值

for(int i = 0; i < p; i++)   //输出
y -= a[i+1] * s[i+1];

for(int i = p; i > 0; i--)   //更新
s[i] = s[i-1];

s[0] = y;

return y;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: