andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 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 | #define _USE_MATH_DEFINES |
| 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "common_audio/window_generator.h" |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 14 | |
| 15 | #include <cmath> |
| 16 | #include <complex> |
| 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/checks.h" |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 19 | |
| 20 | using std::complex; |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | // Modified Bessel function of order 0 for complex inputs. |
| 25 | complex<float> I0(complex<float> x) { |
| 26 | complex<float> y = x / 3.75f; |
| 27 | y *= y; |
| 28 | return 1.0f + y * ( |
| 29 | 3.5156229f + y * ( |
| 30 | 3.0899424f + y * ( |
| 31 | 1.2067492f + y * ( |
| 32 | 0.2659732f + y * ( |
| 33 | 0.360768e-1f + y * 0.45813e-2f))))); |
| 34 | } |
| 35 | |
| 36 | } // namespace |
| 37 | |
| 38 | namespace webrtc { |
| 39 | |
| 40 | void WindowGenerator::Hanning(int length, float* window) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 41 | RTC_CHECK_GT(length, 1); |
| 42 | RTC_CHECK(window != nullptr); |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 43 | for (int i = 0; i < length; ++i) { |
| 44 | window[i] = 0.5f * (1 - cosf(2 * static_cast<float>(M_PI) * i / |
| 45 | (length - 1))); |
| 46 | } |
| 47 | } |
| 48 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 49 | void WindowGenerator::KaiserBesselDerived(float alpha, size_t length, |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 50 | float* window) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 51 | RTC_CHECK_GT(length, 1U); |
| 52 | RTC_CHECK(window != nullptr); |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 53 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 54 | const size_t half = (length + 1) / 2; |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 55 | float sum = 0.0f; |
| 56 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 57 | for (size_t i = 0; i <= half; ++i) { |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 58 | complex<float> r = (4.0f * i) / length - 1.0f; |
| 59 | sum += I0(static_cast<float>(M_PI) * alpha * sqrt(1.0f - r * r)).real(); |
| 60 | window[i] = sum; |
| 61 | } |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 62 | for (size_t i = length - 1; i >= half; --i) { |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 63 | window[length - i - 1] = sqrtf(window[length - i - 1] / sum); |
| 64 | window[i] = window[length - i - 1]; |
| 65 | } |
| 66 | if (length % 2 == 1) { |
| 67 | window[half - 1] = sqrtf(window[half - 1] / sum); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | } // namespace webrtc |
| 72 | |