Line data Source code
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 :
11 : #ifndef _USE_MATH_DEFINES
12 : #define _USE_MATH_DEFINES
13 : #endif
14 :
15 : #include "webrtc/common_audio/window_generator.h"
16 :
17 : #include <cmath>
18 : #include <complex>
19 :
20 : #include "webrtc/base/checks.h"
21 :
22 : using std::complex;
23 :
24 : namespace {
25 :
26 : // Modified Bessel function of order 0 for complex inputs.
27 0 : complex<float> I0(complex<float> x) {
28 0 : complex<float> y = x / 3.75f;
29 0 : y *= y;
30 0 : return 1.0f + y * (
31 0 : 3.5156229f + y * (
32 0 : 3.0899424f + y * (
33 0 : 1.2067492f + y * (
34 0 : 0.2659732f + y * (
35 0 : 0.360768e-1f + y * 0.45813e-2f)))));
36 : }
37 :
38 : } // namespace
39 :
40 : namespace webrtc {
41 :
42 0 : void WindowGenerator::Hanning(int length, float* window) {
43 0 : RTC_CHECK_GT(length, 1);
44 0 : RTC_CHECK(window != nullptr);
45 0 : for (int i = 0; i < length; ++i) {
46 0 : window[i] = 0.5f * (1 - cosf(2 * static_cast<float>(M_PI) * i /
47 0 : (length - 1)));
48 : }
49 0 : }
50 :
51 0 : void WindowGenerator::KaiserBesselDerived(float alpha, size_t length,
52 : float* window) {
53 0 : RTC_CHECK_GT(length, 1U);
54 0 : RTC_CHECK(window != nullptr);
55 :
56 0 : const size_t half = (length + 1) / 2;
57 0 : float sum = 0.0f;
58 :
59 0 : for (size_t i = 0; i <= half; ++i) {
60 0 : complex<float> r = (4.0f * i) / length - 1.0f;
61 0 : sum += I0(static_cast<float>(M_PI) * alpha * sqrt(1.0f - r * r)).real();
62 0 : window[i] = sum;
63 : }
64 0 : for (size_t i = length - 1; i >= half; --i) {
65 0 : window[length - i - 1] = sqrtf(window[length - i - 1] / sum);
66 0 : window[i] = window[length - i - 1];
67 : }
68 0 : if (length % 2 == 1) {
69 0 : window[half - 1] = sqrtf(window[half - 1] / sum);
70 : }
71 0 : }
72 :
73 : } // namespace webrtc
74 :
|