OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/audio_processing/vad/pole_zero_filter.h" | 11 #include "webrtc/modules/audio_processing/agc/pole_zero_filter.h" |
12 | 12 |
13 #include <stdlib.h> | 13 #include <stdlib.h> |
14 #include <string.h> | 14 #include <string.h> |
15 #include <algorithm> | 15 #include <algorithm> |
16 | 16 |
17 namespace webrtc { | 17 namespace webrtc { |
18 | 18 |
19 PoleZeroFilter* PoleZeroFilter::Create(const float* numerator_coefficients, | 19 PoleZeroFilter* PoleZeroFilter::Create(const float* numerator_coefficients, |
20 int order_numerator, | 20 int order_numerator, |
21 const float* denominator_coefficients, | 21 const float* denominator_coefficients, |
22 int order_denominator) { | 22 int order_denominator) { |
23 if (order_numerator < 0 || order_denominator < 0 || | 23 if (order_numerator < 0 || |
| 24 order_denominator < 0 || |
24 order_numerator > kMaxFilterOrder || | 25 order_numerator > kMaxFilterOrder || |
25 order_denominator > kMaxFilterOrder || denominator_coefficients[0] == 0 || | 26 order_denominator > kMaxFilterOrder || |
26 numerator_coefficients == NULL || denominator_coefficients == NULL) | 27 denominator_coefficients[0] == 0 || |
| 28 numerator_coefficients == NULL || |
| 29 denominator_coefficients == NULL) |
27 return NULL; | 30 return NULL; |
28 return new PoleZeroFilter(numerator_coefficients, order_numerator, | 31 return new PoleZeroFilter(numerator_coefficients, order_numerator, |
29 denominator_coefficients, order_denominator); | 32 denominator_coefficients, order_denominator); |
30 } | 33 } |
31 | 34 |
32 PoleZeroFilter::PoleZeroFilter(const float* numerator_coefficients, | 35 PoleZeroFilter::PoleZeroFilter(const float* numerator_coefficients, |
33 int order_numerator, | 36 int order_numerator, |
34 const float* denominator_coefficients, | 37 const float* denominator_coefficients, |
35 int order_denominator) | 38 int order_denominator) |
36 : past_input_(), | 39 : past_input_(), |
(...skipping 10 matching lines...) Expand all Loading... |
47 | 50 |
48 if (denominator_coefficients_[0] != 1) { | 51 if (denominator_coefficients_[0] != 1) { |
49 for (int n = 0; n <= order_numerator_; n++) | 52 for (int n = 0; n <= order_numerator_; n++) |
50 numerator_coefficients_[n] /= denominator_coefficients_[0]; | 53 numerator_coefficients_[n] /= denominator_coefficients_[0]; |
51 for (int n = 0; n <= order_denominator_; n++) | 54 for (int n = 0; n <= order_denominator_; n++) |
52 denominator_coefficients_[n] /= denominator_coefficients_[0]; | 55 denominator_coefficients_[n] /= denominator_coefficients_[0]; |
53 } | 56 } |
54 } | 57 } |
55 | 58 |
56 template <typename T> | 59 template <typename T> |
57 static float FilterArPast(const T* past, int order, const float* coefficients) { | 60 static float FilterArPast(const T* past, int order, |
| 61 const float* coefficients) { |
58 float sum = 0.0f; | 62 float sum = 0.0f; |
59 int past_index = order - 1; | 63 int past_index = order - 1; |
60 for (int k = 1; k <= order; k++, past_index--) | 64 for (int k = 1; k <= order; k++, past_index--) |
61 sum += coefficients[k] * past[past_index]; | 65 sum += coefficients[k] * past[past_index]; |
62 return sum; | 66 return sum; |
63 } | 67 } |
64 | 68 |
65 int PoleZeroFilter::Filter(const int16_t* in, | 69 int PoleZeroFilter::Filter(const int16_t* in, |
66 int num_input_samples, | 70 int num_input_samples, |
67 float* output) { | 71 float* output) { |
68 if (in == NULL || num_input_samples < 0 || output == NULL) | 72 if (in == NULL || num_input_samples < 0 || output == NULL) |
69 return -1; | 73 return -1; |
70 // This is the typical case, just a memcpy. | 74 // This is the typical case, just a memcpy. |
71 const int k = std::min(num_input_samples, highest_order_); | 75 const int k = std::min(num_input_samples, highest_order_); |
72 int n; | 76 int n; |
73 for (n = 0; n < k; n++) { | 77 for (n = 0; n < k; n++) { |
74 output[n] = in[n] * numerator_coefficients_[0]; | 78 output[n] = in[n] * numerator_coefficients_[0]; |
75 output[n] += FilterArPast(&past_input_[n], order_numerator_, | 79 output[n] += FilterArPast(&past_input_[n], order_numerator_, |
76 numerator_coefficients_); | 80 numerator_coefficients_); |
77 output[n] -= FilterArPast(&past_output_[n], order_denominator_, | 81 output[n] -= FilterArPast(&past_output_[n], order_denominator_, |
78 denominator_coefficients_); | 82 denominator_coefficients_); |
79 | 83 |
80 past_input_[n + order_numerator_] = in[n]; | 84 past_input_[n + order_numerator_] = in[n]; |
81 past_output_[n + order_denominator_] = output[n]; | 85 past_output_[n + order_denominator_] = output[n]; |
82 } | 86 } |
83 if (highest_order_ < num_input_samples) { | 87 if (highest_order_ < num_input_samples) { |
84 for (int m = 0; n < num_input_samples; n++, m++) { | 88 for (int m = 0; n < num_input_samples; n++, m++) { |
85 output[n] = in[n] * numerator_coefficients_[0]; | 89 output[n] = in[n] * numerator_coefficients_[0]; |
86 output[n] += | 90 output[n] += FilterArPast(&in[m], order_numerator_, |
87 FilterArPast(&in[m], order_numerator_, numerator_coefficients_); | 91 numerator_coefficients_); |
88 output[n] -= FilterArPast(&output[m], order_denominator_, | 92 output[n] -= FilterArPast(&output[m], order_denominator_, |
89 denominator_coefficients_); | 93 denominator_coefficients_); |
90 } | 94 } |
91 // Record into the past signal. | 95 // Record into the past signal. |
92 memcpy(past_input_, &in[num_input_samples - order_numerator_], | 96 memcpy(past_input_, &in[num_input_samples - order_numerator_], |
93 sizeof(in[0]) * order_numerator_); | 97 sizeof(in[0]) * order_numerator_); |
94 memcpy(past_output_, &output[num_input_samples - order_denominator_], | 98 memcpy(past_output_, &output[num_input_samples - order_denominator_], |
95 sizeof(output[0]) * order_denominator_); | 99 sizeof(output[0]) * order_denominator_); |
96 } else { | 100 } else { |
97 // Odd case that the length of the input is shorter that filter order. | 101 // Odd case that the length of the input is shorter that filter order. |
98 memmove(past_input_, &past_input_[num_input_samples], | 102 memmove(past_input_, &past_input_[num_input_samples], order_numerator_ * |
99 order_numerator_ * sizeof(past_input_[0])); | 103 sizeof(past_input_[0])); |
100 memmove(past_output_, &past_output_[num_input_samples], | 104 memmove(past_output_, &past_output_[num_input_samples], order_denominator_ * |
101 order_denominator_ * sizeof(past_output_[0])); | 105 sizeof(past_output_[0])); |
102 } | 106 } |
103 return 0; | 107 return 0; |
104 } | 108 } |
105 | 109 |
106 } // namespace webrtc | 110 } // namespace webrtc |
| 111 |
OLD | NEW |