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