|
// filter.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void filter(double *x, int len_x, double *coeff_b, int len_b, double *coeff_a, int len_a, double* zi, int len_zi, double* &filter_x, int& len_filter_x);
int main(int argc, char *argv[])
{
int i;
// 第一个参数
double x[20] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 };
// 第二个参数
int len_x = 20;
// 第三个参数
double coeff_b[18] = { -0.000000,0.000000,0.000000,0.000000,0.000001,0.000020,0.000153,0.000539,0.000957,0.000888,0.000430,0.000105,0.000012,0.000001,0.000000,0.000000,-0.000000,0.000000 };
// 第四个参数
int len_b = 18;
// 第五个参数
double coeff_a[18] = { 1.000000,-8.507642,38.059387,-116.740268,272.044806,-507.528390,781.139627,-1009.730407,1107.339849,-1034.816858,823.630706,-555.293117,313.568811,-145.476731,53.711375,-14.938188,2.813064,-0.272917 };
// 第六个参数
int len_a = 18;
// 第七个参数
double* zi = new double[100];
memset((double*)zi, 0, sizeof(double) * 100);
// 第八个参数
int len_zi = 100;
// 第九个参数
double* filter_x = NULL;
// 第十个参数
int len_filter_x = 0;
filter(x, len_x, coeff_b, len_b, coeff_a, len_a, zi, len_zi, filter_x, len_filter_x);
printf("Uint Impulse Response \n");
for (i = 0; i < len_zi; i++) {
printf("%f\n",zi[i]);
}
system("pause");
return 0;
}
/*函数使用说明:
1、
2、对于len_a=1的情况,函数内部补充数组a的个数与b相同;
3、函数与matlab的filter函数区别在于zi的个数不同,本函数为len_b,matlab则为len_b-1
filter(x, 20, &vecData[0], 11, &vecData1[0], 11, zi, 20, y1, hhg);
*/
/**************************************************************************************************
* 函 数 : void filter(b,a,m,n,x,len,px,py)
* 调 用 : filter
* 参 数 :
* x 一个数组 double x[20]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; 长度是
* len_x x 的长度
* coeff_b 系数
* len_b 滤波器分母系数个数
* coeff_a 系数
* len_a 滤波器分子系数个数
* zi zi 既是输入参数(initial conditions),又是输出参数(final conditions),因此,zi需要赋值,首次调用赋0;
* len_zi zi 长度
* filter_x
* len_filter_x
*************************************************************************************************/
void filter(double *x, int len_x, double *coeff_b, int len_b, double *coeff_a, int len_a, double* zi, int len_zi, double* &filter_x, int& len_filter_x)
{
len_filter_x = len_x;
filter_x = new double[len_x];
if (len_a == 1)
{
for (int m = 0; m<len_x; m++)
{
filter_x[m] = coeff_b[0] * x[m] + zi[0];
for (int i = 1; i<len_b; i++)
{
zi[i - 1] = coeff_b[i] * x[m] + zi[i];//-coeff_a[i]*filter_x[m];
}
}
}
else
{
for (int m = 0; m<len_x; m++)
{
filter_x[m] = coeff_b[0] * x[m] + zi[0];
for (int i = 1; i<len_b; i++)
{
zi[i - 1] = coeff_b[i] * x[m] + zi[i] - coeff_a[i] * filter_x[m];
}
}
}
}
|
|