measure.c

来自「Reference Implementation of G.711 standa」· C语言 代码 · 共 549 行 · 第 1/2 页

C
549
字号
/*==============================================================================  MEASURE.C  ~~~~~~~~~  Program to calculate the max, min, and average values for a file, as   well as 16-bit CRCs (CCITT, XMODEM, ARC), if requested.  Usage:  ~~~~~~  $ measure file|- [other files ...]  where:  file          is the name of the file to measured; - is stdin                process all the files specified          Options:  ~~~~~~~~  -h|-?         show program usage  -blk size     change the number of samples per block [default 256]  -skip n       number of blocks to skip  -crc          also calculate crc for files  -hex          carry-out hex dump  Compile:  ~~~~~~~~  SunC: cc -o measure measure.c -lm # or #        acc -o measure measure.c -lm  GNUC: gcc -o measure measure.c -lm  VMS:  cc measure.c        link measure   History:  ~~~~~~~~  10.Mar.94   1.0   1st release by simao@cpqd.ansp.br  21.Aug.95   1.1   Fixed bug with zero-length files <simao@ctd.comsat.com>.==============================================================================*//* OS definition */#if defined(__MSDOS__) && !defined(MSDOS) /* def. for autom. compil.,TurboC */# define MSDOS	  /* this is already defined for Microsoft C 5.1 */#endif/* includes in general */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>#include "ugstdemo.h"/* Generic defines */#define YES 1#define NO  0/* Defines for CRC routine */#define W       16		/* CRC width */#define WTYPE	unsigned short	/* Basic data type */#define B	8		/* the number of bits per char *//* Local function prototypes */WTYPE updcrc    ARGS((WTYPE icrc, unsigned char *icp, int icnt, WTYPE *crctab,                 char swapped));/* Macros related to crc calculations */#define get_ccitt_crc(crc,buf,n)  updcrc(crc, buf, n, crctab_ccitt, 0)#define get_arc_crc(crc,buf,n)    updcrc(crc, buf, n, crctab_arc, 1)#define get_xmodem_crc(crc,buf,n) updcrc(crc, buf, n, crctab_xmodem, 1)/* Global variables related to crc calculations */WTYPE           crc_a = 0, crc_c = 0, crc_x = 0;int             init_crc_a = 0L, init_crc_c = -1L, init_crc_x = 0L;/*  * -------------------------------------------------------------------------- * ... Statistics routines ...  *     Simao 07.Mar.94 * -------------------------------------------------------------------------- */double get_sum(samples, count)short *samples;long count;{  double sum = 0;    while(count>0)    sum += samples[--count];  return(sum);}double get_square(samples, count)short *samples;long count;{  double sum = 0, tmp;    while(count>0)  {    tmp = samples[--count];    sum += tmp * tmp;  }  return(sum);}/*   --------------------------------------------------------------------------  Find maximum in a short array; return as a double; uses parameter   'max' as a default, or as the so-far found, maximum value.  --------------------------------------------------------------------------*/double get_max(max, samples, count)double max;short *samples;long count;{  double tmp;     while(count>0)  {    count--;    tmp = samples[count];    max = tmp>max? tmp : max;  }  return max;}/*   --------------------------------------------------------------------------  Find minimum in a short array; return as a double; uses parameter   'min' as a default, or as the so-far found, minimum value.  --------------------------------------------------------------------------*/double get_min(min, samples, count)double min;short *samples;long count;{  double tmp;     while(count>0)  {    count--;    tmp = samples[count];    min = tmp<min? tmp : min;  }  return min;}double get_var(rms_x, avg_x, count)double rms_x, avg_x;long count;{ return ((rms_x - avg_x * avg_x / (double)count) / (double)count);}/* ....................... end of get_...() ...........................*//*  ---------------------------------------------------------------------------  WTYPE updcrc (WTYPE icrc, unsigned char *icp, int icnt, WTYPE *crctab,   ~~~~~~~~~~~~  char swapped);    Description:  ~~~~~~~~~~~~  Calculate, intelligently, the CRC-16 of a dataset incrementally given a  buffer full at a time. The parameters on which the function relyes are:  Parameter             CCITT   XMODEM  ARC  ---------             ------  ------  ----  the polynomial P	0x1021	0x1021	A001  initial CRC value:	-1	0	0  bit-order swap        No (0)  Yes (1) Yes (1)  bits in CRC:          16	16	16  Usage:  ~~~~~~  	newcrc = updcrc( oldcrc, bufadr, buflen, crctab, swapped)  		   WTYPE oldcrc;                   int buflen;  		   char *bufadr;                   WTYPE *crctab;                   char swapped;  Notes:   Regards the data stream as an integer whose MSB is the MSB of the first   byte recieved.  This number is 'divided' (using xor instead of subtraction)   by the crc-polynomial P.   XMODEM does things a little differently, essentially treating the LSB of   the first data byte as the MSB of the integer.  Original Author:  ~~~~~~~~~~~~~~~~  Mark G. Mendel, 7/86  UUCP: ihnp4!umn-cs!hyper!mark, GEnie: mgm  [ Original code available in comp.sources.unix volume 6 (1989) ]  History:  ~~~~~~~~  xx.Jul.86 v1.0  Release by the author [posted to c.s.misc in 1989]  01.Sep.93 v1.0a Adapted by Simao <simao@cpqd.ansp.br> to be used in the 		  x{en,de}code.c.  ---------------------------------------------------------------------------*//* Table for CCITT CRCs */static WTYPE    crctab_ccitt[1 << B] = {  0x0, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,  0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,  0x1231, 0x210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,  0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,  0x2462, 0x3443, 0x420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,  0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,  0x3653, 0x2672, 0x1611, 0x630, 0x76d7, 0x66f6, 0x5695, 0x46b4,  0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,  0x48c4, 0x58e5, 0x6886, 0x78a7, 0x840, 0x1861, 0x2802, 0x3823,  0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,  0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0xa50, 0x3a33, 0x2a12,  0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,  0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0xc60, 0x1c41,  0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,  0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0xe70,  0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,  0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,  0x1080, 0xa1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,  0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,  0x2b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,  0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,  0x34e2, 0x24c3, 0x14a0, 0x481, 0x7466, 0x6447, 0x5424, 0x4405,  0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,  0x26d3, 0x36f2, 0x691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,  0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,  0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x8e1, 0x3882, 0x28a3,  0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,  0x4a75, 0x5a54, 0x6a37, 0x7a16, 0xaf1, 0x1ad0, 0x2ab3, 0x3a92,  0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,  0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0xcc1,  0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,  0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0xed1, 0x1ef0,};/* Table for xmodem CRCs */static WTYPE    crctab_xmodem[1 << B] = {  0x0, 0x17ce, 0xfdf, 0x1811, 0x1fbe, 0x870, 0x1061, 0x7af,  0x1f3f, 0x8f1, 0x10e0, 0x72e, 0x81, 0x174f, 0xf5e, 0x1890,  0x1e3d, 0x9f3, 0x11e2, 0x62c, 0x183, 0x164d, 0xe5c, 0x1992,  0x102, 0x16cc, 0xedd, 0x1913, 0x1ebc, 0x972, 0x1163, 0x6ad,  0x1c39, 0xbf7, 0x13e6, 0x428, 0x387, 0x1449, 0xc58, 0x1b96,  0x306, 0x14c8, 0xcd9, 0x1b17, 0x1cb8, 0xb76, 0x1367, 0x4a9,  0x204, 0x15ca, 0xddb, 0x1a15, 0x1dba, 0xa74, 0x1265, 0x5ab,  0x1d3b, 0xaf5, 0x12e4, 0x52a, 0x285, 0x154b, 0xd5a, 0x1a94,  0x1831, 0xfff, 0x17ee, 0x20, 0x78f, 0x1041, 0x850, 0x1f9e,  0x70e, 0x10c0, 0x8d1, 0x1f1f, 0x18b0, 0xf7e, 0x176f, 0xa1,  0x60c, 0x11c2, 0x9d3, 0x1e1d, 0x19b2, 0xe7c, 0x166d, 0x1a3,  0x1933, 0xefd, 0x16ec, 0x122, 0x68d, 0x1143, 0x952, 0x1e9c,  0x408, 0x13c6, 0xbd7, 0x1c19, 0x1bb6, 0xc78, 0x1469, 0x3a7,  0x1b37, 0xcf9, 0x14e8, 0x326, 0x489, 0x1347, 0xb56, 0x1c98,  0x1a35, 0xdfb, 0x15ea, 0x224, 0x58b, 0x1245, 0xa54, 0x1d9a,  0x50a, 0x12c4, 0xad5, 0x1d1b, 0x1ab4, 0xd7a, 0x156b, 0x2a5,  0x1021, 0x7ef, 0x1ffe, 0x830, 0xf9f, 0x1851, 0x40, 0x178e,  0xf1e, 0x18d0, 0xc1, 0x170f, 0x10a0, 0x76e, 0x1f7f, 0x8b1,  0xe1c, 0x19d2, 0x1c3, 0x160d, 0x11a2, 0x66c, 0x1e7d, 0x9b3,  0x1123, 0x6ed, 0x1efc, 0x932, 0xe9d, 0x1953, 0x142, 0x168c,  0xc18, 0x1bd6, 0x3c7, 0x1409, 0x13a6, 0x468, 0x1c79, 0xbb7,  0x1327, 0x4e9, 0x1cf8, 0xb36, 0xc99, 0x1b57, 0x346, 0x1488,  0x1225, 0x5eb, 0x1dfa, 0xa34, 0xd9b, 0x1a55, 0x244, 0x158a,  0xd1a, 0x1ad4, 0x2c5, 0x150b, 0x12a4, 0x56a, 0x1d7b, 0xab5,  0x810, 0x1fde, 0x7cf, 0x1001, 0x17ae, 0x60, 0x1871, 0xfbf,  0x172f, 0xe1, 0x18f0, 0xf3e, 0x891, 0x1f5f, 0x74e, 0x1080,  0x162d, 0x1e3, 0x19f2, 0xe3c, 0x993, 0x1e5d, 0x64c, 0x1182,  0x912, 0x1edc, 0x6cd, 0x1103, 0x16ac, 0x162, 0x1973, 0xebd,

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?