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.

111 lines
3.4 KiB

  1. /******************************************************************************
  2. * nid+.cpp *
  3. *---------*
  4. * I/O library functions for extended speech files (vapi format)
  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 FILTER_ORDER 40 /* Filter length 2*N+1 */
  13. /*****************************************************************************
  14. * sig *
  15. *----------*
  16. * Description:
  17. * Implements (-1)^(M-n). It might be better to implement it with
  18. * a macro. M is the filter order, so this function is simmetrical from M.
  19. ******************************************************************* PACOG ***/
  20. static double Sign (int n)
  21. {
  22. if ((n-FILTER_ORDER)%2)
  23. {
  24. return -1.0;
  25. }
  26. return 1.0;
  27. }
  28. /*****************************************************************************
  29. * NonIntegerDelay *
  30. *-----------------*
  31. * Description:
  32. * Introduces a non integer delay by sinc interpolation.
  33. * The filter is simply truncated. Better response could be achieved by
  34. * using a different windowing function. So far, the response is adjusted
  35. * augmenting the number of points used in the interpolation.
  36. *
  37. * dDelay: 0<dDelay<1, fraction of a sampling period. No negative values
  38. * are allowed.
  39. ******************************************************************* PACOG ***/
  40. int NonIntegerDelay (double* pdSamples, int iNumSamples, double dDelay)
  41. {
  42. double coefs[2*FILTER_ORDER+1];
  43. double* sampTemp;
  44. double constant;
  45. double acum;
  46. int firstCoef;
  47. int lastCoef;
  48. int i;
  49. int j;
  50. if (pdSamples && iNumSamples>0 && dDelay != 0.0 && fabs (dDelay) <1.0 )
  51. {
  52. //-- Allocate a temporary buffer to hold the pdSamples to filter.
  53. // The filtered samples go directly into the original buffer.
  54. if ( (sampTemp = new double [iNumSamples + 2*FILTER_ORDER+1]) == 0)
  55. {
  56. return 0;
  57. }
  58. memset (sampTemp, 0, sizeof(double)* FILTER_ORDER);
  59. memcpy (sampTemp+FILTER_ORDER, pdSamples, iNumSamples * sizeof (*pdSamples));
  60. memset (sampTemp+iNumSamples+FILTER_ORDER, 0, sizeof(double)* FILTER_ORDER);
  61. // Set up the filter. The amplitude constant does not depend
  62. // on the sign of dDelay (sin(-x)= -sin(x))
  63. constant= fabs( sin(M_PI*dDelay)/M_PI );
  64. // Without windowing, the coefs are samples of abs(1/x)
  65. coefs[FILTER_ORDER]=1.0/fabs(dDelay);
  66. if (dDelay>0.0)
  67. {
  68. for (j=1;j<=FILTER_ORDER;j++)
  69. {
  70. coefs[FILTER_ORDER+j] = -Sign(j) / (j-(double)dDelay);
  71. coefs[FILTER_ORDER-j] = Sign(j) / (j+(double)dDelay);
  72. }
  73. firstCoef=1;
  74. lastCoef=2*FILTER_ORDER;
  75. }
  76. else
  77. {
  78. for (j=1;j<=FILTER_ORDER;j++)
  79. {
  80. coefs[FILTER_ORDER-j] = -Sign(j) / (j+(double)dDelay);
  81. coefs[FILTER_ORDER+j] = Sign(j) / (j-(double)dDelay);
  82. }
  83. firstCoef = 0;
  84. lastCoef = 2*FILTER_ORDER-1;
  85. }
  86. for (i=0;i<iNumSamples;i++)
  87. {
  88. acum = 0.0;
  89. for (j=firstCoef; j<=lastCoef; j++ )
  90. {
  91. acum += sampTemp[i+j] * coefs[j];
  92. }
  93. pdSamples[i]=constant*acum;
  94. }
  95. delete[] sampTemp;
  96. }
  97. return 1;
  98. }