stat.c

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

C
783
字号
/* * 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." * * * stat.c - process stat_output in fea_stats(1-ESPS) *  * Author: Ajaipal S. Virdy, Entropic Speech, Inc. */#ifdef SCCS    static char *sccsid = "@(#)stat.c	1.13	7/8/96	ESI";#endif#include <stdio.h>#include <esps/esps.h>#include <esps/unix.h>#include <esps/fea.h>#include <assert.h>#include <esps/spsassert.h>#include <esps/feastat.h>#include "global.h"#include "stat.h"#define REALLOC(typ,num,var) { \    if ((var) == NULL) \	(var) = (typ *) emalloc((unsigned) (num) * sizeof(typ)); \    else (var) = (typ *) realloc((char *) (var), \				    (unsigned) (num) * sizeof(typ)); \    spsassert((var), "can't allocate memory."); \}char *emalloc();#if !defined(DEC_ALPHA) && !defined(HP700)char  *ecalloc(), *calloc(), *realloc();#endif/* * S P S *  F U N C T I O N S *   R E F E R E N C E D */double	matrix_inv_d();int	lin_search2();char	*get_fea_ptr();float	**f_mat_alloc();double	**d_mat_alloc();char	*savestring();char	template[20] = "/usr/tmp/fsXXXXXX";voidProcess_stat_output(){    float	*mean;    double	**covarmat;    double	**invcovar;/* * The -E option has not been implemented yet, so don't declare these * variables: * *  float	*eigenval; *  float	**eigenvec; * */    int		i_rec;    int		i, j, k;	/* indexing variables for arrays */    int		ptr;		/* pointer to fields in a FEA file record */    double	*d_ptr; 	/* pointer to a double */    float	*f_ptr; 	/* pointer to a float */    long	*l_ptr; 	/* pointer to a long */    short	*s_ptr; 	/* pointer to a short */    char	*b_ptr; 	/* pointer to a short */    double	value;		/* temporary variables */    int		n_class = 0;	/* number of classifications */    short	class;    char	**classification = NULL;    char	**s;    struct    feastat	*stat_rec;	/* FEA_STAT file record pointer */    struct    header	*tmp_oh;	/* temporary file header */    struct    feastat	*tmp_stat_rec;	/* Temporary FEA_STAT file record pointer */    FILE	*tstrm; 	/* temporary file pointer */    int	add_covar = NO;	     /* add COVARIANCE field if flag set */    int	add_invcovar = NO;   /* add INVERSE COVARIANCE field if flag set */    int	add_eigen = NO;	     /* add EIGENVALUES, EIGENVECTORS if flag set */    int	covar_exists = NO;    /* does COVARIANCE field exist? */    int	invcovar_exists = NO; /* does INVERSE COVARIANCE field exist? */    int	eigen_exists = NO;    /* does EIGENVALUES, EIGENVECTORS field exist? */    char	*Module = "stat_out";    if (debug_level > 9) {	Fprintf (stderr, "Version = %s, Date = %s\n", Version, Date);	Fprintf (stderr, "command_line = %s\n", command_line);        (void) fflush (stderr);    }    if (Mflag) {	if (debug_level > 1)	    Fprintf (stderr,	    "%s: Computing Mean Vector ...\n", Module);    }    ptr = stat_field[0];    statsize = n_items[0];    if (debug_level > 1)	Fprintf (stderr,	"%s: allocating memory: statsize = %ld\n", Module, statsize);    if (Mflag)	if ( (mean = (float *) ecalloc((unsigned) statsize, sizeof(float)))	     == NULL )	{	    Fprintf (stderr,		"%s: calloc: could not allocate memory for mean.\n",		Module);	    exit(1);	}    if (Cflag || Iflag)	if (( covarmat = (double **) d_mat_alloc((unsigned) statsize,		    (unsigned) statsize) ) == NULL)	{	    Fprintf (stderr,		"%s: d_mat_alloc: could not allocate memory for covarmat.\n",		Module);	    exit(1);	}    if (Iflag){      if( n_rec < statsize ){	Fprintf(stderr, "ERROR: %s: Number of sample vector (%d) < number of features (%d).\n\tWill get singular covariance matrix.\n\tCan NOT do inverse covariance matrix.\n\tNeed at least %d linearly independent sample vectors -- exiting\n", Module, n_rec, statsize, statsize);	if(update_file == NO) unlink(outfile);	exit(1);      }      if (( invcovar = (double **) d_mat_alloc((unsigned) statsize,					      (unsigned) statsize) ) == NULL)	{	  Fprintf (stderr, "%s: d_mat_alloc: invcovar malloc failed.\n", 		   Module);	  exit(1);	}    }    /*     * if (stat_out && !Aflag) then     *    memory for Data has not been allocated, nor data stored.     */    if (!Aflag)	if (( Data = (float **) f_mat_alloc((unsigned) n_rec,		   (unsigned) statsize) ) == NULL)	{	    Fprintf (stderr,		"%s: f_mat_alloc: could not allocate memory for Data.\n",		Module);	    exit(1);	}    for (k = 0; k < statsize; k++)	mean[k] = 0.0;    for (i_rec = 0; i_rec < n_rec; i_rec++) {	if (debug_level > 8)	    Fprintf (stderr,		"\t%s: getting record number %d.\n", Module, s_rec + i_rec);	if (get_fea_rec (fea_rec, esps_hdr, instrm) == EOF) {	    Fprintf (stderr,		"%s: ran out of data after record number %d.\n",		Module, s_rec + i_rec);	    exit(1);	}       /*	* Position the pointers to the correct places.	*/	switch (fea_hdr->types[ptr]) {	    case DOUBLE: d_ptr = (double *) get_fea_ptr (fea_rec,			fea_hdr->names[ptr], esps_hdr);			break;	    case FLOAT: f_ptr = (float *) get_fea_ptr (fea_rec,			fea_hdr->names[ptr], esps_hdr);			break;	    case LONG:  l_ptr = (long *) get_fea_ptr (fea_rec,			fea_hdr->names[ptr], esps_hdr);			break;	    case SHORT: s_ptr = (short *) get_fea_ptr (fea_rec,			fea_hdr->names[ptr], esps_hdr);			break;	    case BYTE: b_ptr = (char *) get_fea_ptr (fea_rec,			fea_hdr->names[ptr], esps_hdr);			break;	    default:    Fprintf(stderr,			    "%s: incorrect data type.\n", Module);	}	for (k = 0; k < statsize; k++) {	    switch (fea_hdr->types[ptr])	    {	    case DOUBLE:		value = (float) *(d_ptr + item_arrays[0][k]);		break;	    case FLOAT:		value = *(f_ptr + item_arrays[0][k]);		break;	    case LONG:		value = (float) *(l_ptr + item_arrays[0][k]);		break;	    case SHORT:		value = (float) *(s_ptr + item_arrays[0][k]);		break;	    case BYTE:		value = (float) *(b_ptr + item_arrays[0][k]);		break;	    default:		Fprintf(stderr, "%s: incorrect data type.\n", Module);	    }	    mean[k] += value / n_rec;	    if (!Aflag)		Data[i_rec][k] = value;	    if (debug_level > 20)	    {		Fprintf (stderr, "\n\trecord #%d, k = %d, value = %g\n",		    s_rec + i_rec, k, value);		Fprintf (stderr, "\tmean[%d] = %g\n",		    k, mean[k]);	    }	}  /* for (k = 0; k < n_item; k++) */    }  /* end for (i_rec = 0; i_rec < n_rec; i_rec++) */    if (debug_level > 20)	for (k = 0; k < statsize; k++)	    Fprintf (stderr, "mean[%d] = %g\n", k, mean[k]);    if (debug_level > 2)	Fprintf (stderr, "%s: Update or create output FEA_STAT file.\n",	    Module);    /*     * Update FEA_STAT file or Create output FEA_STAT file.     */    if (update_file) {	if (debug_level > 1) {	    Fprintf (stderr,	    "%s: Updating file %s.\n", Module, outfile);	    (void) fflush (stderr);	}	if (debug_level > 8) {	    Fprintf (stderr,	    "%s: checking -{MICE} options.\n", Module);	    (void) fflush (stderr);	}	stat_rec = allo_feastat_rec (fea_oh);       /*	* If an option field does not exist in the existing FEA_STAT	* file, then create that optional field if and only if the	* corresponding flag was given on the command line.	* For example, if the existing FEA_STAT file only contains the	* mean vector and the user requested the -C option, then the	* covariance field must be created in the updated FEA_STAT	* file.  In the context of this program, set add_covar equal to	* the value of Cflag if the covariance field does not exist,	* otherwise set add_covar to YES.  add_covar will then be used	* in the call to init_feastat_hd to allocate the right amount	* of memory.	*/	if (get_fea_ptr (stat_rec->fea_rec, "covar", fea_oh) == NULL) {	    add_covar = Cflag;	    covar_exists = NO;	} else {	    add_covar = YES;	    covar_exists = YES;	}	if (get_fea_ptr(stat_rec->fea_rec, "invcovar", fea_oh) == NULL) {	    add_invcovar = Iflag;	    invcovar_exists = NO;	} else {	    add_invcovar = YES;	    invcovar_exists = YES;	}	if (get_fea_ptr(stat_rec->fea_rec, "eigenvec", fea_oh) == NULL) {	    add_eigen = Eflag;	    eigen_exists = NO;	} else {	    add_eigen = YES;	    eigen_exists = YES;	}       /* Open a temporary file for writing out data. */	if ((tstrm = fopen (mktemp (template), "w+")) == NULL)	    CANTOPEN (Module, template);       /* Create new header for our updated FEA_STAT file */	tmp_oh = new_header (FT_FEA);	assert (tmp_oh);       /*	* Get all possible classifications from the header of the	* the existing FEA_STAT file (feaoh).	*/	fea_hdr = fea_oh->hd.fea;	if (debug_level > 10)	    Fprintf (stderr, 		"%s: number of feature fields in %s = %d.\n",		Module, outfile, fea_hdr->field_count);	i = lin_search2(fea_hdr->names, "class");	s = fea_hdr->enums[i];	if (debug_level > 10)	    Fprintf (stderr, 		"\t%s: *s = *fea_hdr->enums[%d] = %s.\n",		Module, i, *s);	while (*s != NULL) {	    n_class++;	    REALLOC(char *, n_class, classification)	    classification[n_class-1] = savestring (*s);	    assert (classification[n_class-1]);	    if (debug_level > 10)		Fprintf (stderr, 		    "\t%s: fea_hdr->enums[%d] = %s, n_class = %d.\n",		    Module, i, *s, n_class);	    s++;	}       /*	* If the class name which was specified on the command line	* with the -n option is not present in the enums array, add	* it as the last non-NULL element.  This way	* the stat_rec->class values do not need to be modifed.  What	* we are basically doing is adding the new classification names	* to the end of the stack.	*/	if ((class = lin_search2(fea_hdr->enums[i], class_name)) == -1)	{	    n_class++;	    REALLOC(char *, n_class, classification)	    classification[n_class-1] = savestring (class_name);	    class = n_class - 1;

⌨️ 快捷键说明

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