예제 #1
0
int32 fe_compute_melcosine(melfb_t *MEL_FB)
{
//SPEC: change to doing this routine in double; seems to help 
// consistency across platforms, and there doesn't seem to be 
// any particular reason to demand single here.  jh/17/apr/2005
    double period, freq;
    int32 i,j;
    
    period = (double)2*MEL_FB->num_filters;

    if ((MEL_FB->mel_cosine = (float **) fe_create_2d(MEL_FB->num_cepstra,MEL_FB->num_filters,
					      sizeof(float)))==NULL){
	fprintf(stderr,"memory alloc failed in fe_compute_melcosine()\n...exiting\n");
	exit(0);
    }
    
    
    for (i=0; i<MEL_FB->num_cepstra; i++) {
	freq = 2*(double)M_PI*(double)i/period;
	for (j=0;j< MEL_FB->num_filters;j++)
	    MEL_FB->mel_cosine[i][j] = (float)cos((double)(freq*(j+0.5)));	
    }    

    return(0);
	
}
예제 #2
0
파일: new_fe.c 프로젝트: 10v/cmusphinx
/*********************************************************************
   FUNCTION: fe_process_utt
   PARAMETERS: fe_t *FE, int16 *spch, int32 nsamps, float **cep
   RETURNS: number of frames of cepstra computed 
   DESCRIPTION: processes the given speech data and returns
   features. will prepend overflow data from last call and store new
   overflow data within the FE
**********************************************************************/
int32 fe_process_utt(fe_t *FE, int16 *spch, int32 nsamps, float32 ***cep_block)	/* RAH, upgraded cep_block to float32 */
{
    int32 frame_start, frame_count=0, whichframe=0;
    int32 i, spbuf_len, offset=0;  
    double *spbuf, *fr_data, *fr_fea;
    int16 *tmp_spch = spch;
    float32 **cep=NULL;
    
    /* are there enough samples to make at least 1 frame? */
    if (nsamps+FE->NUM_OVERFLOW_SAMPS >= FE->FRAME_SIZE){
      
      /* if there are previous samples, pre-pend them to input speech samps */
      if ((FE->NUM_OVERFLOW_SAMPS > 0)) {
	
	if ((tmp_spch = (int16 *) malloc (sizeof(int16)*(FE->NUM_OVERFLOW_SAMPS +nsamps)))==NULL){
	    fprintf(stderr,"memory alloc failed in fe_process_utt()\n...exiting\n");
	    exit(0);
	}
	/* RAH */
	memcpy (tmp_spch,FE->OVERFLOW_SAMPS,FE->NUM_OVERFLOW_SAMPS*(sizeof(int16))); /* RAH */
	memcpy(tmp_spch+FE->NUM_OVERFLOW_SAMPS, spch, nsamps*(sizeof(int16))); /* RAH */
	/* memcpy(FE->OVERFLOW_SAMPS + FE->NUM_OVERFLOW_SAMPS, spch, nsamps*(sizeof(int16))); */ /*  */
	/* spch = FE->OVERFLOW_SAMPS; */ /*  */
	nsamps += FE->NUM_OVERFLOW_SAMPS;
	FE->NUM_OVERFLOW_SAMPS = 0; /*reset overflow samps count */
      }
      /* compute how many complete frames  can be processed and which samples correspond to those samps */
      frame_count=0;
      for (frame_start=0; frame_start+FE->FRAME_SIZE <= nsamps; frame_start += FE->FRAME_SHIFT)
	frame_count++;


      /*      if (cep!=NULL) fe_free_2d((void**)cep); */ /* It should never not be NULL */
      /* 01.14.01 RAH, added +1 Adding one gives us space to stick the last flushed buffer*/
      if ((cep = (float32 **)fe_create_2d(frame_count+1,FE->NUM_CEPSTRA,sizeof(float32))) == NULL) {
	fprintf(stderr,"memory alloc for cep failed in fe_process_utt()\n\tfe_create_2d(%ld,%d,%d)\n...exiting\n",(long int) (frame_count+1),FE->NUM_CEPSTRA,sizeof(float32));  /* typecast to make the compiler happy - EBG */
	exit(0);
      }


      spbuf_len = (frame_count-1)*FE->FRAME_SHIFT + FE->FRAME_SIZE;    
      /* assert(spbuf_len <= nsamps);*/
      if ((spbuf=(double *)calloc(spbuf_len, sizeof(double)))==NULL){
	  fprintf(stderr,"memory alloc failed in fe_process_utt()\n...exiting\n");
	  exit(0);
      }
      
      /* pre-emphasis if needed,convert from int16 to double */
      if (FE->PRE_EMPHASIS_ALPHA != 0.0){
	fe_pre_emphasis(tmp_spch, spbuf, spbuf_len, FE->PRE_EMPHASIS_ALPHA, FE->PRIOR);
      } else{
	fe_short_to_double(tmp_spch, spbuf, spbuf_len);
      }
      
      /* frame based processing - let's make some cepstra... */    
      fr_data = (double *)calloc(FE->FRAME_SIZE, sizeof(double));
      fr_fea = (double *)calloc(FE->NUM_CEPSTRA, sizeof(double));
      
      if (fr_data==NULL || fr_fea==NULL){
	  fprintf(stderr,"memory alloc failed in fe_process_utt()\n...exiting\n");
	  exit(0);
      }

      for (whichframe=0;whichframe<frame_count;whichframe++){
	for (i=0;i<FE->FRAME_SIZE;i++)
	  fr_data[i] = spbuf[whichframe*FE->FRAME_SHIFT + i];
	
	fe_hamming_window(fr_data, FE->HAMMING_WINDOW, FE->FRAME_SIZE);
	
	fe_frame_to_fea(FE, fr_data, fr_fea);
	
	for (i=0;i<FE->NUM_CEPSTRA;i++)
	  cep[whichframe][i] = (float32)fr_fea[i];
      }
      /* done making cepstra */
      
      
      /* assign samples which don't fill an entire frame to FE overflow buffer for use on next pass */
      if (spbuf_len < nsamps)	{
	offset = ((frame_count)*FE->FRAME_SHIFT);
	memcpy(FE->OVERFLOW_SAMPS,tmp_spch+offset,(nsamps-offset)*sizeof(int16));
	FE->NUM_OVERFLOW_SAMPS = nsamps-offset;
	FE->PRIOR = tmp_spch[offset-1];
	assert(FE->NUM_OVERFLOW_SAMPS<FE->FRAME_SIZE);
      }
      
      if (spch != tmp_spch) 
	free (tmp_spch);
      
      free(spbuf);
      free(fr_data);
      free(fr_fea);
    }
    
    /* if not enough total samps for a single frame, append new samps to
       previously stored overlap samples */
    else { 
      memcpy(FE->OVERFLOW_SAMPS+FE->NUM_OVERFLOW_SAMPS,tmp_spch, nsamps*(sizeof(int16)));
      FE->NUM_OVERFLOW_SAMPS += nsamps;
      assert(FE->NUM_OVERFLOW_SAMPS < FE->FRAME_SIZE);
      frame_count=0;
    }

    *cep_block = cep; /* MLS */
    return frame_count;
}
예제 #3
0
int32 fe_build_melfilters(melfb_t *MEL_FB)
{    
    int32 i, whichfilt, start_pt;
    float leftfr, centerfr, rightfr, fwidth, height, *filt_edge;
    float melmax, melmin, dmelbw, freq, dfreq, leftslope,rightslope;

    /*estimate filter coefficients*/
    MEL_FB->filter_coeffs = (float **)fe_create_2d(MEL_FB->num_filters, MEL_FB->fft_size, sizeof(float));
    MEL_FB->left_apex = (float *) calloc(MEL_FB->num_filters,sizeof(float));
    MEL_FB->width = (int32 *) calloc(MEL_FB->num_filters,sizeof(int32));
    
    if (MEL_FB->doublewide==ON)
	filt_edge = (float32 *) calloc(MEL_FB->num_filters+4,sizeof(float32));
    else	
	filt_edge = (float32 *) calloc(MEL_FB->num_filters+2,sizeof(float32));

    if (MEL_FB->filter_coeffs==NULL || MEL_FB->left_apex==NULL || MEL_FB->width==NULL || filt_edge==NULL){
	fprintf(stderr,"memory alloc failed in fe_build_mel_filters()\n...exiting\n");
	exit(0);
    }
    
    dfreq = MEL_FB->sampling_rate/(float)MEL_FB->fft_size;
    
    melmax = fe_mel(MEL_FB->upper_filt_freq);
    melmin = fe_mel(MEL_FB->lower_filt_freq);
    dmelbw = (melmax-melmin)/(MEL_FB->num_filters+1);
    
    if (MEL_FB->doublewide==ON){
        for (i=0;i<=MEL_FB->num_filters+3; ++i){
	    filt_edge[i] = fe_melinv(i*dmelbw + melmin);
        }
    }
    else {
	for (i=0;i<=MEL_FB->num_filters+1; ++i){
	    filt_edge[i] = fe_melinv(i*dmelbw + melmin);   
	}
    }
    
    

    for (whichfilt=0;whichfilt<MEL_FB->num_filters; ++whichfilt) {
      /*line triangle edges up with nearest dft points... */

      if (MEL_FB->doublewide==ON){
	leftfr   = (float)((int32)((filt_edge[whichfilt]/dfreq)+0.5))*dfreq;
	centerfr = (float)((int32)((filt_edge[whichfilt+2]/dfreq)+0.5))*dfreq;
	rightfr  = (float)((int32)((filt_edge[whichfilt+4]/dfreq)+0.5))*dfreq; 
      }else{
	leftfr   = (float)((int32)((filt_edge[whichfilt]/dfreq)+0.5))*dfreq;
	centerfr = (float)((int32)((filt_edge[whichfilt+1]/dfreq)+0.5))*dfreq;
	rightfr  = (float)((int32)((filt_edge[whichfilt+2]/dfreq)+0.5))*dfreq;
      }
      
      
      
      MEL_FB->left_apex[whichfilt] = leftfr;
      
      fwidth = rightfr - leftfr;
      
      /* 2/fwidth for triangles of area 1 */
      height = 2/(float)fwidth;
      if (centerfr != leftfr) {
        leftslope = height/(centerfr-leftfr);
      }
      if (centerfr != rightfr) {
        rightslope = height/(centerfr-rightfr);
      }
      /* Round to the nearest integer instead of truncating and adding
	 one, which breaks if the divide is already an integer */      
      start_pt = (int32)(leftfr/dfreq + 0.5);
      freq = (float32)start_pt*dfreq;
      i=0;
      
      while (freq<centerfr){
	MEL_FB->filter_coeffs[whichfilt][i] = (freq-leftfr)*leftslope;	    
//      fprintf (stderr, "         MEL_FB->filter_coeffs[%d][%d]=%14f\n", whichfilt,i,MEL_FB->filter_coeffs[whichfilt][i]);
	freq += dfreq;
	i++;
      }
      /* If the two floats are equal, the leftslope computation above
	 results in Inf, so we handle the case here. */
      if (freq==centerfr){
	MEL_FB->filter_coeffs[whichfilt][i] = height;	    
	freq += dfreq;
	i++;
      }
      while (freq<rightfr){
	MEL_FB->filter_coeffs[whichfilt][i] = (freq-rightfr)*rightslope;
//      fprintf (stderr, "     ....MEL_FB->filter_coeffs[%d][%d]=%14f\n", whichfilt,i,MEL_FB->filter_coeffs[whichfilt][i]);
	freq += dfreq;
	i++;
      }
      
      MEL_FB->width[whichfilt] = i;
    }
    
    free(filt_edge);
    return(0);
}
예제 #4
0
/*********************************************************************
   FUNCTION: fe_process_utt
   PARAMETERS: fe_t *FE, int16 *spch, int32 nsamps, mfcc_t **cep, int32 nframes
   RETURNS: status, successful or not
   DESCRIPTION: processes the given speech data and returns
   features. will prepend overflow data from last call and store new
   overflow data within the FE
**********************************************************************/
int32
fe_process_utt(fe_t * FE, int16 * spch, int32 nsamps,
               mfcc_t *** cep_block, int32 * nframes)
{
    int32 frame_start, frame_count = 0, whichframe = 0;
    int32 i, spbuf_len, offset = 0;
    frame_t *spbuf, *fr_data;
    int16 *tmp_spch = spch;
    mfcc_t **cep = NULL;
    int32 return_value = FE_SUCCESS;
    int32 frame_return_value;

    /* Added byte-swapping for Endian-ness compatibility */
    if (FE->swap) 
        for (i = 0; i < nsamps; i++)
            SWAP_INT16(&spch[i]);

    /* are there enough samples to make at least 1 frame? */
    if (nsamps + FE->NUM_OVERFLOW_SAMPS >= FE->FRAME_SIZE) {

        /* if there are previous samples, pre-pend them to input speech samps */
        if ((FE->NUM_OVERFLOW_SAMPS > 0)) {

            if ((tmp_spch =
                 (int16 *) malloc(sizeof(int16) *
                                  (FE->NUM_OVERFLOW_SAMPS +
                                   nsamps))) == NULL) {
                E_WARN("memory alloc failed in fe_process_utt()\n");
                return FE_MEM_ALLOC_ERROR;
            }
            /* RAH */
            memcpy(tmp_spch, FE->OVERFLOW_SAMPS, FE->NUM_OVERFLOW_SAMPS * (sizeof(int16)));     /* RAH */
            memcpy(tmp_spch + FE->NUM_OVERFLOW_SAMPS, spch, nsamps * (sizeof(int16)));  /* RAH */
            nsamps += FE->NUM_OVERFLOW_SAMPS;
            FE->NUM_OVERFLOW_SAMPS = 0; /*reset overflow samps count */
        }
        /* compute how many complete frames  can be processed and which samples correspond to those samps */
        frame_count = 0;
        for (frame_start = 0;
             frame_start + FE->FRAME_SIZE <= nsamps;
             frame_start += FE->FRAME_SHIFT)
            frame_count++;


        if ((cep =
             (mfcc_t **) fe_create_2d(frame_count + 1,
                                      FE->FEATURE_DIMENSION,
                                      sizeof(mfcc_t))) == NULL) {
            E_WARN
                ("memory alloc for cep failed in fe_process_utt()\n\tfe_create_2d(%ld,%d,%d)\n",
                 (long int) (frame_count + 1),
                 FE->FEATURE_DIMENSION, sizeof(mfcc_t));
            return (FE_MEM_ALLOC_ERROR);
        }
        spbuf_len = (frame_count - 1) * FE->FRAME_SHIFT + FE->FRAME_SIZE;

        if ((spbuf =
             (frame_t *) calloc(spbuf_len, sizeof(frame_t))) == NULL) {
            E_WARN("memory alloc failed in fe_process_utt()\n");
            return (FE_MEM_ALLOC_ERROR);
        }

        /* Add dither, if requested */
        if (FE->dither) {
            fe_dither(tmp_spch, spbuf_len);
        }

        /* pre-emphasis if needed, convert from int16 to float64 */
        if (FE->PRE_EMPHASIS_ALPHA != 0.0) {
            fe_pre_emphasis(tmp_spch, spbuf, spbuf_len,
                            FE->PRE_EMPHASIS_ALPHA, FE->PRIOR);
        }
        else {
            fe_short_to_frame(tmp_spch, spbuf, spbuf_len);
        }

        /* frame based processing - let's make some cepstra... */
        fr_data = (frame_t *) calloc(FE->FRAME_SIZE, sizeof(frame_t));
        if (fr_data == NULL) {
            E_WARN("memory alloc failed in fe_process_utt()\n");
            return (FE_MEM_ALLOC_ERROR);
        }

        for (whichframe = 0; whichframe < frame_count; whichframe++) {

            for (i = 0; i < FE->FRAME_SIZE; i++)
                fr_data[i] = spbuf[whichframe * FE->FRAME_SHIFT + i];

            fe_hamming_window(fr_data, FE->HAMMING_WINDOW, FE->FRAME_SIZE, FE->remove_dc);

            frame_return_value =
                fe_frame_to_fea(FE, fr_data, cep[whichframe]);

            if (FE_SUCCESS != frame_return_value) {
                return_value = frame_return_value;
            }
        }
        /* done making cepstra */


        /* assign samples which don't fill an entire frame to FE overflow buffer for use on next pass */
        if ((offset = ((frame_count) * FE->FRAME_SHIFT)) < nsamps) {
            memcpy(FE->OVERFLOW_SAMPS, tmp_spch + offset,
                   (nsamps - offset) * sizeof(int16));
            FE->NUM_OVERFLOW_SAMPS = nsamps - offset;
            FE->PRIOR = tmp_spch[offset - 1];
            assert(FE->NUM_OVERFLOW_SAMPS < FE->FRAME_SIZE);
        }

        if (spch != tmp_spch)
            free(tmp_spch);

        free(spbuf);
        free(fr_data);
    }

    /* if not enough total samps for a single frame, append new samps to
       previously stored overlap samples */
    else {
        memcpy(FE->OVERFLOW_SAMPS + FE->NUM_OVERFLOW_SAMPS,
               tmp_spch, nsamps * (sizeof(int16)));
        FE->NUM_OVERFLOW_SAMPS += nsamps;
        assert(FE->NUM_OVERFLOW_SAMPS < FE->FRAME_SIZE);
        frame_count = 0;
    }

    *cep_block = cep;           /* MLS */
    *nframes = frame_count;
    return return_value;
}