comparison tools/filt1.c @ 0:5242703e91d3 tip

Initial checkin for AIM92 aimR8.2 (last updated May 1997).
author tomwalters
date Fri, 20 May 2011 15:19:45 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:5242703e91d3
1 /*
2 filt1.c 1st order LP filter using an exponential smoother
3 (recursive "leaky integrator" filter)
4
5 The n'th recursive update is:
6
7 a) y[n] = a.y[n-1] + x[n] recursive sum
8 b) y[n] = a.y[n-1] + (1-a).x[n] recursive mean
9
10 where decay constant a = exp(-Ts/T)
11 and Ts is sample interval in seconds
12 T is decay time-constant in seconds
13
14 The 1st order LP filter is an exponential window which decays into the past.
15 With decay constant a=1 there is infinite memory because it does not decay.
16 The result is either a steadily accumulating sum or mean.
17 (A zero mean is assumed).
18 With 0 < a < 1 there is a finite decaying memory.
19 The smaller `a', the smaller the memory (it decays faster).
20 With a = 0 the filter output is identical with its input.
21
22 The window width parameter is the time-constant parameter.
23 The half life of the exponential window is given by -T.ln(0.5) = 0.693T
24 ie. for a given time constant, T secs, the window decays to half of its
25 current value in a period of 0.693T secs.
26 (Thus T can be set to accomodate an expected period).
27
28 When the time-constant parameter is given with time units (s or ms) then
29 it is converted to a decay constant using a = exp(-Ts/T).
30 Otherwise it is taken to be the decay constant directly.
31
32 */
33
34 #include <stdio.h>
35 #include <math.h>
36 #include "options.h"
37 #include "units.h"
38 #include "strmatch.h"
39
40 char applic[] = "1st order low-pass filter using exponential smoother." ;
41
42 static char *helpstr, *debugstr, *sampstr, *tcstr, *destr ;
43
44 static Options option[] = {
45 { "help" , "off" , &helpstr , "help" , DEBUG },
46 { "debug" , "off" , &debugstr , "debugging switch" , DEBUG },
47 { "samplerate", "20kHz" , &sampstr , "Samplerate " , VAL },
48 { "tc" , "3ms" , &tcstr , "Decay time constant" , VAL },
49 { "de" , "mean" , &destr , "Difference equation" , VAL },
50 ( char * ) 0 } ;
51
52
53 int samplerate ;
54 double a ; /* decay constant */
55
56
57 main (argc, argv)
58 int argc;
59 char **argv;
60 {
61 FILE *fp ;
62 short x, y ;
63 int morehelp() ;
64
65 fp = openopts( option,argc,argv ) ;
66 if ( !isoff( helpstr ) )
67 helpopts1( helpstr, argv[0], applic, option, morehelp ) ;
68
69 samplerate = to_Hz( sampstr ) ;
70
71 if ( Units( tcstr ) )
72 a = exp( -(double)( 1. / ( samplerate * to_s( tcstr, samplerate ) ) ) ) ;
73 else
74 a = (double)atof( tcstr ) ;
75
76 y = 0 ; /* initialization */
77
78 if ( iststr( destr, "mean" ) )
79 while ( fread( &x, sizeof(short), 1, fp ) ) {
80 y = a * y + ( 1 - a ) * x ;
81 fwrite( &y, sizeof(short), 1, stdout ) ;
82 }
83
84 else if ( iststr( destr, "sum" ) )
85 while ( fread( &x, sizeof(short), 1, fp ) ) {
86 y = a * y + x ;
87 fwrite( &y, sizeof(short), 1, stdout ) ;
88 }
89
90 else {
91 fprintf( stderr,"unknown de [%s]\n", destr ) ;
92 exit( 1 ) ;
93 }
94
95 fclose( fp );
96 }
97
98
99 /* Return 1 if the str has appended units. Otherwise return 0. */
100
101 Units(str)
102 char *str ;
103 {
104 char *eptr = str + strlen( str ) ;
105
106 if( isdigit( *--eptr ) ) return 0 ; /* last char is digit, so no units */
107 else return 1 ;
108 }
109
110
111
112 morehelp()
113 {
114 fprintf(stderr,"\nde: \n");
115 fprintf(stderr," sum y[n] = a.y[n-1] + x[n] \n");
116 fprintf(stderr," mean y[n] = a.y[n-1] + (1-a).x[n] (unity steady-state gain)\n");
117 exit( 1 ) ;
118 }