www.pudn.com > ETSI_ES_202_212_software.rar > WaveProc.c


/*===============================================================================
 *      ETSI ES 202 212   Distributed Speech Recognition
 *      Extended Advanced Front-End Feature Extraction Algorithm & Compression Algorithm
 *      Speech Reconstruction Algorithm.
 *      C-language software implementation                                      
 *      Version 1.1.1   October, 2003                                            
 *===============================================================================*/
/*-------------------------------------------------------------------------------
 *
 * FILE NAME: WaveProc.c
 * PURPOSE: Apply SNR-dependent Waveform Processing on the de-noised frame.
 *
 *-------------------------------------------------------------------------------*/
/*-----------------
 * File Inclusions
 *-----------------*/
#include 
#include 
#include 

#include "ParmInterface.h"
#include "WaveProcExports.h"

/*------------------------
 * Definitions and Macros
 *------------------------*/
#define WP_EPS                0.2
#define WP_PERCENT_WIDTH_PAR 80

struct WaveProcStructX
{
  X_INT16	FrameLength;
  X_INT16   perCentWidthPar;
  X_FLOAT32 eps;
};

/*------------
 * Prototypes
 *------------*/
static void    sort_it (X_INT16 *MaxPos, X_INT16 NumOfMax);
static void    TeagerEng (X_FLOAT32 *TeagerWindow, WaveProcStructX *WPX, X_FLOAT32 *Data);
static void    GetTeagerFilter (X_FLOAT32 *TeagerWindow, WaveProcStructX *WPX, X_INT16 *MaxPos);
static X_INT16 GetMaximaPositions (X_INT32 *teager, X_INT16 *MaxPos, X_INT16 Length);

/*-----------
 * Functions
 *-----------*/
/*----------------------------------------------------------------------------
 * FUNCTION NAME: sort_it
 *
 * PURPOSE: Sort positions of maxima
 *
 * INPUT:
 *  MaxPos         Pointer to positions of maxima
 *  NumOfMax       Number of maxima
 *
 * OUTPUT:
 *  Sorted maxima positions in MaxPos
 *
 * RETURN VALUE:
 *   none
 *
 *---------------------------------------------------------------------------*/
static void sort_it (X_INT16 *MaxPos, X_INT16 NumOfMax)
{
  X_INT16 ii;
  X_INT16 jj;
  X_INT16 jj_aux = 0;
  X_INT16 max_aux[10];
  
  for (ii=0 ; ii max_aux[ii])
		  {
			max_aux[ii] = MaxPos[jj];
			jj_aux = jj;
		  }
      MaxPos[jj_aux] = 0;
    }

  for (ii=0 ; ii teager_max)
		{
		  maxFound = 1;
		  teager_max = teager[i];
		  maxR[0] = i;
		  maxL[0] = i;
		}
    }

  counterR = 0;
  counterL = 0;

  if (maxFound == 1)
	{
	  while (((maxR[counterR] + 25) < Length) && (maxFound == 1))
		{
		  teager_max = 0;
		  maxFound = 0;
		  for (i=25 ; i<80 ; i++)
			if ((maxR[counterR] + i ) < Length)
			  if (teager[maxR[counterR] + i] >= teager_max)
				{
				  maxFound = 1;
				  teager_max = teager[maxR[counterR] + i];
				  maxR[counterR+1] = maxR[counterR]+i;
				}
		  if (maxFound == 1) counterR++;
		}

	  maxFound = 1;
	  while (((maxL[counterL]-25) > 0) && (maxFound == 1))
		{
		  teager_max = 0;
		  maxFound = 0;
		  for (i=25 ; i<80 ; i++)
			if ((maxL[counterL]-i) > -1)
			  if (teager[maxL[counterL]-i] >= teager_max)
				{
				  maxFound = 1;
				  teager_max = teager[maxL[counterL]-i];
				  maxL[counterL+1] = maxL[counterL]-i;
				}
		  if (maxFound == 1) counterL++;
		}
	  for (i=0 ; i<=counterR ; i++)
		MaxPos[i] = maxR[i];
	  for (i=(counterR+1) ; i<=(counterR+counterL) ; i++)
		MaxPos[i] = maxL[i-counterR];

	  sort_it (MaxPos, counterR+counterL+1);

	  return (counterR+counterL+1);
	}
  else 
	{
	  return (0);
	}
}

/*----------------------------------------------------------------------------
 * FUNCTION NAME: TeagerEng
 *
 * PURPOSE:  Compute Teager energy from input data (time domain)
 *           engT[t] = data[t]*data[t]-data[t-1]*data[t+1]     
 *
 * INPUT:
 *  TeagerWindow       Pointer to output Teager energy
 *  WPX                Pointer to waveform processing data stuct
 *  Data               Pointer to input waveform
 * 
 * OUTPUT:
 *  Teager energy is stored in TeagerWindow
 *
 * RETURN VALUE:
 *  none
 *
 *---------------------------------------------------------------------------*/
static void TeagerEng (X_FLOAT32 *TeagerWindow, WaveProcStructX *WPX, X_FLOAT32 *Data)
{
  X_INT16 i;
  X_INT16 fLength = WPX->FrameLength;

  TeagerWindow[0] = fabs (Data[0] * Data[0] - Data[0] * Data[1]);

  for (i=1 ; iperCentWidthPar;
  X_INT16 FrameLength = WPX->FrameLength;

  X_INT32 teagerInt[9];
  X_INT32 teagerSmoothed[200];

  X_FLOAT32 lowVal;
  X_FLOAT32 highVal;

  /*----------------------------
   * smoothing of Teager energy
   *----------------------------*/
  #define FactIntTeager 1.0	

  teagerInt[4] = (int) floor (TeagerWindow[0] * FactIntTeager + 0.5);
  teagerInt[0] = teagerInt[1] = teagerInt[2] = teagerInt[3] = teagerInt[4];
  for (i=1 ; i<5 ; i++)
	{
	  teagerInt[i+4] = (int) floor (TeagerWindow[i] * FactIntTeager + 0.5);
	}
  teagerSmoothed[0] = teagerInt[0] + teagerInt[1] + teagerInt[2] + teagerInt[3] + teagerInt[4]
	+ teagerInt[5] + teagerInt[6] + teagerInt[7] + teagerInt[8];
 
  for (i=1,io=0 ; ieps) / 2.0;
  highVal = (1 + WPX->eps) / 2.0;

  for (i=0 ; i 1)
	{
	  for (i=0 ; i<(NoM-1) ; i++)
		for (j=(MaxPos[i]-4) ; j<((MaxPos[i]-4)+((pWP*(MaxPos[i+1]-MaxPos[i])+99)/100)) ; j++)
		  if (j>=0) TeagerWindow[j] = highVal;

	  for (j=(MaxPos[NoM-1]-4) ; j<((MaxPos[NoM-1]-4)+((pWP*(MaxPos[NoM-1] - MaxPos[NoM-2])+99)/100)) ; j++)
		if (jWPX;

  WPX->eps = WP_EPS;
  WPX->FrameLength = This->FrameLength;
  WPX->perCentWidthPar = WP_PERCENT_WIDTH_PAR;
}

/*----------------------------------------------------------------------------
 * FUNCTION NAME: DoWaveProcDelete
 *
 * PURPOSE:
 *              
 *
 * INPUT:
 *        
 *
 * OUTPUT:
 *
 *
 * RETURN VALUE:
 *   
 *
 *---------------------------------------------------------------------------*/
extern void DoWaveProcDelete (WaveProcStructX *WPX)
{
  if (WPX != NULL)
    free (WPX);
}

/*----------------------------------------------------------------------------
 * FUNCTION NAME: DoWaveProc
 *
 * PURPOSE: Performs waveform processing on input waveform
 *              
 *
 * INPUT:
 *  Data      Pointer to input waveform
 *  This      Pointer to front-end data struct
 *
 * OUTPUT:
 *  Processed waveform is in Data
 *
 * RETURN VALUE:
 *   True (boolean)
 *
 *---------------------------------------------------------------------------*/
extern BOOLEAN DoWaveProc (X_FLOAT32 *Data, FEParamsX *This)
{
  WaveProcStructX *WPX = This->WPX;

  X_INT16 i;
  X_INT16 MaxPos[10] = {0,0,0,0,0,0,0,0,0,0};

  X_FLOAT32 Win;
  X_FLOAT32 CurWin;
  X_FLOAT32 NxtWin;
  X_FLOAT32 Energy;
  X_FLOAT32 *TeagerWindow;

  /*------------------
   * low energy check
   *------------------*/
  Energy = 0.0;
  for (i=0; iFrameLength ; i++) Energy += (Data[i] * Data[i]);

  if (Energy >= 100.0)
	{      
	  /*-------------------
	   * memory allocation
	   *-------------------*/
	  TeagerWindow = (X_FLOAT32 *) malloc (sizeof (X_FLOAT32) * WPX->FrameLength);
	  if (TeagerWindow == NULL)
		{
		  fprintf (stderr, "ERROR:   Memory allocation error occured!\r\n");
		  exit(0);
		}

	  /*-----------------------
	   * compute Teager energy
	   *-----------------------*/
	  TeagerEng (TeagerWindow, WPX, Data);
  
	  /*-------------------
	   * get Teager filter
	   *-------------------*/
	  GetTeagerFilter (TeagerWindow, WPX, MaxPos);
  
	  /*---------------------
	   * apply Teager filter
	   *---------------------*/
	  CurWin = TeagerWindow[0];

	  for (i=0 ; iFrameLength-1 ; i++)
		{
		  NxtWin = TeagerWindow[i+1];
		  Win = CurWin + NxtWin;
		  Data [i] *= Win;
		  CurWin = NxtWin;
		}
		
	  NxtWin = TeagerWindow[i];
	  Win = CurWin + NxtWin;
	  Data [i] *= Win;

	  free (TeagerWindow);
	}
  return TRUE;
}