comparison model/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 Copyright (c) Applied Psychology Unit, Medical Research Council. 1988, 1989
3 ===========================================================================
4
5 Permission to use, copy, modify, and distribute this software without fee
6 is hereby granted for research purposes, provided that this copyright
7 notice appears in all copies and in all supporting documentation, and that
8 the software is not redistributed for any fee (except for a nominal shipping
9 charge). Anyone wanting to incorporate all or part of this software in a
10 commercial product must obtain a license from the Medical Research Council.
11
12 The MRC makes no representations about the suitability of this
13 software for any purpose. It is provided "as is" without express or implied
14 warranty.
15
16 */
17
18 /*
19 units.c
20 =======
21
22
23 Unit conversion routines for APU model.
24
25
26
27 Copyright (c), 1989 The Medical Research Council, Applied Psychology Unit.
28
29
30 Author : John Holdsworth
31 Written : 22th March, 1989.
32
33 Edited : Mike Allerhand, 1990.
34
35 */
36
37 #ifndef lint
38 static char *sccs_id = "@(#)units.c 1.3 John Holdsworth, Mike Allerhand (MRC-APU) 6/6/91" ;
39 #endif
40
41
42
43 /*********************** Unit conversion routines ***************************
44 * Values are stored as strings.
45 * Units are specified by the final char:
46 * p sample points.
47 * s seconds.
48 * b or B decibels.
49 * Hz Hertz.
50 * Unit multipliers are specified by the optional penultimate char:
51 * k kilo (scale by one thousand).
52 * d deci (scale by one tenth).
53 * c centi (scale by one hundreth).
54 * m milli (scale by one thousandth).
55 * u (scale by one millionth).
56 *
57 * Examples: "10p" time period of 10 sample points
58 * "10ms" time period of 10 milliseconds
59 * "10kHz" frequency of 10 kiloHertz
60 *
61 * Conversion routines:
62 * units( ch ) Multiplier for unit, (k,d,c,m,u).
63 * Freq( str ) String to value (eg: 10, 10Hz, 10kHz).
64 * Samples( str ) String to value (num samples) (eg: 10p, 10s, 10ms).
65 * Scalar( str ) String to value (scalar number), also converts dB's.
66 *
67 * Examples: Freq("10000") returns 10000
68 * Freq("10kHz") returns 10000
69 * Samples("3p") returns 3
70 * Samples("3ms") return 30 (when the sample rate is 10kHz).
71 * Samples("3") like "3ms" (milliseconds is default unit).
72 * Scalar("10.") returns 10
73 * Scalar( "20dB" ) returns 10
74 *
75 * See also stitch.h for conversion routines:
76 * ToPoints(type,bytes) Count in bytes to number of points of given type.
77 * ToBytes(type,points ) Number of points of given type to count in bytes.
78 *
79 * Cycles( str, cfreq ) This is like Samples( str ), except that it also
80 * allows `c' (cycles) units. A given number of cycles
81 * of a particular channel are converted to samples.
82 * This requires the centre frequency of the channel,
83 * cfreq = frequencies[chan].
84 ****************************************************************************/
85
86 #include <math.h>
87
88 #if defined(THINK_C) || defined(NeXT)
89 #include <stdlib.h>
90 #endif
91
92 #include "units.h"
93 #include "formulae.h"
94
95 extern char *samplestr ;
96
97 static double units( ch )
98 char ch ;
99 {
100 switch( ch ) {
101 case 'k' :
102 return ( 1000. ) ;
103 case 'd' :
104 return ( 0.1 ) ;
105 case 'c' :
106 return ( 0.01 ) ;
107 case 'm' :
108 return ( 0.001 ) ;
109 case 'u' :
110 return ( 0.000001 ) ;
111
112 default :
113 return ( 1. ) ;
114 }
115 }
116
117 double Freq( str )
118 char *str ;
119 {
120 char *eptr = str + strlen( str ) ;
121 double f ;
122
123 --eptr ;
124
125 if( *eptr == 'e' )
126 return ( FofErbScale( atof( str ) ) * units( *--eptr ) ) ;
127
128 if( *eptr == 'z' )
129 --eptr ;
130
131 if( *eptr == 'H' || *eptr == 'h' )
132 return ( atof( str ) * units( *--eptr ) ) ;
133
134 return ( atof( str ) * units( *eptr ) ) ;
135 }
136
137 double Samples( str, samplerate )
138 char *str ;
139 double samplerate ;
140 {
141 char *eptr = str + strlen( str ) ;
142
143 switch( *--eptr ) {
144 case 's' : /* seconds */
145 return ( atof( str ) * samplerate * units( *--eptr ) ) ;
146 case 'p' : /* points */
147 return ( atof( str ) * units( *--eptr ) ) ;
148
149 default: /* default to milliseconds for specification of sample parameters */
150 return ( atof( str ) * samplerate * units( 'm' ) ) ;
151 }
152 }
153
154 double Scalar( str )
155 char *str ;
156 {
157 char *eptr = str + strlen( str ) ;
158
159 if( *--eptr == 'B' || *eptr == 'b' )
160 return ( pow( 10., atof( str ) * units( *--eptr ) / 2. ) ) ;
161 else
162 return ( atof( str ) * units( *eptr ) ) ;
163 }
164
165 double Cycles( str, cfreq, samplerate )
166 char *str ;
167 double cfreq, samplerate ; /* centre frequency of a particular channel */
168 {
169 char *eptr = str + strlen( str ) ;
170
171 /* Look at last char in the string */
172 switch( *--eptr ) {
173 case 's' : /* seconds */
174 return ( atof( str ) * samplerate * units( *--eptr ) ) ;
175 case 'p' : /* points */
176 return ( atof( str ) * units( *--eptr ) ) ;
177 case 'c' : /* cycles */
178 return ( atof( str ) * samplerate / cfreq ) ;
179
180 default: /* default to milliseconds, 'ms' */
181 return ( atof( str ) * samplerate * units( 'm' ) ) ;
182
183 }
184 }
185