Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
4.4 KiB

  1. /******************************************************************************
  2. * sigproc.cpp *
  3. *-------------*
  4. *
  5. *------------------------------------------------------------------------------
  6. * Copyright (C) 1999 Entropic, Inc
  7. * Copyright (C) 2000 Microsoft Corporation Date: 03/02/00
  8. * All Rights Reserved
  9. *
  10. ********************************************************************* PACOG ***/
  11. #include "sigprocInt.h"
  12. #define PREEMP_FACTOR 0.97F
  13. /*****************************************************************************
  14. * Cepstrum *
  15. *----------*
  16. * Description:
  17. * compute lpc ceptrum form lpc coefficients
  18. *
  19. ******************************************************************* PACOG ***/
  20. double* Cepstrum(double *pdData, int iNumData, int iLpcOrder, int iCepOrder)
  21. {
  22. double* pdCepCoef = 0;
  23. double* pdWindow = 0;
  24. double* pdLpcCoef = 0;
  25. assert(iLpcOrder > 0);
  26. assert(iCepOrder > 0);
  27. // get lpc coef
  28. pdWindow = ComputeWindow (WINDOW_HAMM, iNumData, false);
  29. if (!pdWindow)
  30. {
  31. return 0;
  32. }
  33. for (int i=1; i<iNumData; i++)
  34. {
  35. pdWindow[i] *= pdData[i] - PREEMP_FACTOR * pdData[i-1];
  36. }
  37. pdLpcCoef = GetDurbinCoef (pdWindow , iNumData, iLpcOrder, LPC_ALFA, NULL);
  38. if ((pdCepCoef = new double[iCepOrder]) == 0)
  39. {
  40. return 0;
  41. }
  42. Alfa2Cepstrum (pdLpcCoef, iLpcOrder, pdCepCoef, iCepOrder);
  43. delete[] pdWindow;
  44. delete[] pdLpcCoef;
  45. return pdCepCoef;
  46. }
  47. /*****************************************************************************
  48. * Alfa2Cepstrum *
  49. *---------------*
  50. * Description:
  51. * compute lpc cepstrum from lpc coefficients
  52. * a = alfa (lpc) coefs, input
  53. * p = order of lpc analysis
  54. * c = cepstrum coefs, output
  55. * n = order of cepstral analysis
  56. ******************************************************************* PACOG ***/
  57. void Alfa2Cepstrum (double* pdAlfa, int iNumAlfa, double* pdCepstrum, int iNumCepstrum)
  58. {
  59. double dAux;
  60. int k;
  61. pdCepstrum[0] = -pdAlfa[0];
  62. for (int i = 1; i < iNumCepstrum; i++)
  63. {
  64. if (i<iNumAlfa)
  65. {
  66. pdCepstrum[i] = -pdAlfa[i];
  67. }
  68. else
  69. {
  70. pdCepstrum[i] = 0.0;
  71. }
  72. dAux = 0.0;
  73. for (k = 1; k<=i && k<=iNumAlfa; k++)
  74. {
  75. dAux += ((double)(i+1-k)/(double)(i+1)) * pdCepstrum[i-k] * pdAlfa[k-1];
  76. }
  77. pdCepstrum[i] -= dAux;
  78. }
  79. }
  80. /*****************************************************************************
  81. * EuclideanDist *
  82. *---------------*
  83. * Description:
  84. *
  85. ******************************************************************* PACOG ***/
  86. double EuclideanDist (double c1[], double c2[], int iLen)
  87. {
  88. double dDist = 0.0;
  89. for (int i = 0; i < iLen; i++)
  90. {
  91. dDist += (c1[i] - c2[i]) * (c1[i] - c2[i]);
  92. }
  93. return dDist;
  94. }
  95. /*****************************************************************************
  96. * Energy *
  97. *--------*
  98. * Description:
  99. * Compute the time-weighted RMS of a size segment of data. The data
  100. * is weighted by a window of type w_type before RMS computation. w_type
  101. * is decoded above in window().
  102. ******************************************************************* PACOG ***/
  103. double Energy (double* pdData, int iNumData, int iWindType)
  104. {
  105. static int iWindLen = 0;
  106. static double *pdWindow = 0;
  107. double dWData;
  108. double dSum = 0.0;
  109. assert (pdData);
  110. if (iWindLen != iNumData)
  111. {
  112. if (pdWindow)
  113. {
  114. delete[] pdWindow;
  115. }
  116. pdWindow = ComputeWindow (iWindType, iNumData, false);
  117. if (!pdWindow)
  118. {
  119. fprintf (stderr, "Memory error in Energy\n");
  120. return(0.0);
  121. }
  122. iWindLen = iNumData;
  123. }
  124. for (int i=0; i<iNumData; i++)
  125. {
  126. dWData = pdData[i] * pdWindow[i];
  127. dSum += dWData * dWData;
  128. }
  129. return (double)sqrt((double)(dSum/iNumData));
  130. }
  131. /*****************************************************************************
  132. * RemoveDc *
  133. *----------*
  134. * Description:
  135. *
  136. ******************************************************************* PACOG ***/
  137. int RemoveDc (double* pdSamples, int iNumSamples)
  138. {
  139. double dDc = 0.0;
  140. assert (pdSamples);
  141. assert (iNumSamples>0);
  142. for (int i=0; i<iNumSamples; i++)
  143. {
  144. dDc += pdSamples[i];
  145. }
  146. dDc /= iNumSamples;
  147. for (i=0; i<iNumSamples; i++)
  148. {
  149. pdSamples[i] -= dDc;
  150. }
  151. return (int) dDc;
  152. }