Mercurial > hg > aim92
comparison tools/units.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 units.c | |
3 --------- | |
4 Return binary doubles for given string values, according to units. | |
5 Units are specified by the final char: | |
6 p sample points. | |
7 s seconds. | |
8 ms milliseconds | |
9 Hz Hertz. | |
10 kHz KiloHertz | |
11 erb equivalent rectangular bandwidth [Hz] | |
12 | |
13 ***************************************************************************/ | |
14 | |
15 #include <stdio.h> | |
16 #include <math.h> | |
17 #include <ctype.h> | |
18 #include "units.h" | |
19 #include "strmatch.h" | |
20 | |
21 | |
22 /* | |
23 Return the units index number of the best matching units at the tail of | |
24 string `s'. Return -1 if no units are found. | |
25 */ | |
26 | |
27 int unitindex( s ) | |
28 char *s ; | |
29 { | |
30 int i ; | |
31 | |
32 if ( ( i = strnumspn( s ) ) == 0 ) { /* case of no digits found */ | |
33 fprintf( stderr, "bad value [%s]\n", s ) ; | |
34 exit( 1 ) ; | |
35 } | |
36 if ( i == strlen( s ) ) | |
37 return (-1) ; /* case of no units found */ | |
38 | |
39 switch ( i = listindex( units, s+i ) ) { | |
40 case (-1) : fprintf(stderr,"unknown units [%s]\n", s ) ; | |
41 exit( 1 ) ; | |
42 case (-2) : fprintf(stderr,"ambiguous units [%s]\n", s ) ; | |
43 exit( 1 ) ; | |
44 } | |
45 return i ; /* index of units */ | |
46 } | |
47 | |
48 | |
49 | |
50 /* | |
51 Return a ptr to the units string at the tail of string `s'. | |
52 Otherwise return a ptr to the null terminator of `s' if no units are found. | |
53 */ | |
54 | |
55 char *unitstr( s ) | |
56 char *s ; | |
57 { | |
58 unitindex( s ) ; /* check for valid (or no) units */ | |
59 return ( strnumptr( s ) ) ; | |
60 } | |
61 | |
62 | |
63 /* | |
64 Search the end of the given string for units (such as ms, kHz, etc). | |
65 Delete any units found by overwriting the start of the units string with '\0'. | |
66 */ | |
67 | |
68 stripunits( s ) | |
69 char *s ; | |
70 { | |
71 *( unitstr( s ) ) = '\0' ; | |
72 } | |
73 | |
74 | |
75 /* | |
76 Return an allocated string to the square root of the given value with original | |
77 units appended. | |
78 */ | |
79 | |
80 char *sqrt_units( s ) | |
81 char *s ; | |
82 { | |
83 char s1[64] ; | |
84 | |
85 sprintf( s1, "%.2f%s", sqrt( (double)atof( s ) ), unitstr( s ) ) ; | |
86 return ( strcpy( (char *)malloc( strlen( s1 ) + 1 ), s1 ) ) ; | |
87 } | |
88 | |
89 | |
90 /* | |
91 Return the type of a <number>: INT, FLOAT, or 0 (if not a number). | |
92 <number> = [-]<digits>[.]<digits> | |
93 where either, but not both, of the digit strings may be empty. | |
94 */ | |
95 | |
96 int numbertype( str ) | |
97 char *str ; | |
98 { | |
99 if ( strnumspn( str ) == 0 ) return 0 ; | |
100 if ( strchr( str, '.' ) == (char *)0 ) return INT ; | |
101 else return FLOAT ; | |
102 } | |
103 | |
104 | |
105 /* | |
106 Return 1 if str is an int, otherwise return 0. | |
107 */ | |
108 | |
109 int isint( str ) | |
110 char *str ; | |
111 { | |
112 if ( numbertype( str ) == INT ) return 1 ; | |
113 return 0 ; | |
114 } | |
115 | |
116 /* | |
117 Return 1 if str is a float, otherwise return 0. | |
118 */ | |
119 | |
120 int isfloat( str ) | |
121 char *str ; | |
122 { | |
123 if ( numbertype( str ) == FLOAT ) return 1 ; | |
124 return 0 ; | |
125 } | |
126 | |
127 | |
128 /* | |
129 Units converters. | |
130 Generally called: to_<unit> where <unit> is the output units. | |
131 Input strings without units have `default' units. | |
132 */ | |
133 | |
134 double to_Hz( str, samplerate ) /* default units Hz */ | |
135 char *str ; | |
136 int samplerate ; | |
137 { | |
138 switch ( unitindex( str ) ) { | |
139 case (-1) : return atof( str ) ; | |
140 case 0 : return ( (double)samplerate / atof( str ) ) ; | |
141 case 1 : return ( 1. / atof( str ) ) ; | |
142 case 2 : return ( 1000. / atof( str ) ) ; | |
143 case 3 : return atof( str ) ; | |
144 case 4 : return ( atof( str ) * 1000 ) ; | |
145 case 5 : return ( ( exp( (double)(atof( str ) / 9.265 )) - 1 ) * 9.265 * 24.7 ) ; | |
146 default : fprintf(stderr, "can't convert [%s] to Hz\n", str ) ; | |
147 exit( 1 ) ; | |
148 } | |
149 } | |
150 | |
151 double to_kHz( str, samplerate ) /* default units Hz */ | |
152 char *str ; | |
153 int samplerate ; | |
154 { | |
155 switch ( unitindex( str ) ) { | |
156 case (-1) : return ( atof( str ) * .001 ) ; | |
157 case 0 : return ( ( (double)samplerate / atof( str ) ) * 0.001 ) ; | |
158 case 1 : return ( ( 1. / atof( str ) ) * 0.001 ) ; | |
159 case 2 : return ( 1. / atof( str ) ) ; | |
160 case 3 : return ( atof( str ) * .001 ) ; | |
161 case 4 : return atof( str ) ; | |
162 case 5 : return ( ( exp( (double)(atof( str ) / 9.265 )) - 1 ) * 9.265 * 24.7 * .001 ) ; | |
163 default : fprintf(stderr, "can't convert [%s] to kHz\n", str ) ; | |
164 exit( 1 ) ; | |
165 } | |
166 } | |
167 | |
168 double to_erb( str ) /* default units erb */ | |
169 char *str ; | |
170 { | |
171 switch ( unitindex( str ) ) { | |
172 case (-1) : return atof( str ) ; | |
173 case 3 : return ( 9.265 * log( (double)(1 + atof( str )/(9.265*24.7)) ) ) ; | |
174 case 4 : return ( 9.265 * log( (double)(1 + (1000*atof( str ))/(9.265*24.7)) ) ) ; | |
175 case 5 : return atof( str ) ; | |
176 default : fprintf(stderr, "can't convert [%s] to erb\n", str ) ; | |
177 exit( 1 ) ; | |
178 } | |
179 } | |
180 | |
181 double to_p( str, samplerate ) /* default units p */ | |
182 char *str ; | |
183 int samplerate ; | |
184 { | |
185 switch ( unitindex( str ) ) { | |
186 case (-1) : return atof( str ) ; | |
187 case 0 : return atof( str ) ; | |
188 case 1 : return ( atof( str ) * samplerate ) ; | |
189 case 2 : return ( atof( str ) * samplerate * .001 ) ; | |
190 case 3 : return ( samplerate / atof( str ) ) ; | |
191 case 4 : return ( samplerate / ( 1000 * atof( str ) ) ) ; | |
192 default : fprintf(stderr, "can't convert [%s] to samples\n", str ) ; | |
193 exit( 1 ) ; | |
194 } | |
195 } | |
196 | |
197 | |
198 | |
199 double to_s( str, samplerate ) /* default units p (sample points) */ | |
200 char *str ; | |
201 int samplerate ; | |
202 { | |
203 switch ( unitindex( str ) ) { | |
204 case (-1) : return ( atof( str ) / samplerate ) ; | |
205 case 0 : return ( atof( str ) / samplerate ) ; | |
206 case 1 : return atof( str ) ; | |
207 case 2 : return ( atof( str ) * .001 ) ; | |
208 case 3 : return ( 1. / atof( str ) ) ; | |
209 case 4 : return ( 1. / ( 1000 * atof( str ) ) ) ; | |
210 default : fprintf(stderr, "can't convert [%s] to secs\n", str ) ; | |
211 exit( 1 ) ; | |
212 } | |
213 } | |
214 | |
215 | |
216 double to_ms( str, samplerate ) /* default units p */ | |
217 char *str ; | |
218 int samplerate ; | |
219 { | |
220 switch ( unitindex( str ) ) { | |
221 case (-1) : return ( atof( str ) * 1000 / samplerate ) ; | |
222 case 0 : return ( atof( str ) * 1000 / samplerate ) ; | |
223 case 1 : return ( atof( str ) * 1000 ) ; | |
224 case 2 : return atof( str ) ; | |
225 case 3 : return ( 1000. / atof( str ) ) ; | |
226 case 4 : return ( 1. / atof( str ) ) ; | |
227 default : fprintf(stderr, "can't convert [%s] to ms\n", str ) ; | |
228 exit( 1 ) ; | |
229 } | |
230 } |