巴特沃斯滤波器

#include "math.h"
#include<stdio.h>
#include<string.h>
#include<stdlib.h>


#define MAX 2500
#define PI 3.1415926

/****************************************************************************************
* biliner(int fc,int N,int fs,float* x)
*  参数说明  fc 截止频率
*            fs 采样率
*            N  (1=<N<=3)  阶数
*            *x 传入的待滤波数组
*双线性法基于巴特沃斯的数字滤波器    
****************************************************************************************/

using namespace std;


void biliner(int fc,int N,int fs,float *x)
{
	 //定义变量
	 long i=0;   
	 float W,Wc,t,y[MAX];//MAX是3000个点
	 //转化为数字角频率
	 W=2*PI*fc/fs;
	 //预畸变
	 Wc=2*fs*tan(w/2);
	 //去归一化影响
	 t=2*fs/wc;
     switch(N)
     {
	  /*一阶情况*/
	  case 1:
		    //Hs=1/(p+1);
		    y[0]=x[0]/(1+t);
		    for(i=1;i<MAX;i++)
		     y[i]=(x[i]+x[i-1]-(1-t)*y[i-1])/(1+t);
		    break;
		    /*二阶情况*/
	   case 2: 
		    //Hs=1/(p*p+1.414*p+1);
		    y[0]=x[0]/(t*t+1.414*t+1);
		    y[1]=(x[1]+2*x[0]-(2-2*t*t)*y[0])/(t*t+1.414*t+1);
		    for(i=2;i<MAX;i++)
		     y[i]=(x[i]+2*x[i-1]+x[i-2]-(2-2*t*t)*y[i-1]-(1-1.414*t+t*t)*y[i-2])/(t*t+1.414*t+1);
		    break;
		    /*三阶*/
	   case 3:
		    y[0]=x[0]/(t*t*t+2*t*t+2*t+1);
		    y[1]=(x[1]+3*x[0]-(3-3*t*t*t-2*t*t+2*t)*y[0])/(t*t*t+2*t*t+2*t+1);
		    y[2]=(x[2]+3*x[1]+3*x[0]-(3-3*t*t*t-2*t*t+2*t)*y[1]-(3*t*t*t-2*t*t-2*t+3)*y[0])/(t*t*t+2*t*t+2*t+1);
		    for(i=3; i < MAX ;i++)
		     y[i]=(x[i]+3*x[i-1]+3*x[i-2]+x[i-3]-(3-3*t*t*t-2*t*t+2*t)*y[i-1]-(3*t*t*t-2*t*t-2*t+3)*y[i-2]-(1-t*t*t+2*t*t-2*t)*y[i-3])/(t*t*t+2*t*t+2*t+1);
		       break;
	    default:
			    return ;
				break;
	  }
	  
	  for(i=0;i<MAX;i++)
	    x[i]=y[i];  //输出x[] 
	    
	 return;
}
int main(int argc, char *argv[])
{
	 float x[MAX];
	 int i,lenth,m;
	 FILE *fpx=fopen("newdata.txt","w");
	 FILE *fp=fopen("ChannelData.txt","r");
	 if(fp==NULL||fpx==NULL)
	 {
	 printf("失败!!!\n");
	 return 0;
	 }
	 for(i=0;i<MAX;i++)
	 fscanf(fp,"%f ",&x[i]);//输入x[] 
	
	 biliner(2,200,2,x);
	 
	 for(i=0;i<MAX;i++)
	  fprintf(fpx,"%f\n",x[i]);//输出x[] 
	  fclose(fpx);
	  fclose(fp);

}

ButterWorth巴特沃斯滤波64B位 C++库,支持高通、低通、带通、带阻滤波。需要32位库请私信。 提供C#调用方法: public static class ButterFilter { [DllImport("V_Filter.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] public extern static void DeleteFilter(IntPtr filter); [DllImport("V_Filter.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] public extern static double FilterProcess(IntPtr filter, double data); [DllImport("V_Filter.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] public extern static IntPtr CreateHighPass(double sampleRate, double order, double cutoffFrequency); [DllImport("V_Filter.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] public extern static IntPtr CreateLowPass(double sampleRate, double order, double cutoffFrequency); [DllImport("V_Filter.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] public extern static IntPtr CreateBandPass(double sampleRate, double order, double centerFrequency, double bandWidth); [DllImport("V_Filter.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] public extern static IntPtr CreateBandStop(double sampleRate, double order, double centerFrequency, double bandWidth); } 初始化滤波器: lowpassFilter= ButterFilter.CreateLowPass(sampleRate, order, endFreq); highpassFilter= ButterFilter.CreateHighPass(sampleRate, order, endFreq); bandstopFilter= ButterFilter.CreateBandStop(sampleRate, order,beginFreq, endFreq - beginFreq); bandPassFilter= ButterFilter.CreateBandPass(sampleRate, order,beginFreq, endFreq - beginFreq); 数据滤波: value = ButterFilter.FilterProcess(highpassFilter, value);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值