差分方程--C语言--低通滤波器的学习

用matlab实现

/*
  *@breif: low filter
  *
  *pSourceDataBuf[in]    : source data to input to filter
  *u8SourceDataBufLen[in]  : source data length
  *pDestDataBuf[out]         : filtered data buffer
  *Y_PRE[in/out]: global variant
  *X_PRE[in/out]: global variant
  *
  *return:  0(SUCCESS)/-1(FAILURE)
  */
  
  //psource--6
int8 low_filter_N5(double *pSourceDataBuf,uint8 u8SourceDataBufLen,double *pDestDataBuf,double *Y_PRE,double *X_PRE)
{
	double Y[N_LOW + 1] = {0.0};
    int8 iLopOut = 0;
	int8 iLopIn = 0;

    // init
	for(iLopOut = 0; iLopOut < N_LOW + 1; iLopOut++) 
	{
		Y[iLopOut] = 0.0;
	}

    A_LOW[0] = 0.0;
	
	// calculate 0~5 
	for(iLopOut = 0; iLopOut < N_LOW + 1; iLopOut++) 
	{
        // 前面这部分说明过去的X,可以在输入中找到。比如Y(2),
        // 那输入的X(0) X(1)可以使用。剩下的Xpre(0)-X(2)Xpre(1)-X(3)
        // Xpre(2)-X(4) Xpre(3)-X(5)就使用全局变量。这里计算分开了两次,
        // 根据输入序列是否可作为历史记录使用来区分。这里的X(5)是指过去的第5个数据。
        for(iLopIn = 0; iLopOut >= iLopIn; iLopIn++)
        {
            Y[iLopOut] += B_LOW[iLopIn]*pSourceDataBuf[iLopOut - iLopIn] - A_LOW[iLopIn]*Y[iLopOut - iLopIn];
        }

		//iLopIn=1 <= iLopOut=0
		//Y[0] = B_LOW[0]*pSourceDataBuf[0] - A_LOW[0]*Y[0];

		//iLopIn=5<6  iLopOut=0
		//Y[0] = B_LOW[0]*pSourceDataBuf[0] - A_LOW[0]*Y[0];
		//		+B_LOW[1]*X_PRE[0] - A_LOW[1]*Y_PRE[0]
		// 		+B_LOW[2]*X_PRE[1] - A_LOW[2]*Y_PRE[1]
		//		+B_LOW[3]*X_PRE[2] - A_LOW[2]*Y_PRE[2]
		//		+B_LOW[4]*X_PRE[3] - A_LOW[4]*Y_PRE[3]
		//		+B_LOW[5]*X_PRE[4] - A_LOW[5]*Y_PRE[4]

		// iLopOut = 1; Int 5
		//Y[1] = B_LOW[0]*pSourceDataBuf[1] - A_LOW[0]*Y[1]
		//     + B_LOW[1]*pSourceDataBuf[0] - A_LOW[1]*Y[0]
		// 		+B_LOW[2]*X_PRE[0] - A_LOW[2]*Y_PRE[0]
		//		+B_LOW[3]*X_PRE[1] - A_LOW[2]*Y_PRE[1]
		//		+B_LOW[4]*X_PRE[2] - A_LOW[4]*Y_PRE[2]
		//		+B_LOW[5]*X_PRE[3] - A_LOW[5]*Y_PRE[3]

		
		
		for(; iLopIn < N_LOW + 1; iLopIn++) 
		{
            Y[iLopOut] += B_LOW[iLopIn]*X_PRE[iLopIn - iLopOut - 1] - A_LOW[iLopIn]*Y_PRE[iLopIn - iLopOut - 1];
		}
	}
	A_LOW[0] = 1.0;

	for(iLopOut = 0; iLopOut < N_LOW; iLopOut++)
	{
        X_PRE[iLopOut] = pSourceDataBuf[N_LOW - iLopOut];
		Y_PRE[iLopOut] = Y[N_LOW - iLopOut];
	}
	//  backwards 6 datas
	//	X_PRE[0] = pSourceDataBuf[N];  	 Y_PRE[0] = Y[N_LOW ];
	//	X_PRE[1] = pSourceDataBuf[N-1];  Y_PRE[1] = Y[N_LOW-1];
	//	X_PRE[2] = pSourceDataBuf[N-2];  Y_PRE[2] = Y[N_LOW-2];
	//	X_PRE[3] = pSourceDataBuf[N-3];  Y_PRE[3] = Y[N_LOW-3];
	//	X_PRE[4] = pSourceDataBuf[N-4];  Y_PRE[4] = Y[N_LOW-4];
	//	X_PRE[5] = pSourceDataBuf[N-5];  Y_PRE[5] = Y[N_LOW-5];

	for(iLopOut = 0; iLopOut < N_LOW + 1; iLopOut++)
	{
        pDestDataBuf[iLopOut] = Y[iLopOut];
	}

	return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要实现巴特沃斯低通滤波器,需要进行以下步骤: 1. 设计巴特沃斯滤波器的模拟滤波器原型,确定通带截止频率、阻带截止频率以及通带和阻带最大衰减量。 2. 通过双线性变换将模拟滤波器原型转换为数字滤波器。 3. 对数字滤波器进行离散化,得到巴特沃斯低通数字滤波器差分方程式或者直接对其进行z变换得到其传递函数。 4. 将差分方程式或传递函数转换为C语言代码实现。 以下是一个C语言实现巴特沃斯低通滤波器的示例代码: ```c #include <stdio.h> #include <math.h> #define PI 3.14159265358979323846 typedef struct { double a[3]; double b[3]; } filter_coef; void butterworth_lowpass(double f0, double fs, double db_gain, filter_coef *coef) { double w0 = 2.0 * PI * f0 / fs; double bw = w0 / db_gain; double a1 = (-2.0 * cos(w0)) / (1.0 + bw * sin(w0) + bw * bw); double a2 = (1.0 - bw * sin(w0) + bw * bw) / (1.0 + bw * sin(w0) + bw * bw); double b0 = (1.0 - cos(w0)) / 2.0; double b1 = 1.0 - cos(w0); double b2 = (1.0 - cos(w0)) / 2.0; coef->a[0] = a2; coef->a[1] = a1; coef->a[2] = 1.0; coef->b[0] = b0; coef->b[1] = b1; coef->b[2] = b2; } int main() { double f0 = 1000.0; // 通带截止频率 double fs = 44100.0; // 采样频率 double db_gain = 3.0; // 通带最大衰减量 filter_coef coef; butterworth_lowpass(f0, fs, db_gain, &coef); // 打印系数 printf("a0 = %f, a1 = %f, a2 = %f\n", coef.a[0], coef.a[1], coef.a[2]); printf("b0 = %f, b1 = %f, b2 = %f\n", coef.b[0], coef.b[1], coef.b[2]); return 0; } ``` 该代码实现了一个巴特沃斯低通滤波器,并输出了其系数。其中,`f0`表示通带截止频率,`fs`表示采样频率,`db_gain`表示通带最大衰减量,`coef`是一个结构体,用于存储滤波器的系数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一枚努力的程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值