tomwalters@0: /* tomwalters@0: Copyright (c) Applied Psychology Unit, Medical Research Council. 1988, 1989 tomwalters@0: =========================================================================== tomwalters@0: tomwalters@0: Permission to use, copy, modify, and distribute this software without fee tomwalters@0: is hereby granted for research purposes, provided that this copyright tomwalters@0: notice appears in all copies and in all supporting documentation, and that tomwalters@0: the software is not redistributed for any fee (except for a nominal shipping tomwalters@0: charge). Anyone wanting to incorporate all or part of this software in a tomwalters@0: commercial product must obtain a license from the Medical Research Council. tomwalters@0: tomwalters@0: The MRC makes no representations about the suitability of this tomwalters@0: software for any purpose. It is provided "as is" without express or implied tomwalters@0: warranty. tomwalters@0: tomwalters@0: */ tomwalters@0: tomwalters@0: /* tomwalters@0: units.c tomwalters@0: ======= tomwalters@0: tomwalters@0: tomwalters@0: Unit conversion routines for APU model. tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: Copyright (c), 1989 The Medical Research Council, Applied Psychology Unit. tomwalters@0: tomwalters@0: tomwalters@0: Author : John Holdsworth tomwalters@0: Written : 22th March, 1989. tomwalters@0: tomwalters@0: Edited : Mike Allerhand, 1990. tomwalters@0: tomwalters@0: */ tomwalters@0: tomwalters@0: #ifndef lint tomwalters@0: static char *sccs_id = "@(#)units.c 1.3 John Holdsworth, Mike Allerhand (MRC-APU) 6/6/91" ; tomwalters@0: #endif tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: /*********************** Unit conversion routines *************************** tomwalters@0: * Values are stored as strings. tomwalters@0: * Units are specified by the final char: tomwalters@0: * p sample points. tomwalters@0: * s seconds. tomwalters@0: * b or B decibels. tomwalters@0: * Hz Hertz. tomwalters@0: * Unit multipliers are specified by the optional penultimate char: tomwalters@0: * k kilo (scale by one thousand). tomwalters@0: * d deci (scale by one tenth). tomwalters@0: * c centi (scale by one hundreth). tomwalters@0: * m milli (scale by one thousandth). tomwalters@0: * u (scale by one millionth). tomwalters@0: * tomwalters@0: * Examples: "10p" time period of 10 sample points tomwalters@0: * "10ms" time period of 10 milliseconds tomwalters@0: * "10kHz" frequency of 10 kiloHertz tomwalters@0: * tomwalters@0: * Conversion routines: tomwalters@0: * units( ch ) Multiplier for unit, (k,d,c,m,u). tomwalters@0: * Freq( str ) String to value (eg: 10, 10Hz, 10kHz). tomwalters@0: * Samples( str ) String to value (num samples) (eg: 10p, 10s, 10ms). tomwalters@0: * Scalar( str ) String to value (scalar number), also converts dB's. tomwalters@0: * tomwalters@0: * Examples: Freq("10000") returns 10000 tomwalters@0: * Freq("10kHz") returns 10000 tomwalters@0: * Samples("3p") returns 3 tomwalters@0: * Samples("3ms") return 30 (when the sample rate is 10kHz). tomwalters@0: * Samples("3") like "3ms" (milliseconds is default unit). tomwalters@0: * Scalar("10.") returns 10 tomwalters@0: * Scalar( "20dB" ) returns 10 tomwalters@0: * tomwalters@0: * See also stitch.h for conversion routines: tomwalters@0: * ToPoints(type,bytes) Count in bytes to number of points of given type. tomwalters@0: * ToBytes(type,points ) Number of points of given type to count in bytes. tomwalters@0: * tomwalters@0: * Cycles( str, cfreq ) This is like Samples( str ), except that it also tomwalters@0: * allows `c' (cycles) units. A given number of cycles tomwalters@0: * of a particular channel are converted to samples. tomwalters@0: * This requires the centre frequency of the channel, tomwalters@0: * cfreq = frequencies[chan]. tomwalters@0: ****************************************************************************/ tomwalters@0: tomwalters@0: #include tomwalters@0: tomwalters@0: #if defined(THINK_C) || defined(NeXT) tomwalters@0: #include tomwalters@0: #endif tomwalters@0: tomwalters@0: #include "units.h" tomwalters@0: #include "formulae.h" tomwalters@0: tomwalters@0: extern char *samplestr ; tomwalters@0: tomwalters@0: static double units( ch ) tomwalters@0: char ch ; tomwalters@0: { tomwalters@0: switch( ch ) { tomwalters@0: case 'k' : tomwalters@0: return ( 1000. ) ; tomwalters@0: case 'd' : tomwalters@0: return ( 0.1 ) ; tomwalters@0: case 'c' : tomwalters@0: return ( 0.01 ) ; tomwalters@0: case 'm' : tomwalters@0: return ( 0.001 ) ; tomwalters@0: case 'u' : tomwalters@0: return ( 0.000001 ) ; tomwalters@0: tomwalters@0: default : tomwalters@0: return ( 1. ) ; tomwalters@0: } tomwalters@0: } tomwalters@0: tomwalters@0: double Freq( str ) tomwalters@0: char *str ; tomwalters@0: { tomwalters@0: char *eptr = str + strlen( str ) ; tomwalters@0: double f ; tomwalters@0: tomwalters@0: --eptr ; tomwalters@0: tomwalters@0: if( *eptr == 'e' ) tomwalters@0: return ( FofErbScale( atof( str ) ) * units( *--eptr ) ) ; tomwalters@0: tomwalters@0: if( *eptr == 'z' ) tomwalters@0: --eptr ; tomwalters@0: tomwalters@0: if( *eptr == 'H' || *eptr == 'h' ) tomwalters@0: return ( atof( str ) * units( *--eptr ) ) ; tomwalters@0: tomwalters@0: return ( atof( str ) * units( *eptr ) ) ; tomwalters@0: } tomwalters@0: tomwalters@0: double Samples( str, samplerate ) tomwalters@0: char *str ; tomwalters@0: double samplerate ; tomwalters@0: { tomwalters@0: char *eptr = str + strlen( str ) ; tomwalters@0: tomwalters@0: switch( *--eptr ) { tomwalters@0: case 's' : /* seconds */ tomwalters@0: return ( atof( str ) * samplerate * units( *--eptr ) ) ; tomwalters@0: case 'p' : /* points */ tomwalters@0: return ( atof( str ) * units( *--eptr ) ) ; tomwalters@0: tomwalters@0: default: /* default to milliseconds for specification of sample parameters */ tomwalters@0: return ( atof( str ) * samplerate * units( 'm' ) ) ; tomwalters@0: } tomwalters@0: } tomwalters@0: tomwalters@0: double Scalar( str ) tomwalters@0: char *str ; tomwalters@0: { tomwalters@0: char *eptr = str + strlen( str ) ; tomwalters@0: tomwalters@0: if( *--eptr == 'B' || *eptr == 'b' ) tomwalters@0: return ( pow( 10., atof( str ) * units( *--eptr ) / 2. ) ) ; tomwalters@0: else tomwalters@0: return ( atof( str ) * units( *eptr ) ) ; tomwalters@0: } tomwalters@0: tomwalters@0: double Cycles( str, cfreq, samplerate ) tomwalters@0: char *str ; tomwalters@0: double cfreq, samplerate ; /* centre frequency of a particular channel */ tomwalters@0: { tomwalters@0: char *eptr = str + strlen( str ) ; tomwalters@0: tomwalters@0: /* Look at last char in the string */ tomwalters@0: switch( *--eptr ) { tomwalters@0: case 's' : /* seconds */ tomwalters@0: return ( atof( str ) * samplerate * units( *--eptr ) ) ; tomwalters@0: case 'p' : /* points */ tomwalters@0: return ( atof( str ) * units( *--eptr ) ) ; tomwalters@0: case 'c' : /* cycles */ tomwalters@0: return ( atof( str ) * samplerate / cfreq ) ; tomwalters@0: tomwalters@0: default: /* default to milliseconds, 'ms' */ tomwalters@0: return ( atof( str ) * samplerate * units( 'm' ) ) ; tomwalters@0: tomwalters@0: } tomwalters@0: } tomwalters@0: