acf.c

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

C
1,039
字号
/* * This material contains unpublished, proprietary software of  * Entropic Research Laboratory, Inc. Any reproduction, distribution,  * or publication of this work must be authorized in writing by Entropic  * Research Laboratory, Inc., and must bear the notice:  * *    "Copyright (c) 1988-1990  Entropic Speech, Inc.  *    "Copyright (c) 1990-1996  Entropic Research Laboratory, Inc.  *                   All rights reserved" * * The copyright notice above does not evidence any actual or intended  * publication of this source code.      * * Written by:  Bill Byrne; based on John Shore's program frame.c * Checked by: * Revised by: * * Brief description: *  This program takes a single channel FEA_SD file, reads frames  	 *  (possibly overlapping), applies an optional window to the data,       *  and produces acoustic feastures based on each frame. */static char *sccs_id = "@(#)acf.c	1.12	1/21/97	ESI/ERL";#include <stdio.h>#include <esps/esps.h>#include <esps/unix.h>#include <esps/sd.h>#include <esps/fea.h>#include <esps/anafea.h>#include <esps/feaspec.h>#include <esps/window.h>#include <esps/limits.h>#include <math.h>#define REQUIRE(test,text) {if (!(test)) {(void) fprintf(stderr, \"%s: %s - exiting\n", PROG, text); exit(1);}}#define SYNTAX {\fprintf(stderr, "acf [-{r,s} range] [-x debug_level] [-P param] input.sd output.fea\n") ; \exit(1);}#define WT_PREFIX "WT_"#define GPFLAGSET(flagname) \(symtype(flagname)!=ST_UNDEF&&getsym_i(flagname)==1)#define GPSTRING(itemname,default) \symtype(itemname)!=ST_UNDEF ? getsym_s(itemname) : default;void	lrange_switch();void    frange_switch();int	get_sd_orecd();char	*get_cmd_line();long    *grange_switch();double  get_genhd_val();int     lin_search();long	n_rec();void	pr_farray();char	*PROG = "acf";char	*Version = "1.12";char	*Date = "1/21/97";int     debug_level = 0;	/* debug level; global for library*/void    fft_cepstrum_r();void    get_rfft();void    blt();double log10();/* * MAIN PROGRAM */main(argc, argv)    int  argc;    char **argv;{    extern int	    optind;		/* for use of getopt() */    extern char	    *optarg;		/* for use of getopt() */    int	    ch;			/* command-line option letter */    long    frame_len;		/* length of each frame */    int	    rflag = 0;		/* -r option specified? */    int	    sflag = 0;		/* -s option specified? */    long    step;		/* shift between successive frame positions */    char    *rrange;		/* arguments of -r option */    long    start;		/* starting points in the input files */    long    nan;		/* total number of samples to analyze */    double  nan_f;              /* number of samples to analys in param file */    double  start_f;            /* starting time in the input file */    double  start_time;         /* generic header item value from input file */    double  last_f;             /* total number of seconds to analyze */    long    last;		/* end of range */    char    *window_type;	/* name of type of window to apply to data */    char    *pref_w_type;	/* window type name with added prefix */    int	    win;		/* window type code */    extern char	    *window_types[];    /* standard window type names */    char    *param_name = NULL;				/* parameter file name */    char    *iname;		/* input file name */    FILE    *ifile;		/* input stream */    struct header	    *ihd;		/* input file header */    float   *x;			/* sampled data from the input file */    char    *oname;		/* output file name */    FILE    *ofile;		/* output stream */    struct header	    *ohd;		/* output file header */    struct fea_data	    *fea_rec;		/* fea output record */    struct  feaspec             *spec_rec;          /* feaspec output record */    short   units=0;            /* range format units=0 => samples;				                units=1 => seconds */    static  char *units_val[] = {"SAMPLES", "SECONDS", NULL};                                /* valid values of units parameter */    long    n_frames;		/* number of frames to process */    long    j, k;		/* loop index */    int	    more = 1;		/* flag to say more frames out there */    int	    first;		/* flag for initial call of get_sd_orecd() */    float   src_sf;             /* sampling freq of input */    float   preemphasis = 0.0;  /* preemphasis value */    float   dummy;    int     ptr;    long    actsize;	        /* actual frame length */    int     sd_flag=0;          /* store windowed data in FEA rec */    char    *sd_fname;		/* name for output sd field */    float   *sd_preemp_ptr;	/* pointer to preemphasized data */    float   *sd_ptr;		/* pointer to sd field in FEA record */				/* also used as destination of windowing */    int     p_flag=0;           /* store frame power in FEA rec */    char    *p_fname;           /* power field name */    float   *p_ptr;             /* pointer to power field in FEA rec */        int     zc_flag=0;          /* store frame zero crossings in FEA rec */    char    *zc_fname;          /* zero crossing field name */    float   *zc_ptr;            /* ptr to zero crossing field in FEA rec*/        int     ac_flag=0;          /* store autocorrelation in FEA rec*/    int     ac_yes=0;           /* compute autocorr */    char    *ac_fname;          /* autocorr field name */    double  *ac_data;           /* pointer to internal autocorr data */    float   *ac_ptr;            /* pointer to autocorr data in output record */    int     ac_order=0;         /* autocorr field dim in FEA rec */    int     rc_flag=0;          /* store reflection coefficients in FEA rec*/    int     rc_lpc_yes=0;       /* compute refl coffs */    char    *rc_fname;          /* refl coeff field name */    float   *rc_ptr;            /* pointer to refl coeff field in FEA rec */    float   *rc_data;           /* pointer to refl coeff data; if lpccep_order >                                   ad_order, padd reflection coeffs with 0's to 				   satisfy recursion relationship */    int     lpc_flag=0;         /* store linear prediction coeffs in FEA rec*/    char    *lpc_fname;         /* lpc field name */    float   *lpc_ptr;           /* pointer to lpc field in FEA rec */    float   *lpc_data;           /* pointer to lpc data */    float   lpc_gain;           /* lpc gain */     int     lsf_flag=0;         /* store line spectral frequencies in FEA rec*/    char    *lsf_fname;         /* lsf field name */    float   *lsf_ptr;           /* pointer to lsf field in FEA rec */    float   lsf_freq_res=10.0;  /* lsf frequency resolution */    int     lar_flag=0;         /* store log area ratios in FEA rec*/    char    *lar_fname;         /* lar field name */    float   *lar_ptr;           /* pointer to lar field in FEA rec */    int     lpccep_flag=0;      /* store lpc cepstrum in FEA rec*/    char    *lpccep_fname;      /* lpc cepstrum field name */    float   *lpccep_ptr;        /* pointer to lpc cepstrum field in FEA rec*/    int     lpccep_order=0;     /* number of lpccep coeffs computed */    int     lpccep_deriv_flag=0;         char    *lpccep_range;     long    *lpccep_index=NULL;    float   *lpccep_data=NULL;    long    lpccep_field_len;   /* size of lpc cepstral field in FEA rec */    int     fft_flag=0;         /* store fft in FEA rec*/    int     fft_order=0;        /* order of fft */    int     fft_tlen=0;         /* 1 << fft_order */    int     hf, num_freqs;      /* fft_rlen/2 and and hf + 1*/    double  scale;              /* scaling param */    float   *wr, *wi;           /* dummy vars for fft computation */    int     fftcep_flag=0;      /* store fft cepstrum in FEA rec */    char    *fftcep_fname;      /* fft cepstrum field name */    float   *fftcep_ptr;        /* pointer to fft cepstrum field in FEA rec*/    int     fftcep_order=0;     /* fftcep field dim in FEA rec */    float   *fftcep_imag_ptr;   /* pointer for imaginary data in cepstrum */    float   *fftcep_real_ptr;   /* pointer for real data in cepstrum */    int     fftcep_tlen=0;      /* 1 << fftcep_order */    int     fftcep_deriv_flag=0;         char    *fftcep_range;     long    *fftcep_index=NULL;    long    fftcep_field_len;       float   warp_param=0.0;     /* warping param */    /* Parse command-line options. */    while ((ch = getopt(argc, argv, "r:s:x:P:")) != EOF)        switch (ch)	{	case 'r':	    rrange = optarg;	    rflag++;	    break;        case 's':	    rrange = optarg;	    sflag++;	    break;	case 'x':	    debug_level = atoi(optarg);	    break;	case 'P':	    param_name = optarg;	    break;	default:	    SYNTAX	    break;	}    if ( rflag && sflag ) {      Fprintf(stderr, "can only specify one of -r or -s\n");      exit(1);    }/* Process file names and open files. */    if (argc - optind > 2) {	Fprintf(stderr,	    "%s: too many file names specified.\n", PROG);	SYNTAX    }    if (argc - optind < 2) {	Fprintf(stderr,	    "%s: too few file names specified.\n", PROG);	SYNTAX    }    iname = eopen(PROG,	    argv[optind++], "r", FT_FEA, FEA_SD, &ihd, &ifile);    oname = eopen(PROG,	    argv[optind++], "w", NONE, NONE, &ohd, &ofile);    REQUIRE( iname != NULL && oname != NULL,	    "ERROR: trouble with opening i/o files.\n");    REQUIRE(get_fea_siz("samples", ihd, (short *) NULL, (long **) NULL) == 1,	    "sorry, can't deal with multi-channel files");    REQUIRE(!is_field_complex(ihd, "samples"), 	    "sorry, can't deal with complex data");     /* Get parameter values. */    if (ifile != stdin)	(void) read_params(param_name, SC_CHECK_FILE, iname);    else        (void) read_params(param_name, SC_NOCOMMON, iname);    if (symtype("units") != ST_UNDEF)       REQUIRE( (units = lin_search( units_val, getsym_s("units"))) != -1,	      "invalid specification of parameter units.");    if (debug_level>1) {      if (units==0)	Fprintf(stderr, "%s: units in samples.\n", PROG);      else      	Fprintf(stderr, "%s: units in seconds.\n", PROG);    }    /* get sample frequency of input */    src_sf = get_genhd_val("record_freq", ihd, (double) 0.0);    REQUIRE( !(src_sf == 0.0 && sflag ), 	    "record_freq not defined in input: can't use -s");    if (src_sf == 0.0) {      REQUIRE( units != 0, 	      "record_freq not in input header: can't specify units as seconds.");      src_sf = 1.0;      }    start_time = get_genhd_val( "start_time", ihd, 0.0);    /* if -r option specified, get start and last */    if (rflag) {      start = 1;      last = LONG_MAX;      lrange_switch(rrange, &start, &last, 0);    }    /* if -s option specified, get start and last and convert to samples */    if (sflag) {      start_f = start_time;      last_f = DBL_MAX;      (void) frange_switch(rrange, &start_f, &last_f);      REQUIRE(start_f >= start_time, "can't start before beginning of input file");      REQUIRE(last_f >= start_f, "empty range specified for input file");      start = (start_f==start_time) ? 1 : (long) ((start_f-start_time)*src_sf+1);      last = (last_f==DBL_MAX) ? LONG_MAX : (long) (last_f*src_sf+1);    }    if (last < LONG_MAX)       nan = last - start + 1;    /* if not -s or -r, get start and nan from param file (as floats) -       allow for common processing, in which start and nan are ints */    if (!sflag && !rflag) {	switch(symtype("start")) {	    case ST_FLOAT :  /*assume read from param file*/	         start_f = getsym_d("start");		 if (start_f==0)		   start = 1;		 else		   start = (units == 0) ? (long) start_f		       : (long) ((start_f-start_time) * src_sf + 1);		 break;            case ST_INT   :  /*assume read from common file*/		 start = getsym_i("start");		 break;            case ST_UNDEF :  /*not defined - start at begining*/		 start = 1;		 break;	    default:                 Fprintf(stderr, "%s: Invalid type of value start.\n", PROG);		 exit(1);	    }	switch(symtype("nan")) {	    case ST_FLOAT :  /*assume read from param file*/	         nan_f = getsym_d("nan");		 nan = (units == 0) ? (long) nan_f : (long) (nan_f * src_sf);		 break;            case ST_INT   :  /*assume read from common file*/		 nan = getsym_i("nan");		 break;            case ST_UNDEF :  /*not defined */	         nan = 0;		 break;	    default:                 Fprintf(stderr, "%s: Invalid type of value nan.\n", PROG);		 exit(1);	    }	if (nan < LONG_MAX)	  last = nan + start - 1;

⌨️ 快捷键说明

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