您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > C/C++资料 > c语言写的fir低通滤波器
根据fir滤波器的公式y(n)=∑h(m)x(n-m);(m:0~(N-1)).利用MATLAB产生滤波器系数(h(n))并归一化,下面为一个LP滤波算法voidfilter(void){uint16i,j;fp32sum;int16x1[2030];fp32h[19]={-0.0027,-0.0025,0.0050,0.0157,-0.0000,-0.0471,-0.0482,0.0838,0.2953,0.4013,0.2953,0.0838,-0.0482,-0.0471,-0.0000,0.0157,0.0050,-0.0025,-0.0027};for(i=0;i2020;i++)x1[i]=data0[i];for(i=0;i2020;i++){sum=0.0;for(j=0;j19;j++){if(i=j)sum+=h[j]*x1[i-j];else;}data0[i]=(int16)sum;}for(i=0;i2000;i++){data0[i]=data0[i+20];}}考虑到前19个点为不完全累加和,故抛去前19个点。(应该是前后各18个点都是不完全累加和,都应该去掉,对于数据分段进入滤波器的情况,应该把前一段的后面数据放到下一段的前面,这段时间我在解调FSK时遇到了这个问题,通过滤波器的数据的分段处理。)设输入数据x[N],输出数据y[N],滤波器系数h[n]1.直接法(由y(m)=h(0)*x(m)+h(1)*x(m-1)+...+h(N-1)*x(m-n-1));voidfir(shortx[],shorth[],shorty[]){inti,j;longlongsum;for(j=0;jN;j++){sum=0;for(i=0;in;i++)sum+=x[j-i]*h[i];y[j]=sum15;}}乘法器使用次数:N*n2.逆推法:voidfir(shortx[],shorth[],shorty[]){inti,j;longsum;for(j=0;jn;j++){for(i=0;iN;i++){sum=0;sum=h[j]*x[i]y[i]+=sum15;}}}乘法器使用次数:N*n3.倒序法:(输入输出可以是同一量)voidfir(shortx[],shorth[],shorty[]){inti,j;longlongsum;for(j=N;j0;j--){sum=0;for(i=n;i0;i--)sum+=x[j-i]*h[i];y[j]=sum15;}}#includestdio.h#includemath.h#definetrue1#definefalse0#definen8#definebufsize100/*thebuffersizeis100*//*globaldeclarations*/intin_buffer[bufsize];/*processingdatabuffers*/intout_buffer[bufsize];/*functions*/staticintprocessing(int*input,int*output);staticvoiddataio(void);staticlonground(longa);voidmain(){int*input=&in_buffer[0];int*output=&out_buffer[0];puts(the1stexperimentstarted\n);/*loopforever*/while(true){/**readinputdatausingaprobe-pointconnectedtoahostfile.*writeoutputdatatoagraphconnectedthroughaprobe-point.*///readtheinputsignal.//iftheinputfileissine1.dat,thesignalcontains300hz,400hzand500hz.//iftheinputfileissine2.dat,thesignalcontains100hz,400hzand500hz.//thesamplingfrequencyis1200hz.dataio();/*removethefrequencycompomentof400hzand500hz*/processing(input,output);//writetheoutputsignal.//theoutputfileisresult.dat.dataio();}}/**========processing========**function:applyalow-passfirfiltertoinputsignalandremovethefrequencyhigherthan350hz.**parameters:addressofinputandoutputbuffers.**returnvalue:true.*/staticintprocessing(int*input,int*output){inti,size=bufsize;shortxx0,x,y;//shortz[n]={0,0,0,0,0,0,0,0,0};shortz[n]={0,0,0,0,0,0,0,0};//shortw[2*n+1]={22,356,155,990,466,220,777,216,777,26,466,9,155,0,22};//shortw[2*n+1]={6,457,56,1024,224,418,523,382,784,99,784,43,523};//shortw[2*n+1]={330*2,3299*2,1982*2,6867*2,4955*2,1594*2,6607*2,1065*2,4955*2,109*2,1982*2,17*2,330*2};//shortw[2*n+1]={661,6598,3964,13733,9910,3187,13214,2131,9910,217,3964,34,661};//shortw[2*n+1]={58,5628,526,8192,2105,5883,4913,3829,7369,1543,7369,504,4913,102,2105,14,526,1,58};//shortw[2*n+1]={28,4432,280,8192,1259,4883,3356,3975,5873,1509,7048,644,5873,142,3356,30,1259,3,280,0,28};//shortw[2*n+1]={26,651,182,1024,545,421,909,247,909,51,545,11,182,1,26};//shortw[2*n+1]={831,20846,5815,32768,17445,13486,29075,7888,29075,1647,17445,349,5815,21,831};//shortw[2*n+1]={208,5211,1454,8192,4361,3371,7269,1972,7269,412,4361,87,1454,5,208};shortw[2*n+1]={101,4356,810,8192,2835,3403,5670,2517,7088,605,5670,193,2835,21,810};//shortw[2*n+1]={101,4356,810,8192,2835,3403,5670,2517,7088,605,5670,193,2835,21,810,2,101};//shortw[2*n+1]={50,3814,454,8192,1815,3504,4235*,3084,6353,831,6353,349,4235,50,1815,8,454,0,50};longy0,z0;//22222222222222while(size--){xx0=*input++;x=xx0*6;z0=(long)x15;y0=0;for(i=0;in;i++){z0-=(long)w[2*i+1]*(long)z[i];y0+=(long)w[2*i+2]*(long)z[i];}y0+=(long)w[0]*(z015);y0=round(y0);for(i=n-1;i0;i--)z[i]=z[i-1];z0=round(z0);z[0]=(short)(z015);y=(short)(y015);*output++=y;}/*additionalprocessingload*/return(true);}/**========dataio========**function:readinputsignalandwriteprocessedoutputsignal.**parameters:none.**returnvalue:none.*/staticvoiddataio(){/*dodatai/o*/return;}staticlonground(longa){longx3;x3=a&(0xffff0000);returnx3;}
本文标题:c语言写的fir低通滤波器
链接地址:https://www.777doc.com/doc-7027648 .html