/************************************************************** * Program to demonstrate the Butterworth filter * * (removing frequencies greater then Fc) * * * * C++ version by J-P Moreau, Paris * * (www.jpmoreau.fr) * * ----------------------------------------------------------- * * SAMPLE RUN: * * * * Input data file (tfft.dat): * * * * (The test signal contains 3 frequencies: 50, 250, 500 hz) * * * * 1024 * * 0.00000000000000E+0000 0.00000000000000E+0000 * * 1.95503421309917E-0004 3.53914399999776E+0001 * * 3.91006842619834E-0004 5.95684899999760E+0001 * * 5.86510263929974E-0004 6.54621699999552E+0001 * * 7.82013685239669E-0004 5.24038399999845E+0001 * * ... ... * * 1.98826979472187E-0001 2.77372500000183E-0001 * * 1.99022482893497E-0001 -2.43361500000174E+0000 * * 1.99217986314807E-0001 -4.84236799999780E+0000 * * 1.99413489736116E-0001 -6.02247899999929E+0000 * * 1.99608993157426E-0001 -5.45615399999951E+0000 * * 1.99804496578736E-0001 -3.22824200000105E+0000 * * 2.00000000000045E-0001 -2.96010699999982E-0003 * * * * Output file (tfilter.lst): * * * * Time Input Signal Filtered Signal * * ------------------------------------------------------- * * 0.0000 0.000000 0.000000 * * 0.0002 35.391440 0.158005 * * 0.0004 59.568490 1.278083 * * 0.0006 65.462170 5.003994 * * 0.0008 52.403840 12.809453 * * 0.0010 26.229620 24.336649 * * ... ... ... * * 0.1988 0.277373 -2.224546 * * 0.1990 -2.433615 -0.802179 * * 0.1992 -4.842368 0.588941 * * 0.1994 -6.022479 1.320860 * * 0.1996 -5.456154 1.036090 * * 0.1998 -3.228242 -0.213007 * * 0.2000 -0.002960 -1.978450 * * * * Note: The cut off frequency Fc is 500 hz, the filter order * * is 4 (A Fourier analysis of the filtered signal shows * * that frequencies greater than Fc are satisfactorily * * removed but with a slight time shift of the signal). * **************************************************************/ #include #include #define MACH_EPS 1e-12 #define SIZE 1025 typedef double Filter_Coef[6][10]; typedef double Memory_Coef[3][10]; FILE *f_in, *f_out; int i,order,ndata; double dt,Fc,t,temp,tbegin,tend,yy; double signal[SIZE],filtered[SIZE]; Filter_Coef C; Memory_Coef D; int NSections; double Tg,Xdc; /********************************************************************** * Filtering a signal F(t) by Butterworth method * * (removing frequencies greater then Fc) * * ------------------------------------------------------------------- * * Calling mode: Filter(Xs,Xd,NSections,C,D); * * ------------------------------------------------------------------- * * INPUTS: * * ------- * * Xd.......: current value of input signal (double) * * NSections: Number of required 2nd order sections (integer) * * = n/2 for n even * * = (n+1)/2 for n odd * * n........: order of filter (1 to 4) * * C........: Table[1..5,1..NSections] of filter coefficients * * calculated previously by BUTTERWORTH procedure * * D........: Table[1..2,1..NSections] of coefficients defining * * the filter memory, initialized by INIT procedure. * * ------------------------------------------------------------------- * * OUTPUTS: * * ------- * * D........: Table updated after the call to Filter procedure * * Xs.......: current value of filtered signal (double) * * ------------------------------------------------------------------- * * Référence * * --------- * * "Lawrence R.Rabiner et Bernard Gold * * Theory and application of digital processing. * * Prentice Hall Inc., EnglewoodclIFfs,NEW JERSEY,1975" [BIBLI 15]. * * * * C++ version by J-P Moreau, Paris * * from Fortran version by J-P Dumont / Tuan Dang Trong * **********************************************************************/ void Filter( double *Xs,double Xd, int NSections, Filter_Coef C, Memory_Coef D ) { double x,y,err; int i; x=Xd; for (i=1; i 2.0*Pi) { m=int(Arg/2.0/Pi); Arg=Arg-(m*2.0*Pi); } Omega= tan(Arg); OmegaSq=Omega*Omega; Modn=(n % 2); if (Modn==0) temp=HALF; else temp=Zero; Ns2=n/2; *NSections=Ns2+Modn; *Tg=Zero; if (n>1) for (i=1; i