feaspecsupp.c

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

C
551
字号
/* * This material contains proprietary software of Entropic Speech, Inc. * Any reproduction, distribution, or publication without the 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 1988 Entropic Speech, Inc. All Rights Reserved." * * Program: feaspecsupp.c * * Written by:  Rodney Johnson * Checked by:   * * This program contains support routines for FEA file subtype FEA_SPEC */#ifndef lint    static char *sccs_id = "@(#)feaspecsupp.c	1.6	7/2/90	 ESI";#endif/* * Include files. */#include <stdio.h>#include <esps/spsassert.h>#include <esps/esps.h>#include <esps/unix.h>#include <esps/fea.h>#include <esps/feaspec.h> char *savestring();char **get_feaspec_xfields();void free_feaspec_xfields();/* * String array definitions. * Keep consistent with constant symbols in esps/feaspec.h. */char	*spfmt_names[] = {"NONE", "SYM_CTR", "SYM_EDGE", "ASYM_CTR",			    "ASYM_EDGE", "ARB_VAR", "ARB_FIXED", NULL};char	*sptyp_names[] = {"NONE", "PWR", "DB", "REAL", "CPLX",  NULL};char	*spfrm_names[] = {"NONE", "FIXED", "VARIABLE", NULL};/* * This function fills in the header of a FEA file to make it a * file of subtype FEA_SPEC. */#define ADDFLD(name,size,rank,dimen,type,enums) \        {if (add_fea_fld((name),(long)(size),(rank),(long*)(dimen), \                type,(char**)(enums),hd) == -1) return 1;}intinit_feaspec_hd(hd, def_tot_power, freq_format, spec_type,		contin, num_freqs, frame_meth, freqs, sf, frmlen, 		re_spec_format)    struct header   *hd;	/* FEA file header*/    int		    def_tot_power;  /* Define tot_power field? */    int		    freq_format;    /* How set of frequencies defined. */    int		    spec_type;	/* Are data log power, complex, etc.? */    int		    contin;	/* Continuous or discrete spectrum? */    long	    num_freqs;	/* Number of frequencies. */    int		    frame_meth;	/* How frame length is determined. */    float	    *freqs;	/* Frequencies (if given in header). */    double	    sf;		/* Sampling frequency. */    long	    frmlen;	/* Number of points in analysis frame. */    int	    	    re_spec_format; /* FLOAT or BYTE */{    static char	*no_yes[] = {"NO", "YES", NULL};    spsassert(hd, "init_feaspec_hd: hd is NULL");    spsassert(hd->common.type == FT_FEA, "init_feaspec_hd: file not FEA");    if (spec_type == SPTYP_DB)    	spsassert(re_spec_format == BYTE || re_spec_format == FLOAT,	 "init_feaspec_hd: re_spec_format not BYTE or FLOAT");    /*     * First, put in the generic header items.     */    *add_genhd_e("freq_format", (short *) NULL, 1, spfmt_names, hd) =	freq_format;    *add_genhd_e("spec_type", (short *) NULL, 1, sptyp_names, hd) = spec_type;    *add_genhd_e("contin", (short *) NULL, 1, no_yes, hd) = contin;    *add_genhd_l("num_freqs", (long *) NULL, 1, hd) = num_freqs;    *add_genhd_e("frame_meth", (short *) NULL, 1, spfrm_names, hd) = frame_meth;    if (freq_format == SPFMT_ARB_FIXED)    {	float	*ptr;	int	i;	spsassert(freqs, "init_feaspec_hd: vector of freqs is NULL");	ptr = add_genhd_f("freqs", (float *) NULL, (int) num_freqs, hd);	for (i = 0; i < num_freqs; i++)	    ptr[i] = freqs[i];    }    switch (freq_format)    {    case SPFMT_SYM_CTR:    case SPFMT_SYM_EDGE:    case SPFMT_ASYM_CTR:    case SPFMT_ASYM_EDGE:	*add_genhd_f("sf", (float *) NULL, 1, hd) = sf;	break;    default:	break;    }    if (frame_meth == SPFRM_FIXED)    {	*add_genhd_l("frmlen", (long *) NULL, 1, hd) = frmlen;    }    /*     * Then define the record fields.     */    if (def_tot_power)	ADDFLD("tot_power", 1, 0, NULL, FLOAT, NULL)    if (spec_type != SPTYP_DB ||        (spec_type == SPTYP_DB && re_spec_format == FLOAT))    	ADDFLD("re_spec_val", num_freqs, 1, NULL, FLOAT, NULL)    else    	ADDFLD("re_spec_val", num_freqs, 1, NULL, BYTE, NULL)    if (spec_type == SPTYP_CPLX)	ADDFLD("im_spec_val", num_freqs, 1, NULL, FLOAT, NULL)    if (freq_format == SPFMT_ARB_VAR)    {	ADDFLD("n_frqs", 1, 0, NULL, LONG, NULL)	ADDFLD("frqs", num_freqs, 1, NULL, FLOAT, NULL)    }    if (frame_meth == SPFRM_VARIABLE)	ADDFLD("frame_len", 1, 0, NULL, LONG, NULL)    hd->hd.fea->fea_type = FEA_SPEC;    return 0;}/* * This function allocates a record for the FEA file subtype FEA_SPEC. */#define GETPTR(member,type,field) \        {spec_rec->member = (type *) get_fea_ptr(fea_rec, field, hd);}struct feaspec *allo_feaspec_rec(hd, re_spec_format)    struct header   *hd;    int	    	    re_spec_format;{    struct fea_data *fea_rec;    struct feaspec  *spec_rec;    short	    *freq_format;    long	    *num_freqs;    short	    *frame_meth;    float	    *freqs;    float	    *sf;    long	    *frmlen;    double	    f, step;    int		    i;    int	    	    spec_type;    spsassert(hd, "allo_feaspec_rec: hd is NULL");    spsassert(hd->common.type == FT_FEA		&& hd->hd.fea->fea_type == FEA_SPEC, 	"allo_feaspec_rec: file not FEA or not FEA_SPEC");    spec_type = *get_genhd_s("spec_type", hd);    spsassert(spec_type, "allo_feaspec_rec: missing header item spec_type");    if (spec_type == SPTYP_DB) {    	spsassert(re_spec_format == BYTE || re_spec_format == FLOAT,	 "allo_feaspec_rec: re_spec_format not BYTE or FLOAT");    }    else	re_spec_format = FLOAT;    spec_rec = (struct feaspec *) calloc(1, sizeof(struct feaspec));    spsassert(spec_rec, "allo_feaspec_rec: calloc failed on spec_rec");    spec_rec->fea_rec = fea_rec = allo_fea_rec(hd);    if (hd->common.tag)	spec_rec->tag = &fea_rec->tag;    else	spec_rec->tag = NULL;    num_freqs = get_genhd_l("num_freqs", hd);    spsassert(num_freqs, "allo_feaspec_rec: num_freqs undefined.")    spec_rec->re_spec_val_b = NULL;    spec_rec->re_spec_val = NULL;    if (re_spec_format == FLOAT && get_fea_type("re_spec_val",hd) == FLOAT)    	GETPTR(re_spec_val, float, "re_spec_val")    else if (re_spec_format == BYTE && get_fea_type("re_spec_val",hd) == BYTE)    	GETPTR(re_spec_val_b, char, "re_spec_val")    else if (re_spec_format == BYTE) {	spec_rec->re_spec_val_b = malloc((unsigned) *num_freqs);	spsassert(spec_rec->re_spec_val_b,		"allo_feaspec_rec: malloc failed on re_spec_val_b.");    }    else if (re_spec_format == FLOAT) {	spec_rec->re_spec_val = malloc_f((unsigned) *num_freqs);	spsassert(spec_rec->re_spec_val,		"allo_feaspec_rec: malloc failed on re_spec_val.")    }    GETPTR(tot_power, float, "tot_power")    GETPTR(im_spec_val, float, "im_spec_val")    GETPTR(n_frqs, long, "n_frqs")    GETPTR(frqs, float, "frqs")    GETPTR(frame_len, long, "frame_len")    if (!spec_rec->im_spec_val)    {	spec_rec->im_spec_val = malloc_f((unsigned) *num_freqs);	spsassert(spec_rec->im_spec_val,		"allo_feaspec_rec: malloc failed on im_spec_val.")	for (i = 0; i < *num_freqs; i++) spec_rec->im_spec_val[i] = 0.0;    }    if (!spec_rec->frqs)    {	freq_format = get_genhd_s("freq_format", hd);	spsassert(freq_format, "allo_feaspec_rec: freq_format undefined.")	spec_rec->n_frqs = malloc_l((unsigned) 1);	spsassert(spec_rec->n_frqs,	    "allo_feaspec_rec: malloc failed on n_frqs.")	*spec_rec->n_frqs = *num_freqs;	spec_rec->frqs = malloc_f((unsigned) *num_freqs);	spsassert(spec_rec->frqs, "allo_feaspec_rec: malloc failed on frqs.")	switch(*freq_format)	{	case SPFMT_SYM_CTR:	case SPFMT_SYM_EDGE:	case SPFMT_ASYM_CTR:	case SPFMT_ASYM_EDGE:	    sf = get_genhd_f("sf", hd);	    spsassert(sf, "allo_feaspec_rec: sf undefined.")	    switch (*freq_format)	    {	    case SPFMT_SYM_CTR:		step = *sf / (2 * *num_freqs);		f = step/2;		break;	    case SPFMT_SYM_EDGE:		step = *sf / (2 * (*num_freqs - 1));		f = 0.0;		break;	    case SPFMT_ASYM_CTR:		step = *sf / *num_freqs;		f = (step - *sf)/2;		break;	    case SPFMT_ASYM_EDGE:		step = *sf / (*num_freqs - 1);		f = - *sf/2;		break;	    }	    for (i = 0; i < *num_freqs; i++, f += step) spec_rec->frqs[i] = f;	    break;	case SPFMT_ARB_VAR:	    Fprintf(stderr,		"%s: frqs field required by freq_format but not present.\n",		"allo_feaspec_rec");	    exit(1);	    break;	case SPFMT_ARB_FIXED:	    freqs = get_genhd_f("freqs", hd);	    spsassert(freqs,		"allo_feaspec_rec: header item freqs undefined.")

⌨️ 快捷键说明

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