classify.c

来自「speech signal process tools」· C语言 代码 · 共 593 行 · 第 1/2 页

C
593
字号
/*----------------------------------------------------------------------+|									||   This material contains proprietary software of Entropic Speech,	||   Inc.  Any reproduction, distribution, or publication without the	||   prior written permission of Entropic Speech, Inc. is strictly	||   prohibited.  Any public distribution of copies of this work		||   authorized in writing by Entropic Speech, Inc. must bear the	||   notice								||									||    "Copyright (c) 1987 Entropic Speech, Inc. All rights reserved."	||									|+-----------------------------------------------------------------------+| 									||  Program:	main.c							|| 									||  Written by:  Rodney Johnson.						||  Checked by:								|| 									||  Main module of classify.						||									||  Classifies records in an ESPS feature file by the maximum-likeli-	||  hood method, according to information in a statistics file.		||									|+----------------------------------------------------------------------*/#ifndef lint    static char *sccs_id = "@(#)classify.c	3.10	6/9/93	ESI";#endif#define VERSION "3.10"#define DATE "6/9/93"/* * system include files */#include <stdio.h>#include <sys/types.h>/* * ESPS include files */#include <esps/esps.h>#include <esps/unix.h>#include <esps/fea.h>#include <esps/spsassert.h>#include <esps/feastat.h>/* * defines */#define DEBUG(n) if (debug_level >= n) Fprintf#define Fprintf (void)fprintf#define PROG Fprintf(stderr, "%s: ", ProgName)#define EXIT Fprintf(stderr, "\n"); exit(1);#define ERROR_EXIT(text) {PROG; Fprintf(stderr, (text)); EXIT}#define ERROR_EXIT1(fmt,a) {PROG; Fprintf(stderr, (fmt), (a)); EXIT}#define ERROR_EXIT2(fmt,a,b) {PROG; Fprintf(stderr, (fmt), (a), (b)); EXIT}#define TRYALLOC(type,num,var,msg) { \    if (((var) = (type *) calloc((unsigned)(num),sizeof(type))) == NULL) \    ERROR_EXIT1("Can't allocate memory--%s", (msg))}#define SYNTAX \USAGE("classify [-P param_file][-d][-e elements][-f field][-h file.his][-x debug_level]\n [-C field][-L field] input.stat input.fea output.fea") \;/* * system functions and variables */extern int  optind;extern char *optarg;time_t	time();char	*ctime();/* * external ESPS functions */char	*get_cmd_line();void	write_header();struct header	*read_header();int	lin_search();void	add_comment();char	*fea_decode();long	*grange_switch();char	**get_fea_deriv();long	get_fea_siz();short	get_fea_type();char	*get_deriv_vec();void	copy_fea_rec();/* * global function declarations */void	pr_farray();int	classify();int	num_enums();/* * global variable declarations */int     debug_level = 0;       char    *ProgName = "classify";char    *Version = VERSION;char	*Date = DATE;char	*cmd_line;/* * main program */main(argc, argv)    int	    argc;    char    **argv;{    char    *statfield = NULL;	    /* Field name.  Get features from this */				    /* field or its source fields. */    long    feasize;		    /* Size of named field in input. */    long    statsize;		    /* Field size from statistics file. */    long    *elements;		    /* Indices specified with -e option. */    long    num_elems;		    /* Number of indices specified. */    char    *e_arg = NULL;	    /* Argument of -e option. */    short   d_flag = NO,	    /* -d option specified? */	    e_flag = NO,	    /* -e option specified? */	    f_flag = NO,	    /* -f option specified? */            C_flag = NO,            L_flag = NO;    char    *param_file = NULL;    char    *hist_name = NULL;	    /* History-file name. */    FILE    *hist_strm;		    /* History file. */    time_t  tloc;		    /* Current time. */    char    *stat_name;		    /* Statistics-file name. */    FILE    *stat_strm;		    /* Statistics file. */    struct header   *stat_ih;	    /* Statistics-file header. */    struct feastat  *stat_rec,	    /* Statistics-file record. */	    **stat_recs;	    /* All statistics-file records. */    float   **means;		    /* Mean vectors from statistics file. */    float   ***invcovars;	    /* Inverse covariance matrices from */				    /* statistics file. */    int	    max_clas,		    /* Upper bound on number of classes. */	    num_clas;		    /* Actual number of classes. */    char    **fields;		    /* Source-field definitions. */    char    *infea_name;	    /* Name of input feature file. */    FILE    *infea_strm;	    /* Input feature file. */    struct header   *infea_ih;	    /* Input feature-file header. */    struct fea_data *infea_rec;	    /* Input feature-file record. */    char    *fea_ptr;		    /* Pointer to data in input record. */    float   *feavec = NULL;	    /* Features from input record. */    long    length;		    /* Length of feavec. */    char    *outfea_name;	    /* Output feature-file name. */    FILE    *outfea_strm;	    /* Output feature-file.  */    struct header   *outfea_oh;	    /* Output feature-file header. */    struct fea_data *outfea_rec;    /* Output feature-file record. */    char    **enums;		    /* Class names for output header. */    char    *clasfield = "class";   /* Output field name for class. */    short   *class;		    /* Pointer to output field for class. */    char    *postfield = "posteriors";				    /* Output field name for likelihoods. */    float   *posteriors;	    /* Output vector for likelihoods. */    long    *count;		    /* Count records in each class. */    long    nrec;		    /* Number of records. */    int	    c;			    /* Input option letter. */    int	    i, j;		    /* Loop indices. */    cmd_line = get_cmd_line(argc, argv);/* * process command line options */    while ((c = getopt(argc, argv, "de:f:h:x:C:L:P:")) != EOF)    {	switch (c)	{	case 'P':	  param_file = optarg;	  break;	case 'd':	    d_flag = YES;	    break;	case 'e':	    e_flag = YES;	    elements = grange_switch(e_arg = optarg, &num_elems);	    break;	case 'f':	    f_flag = YES;	    statfield = optarg;	    break;	case 'h':	    hist_name = optarg;	    break;	case 'x': 	    debug_level = atoi(optarg);	    break;	case 'C':	    clasfield = optarg;	    C_flag = YES;	    break;	case 'L':	    postfield = optarg;	    L_flag = YES;	    break;	default:	    SYNTAX	    break;	}    }    if (d_flag && (e_flag || f_flag))	ERROR_EXIT("The -d option is incompatible with -e and -f.")    (void) read_params(param_file, SC_NOCOMMON, (char *)NULL);    if(!d_flag && !e_flag && symtype("elements") != ST_UNDEF){      e_arg = getsym_s("elements");      elements = grange_switch(e_arg, &num_elems);      e_flag = YES;    }    if(!d_flag && !f_flag && symtype("in_field") != ST_UNDEF){      statfield = getsym_s("in_field");      f_flag = YES;    }    if(!C_flag && symtype("class_fld_name") != ST_UNDEF)      clasfield = getsym_s("class_fld_name");    if(!L_flag && symtype("like_fld_name") != ST_UNDEF)      postfield = getsym_s("like_fld_name");/* * process file arguments */    if (optind != argc - 3) SYNTAX    stat_name = argv[optind++];    if (strcmp(stat_name, "-") == 0)    {	stat_name = "<stdin>";	stat_strm = stdin;    }    else	TRYOPEN(ProgName, stat_name, "r", stat_strm);    if ((stat_ih = read_header(stat_strm)) == NULL)	NOTSPS(ProgName, stat_name)	;    if (stat_ih->common.type != FT_FEA	    || stat_ih->hd.fea->fea_type != FEA_STAT)	ERROR_EXIT1("%s is not an ESPS statistics file.", stat_name)    infea_name = argv[optind++];    if (strcmp(infea_name, "-") == 0)    {	if (stat_strm == stdin)	    ERROR_EXIT("Input files can't both be standard input.")	infea_name = "<stdin>";	infea_strm = stdin;    }    else	TRYOPEN(ProgName, infea_name, "r", infea_strm);    if ((infea_ih = read_header(infea_strm)) == NULL)	NOTSPS(ProgName, infea_name)    ;    if (infea_ih->common.type != FT_FEA)	ERROR_EXIT1("%s is not an ESPS feature file.", infea_name)    outfea_name = argv[optind++];    if (strcmp(outfea_name, "-") == 0)    {	outfea_name = "<stdout>";	outfea_strm = stdout;    }    else	TRYOPEN(ProgName, outfea_name, "w", outfea_strm);/* * open optional history file and initial output */    if (hist_name != NULL)    {	TRYOPEN(ProgName, hist_name, "w", hist_strm);	tloc = time((long *) NULL);	Fprintf(hist_strm, "Classify history output on %s", ctime(&tloc));	Fprintf(hist_strm, "Classify version %s of %s\n", Version, Date);	Fprintf(hist_strm, "Command line:\n%s", cmd_line);    }/* * Check for complex valued fields; these not supported yet*/    if (is_field_complex(stat_ih, "invcovar") == YES){      Fprintf(stderr,       "classify: Complex inverse covariance matrix not supported - check %s\n",	      stat_name);      exit(1);    }    if (is_field_complex(stat_ih, "mean") == YES){      Fprintf(stderr,       "classify: Complex valued mean vectors not supported yet - check %s\n",	      stat_name);      exit(1);    }     if(f_flag && is_field_complex(infea_ih, statfield) == YES){      Fprintf(stderr,       "classify: Complex valued fields not supported yet - check %s in %s\n", 	      statfield, infea_name);      exit(1);    }/* * read statistics records */    if (get_fea_siz("invcovar", stat_ih, (short *) NULL, (long **) NULL)

⌨️ 快捷键说明

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