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.

179 lines
5.9KB

  1. /*
  2. * Copyright (c) 2015 Paul B Mahol
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <math.h>
  21. #include "libavutil/avassert.h"
  22. #include "window_func.h"
  23. void ff_generate_window_func(float *lut, int N, int win_func, float *overlap)
  24. {
  25. int n;
  26. switch (win_func) {
  27. case WFUNC_RECT:
  28. for (n = 0; n < N; n++)
  29. lut[n] = 1.;
  30. *overlap = 0.;
  31. break;
  32. case WFUNC_BARTLETT:
  33. for (n = 0; n < N; n++)
  34. lut[n] = 1.-fabs((n-(N-1)/2.)/((N-1)/2.));
  35. *overlap = 0.5;
  36. break;
  37. case WFUNC_HANNING:
  38. for (n = 0; n < N; n++)
  39. lut[n] = .5*(1-cos(2*M_PI*n/(N-1)));
  40. *overlap = 0.5;
  41. break;
  42. case WFUNC_HAMMING:
  43. for (n = 0; n < N; n++)
  44. lut[n] = .54-.46*cos(2*M_PI*n/(N-1));
  45. *overlap = 0.5;
  46. break;
  47. case WFUNC_BLACKMAN:
  48. for (n = 0; n < N; n++)
  49. lut[n] = .42659-.49656*cos(2*M_PI*n/(N-1))+.076849*cos(4*M_PI*n/(N-1));
  50. *overlap = 0.661;
  51. break;
  52. case WFUNC_WELCH:
  53. for (n = 0; n < N; n++)
  54. lut[n] = 1.-(n-(N-1)/2.)/((N-1)/2.)*(n-(N-1)/2.)/((N-1)/2.);
  55. *overlap = 0.293;
  56. break;
  57. case WFUNC_FLATTOP:
  58. for (n = 0; n < N; n++)
  59. lut[n] = 1.-1.985844164102*cos( 2*M_PI*n/(N-1))+1.791176438506*cos( 4*M_PI*n/(N-1))-
  60. 1.282075284005*cos( 6*M_PI*n/(N-1))+0.667777530266*cos( 8*M_PI*n/(N-1))-
  61. 0.240160796576*cos(10*M_PI*n/(N-1))+0.056656381764*cos(12*M_PI*n/(N-1))-
  62. 0.008134974479*cos(14*M_PI*n/(N-1))+0.000624544650*cos(16*M_PI*n/(N-1))-
  63. 0.000019808998*cos(18*M_PI*n/(N-1))+0.000000132974*cos(20*M_PI*n/(N-1));
  64. *overlap = 0.841;
  65. break;
  66. case WFUNC_BHARRIS:
  67. for (n = 0; n < N; n++)
  68. lut[n] = 0.35875-0.48829*cos(2*M_PI*n/(N-1))+0.14128*cos(4*M_PI*n/(N-1))-0.01168*cos(6*M_PI*n/(N-1));
  69. *overlap = 0.661;
  70. break;
  71. case WFUNC_BNUTTALL:
  72. for (n = 0; n < N; n++)
  73. lut[n] = 0.3635819-0.4891775*cos(2*M_PI*n/(N-1))+0.1365995*cos(4*M_PI*n/(N-1))-0.0106411*cos(6*M_PI*n/(N-1));
  74. *overlap = 0.661;
  75. break;
  76. case WFUNC_BHANN:
  77. for (n = 0; n < N; n++)
  78. lut[n] = 0.62-0.48*fabs(n/(double)(N-1)-.5)-0.38*cos(2*M_PI*n/(N-1));
  79. *overlap = 0.5;
  80. break;
  81. case WFUNC_SINE:
  82. for (n = 0; n < N; n++)
  83. lut[n] = sin(M_PI*n/(N-1));
  84. *overlap = 0.75;
  85. break;
  86. case WFUNC_NUTTALL:
  87. for (n = 0; n < N; n++)
  88. lut[n] = 0.355768-0.487396*cos(2*M_PI*n/(N-1))+0.144232*cos(4*M_PI*n/(N-1))-0.012604*cos(6*M_PI*n/(N-1));
  89. *overlap = 0.663;
  90. break;
  91. case WFUNC_LANCZOS:
  92. #define SINC(x) (!(x)) ? 1 : sin(M_PI * (x))/(M_PI * (x));
  93. for (n = 0; n < N; n++)
  94. lut[n] = SINC((2.*n)/(N-1)-1);
  95. *overlap = 0.75;
  96. break;
  97. case WFUNC_GAUSS:
  98. #define SQR(x) ((x)*(x))
  99. for (n = 0; n < N; n++)
  100. lut[n] = exp(-0.5 * SQR((n-(N-1)/2)/(0.4*(N-1)/2.f)));
  101. *overlap = 0.75;
  102. break;
  103. case WFUNC_TUKEY:
  104. for (n = 0; n < N; n++) {
  105. float M = (N-1)/2.;
  106. if (FFABS(n - M) >= 0.3 * M) {
  107. lut[n] = 0.5 * (1 + cos((M_PI*(FFABS(n - M) - 0.3 * M))/((1 - 0.3) * M)));
  108. } else {
  109. lut[n] = 1;
  110. }
  111. }
  112. *overlap = 0.33;
  113. break;
  114. case WFUNC_DOLPH: {
  115. double b = cosh(7.6009022095419887 / (N-1)), sum, t, c, norm = 0;
  116. int j;
  117. for (c = 1 - 1 / (b*b), n = (N-1) / 2; n >= 0; --n) {
  118. for (sum = !n, b = t = j = 1; j <= n && sum != t; b *= (n-j) * (1./j), ++j)
  119. t = sum, sum += (b *= c * (N - n - j) * (1./j));
  120. sum /= (N - 1 - n), sum /= (norm = norm ? norm : sum);
  121. lut[n] = sum;
  122. lut[N - 1 - n] = sum;
  123. }
  124. *overlap = 0.5;}
  125. break;
  126. case WFUNC_CAUCHY:
  127. for (n = 0; n < N; n++) {
  128. double x = 2 * ((n / (double)(N - 1)) - .5);
  129. if (x <= -.5 || x >= .5) {
  130. lut[n] = 0;
  131. } else {
  132. lut[n] = FFMIN(1, fabs(1/(1+4*16*x*x)));
  133. }
  134. }
  135. *overlap = 0.75;
  136. break;
  137. case WFUNC_PARZEN:
  138. for (n = 0; n < N; n++) {
  139. double x = 2 * ((n / (double)(N - 1)) - .5);
  140. if (x > 0.25 && x <= 0.5) {
  141. lut[n] = -2 * powf(-1 + 2 * x, 3);
  142. } else if (x >= -.5 && x < -.25) {
  143. lut[n] = 2 * powf(1 + 2 * x, 3);
  144. } else if (x >= -.25 && x < 0) {
  145. lut[n] = 1 - 24 * x * x - 48 * x * x * x;
  146. } else if (x >= 0 && x <= .25) {
  147. lut[n] = 1 - 24 * x * x + 48 * x * x * x;
  148. } else {
  149. lut[n] = 0;
  150. }
  151. }
  152. *overlap = 0.75;
  153. break;
  154. case WFUNC_POISSON:
  155. for (n = 0; n < N; n++) {
  156. double x = 2 * ((n / (double)(N - 1)) - .5);
  157. if (x >= 0 && x <= .5) {
  158. lut[n] = exp(-6*x);
  159. } else if (x < 0 && x >= -.5) {
  160. lut[n] = exp(6*x);
  161. } else {
  162. lut[n] = 0;
  163. }
  164. }
  165. *overlap = 0.75;
  166. break;
  167. default:
  168. av_assert0(0);
  169. }
  170. }