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.

272 lines
6.6 KiB

  1. /******************************************************************************
  2. * window.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. static double* Rectangular (int iLength);
  13. static double* Bartlett (int iLength, bool bSymmetric);
  14. static double* Hanning (int iLength, bool bSymmetric);
  15. static double* Hamming (int iLength, bool bSymmetric);
  16. static double* Blackman (int iLength, bool bSymmetric);
  17. /*****************************************************************************
  18. * WindowSignal *
  19. *--------------*
  20. * Description:
  21. * Multiplies the input signal by a window. The window length is
  22. * the length of the vector. The window is symmetrical if symmetry != 0.
  23. * Parameters:
  24. * samples: Vector to input data. It is modified by the routine.
  25. * type: Type of window, must be one of the types defined in lpc.h
  26. ******************************************************************* PACOG ***/
  27. int WindowSignal (double* pdSamples, int iNumSamples, int iWindType, bool bSymmetric)
  28. {
  29. double* pdWindow;
  30. assert (pdSamples);
  31. assert (iNumSamples>0);
  32. if ( (pdWindow = ComputeWindow(iWindType, iNumSamples, bSymmetric)) == 0)
  33. {
  34. return 0;
  35. }
  36. for (int i=0; i<iNumSamples; i++)
  37. {
  38. pdSamples[i] *= pdWindow[i];
  39. }
  40. delete[] pdWindow;
  41. return 1;
  42. }
  43. /*****************************************************************************
  44. * ComputeWindow *
  45. *---------------*
  46. * Description:
  47. * This is a dispatch function, calling the appropiate function
  48. * to compute every different type of window.
  49. * Parameters:
  50. * Window type and length
  51. * Returns:
  52. * A vector containing the computed window.
  53. ******************************************************************* PACOG ***/
  54. double* ComputeWindow (int iWindType, int iWindLen, bool bSymmetric)
  55. {
  56. double* pdWind = NULL;
  57. assert (iWindLen>0);
  58. if (iWindLen<=0)
  59. {
  60. //fprintf (stderr, "Zero length window requested in ComputeWindow");
  61. return 0;
  62. }
  63. switch (iWindType)
  64. {
  65. case WINDOW_RECT:
  66. pdWind = Rectangular (iWindLen);
  67. break;
  68. case WINDOW_BART:
  69. pdWind = Bartlett (iWindLen, bSymmetric);
  70. break;
  71. case WINDOW_HANN:
  72. pdWind = Hanning (iWindLen, bSymmetric);
  73. break;
  74. case WINDOW_HAMM:
  75. pdWind = Hamming (iWindLen, bSymmetric);
  76. break;
  77. case WINDOW_BLACK:
  78. pdWind = Blackman (iWindLen, bSymmetric);
  79. break;
  80. default:
  81. //fprintf(stderr, "Window type not known");
  82. return 0;
  83. }
  84. return pdWind;
  85. }
  86. /*****************************************************************************
  87. * Rectangular *
  88. *-------------*
  89. * Description:
  90. * Returns a unit vector of the specified length.
  91. ******************************************************************* PACOG ***/
  92. double* Rectangular (int iLength)
  93. {
  94. double* pdWindow;
  95. if ((pdWindow = new double[iLength]) != 0)
  96. {
  97. for (int i=0; i<iLength; i++)
  98. {
  99. pdWindow[i]=1.0;
  100. }
  101. }
  102. return pdWindow;
  103. }
  104. /*****************************************************************************
  105. * Bartlett *
  106. *----------*
  107. * Description:
  108. * Returns a vector with a Bartlett window of the specified length.
  109. ******************************************************************* PACOG ***/
  110. double* Bartlett (int iLength, bool bSymmetric)
  111. {
  112. double* pdWindow;
  113. double dStep;
  114. if ((pdWindow = new double[iLength]) != 0)
  115. {
  116. if (bSymmetric)
  117. {
  118. dStep=2.0/(iLength-1);
  119. }
  120. else
  121. {
  122. dStep=2.0/iLength;
  123. }
  124. for (int i=0; i<iLength/2; i++)
  125. {
  126. pdWindow[i] = i * dStep;
  127. pdWindow[iLength/2+i] = 1.0 - i * dStep;
  128. }
  129. if (iLength %2)
  130. {
  131. if (bSymmetric)
  132. {
  133. pdWindow[iLength-1] = pdWindow[0];
  134. }
  135. else
  136. {
  137. pdWindow[iLength-1] = pdWindow[1];
  138. }
  139. }
  140. }
  141. return pdWindow;
  142. }
  143. /*****************************************************************************
  144. * Hamming *
  145. *---------*
  146. * Description:
  147. * Returns a vector with a Hamming window of the specified length.
  148. ******************************************************************* PACOG ***/
  149. double* Hamming (int iLength, bool bSymmetric)
  150. {
  151. double* pdWindow;
  152. double dArg;
  153. if ((pdWindow = new double[iLength]) != 0)
  154. {
  155. dArg=2.0*M_PI;
  156. if (bSymmetric)
  157. {
  158. dArg/=(double)(iLength - 1);
  159. }
  160. else
  161. {
  162. dArg/=(double)iLength;
  163. }
  164. for (int i=0; i<iLength; i++)
  165. {
  166. pdWindow[i]=0.54 - (0.46*cos(dArg*(double)i));
  167. }
  168. }
  169. return pdWindow;
  170. }
  171. /*****************************************************************************
  172. * Hanning *
  173. *---------*
  174. * Description:
  175. * Returns a vector with a Hamming window of the specified length.
  176. ******************************************************************* PACOG ***/
  177. double* Hanning (int iLength, bool bSymmetric)
  178. {
  179. double* pdWindow;
  180. double dArg;
  181. if ((pdWindow = new double[iLength]) != 0)
  182. {
  183. dArg=2.0*M_PI;
  184. if (bSymmetric)
  185. {
  186. dArg/=(double)(iLength - 1);
  187. }
  188. else
  189. {
  190. dArg/=(double)iLength;
  191. }
  192. for (int i=0; i<iLength; i++)
  193. {
  194. pdWindow[i]=0.5 - (0.5*cos(dArg*(double)i));
  195. }
  196. }
  197. return pdWindow;
  198. }
  199. /*****************************************************************************
  200. * Blackman *
  201. *----------*
  202. * Description:
  203. * Returns a vector with a Blackman window of the specified length.
  204. ******************************************************************* PACOG ***/
  205. double* Blackman (int iLength, bool bSymmetric)
  206. {
  207. double* pdWindow;
  208. double dArg, dArg2;
  209. if ((pdWindow = new double[iLength]) != 0)
  210. {
  211. dArg=2.0*M_PI;
  212. if (bSymmetric)
  213. {
  214. dArg/=(double)(iLength - 1);
  215. }
  216. else
  217. {
  218. dArg/=(double)iLength;
  219. }
  220. dArg2=2.0*dArg;
  221. for (int i=0; i<iLength; i++)
  222. {
  223. pdWindow[i]=0.42 - (0.5*cos(dArg*(double)i)) + (0.08*cos(dArg2*(double)i));
  224. }
  225. }
  226. return pdWindow;
  227. }