diff tools/gauss.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/gauss.c	Fri May 20 15:19:45 2011 +0100
@@ -0,0 +1,95 @@
+/*
+  gauss.c   generate a Gaussian window for a given variance over a given
+  -------   range of standard deviations.
+
+  Output to stdout in binary shorts or floats.
+
+*/
+
+#include <stdio.h>
+#include <math.h>
+#include "options.h"
+#include "units.h"
+#include "strmatch.h"
+#include "sigproc.h"
+
+char applic[]     = "generate a Gaussian window." ;
+
+static char *helpstr, *debugstr, *sampstr,  *varstr ;
+static char *ranstr,  *normstr,  *scalestr, *typestr,  *sizestr ;
+
+static Options option[] = {
+    {   "help"      ,   "off"       ,  &helpstr     ,   "help"                                      , DEBUG   },
+    {   "debug"     ,   "off"       ,  &debugstr    ,   "debugging switch"                          , DEBUG   },
+    {   "samplerate",   "20kHz"     ,  &sampstr     ,   "samplerate "                               , VAL     },
+    {   "variance"  ,   "20"        ,  &varstr      ,   "Variance of Gaussian window"               , VAL     },
+    {   "range"     ,   "4"         ,  &ranstr      ,   "Range in standard deviations about mean"   , VAL     },
+    {   "normalize" ,   "off"       ,  &normstr     ,   "Unit area under curve (unit max if off)"   , SETFLAG },
+    {   "scale"     ,   "1000"      ,  &scalestr    ,   "Scale factor for output"                   , VAL     },
+    {   "type"      ,   "short"     ,  &typestr     ,   "Output datatype (short/float)"             , VAL     },
+    {   "size"      ,   "off"       ,  &sizestr     ,   "Print size of window in samples"           , VAL     },
+   ( char * ) 0 } ;
+
+
+int     samplerate  ;
+
+float  *window ;
+int     points ;
+
+short  *buf    ;
+
+
+main (argc, argv)
+int     argc;
+char  **argv;
+{
+    float f ;
+
+    getopts( option,argc,argv ) ;
+    if ( !isoff( helpstr ) )
+	helpopts( helpstr, argv[0], applic, option ) ;
+
+    samplerate = to_Hz( sampstr ) ;
+
+    window = gauss_window( to_p( sqrt_units( varstr ), samplerate ), atof( ranstr ), &points ) ;
+
+    if ( ison( normstr ) ) normalize_area( window, points ) ;
+    else if ( !isoff( normstr ) ) {
+	fprintf(stderr,"gauss: unknown normalization [%s]\n", normstr) ;
+	exit( 1 ) ;
+    }
+
+    if ( ison( sizestr ) )
+	printf( "%d\n", points ) ;
+
+    else if ( iststr( typestr, "float" ) ) {
+	scalar( window, points, atof( scalestr ) ) ;
+	fwrite( window, sizeof(float), points, stdout ) ;
+    }
+
+    else if ( iststr( typestr, "short" ) ) {
+	buf = (short *)malloc( points * sizeof(short) ) ;
+	if ( ( f = ftos( window, buf, points, atof( scalestr ) ) ) < 1. )
+	    fprintf( stderr,"Warning: 16-bit overflow. Try scale factor < %f\n", f ) ;
+	fwrite( buf, sizeof(short), points, stdout ) ;
+    }
+
+    else
+	fprintf(stderr,"gauss: unknown datatype [%s]\n", typestr) ;
+}
+
+
+scalar( y, n, scale )
+float  *y ;
+int     n ;
+float   scale ;
+{
+    int  i ;
+
+    if ( scale != 1. ) {
+	for ( i=0 ; i < n ; i++ )
+	    y[i] *= scale ;
+    }
+}
+
+