window_func.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. default:
  115. av_assert0(0);
  116. }
  117. }