⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fir filter test.cpp

📁 c语言版各种fir滤波器的实现
💻 CPP
字号:
/**********************************************
* Filename : FIR Filter Test.CPP              *
* Function : Test FIR Filter                  *
**********************************************/

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <direct.h>
#include <process.h>

void dir_filter(float h[],float z[],float x[],float y[],int n,int m);
void cas_filter(float h[],float z[],float x[],float y[],int n,int m);
void LP_filter_odd(float h[],float z[],float x[],float y[],int n,int m);
void LP_filter_even(float h[],float z[],float x[],float y[],int n,int m);
void fre_filter_odd(float c[],float b[],float a[],float z[],float x[],float y[],int n,int m);
void fre_filter_even(float c[],float b[],float a[],float z[],float x[],float y[],int n,int m);
void latc_filter(float k[],float q[],float x[],float y[],int n,int m);

void main( void )
{
    FILE *Stream;
    char lpszFileName[]="\\impulse.dat";
    char *Direct;
	float inputData[101],outData[101];
	float h[6],k[4],z[14],q[4];
	float a[8],b[8],c[4];

	int m,n;
	int i,j;

	
	Direct=(char *)calloc(_MAX_PATH,sizeof(char));
/* Get the current working directory: */
    if(_getcwd( Direct, _MAX_PATH ) == NULL )
       perror( "_getcwd error" );
    else
       strcat(Direct,lpszFileName);

    if((Stream=fopen(Direct,"r"))==NULL)
	{
		printf("Can not open the file!\n");
		free(Direct);
		exit(0);
	}
	else
	{
		i=0;
		while(fscanf(Stream,"%f",&inputData[i])!= EOF)
		{
          i++; 
		}
	}
    fclose(Stream);
	free(Direct);

/* test lattice filter */    
	n=4;
	m=101;
	k[0]=2;k[1]=0.25;k[2]=0.5;k[3]=0.3333;
	q[0]=0;q[1]=0;q[2]=0;q[3]=0;
	latc_filter(k,q,inputData,outData,n,m);
    if((Stream=fopen("latc_response.dat","w"))==NULL)
	{
		printf("Can not open the file!\n");
		exit(0);
	}
	else
	{
		for(j=0;j<101;j++)
		{
			fprintf(Stream,"%f ",outData[j]);
		}
	
	}
	fclose(Stream);


/* test dir filter */ 
	n=5;
	m=101;
    h[0]=0.1;h[1]=0.2;h[2]=0.3;h[3]=0.2;h[4]=0.1;
	z[0]=0.0;z[1]=0.0;z[2]=0.0;z[3]=0.0;
    dir_filter(h,z,inputData,outData,n,m);



	if((Stream=fopen("dir_response.dat","w"))==NULL)
	{
		printf("Can not open the file!\n");
		exit(0);
	}
	else
	{
		for(j=0;j<101;j++)
		{
			fprintf(Stream,"%f ",outData[j]);
		}
	
	}
	fclose(Stream);
    
/* test LP filter while n is odd */ 
	n=5;
	m=101;
	h[0]=0.1;h[1]=0.2;h[3]=0.3;
	z[0]=0.0;z[1]=0.0;z[2]=0.0;z[3]=0.0;
	LP_filter_odd(h,z,inputData,outData,n,m);
	if((Stream=fopen("LPodd_response.dat","w"))==NULL)
	{
		printf("Can not open the file!\n");
		exit(0);
	}
	else
	{
		for(j=0;j<101;j++)
		{
			fprintf(Stream,"%f ",outData[j]);
		}
	
	}
	fclose(Stream);

/* test LP filter while n is even */ 
	n=4;
	m=101;
	h[0]=0.1;h[1]=0.2;
	z[0]=0;z[1]=0;z[2]=0;
	LP_filter_even(h,z,inputData,outData,n,m);
    if((Stream=fopen("LPeven_response.dat","w"))==NULL)
	{
		printf("Can not open the file!\n");
		exit(0);
	}
	else
	{
		for(j=0;j<101;j++)
		{
			fprintf(Stream,"%f ",outData[j]);
		}
	
	}
	fclose(Stream);

    
/* test  FS filter while n is even */ 
	n=6;
	m=101;
    a[0]=-1;a[1]=1.0;a[2]=1;a[3]=1.0;a[4]=-1.0;a[5]=0;a[6]=1;a[7]=0;
	b[0]=-0.86603;b[1]=0.86603;b[2]=-0.5;b[3]=0.5;b[4]=1;b[5]=0;b[6]=1;b[7]=0;
	c[0]=3.4641;c[1]=4;c[2]=11;c[3]=0;
	z[0]=0;z[1]=0;z[2]=0;z[3]=0;z[4]=0;z[5]=0;
	z[6]=0;z[7]=0;z[8]=0;z[9]=0;z[10]=0;z[11]=0;z[12]=0;z[13]=0;
    fre_filter_even(c,b,a,z,inputData,outData,n,m);

    if((Stream=fopen("fre_even_response.dat","w"))==NULL)
	{
		printf("Can not open the file!\n");
		exit(0);
	}
	else
	{
		for(j=0;j<101;j++)
		{
			fprintf(Stream,"%f ",outData[j]);
		}
	
	}
	fclose(Stream);
	
/* test  FS filter while n is odd */
	n=5;
	m=101;
	a[0]=-0.618;a[1]=1.0;a[2]=1.618;a[3]=1.0;a[4]=-1.0;a[5]=0;
	b[0]=-0.809;b[1]=0.809;b[2]=0.309;b[3]=-0.309;b[4]=1;b[5]=0;
	c[0]=0.5818;c[1]=0.0849;c[2]=1.0;
	z[0]=0;z[1]=0;z[2]=0;z[3]=0;z[4]=0;z[5]=0;
	z[6]=0;z[7]=0;z[8]=0;z[9]=0;z[10]=0;

    fre_filter_odd(c,b,a,z,inputData,outData,n,m);

    if((Stream=fopen("fre_odd_response.dat","w"))==NULL)
	{
		printf("Can not open the file!\n");
		exit(0);
	}
	else
	{
		for(j=0;j<101;j++)
		{
			fprintf(Stream,"%f ",outData[j]);
		}
	
	}
	fclose(Stream);

/* test  cas filter */
    n=2;
	m=101;
    h[0]=0.1;h[1]=0.1;h[2]=0.1;h[3]=1;h[4]=1;h[5]=1;
	z[0]=0.0;z[1]=0.0;z[2]=0.0;z[3]=0.0;
	cas_filter(h,z,inputData,outData,n,m);

	if((Stream=fopen("cas_response.dat","w"))==NULL)
	{
		printf("Can not open the file!\n");
		exit(0);
	}
	else
	{
		for(j=0;j<101;j++)
		{
			fprintf(Stream,"%f ",outData[j]);
		}
	
	}
	fclose(Stream);
   
	system("type fre_even_response.dat");
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -