comparison tools/ftos.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
comparison
equal deleted inserted replaced
-1:000000000000 0:5242703e91d3
1 /*
2 ftos.c binary float-to-short conversion.
3 --------
4
5 */
6
7 #include <stdio.h>
8 #include <math.h>
9 #include "options.h"
10
11 char applic[] = "float to short data-type conversion." ;
12
13 static char *helpstr, *errstr ;
14
15 static Options option[] = {
16 { "help" , "off" , &helpstr , "help" , DEBUG },
17 { "error" , "off" , &errstr , "print overflow and quantization errors" , VAL },
18 ( char * ) 0 } ;
19
20
21 #define MAX 32767.
22 #define MIN ( -32767. )
23
24 main(argc, argv)
25 int argc ;
26 char *argv[] ;
27 {
28 FILE *fp ;
29 float X ;
30 short Y ;
31 int i ;
32 float min = MAX, fmin ;
33 float max = MIN, fmax ;
34 float quant, maxquant, sumquant=0 ;
35
36 fp = openopts( option,argc,argv ) ;
37 if ( !isoff( helpstr ) )
38 helpopts( helpstr, argv[0], applic, option ) ;
39
40 if ( isoff( errstr ) ) {
41
42 while ( fread( &X, sizeof(float), 1, fp ) ) {
43 Y = (short)X ;
44 fwrite( &Y, sizeof(short), 1, stdout);
45 }
46 }
47
48 else {
49
50 for ( i = 0 ; fread( &X, sizeof(float), 1, fp ) ; i++ ) {
51 if ( X > max ) max = X ;
52 if ( X < min ) min = X ;
53 quant = ( X - (int)X ) / (int)( 1 + X ) ;
54 sumquant += quant ;
55 if ( quant > maxquant ) maxquant = quant ;
56 Y = (short)X ;
57 fwrite( &Y, sizeof(short), 1, stdout);
58 }
59
60 if ( max > MAX || min < MIN ) {
61
62 if ( max > MAX ) fmax = MAX / max ;
63 if ( min < MIN ) fmin = MIN / min ;
64
65 if ( fmax < fmin ) fprintf( stderr, "16-bit overflow (scale down by a factor < %f\n", fmax ) ;
66 else fprintf( stderr, "16-bit underflow (scale down by a factor < %f\n", fmin ) ;
67 }
68 fprintf( stderr, "max quantization error = %f\n", maxquant ) ;
69 fprintf( stderr, "mean quantization error = %f\n", sumquant/i ) ;
70 }
71
72 fclose(fp);
73 exit(0);
74 }
75
76