find_ep.c

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

C
922
字号
    if((putsym_i("start", (int)s_pt)) == -1){	Fprintf(stderr, 	"find_ep: START could not be written to ESPS Common\n");	exit(1);    }    if(debug_level > 0)	Fprintf(stderr, "find_ep: start (%ld) written to ESPS Common\n", 	s_pt);    if((putsym_i("nan", (int)(e_pt-s_pt+1))) == -1){	Fprintf(stderr, 	"find_ep: NAN could not be written to ESPS Common\n");	exit(1);    }    if(debug_level > 0)	Fprintf(stderr, "find_ep: Nan (%ld)written to ESPS Common\n", 	    e_pt - s_pt +1);    if((putsym_s("prog", "find_ep")) == -1){	Fprintf(stderr, 	"find_ep: PROGRAM Name could not be written to ESPS Common\n");	exit(1);    }    if(!nflag)        if((putsym_s("filename", f1)) == -1){	    Fprintf(stderr, 	    "find_ep: FILE Name could not be written to ESPS Common\n");	    exit(1);	}    exit(0);    return(0);}intendpoints(f1h, f1p, start, s_pt, e_pt, sf, scalad,      sil_req, low_thresh, hflag, high_thresh, fflag,     final_thresh, context, time)/*This function finds the beginning and end point of an utterance  in a sampled data file. It does this by comparing the average adjusted  magnitude in each context size frame with three thresholds.*/#define AAM_MIN      1.2#define HUGE_AAM    10.#define LOW_FACTOR   2.5#define HIGH_FACTOR  4.0#define FINAL_FACTOR 0.8struct header *f1h;FILE *f1p;long  start, *s_pt, *e_pt;int  hflag, fflag;float sil_req, low_thresh, high_thresh, final_thresh, 	context, time, sf, scalad;{/* - quick definition of input parameters    struct header *f1h ;	 input file header pointer    FILE	*f1p ;		 input file sream pointer    int		hflag ;		 high threshold flag     int		fflag ;		 final threshold flag    int		scalad ;	2**(adbits - 12)    long	start ;		 starting point in file     float	sil_req ;	 silence required between words    float	low_thresh ;	 variable for first threshold     float	high_thresh ;	 variable for second threshold     float	final_thresh ;	 variable for end threshold         float	context ;	 internal frame size in milliseconds     float	time ;		 time period for computing statistics     long	*s_pt; 		 starting record position     long	*e_pt;		 ending record position     float	sf ;		 sampling frequency of input data*//* - Returned Error Codes    0	Successful Completion    1	Possible Word at End of File    2	Incorrect Ordering of Thresholds    3	Not Enough Room to Compute Statistics    4	Nonsilence Interval Analyzed    5	No Word Found - Low_Thresh Not Exceeded    6	No Word Found - High_Thresh Not Exceededthe messages are printed by find_ep.c*/    int lowcnt = 1;	    /*counts number of frames after end of word*/    int exlow = NO;	    /* flag for low threshold*/    int ipf;		    /* internal points/frame*/    int nsil;		    /* number of silence frames*/    int nstatf;		    /* number of frames used to compute statistics*/    long start_pt;	    /* points to current starting point*/    int calaam_err = 0;	    /*contains calaam error status*/        float aam = 0;	    /* average adjusted magnitude */    float final_tmp;	    /* holds temporary final threshold */    float high_tmp;	    /* holds temporary high threshold */    int calaam();	    /* compute average adjusted magnitude */    /*     * set up parameters    */        ipf = ROUND(sf*context/1000.);    nsil = ROUND(sil_req/context);    if (nsil < 1) nsil = 1;    nstatf  = ROUND(time/context);    start_pt = start;    if(debug_level > 0){	Fprintf(stderr, "endpoints: Just entered ENDPOINTS\n");	Fprintf(stderr, "endpoints: Scalad = %f\n", scalad);	Fprintf(stderr, "endpoints: Start = %ld\n", start);	Fprintf(stderr, "endpoints: Sil_Req = %f\n", sil_req);	Fprintf(stderr, "endpoints: low_thresh = %f\n", low_thresh);	Fprintf(stderr, "endpoints: high_thresh = %f\n", high_thresh);	Fprintf(stderr, "endpoints: final_thresh = %f\n", final_thresh);	Fprintf(stderr, "endpoints: context = %f\n", context);	Fprintf(stderr, "endpoints: time = %f\n", time);    }    /*     * if needed, compute statistics     */        if(low_thresh <= 0.){	if(debug_level >0)	    Fprintf(stderr, "endpoints: Computing Statistics\n");	if( (calaam_err = calaam( nstatf, ipf, &aam, f1h, f1p)) != 0)	    return(3);	if(aam <= AAM_MIN*scalad)	    aam = AAM_MIN*scalad;	if(aam > HUGE_AAM*scalad)	    return(4);	start_pt += nstatf*ipf;	low_thresh = LOW_FACTOR*aam;	if(debug_level > 0)	    Fprintf(stderr, "endpoints: Low_Thresh = %f\n", low_thresh);    }    /*     * set high and final threshold    */        high_tmp = HIGH_FACTOR*low_thresh;    final_tmp = FINAL_FACTOR*low_thresh;    if(!hflag) 	high_thresh = high_tmp;    if(!fflag)	final_thresh = final_tmp;    /* check threshold ordering*/    if(final_thresh >= low_thresh || low_thresh >= high_thresh)	return(2);    if(debug_level > 0){	Fprintf(stderr, "endpoints: High_Thresh = %f\n", high_thresh);	Fprintf(stderr, "endpoints: Final_Thresh = %f\n", final_thresh);    }LOW_SEARCH: /* label for GOTO */    /*     * Search for frame > low_thresh    */    aam = 0;        if(debug_level > 0)	Fprintf(stderr, "endpoints: Looking for frame > Low_Thresh\n");    while(aam < low_thresh){	if( (calaam_err = calaam( 1, ipf, &aam, f1h, f1p)) != 0){	    if(exlow)		return(6);	    else		return(5);	}	start_pt += ipf;	#ifdef DEBUG	if(debug_level > 2){	    Fprintf(stderr, "endpoints: AAM = %f; Start_Pt = %ld\n", aam,	    start_pt - ipf);	}#endif    }    /*     * store potential starting point of word    */    *s_pt = start_pt - ipf;    if(debug_level >0)	Fprintf(stderr, "endpoints: Potential starting point = %ld\n",	*s_pt);    /*     * Now we must cross high_thresh before crossing final_thresh    */        exlow = YES;    if(debug_level > 0)	Fprintf(stderr, "endpoints: Looking for frame > High_Thresh\n");    while(aam < high_thresh && aam > final_thresh){	if( (calaam_err = calaam( 1, ipf, &aam, f1h, f1p)) != 0)	    return(6);	start_pt += ipf;#ifdef DEBUG	if(debug_level > 2){	    Fprintf(stderr, "endpoints: AAM = %f; Start_Pt = %ld\n", 	    aam, start_pt - ipf);	}#endif    }    /* If aam < final_thresh, throw away potential starting point  and	look for new starting point (frame > low_thresh)*/    if(aam < final_thresh){		if(debug_level > 0)	    Fprintf(stderr, "endpoints: AAM < Final_Thresh; throw away potential starting point and look for frame > Low_Thresh\n");	goto LOW_SEARCH;	    }FINAL_SEARCH: /*label for GOTO */    /*     * Look for crossing of final_thresh    */    if(debug_level > 0)	Fprintf(stderr, "endpoints: Looking for frame < Final_Thresh\n");    while(aam > final_thresh){	if( (calaam_err = calaam( 1, ipf, &aam, f1h, f1p)) != 0)	    break;	start_pt += ipf;#ifdef DEBUG	if(debug_level > 2){	    Fprintf(stderr, "endpoints: AAM = %f, Start_Pt = %ld\n", 	    aam, start_pt - ipf);	}#endif    }    if(aam > final_thresh){	/*EOF reached; We have partial word*/	*e_pt = start_pt - ipf + calaam_err;	return(1);    }    /*     * Mark end of utterance    */        *e_pt = start_pt -1 -ipf;    if(debug_level > 0)	Fprintf(stderr, "endpoints: Potential endpoint of word = %ld\n", 	    *e_pt);    /*     * We must stay below final_thresh for NSIl frames    */    lowcnt = 1;    if(debug_level > 0)	Fprintf(stderr, "endpoints: AAM must stay < Low_Thresh\n");    while(lowcnt < nsil && aam < low_thresh){	if( (calaam_err = calaam( 1, ipf, &aam, f1h, f1p)) != 0)	    break;	start_pt += ipf;	lowcnt++;#ifdef DEBUG        if(debug_level > 2){	    Fprintf(stderr, 	    "endpoints: Frame # %d,  AAM = %f, start_pt  = %ld\n", 	    lowcnt -1, aam, start_pt - ipf);	}#endif    }    if(aam > low_thresh){ /*go back and search for crossing of final_thresh*/	if(debug_level >0)	    Fprintf(stderr, "endpoints: AAM exceeded Low_Thresh; go back and look for crossing of Final_Thresh\n");	goto FINAL_SEARCH;    }    /*     * Utterance found; Is it long enough    */        if( ( (*e_pt - *s_pt + 1)/sf ) < .150 ){	if(debug_level >0)	    Fprintf(stderr, "endpoints: Word not long enough; start again\n");	exlow = NO;	goto LOW_SEARCH;    }    /*     * Got here; Passed all tests    */    return(0);}intcalaam( nf, npf, aam, fh, fp)/* Calculate the average adjusted magnitude (AAM)*//* AAM is the magnitude of sample value minus average value*/FILE *fp;		/*file stream pointer*/struct header *fh;	/* file header pointer*/int nf;			/*number of frames to average over*/int npf;		/*number of points per frame*/float *aam;		/* returned average adjusted magnitude*/{    static double *buff = NULL;   /*data buffer*/    int i;	    /*counter*/    int total_pts;  /*number of point to get*/    float sum=0;    /*sum of points*/    float avg=0;    /*average of points*/    static int buffsize = 0;    total_pts = nf * npf;    if(total_pts > buffsize){        if(buff != NULL) free((char *)buff);        buff = (double *)calloc( (unsigned)total_pts, sizeof(double));        spsassert(buff != NULL, "Couldn't allocate space for buff");    }    buffsize = total_pts;#ifdef DEBUG    if(debug_level >1){	Fprintf(stderr, "calaam: Just entired CALAAM\n");	Fprintf(stderr, "calaam: Number of frames = %d\n", nf);	Fprintf(stderr, "calaam: Number of points/frame = %d\n", npf);	Fprintf(stderr, "calaam: Total number of points = %d\n", total_pts);    }#endif    if( (get_sd_recd(buff, total_pts, fh, fp)) != total_pts)	return(1);#ifdef DEBUG    if(debug_level > 9)	for(i=0;i<total_pts;i++)	    Fprintf(stderr, "calaam: Input point %d = %lf\n", 	    i, buff[i]);#endif    /*     * Compute Average    */    for(i=0;i< total_pts; i++)	sum += buff[i];#ifdef DEBUG    if(total_pts <= 0){	Fprintf(stderr, 	"find_ep: endpoints: calaam: total_pts <= 0\n");	exit(1);    }#endif        avg = sum/(float)total_pts;#ifdef DEBUG    if(debug_level > 2)	Fprintf(stderr, "calaam: Average = %f\n", avg);#endif    /*     * Compute AAM    */    *aam = 0;    for(i=0; i < total_pts; i++){	*aam +=  (float)fabs( (double)(buff[i] - avg) );#ifdef DEBUG	if(debug_level > 7)	Fprintf(stderr, "calaam: magnitude[%d] = %f\n", i, 	(float)fabs( (double)(buff[i] - avg)));#endif    }#ifdef DEBUG    if(debug_level > 2)	Fprintf(stderr, "calaam: sum of AAMs = %f\n", *aam);#endif    *aam = *aam/(float)total_pts;    /*     * ALL DONE; clean up and return    */        return(0);}voiderror_exit(error_status, eflag)int error_status, eflag;{	switch (error_status) {	    case 1: 		Fprintf(stderr,"find_ep: Possible word at end of file\n");		if(eflag){		    break;		}		else{		    exit(1);		}		break;	    case 2: 	        Fprintf(stderr, 		"find_ep: Incorrect ordering of thresholds\n");		exit(1);	    case 3: 		Fprintf(stderr, 		"find_ep: Not enough room to compute statistics\n");		exit(1);	    case 4: 		Fprintf(stderr, "find_ep: Nonsilence interval analyzed\n");		exit(1);	    case 5:		Fprintf(stderr, 		"find_ep: No word found - low_thresh not exceeded\n");		exit(1);	    case 6:		Fprintf(stderr, 		"find_ep: No word found - high_thresh not exceeded\n");		exit(1);	    default:		Fprintf(stderr, 		"find_ep: Invalid error code returned from endpoints()\n");		exit(1);	}    }

⌨️ 快捷键说明

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