diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/units.c	Fri May 20 15:19:45 2011 +0100
@@ -0,0 +1,230 @@
+/***************************************************************************
+  units.c
+ ---------
+  Return binary doubles for given string values, according to units.
+  Units are specified by the final char:
+    p   sample points.
+    s   seconds.
+    ms  milliseconds
+    Hz  Hertz.
+    kHz KiloHertz
+    erb equivalent rectangular bandwidth [Hz]
+
+***************************************************************************/
+
+#include <stdio.h>
+#include <math.h>
+#include <ctype.h>
+#include "units.h"
+#include "strmatch.h"
+
+
+/*
+Return the units index number of the best matching units at the tail of
+string `s'. Return -1 if no units are found.
+*/
+
+int unitindex( s )
+char *s ;
+{
+    int   i  ;
+
+    if ( ( i = strnumspn( s ) ) == 0 ) {        /* case of no digits found */
+	fprintf( stderr, "bad value [%s]\n", s ) ;
+	exit( 1 ) ;
+    }
+    if ( i == strlen( s ) )
+	return (-1) ;                           /* case of no units found  */
+
+    switch ( i = listindex( units, s+i ) ) {
+	case (-1) : fprintf(stderr,"unknown units [%s]\n", s ) ;
+		    exit( 1 ) ;
+	case (-2) : fprintf(stderr,"ambiguous units [%s]\n", s ) ;
+		    exit( 1 ) ;
+    }
+    return i ;                  /* index of units */
+}
+
+
+
+/*
+Return a ptr to the units string at the tail of string `s'.
+Otherwise return a ptr to the null terminator of `s' if no units are found.
+*/
+
+char  *unitstr( s )
+char *s ;
+{
+    unitindex( s ) ;    /* check for valid (or no) units */
+    return ( strnumptr( s ) ) ;
+}
+
+
+/*
+Search the end of the given string for units (such as ms, kHz, etc).
+Delete any units found by overwriting the start of the units string with '\0'.
+*/
+
+stripunits( s )
+char  *s ;
+{
+    *( unitstr( s ) ) = '\0' ;
+}
+
+
+/*
+Return an allocated string to the square root of the given value with original
+units appended.
+*/
+
+char *sqrt_units( s )
+char *s ;
+{
+    char s1[64] ;
+
+    sprintf( s1, "%.2f%s", sqrt( (double)atof( s ) ), unitstr( s ) ) ;
+    return ( strcpy( (char *)malloc( strlen( s1 ) + 1 ), s1 ) ) ;
+}
+
+
+/*
+Return the type of a <number>: INT, FLOAT, or 0 (if not a number).
+   <number> = [-]<digits>[.]<digits>
+where either, but not both, of the digit strings may be empty.
+*/
+
+int numbertype( str )
+char *str ;
+{
+    if ( strnumspn( str ) == 0 )           return 0     ;
+    if ( strchr( str, '.' ) == (char *)0 ) return INT   ;
+    else                                   return FLOAT ;
+}
+
+
+/*
+Return 1 if str is an int, otherwise return 0.
+*/
+
+int isint( str )
+char *str ;
+{
+    if ( numbertype( str ) == INT ) return 1 ;
+    return 0 ;
+}
+
+/*
+Return 1 if str is a float, otherwise return 0.
+*/
+
+int isfloat( str )
+char *str ;
+{
+    if ( numbertype( str ) == FLOAT ) return 1 ;
+    return 0 ;
+}
+
+
+/*
+  Units converters.
+  Generally called: to_<unit> where <unit> is the output units.
+  Input strings without units have `default' units.
+*/
+
+double to_Hz( str, samplerate ) /* default units Hz */
+char *str ;
+int   samplerate ;
+{
+    switch ( unitindex( str ) ) {
+	case (-1) : return   atof( str ) ;
+	case   0  : return   ( (double)samplerate / atof( str ) ) ;
+	case   1  : return   ( 1. / atof( str ) ) ;
+	case   2  : return   ( 1000. / atof( str ) ) ;
+	case   3  : return   atof( str ) ;
+	case   4  : return ( atof( str ) * 1000 ) ;
+	case   5  : return ( ( exp( (double)(atof( str ) / 9.265 )) - 1 ) * 9.265 * 24.7 ) ;
+	default   : fprintf(stderr, "can't convert [%s] to Hz\n", str ) ;
+		    exit( 1 ) ;
+    }
+}
+
+double to_kHz( str, samplerate )    /* default units Hz */
+char *str ;
+int   samplerate ;
+{
+    switch ( unitindex( str ) ) {
+	case (-1) : return ( atof( str ) * .001 ) ;
+	case   0  : return   ( ( (double)samplerate / atof( str ) ) * 0.001 ) ;
+	case   1  : return   ( ( 1. / atof( str ) ) * 0.001 ) ;
+	case   2  : return   ( 1. / atof( str ) ) ;
+	case   3  : return ( atof( str ) * .001 ) ;
+	case   4  : return   atof( str ) ;
+	case   5  : return ( ( exp( (double)(atof( str ) / 9.265 )) - 1 ) * 9.265 * 24.7 * .001 ) ;
+	default   : fprintf(stderr, "can't convert [%s] to kHz\n", str ) ;
+		    exit( 1 ) ;
+    }
+}
+
+double to_erb( str )    /* default units erb */
+char *str ;
+{
+    switch ( unitindex( str ) ) {
+	case (-1) : return   atof( str ) ;
+	case   3  : return ( 9.265 * log( (double)(1 + atof( str )/(9.265*24.7)) ) ) ;
+	case   4  : return ( 9.265 * log( (double)(1 + (1000*atof( str ))/(9.265*24.7)) ) ) ;
+	case   5  : return   atof( str ) ;
+	default   : fprintf(stderr, "can't convert [%s] to erb\n", str ) ;
+		    exit( 1 ) ;
+    }
+}
+
+double to_p( str, samplerate )  /* default units p */
+char *str ;
+int   samplerate ;
+{
+    switch ( unitindex( str ) ) {
+	case (-1) : return   atof( str ) ;
+	case   0  : return   atof( str ) ;
+	case   1  : return ( atof( str ) * samplerate ) ;
+	case   2  : return ( atof( str ) * samplerate * .001 ) ;
+	case   3  : return ( samplerate / atof( str ) ) ;
+	case   4  : return ( samplerate / ( 1000 * atof( str ) ) ) ;
+	default   : fprintf(stderr, "can't convert [%s] to samples\n", str ) ;
+		    exit( 1 ) ;
+    }
+}
+
+
+
+double to_s( str, samplerate )  /* default units p (sample points) */
+char *str ;
+int   samplerate ;
+{
+    switch ( unitindex( str ) ) {
+	case (-1) : return ( atof( str ) / samplerate ) ;
+	case   0  : return ( atof( str ) / samplerate ) ;
+	case   1  : return   atof( str ) ;
+	case   2  : return ( atof( str ) * .001 ) ;
+	case   3  : return ( 1. / atof( str ) ) ;
+	case   4  : return ( 1. / ( 1000 * atof( str ) ) ) ;
+	default   : fprintf(stderr, "can't convert [%s] to secs\n", str ) ;
+		    exit( 1 ) ;
+    }
+}
+
+
+double to_ms( str, samplerate )  /* default units p */
+char *str ;
+int   samplerate ;
+{
+    switch ( unitindex( str ) ) {
+	case (-1) : return ( atof( str ) * 1000 / samplerate ) ;
+	case   0  : return ( atof( str ) * 1000 / samplerate ) ;
+	case   1  : return ( atof( str ) * 1000 ) ;
+	case   2  : return   atof( str ) ;
+	case   3  : return ( 1000. / atof( str ) ) ;
+	case   4  : return ( 1. / atof( str ) ) ;
+	default   : fprintf(stderr, "can't convert [%s] to ms\n", str ) ;
+		    exit( 1 ) ;
+    }
+}