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