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.

71 lines
2.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #define _USE_MATH_DEFINES
  11. #include "common_audio/window_generator.h"
  12. #include <cmath>
  13. #include <complex>
  14. #include "rtc_base/checks.h"
  15. using std::complex;
  16. namespace {
  17. // Modified Bessel function of order 0 for complex inputs.
  18. complex<float> I0(complex<float> x) {
  19. complex<float> y = x / 3.75f;
  20. y *= y;
  21. return 1.0f + y * (3.5156229f +
  22. y * (3.0899424f +
  23. y * (1.2067492f +
  24. y * (0.2659732f +
  25. y * (0.360768e-1f + y * 0.45813e-2f)))));
  26. }
  27. } // namespace
  28. namespace webrtc {
  29. void WindowGenerator::Hanning(int length, float* window) {
  30. RTC_CHECK_GT(length, 1);
  31. RTC_CHECK(window != nullptr);
  32. for (int i = 0; i < length; ++i) {
  33. window[i] =
  34. 0.5f * (1 - cosf(2 * static_cast<float>(M_PI) * i / (length - 1)));
  35. }
  36. }
  37. void WindowGenerator::KaiserBesselDerived(float alpha,
  38. size_t length,
  39. float* window) {
  40. RTC_CHECK_GT(length, 1U);
  41. RTC_CHECK(window != nullptr);
  42. const size_t half = (length + 1) / 2;
  43. float sum = 0.0f;
  44. for (size_t i = 0; i <= half; ++i) {
  45. complex<float> r = (4.0f * i) / length - 1.0f;
  46. sum += I0(static_cast<float>(M_PI) * alpha * sqrt(1.0f - r * r)).real();
  47. window[i] = sum;
  48. }
  49. for (size_t i = length - 1; i >= half; --i) {
  50. window[length - i - 1] = sqrtf(window[length - i - 1] / sum);
  51. window[i] = window[length - i - 1];
  52. }
  53. if (length % 2 == 1) {
  54. window[half - 1] = sqrtf(window[half - 1] / sum);
  55. }
  56. }
  57. } // namespace webrtc