flac_window.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* libFLAC - Free Lossless Audio Codec library
  2. * Copyright (C) 2006,2007 Josh Coalson
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * - Neither the name of the Xiph.org Foundation nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  23. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #if HAVE_CONFIG_H
  32. # include <config.h>
  33. #endif
  34. #include <math.h>
  35. #include "flac_FLAC_assert.h"
  36. #include "flac_FLAC_format.h"
  37. #include "flac_private_window.h"
  38. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  39. #ifndef M_PI
  40. /* math.h in VC++ doesn't seem to have this (how Microsoft is that?) */
  41. #define M_PI 3.14159265358979323846
  42. #endif
  43. void FLAC__window_bartlett(FLAC__real *window, const FLAC__int32 L)
  44. {
  45. const FLAC__int32 N = L - 1;
  46. FLAC__int32 n;
  47. if (L & 1) {
  48. for (n = 0; n <= N/2; n++)
  49. window[n] = 2.0f * n / (float)N;
  50. for (; n <= N; n++)
  51. window[n] = 2.0f - 2.0f * n / (float)N;
  52. }
  53. else {
  54. for (n = 0; n <= L/2-1; n++)
  55. window[n] = 2.0f * n / (float)N;
  56. for (; n <= N; n++)
  57. window[n] = 2.0f - 2.0f * (N-n) / (float)N;
  58. }
  59. }
  60. void FLAC__window_bartlett_hann(FLAC__real *window, const FLAC__int32 L)
  61. {
  62. const FLAC__int32 N = L - 1;
  63. FLAC__int32 n;
  64. for (n = 0; n < L; n++)
  65. window[n] = (FLAC__real)(0.62f - 0.48f * fabs((float)n/(float)N+0.5f) + 0.38f * cos(2.0f * M_PI * ((float)n/(float)N+0.5f)));
  66. }
  67. void FLAC__window_blackman(FLAC__real *window, const FLAC__int32 L)
  68. {
  69. const FLAC__int32 N = L - 1;
  70. FLAC__int32 n;
  71. for (n = 0; n < L; n++)
  72. window[n] = (FLAC__real)(0.42f - 0.5f * cos(2.0f * M_PI * n / N) + 0.08f * cos(4.0f * M_PI * n / N));
  73. }
  74. /* 4-term -92dB side-lobe */
  75. void FLAC__window_blackman_harris_4term_92db_sidelobe(FLAC__real *window, const FLAC__int32 L)
  76. {
  77. const FLAC__int32 N = L - 1;
  78. FLAC__int32 n;
  79. for (n = 0; n <= N; n++)
  80. window[n] = (FLAC__real)(0.35875f - 0.48829f * cos(2.0f * M_PI * n / N) + 0.14128f * cos(4.0f * M_PI * n / N) - 0.01168f * cos(6.0f * M_PI * n / N));
  81. }
  82. void FLAC__window_connes(FLAC__real *window, const FLAC__int32 L)
  83. {
  84. const FLAC__int32 N = L - 1;
  85. const double N2 = (double)N / 2.;
  86. FLAC__int32 n;
  87. for (n = 0; n <= N; n++) {
  88. double k = ((double)n - N2) / N2;
  89. k = 1.0f - k * k;
  90. window[n] = (FLAC__real)(k * k);
  91. }
  92. }
  93. void FLAC__window_flattop(FLAC__real *window, const FLAC__int32 L)
  94. {
  95. const FLAC__int32 N = L - 1;
  96. FLAC__int32 n;
  97. for (n = 0; n < L; n++)
  98. window[n] = (FLAC__real)(1.0f - 1.93f * cos(2.0f * M_PI * n / N) + 1.29f * cos(4.0f * M_PI * n / N) - 0.388f * cos(6.0f * M_PI * n / N) + 0.0322f * cos(8.0f * M_PI * n / N));
  99. }
  100. void FLAC__window_gauss(FLAC__real *window, const FLAC__int32 L, const FLAC__real stddev)
  101. {
  102. const FLAC__int32 N = L - 1;
  103. const double N2 = (double)N / 2.;
  104. FLAC__int32 n;
  105. for (n = 0; n <= N; n++) {
  106. const double k = ((double)n - N2) / (stddev * N2);
  107. window[n] = (FLAC__real)exp(-0.5f * k * k);
  108. }
  109. }
  110. void FLAC__window_hamming(FLAC__real *window, const FLAC__int32 L)
  111. {
  112. const FLAC__int32 N = L - 1;
  113. FLAC__int32 n;
  114. for (n = 0; n < L; n++)
  115. window[n] = (FLAC__real)(0.54f - 0.46f * cos(2.0f * M_PI * n / N));
  116. }
  117. void FLAC__window_hann(FLAC__real *window, const FLAC__int32 L)
  118. {
  119. const FLAC__int32 N = L - 1;
  120. FLAC__int32 n;
  121. for (n = 0; n < L; n++)
  122. window[n] = (FLAC__real)(0.5f - 0.5f * cos(2.0f * M_PI * n / N));
  123. }
  124. void FLAC__window_kaiser_bessel(FLAC__real *window, const FLAC__int32 L)
  125. {
  126. const FLAC__int32 N = L - 1;
  127. FLAC__int32 n;
  128. for (n = 0; n < L; n++)
  129. window[n] = (FLAC__real)(0.402f - 0.498f * cos(2.0f * M_PI * n / N) + 0.098f * cos(4.0f * M_PI * n / N) - 0.001f * cos(6.0f * M_PI * n / N));
  130. }
  131. void FLAC__window_nuttall(FLAC__real *window, const FLAC__int32 L)
  132. {
  133. const FLAC__int32 N = L - 1;
  134. FLAC__int32 n;
  135. for (n = 0; n < L; n++)
  136. window[n] = (FLAC__real)(0.3635819f - 0.4891775f*cos(2.0f*M_PI*n/N) + 0.1365995f*cos(4.0f*M_PI*n/N) - 0.0106411f*cos(6.0f*M_PI*n/N));
  137. }
  138. void FLAC__window_rectangle(FLAC__real *window, const FLAC__int32 L)
  139. {
  140. FLAC__int32 n;
  141. for (n = 0; n < L; n++)
  142. window[n] = 1.0f;
  143. }
  144. void FLAC__window_triangle(FLAC__real *window, const FLAC__int32 L)
  145. {
  146. FLAC__int32 n;
  147. if (L & 1) {
  148. for (n = 1; n <= L+1/2; n++)
  149. window[n-1] = 2.0f * n / ((float)L + 1.0f);
  150. for (; n <= L; n++)
  151. window[n-1] = - (float)(2 * (L - n + 1)) / ((float)L + 1.0f);
  152. }
  153. else {
  154. for (n = 1; n <= L/2; n++)
  155. window[n-1] = 2.0f * n / (float)L;
  156. for (; n <= L; n++)
  157. window[n-1] = ((float)(2 * (L - n)) + 1.0f) / (float)L;
  158. }
  159. }
  160. void FLAC__window_tukey(FLAC__real *window, const FLAC__int32 L, const FLAC__real p)
  161. {
  162. if (p <= 0.0)
  163. FLAC__window_rectangle(window, L);
  164. else if (p >= 1.0)
  165. FLAC__window_hann(window, L);
  166. else {
  167. const FLAC__int32 Np = (FLAC__int32)(p / 2.0f * L) - 1;
  168. FLAC__int32 n;
  169. /* start with rectangle... */
  170. FLAC__window_rectangle(window, L);
  171. /* ...replace ends with hann */
  172. if (Np > 0) {
  173. for (n = 0; n <= Np; n++) {
  174. window[n] = (FLAC__real)(0.5f - 0.5f * cos(M_PI * n / Np));
  175. window[L-Np-1+n] = (FLAC__real)(0.5f - 0.5f * cos(M_PI * (n+Np) / Np));
  176. }
  177. }
  178. }
  179. }
  180. void FLAC__window_welch(FLAC__real *window, const FLAC__int32 L)
  181. {
  182. const FLAC__int32 N = L - 1;
  183. const double N2 = (double)N / 2.;
  184. FLAC__int32 n;
  185. for (n = 0; n <= N; n++) {
  186. const double k = ((double)n - N2) / N2;
  187. window[n] = (FLAC__real)(1.0f - k * k);
  188. }
  189. }
  190. #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */