tomwalters@0
|
1 /*
|
tomwalters@0
|
2 gauss.c generate a Gaussian window for a given variance over a given
|
tomwalters@0
|
3 ------- range of standard deviations.
|
tomwalters@0
|
4
|
tomwalters@0
|
5 Output to stdout in binary shorts or floats.
|
tomwalters@0
|
6
|
tomwalters@0
|
7 */
|
tomwalters@0
|
8
|
tomwalters@0
|
9 #include <stdio.h>
|
tomwalters@0
|
10 #include <math.h>
|
tomwalters@0
|
11 #include "options.h"
|
tomwalters@0
|
12 #include "units.h"
|
tomwalters@0
|
13 #include "strmatch.h"
|
tomwalters@0
|
14 #include "sigproc.h"
|
tomwalters@0
|
15
|
tomwalters@0
|
16 char applic[] = "generate a Gaussian window." ;
|
tomwalters@0
|
17
|
tomwalters@0
|
18 static char *helpstr, *debugstr, *sampstr, *varstr ;
|
tomwalters@0
|
19 static char *ranstr, *normstr, *scalestr, *typestr, *sizestr ;
|
tomwalters@0
|
20
|
tomwalters@0
|
21 static Options option[] = {
|
tomwalters@0
|
22 { "help" , "off" , &helpstr , "help" , DEBUG },
|
tomwalters@0
|
23 { "debug" , "off" , &debugstr , "debugging switch" , DEBUG },
|
tomwalters@0
|
24 { "samplerate", "20kHz" , &sampstr , "samplerate " , VAL },
|
tomwalters@0
|
25 { "variance" , "20" , &varstr , "Variance of Gaussian window" , VAL },
|
tomwalters@0
|
26 { "range" , "4" , &ranstr , "Range in standard deviations about mean" , VAL },
|
tomwalters@0
|
27 { "normalize" , "off" , &normstr , "Unit area under curve (unit max if off)" , SETFLAG },
|
tomwalters@0
|
28 { "scale" , "1000" , &scalestr , "Scale factor for output" , VAL },
|
tomwalters@0
|
29 { "type" , "short" , &typestr , "Output datatype (short/float)" , VAL },
|
tomwalters@0
|
30 { "size" , "off" , &sizestr , "Print size of window in samples" , VAL },
|
tomwalters@0
|
31 ( char * ) 0 } ;
|
tomwalters@0
|
32
|
tomwalters@0
|
33
|
tomwalters@0
|
34 int samplerate ;
|
tomwalters@0
|
35
|
tomwalters@0
|
36 float *window ;
|
tomwalters@0
|
37 int points ;
|
tomwalters@0
|
38
|
tomwalters@0
|
39 short *buf ;
|
tomwalters@0
|
40
|
tomwalters@0
|
41
|
tomwalters@0
|
42 main (argc, argv)
|
tomwalters@0
|
43 int argc;
|
tomwalters@0
|
44 char **argv;
|
tomwalters@0
|
45 {
|
tomwalters@0
|
46 float f ;
|
tomwalters@0
|
47
|
tomwalters@0
|
48 getopts( option,argc,argv ) ;
|
tomwalters@0
|
49 if ( !isoff( helpstr ) )
|
tomwalters@0
|
50 helpopts( helpstr, argv[0], applic, option ) ;
|
tomwalters@0
|
51
|
tomwalters@0
|
52 samplerate = to_Hz( sampstr ) ;
|
tomwalters@0
|
53
|
tomwalters@0
|
54 window = gauss_window( to_p( sqrt_units( varstr ), samplerate ), atof( ranstr ), &points ) ;
|
tomwalters@0
|
55
|
tomwalters@0
|
56 if ( ison( normstr ) ) normalize_area( window, points ) ;
|
tomwalters@0
|
57 else if ( !isoff( normstr ) ) {
|
tomwalters@0
|
58 fprintf(stderr,"gauss: unknown normalization [%s]\n", normstr) ;
|
tomwalters@0
|
59 exit( 1 ) ;
|
tomwalters@0
|
60 }
|
tomwalters@0
|
61
|
tomwalters@0
|
62 if ( ison( sizestr ) )
|
tomwalters@0
|
63 printf( "%d\n", points ) ;
|
tomwalters@0
|
64
|
tomwalters@0
|
65 else if ( iststr( typestr, "float" ) ) {
|
tomwalters@0
|
66 scalar( window, points, atof( scalestr ) ) ;
|
tomwalters@0
|
67 fwrite( window, sizeof(float), points, stdout ) ;
|
tomwalters@0
|
68 }
|
tomwalters@0
|
69
|
tomwalters@0
|
70 else if ( iststr( typestr, "short" ) ) {
|
tomwalters@0
|
71 buf = (short *)malloc( points * sizeof(short) ) ;
|
tomwalters@0
|
72 if ( ( f = ftos( window, buf, points, atof( scalestr ) ) ) < 1. )
|
tomwalters@0
|
73 fprintf( stderr,"Warning: 16-bit overflow. Try scale factor < %f\n", f ) ;
|
tomwalters@0
|
74 fwrite( buf, sizeof(short), points, stdout ) ;
|
tomwalters@0
|
75 }
|
tomwalters@0
|
76
|
tomwalters@0
|
77 else
|
tomwalters@0
|
78 fprintf(stderr,"gauss: unknown datatype [%s]\n", typestr) ;
|
tomwalters@0
|
79 }
|
tomwalters@0
|
80
|
tomwalters@0
|
81
|
tomwalters@0
|
82 scalar( y, n, scale )
|
tomwalters@0
|
83 float *y ;
|
tomwalters@0
|
84 int n ;
|
tomwalters@0
|
85 float scale ;
|
tomwalters@0
|
86 {
|
tomwalters@0
|
87 int i ;
|
tomwalters@0
|
88
|
tomwalters@0
|
89 if ( scale != 1. ) {
|
tomwalters@0
|
90 for ( i=0 ; i < n ; i++ )
|
tomwalters@0
|
91 y[i] *= scale ;
|
tomwalters@0
|
92 }
|
tomwalters@0
|
93 }
|
tomwalters@0
|
94
|
tomwalters@0
|
95
|