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/agc/pole_zero_filter.h" | 11 #include "webrtc/modules/audio_processing/vad/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 || | 23 if (order_numerator < 0 || order_denominator < 0 || |
24 order_denominator < 0 || | |
25 order_numerator > kMaxFilterOrder || | 24 order_numerator > kMaxFilterOrder || |
26 order_denominator > kMaxFilterOrder || | 25 order_denominator > kMaxFilterOrder || denominator_coefficients[0] == 0 || |
27 denominator_coefficients[0] == 0 || | 26 numerator_coefficients == NULL || denominator_coefficients == NULL) |
28 numerator_coefficients == NULL || | |
29 denominator_coefficients == NULL) | |
30 return NULL; | 27 return NULL; |
31 return new PoleZeroFilter(numerator_coefficients, order_numerator, | 28 return new PoleZeroFilter(numerator_coefficients, order_numerator, |
32 denominator_coefficients, order_denominator); | 29 denominator_coefficients, order_denominator); |
33 } | 30 } |
34 | 31 |
35 PoleZeroFilter::PoleZeroFilter(const float* numerator_coefficients, | 32 PoleZeroFilter::PoleZeroFilter(const float* numerator_coefficients, |
36 int order_numerator, | 33 int order_numerator, |
37 const float* denominator_coefficients, | 34 const float* denominator_coefficients, |
38 int order_denominator) | 35 int order_denominator) |
39 : past_input_(), | 36 : past_input_(), |
(...skipping 10 matching lines...) Expand all Loading... |
50 | 47 |
51 if (denominator_coefficients_[0] != 1) { | 48 if (denominator_coefficients_[0] != 1) { |
52 for (int n = 0; n <= order_numerator_; n++) | 49 for (int n = 0; n <= order_numerator_; n++) |
53 numerator_coefficients_[n] /= denominator_coefficients_[0]; | 50 numerator_coefficients_[n] /= denominator_coefficients_[0]; |
54 for (int n = 0; n <= order_denominator_; n++) | 51 for (int n = 0; n <= order_denominator_; n++) |
55 denominator_coefficients_[n] /= denominator_coefficients_[0]; | 52 denominator_coefficients_[n] /= denominator_coefficients_[0]; |
56 } | 53 } |
57 } | 54 } |
58 | 55 |
59 template <typename T> | 56 template <typename T> |
60 static float FilterArPast(const T* past, int order, | 57 static float FilterArPast(const T* past, int order, const float* coefficients) { |
61 const float* coefficients) { | |
62 float sum = 0.0f; | 58 float sum = 0.0f; |
63 int past_index = order - 1; | 59 int past_index = order - 1; |
64 for (int k = 1; k <= order; k++, past_index--) | 60 for (int k = 1; k <= order; k++, past_index--) |
65 sum += coefficients[k] * past[past_index]; | 61 sum += coefficients[k] * past[past_index]; |
66 return sum; | 62 return sum; |
67 } | 63 } |
68 | 64 |
69 int PoleZeroFilter::Filter(const int16_t* in, | 65 int PoleZeroFilter::Filter(const int16_t* in, |
70 int num_input_samples, | 66 int num_input_samples, |
71 float* output) { | 67 float* output) { |
72 if (in == NULL || num_input_samples < 0 || output == NULL) | 68 if (in == NULL || num_input_samples < 0 || output == NULL) |
73 return -1; | 69 return -1; |
74 // This is the typical case, just a memcpy. | 70 // This is the typical case, just a memcpy. |
75 const int k = std::min(num_input_samples, highest_order_); | 71 const int k = std::min(num_input_samples, highest_order_); |
76 int n; | 72 int n; |
77 for (n = 0; n < k; n++) { | 73 for (n = 0; n < k; n++) { |
78 output[n] = in[n] * numerator_coefficients_[0]; | 74 output[n] = in[n] * numerator_coefficients_[0]; |
79 output[n] += FilterArPast(&past_input_[n], order_numerator_, | 75 output[n] += FilterArPast(&past_input_[n], order_numerator_, |
80 numerator_coefficients_); | 76 numerator_coefficients_); |
81 output[n] -= FilterArPast(&past_output_[n], order_denominator_, | 77 output[n] -= FilterArPast(&past_output_[n], order_denominator_, |
82 denominator_coefficients_); | 78 denominator_coefficients_); |
83 | 79 |
84 past_input_[n + order_numerator_] = in[n]; | 80 past_input_[n + order_numerator_] = in[n]; |
85 past_output_[n + order_denominator_] = output[n]; | 81 past_output_[n + order_denominator_] = output[n]; |
86 } | 82 } |
87 if (highest_order_ < num_input_samples) { | 83 if (highest_order_ < num_input_samples) { |
88 for (int m = 0; n < num_input_samples; n++, m++) { | 84 for (int m = 0; n < num_input_samples; n++, m++) { |
89 output[n] = in[n] * numerator_coefficients_[0]; | 85 output[n] = in[n] * numerator_coefficients_[0]; |
90 output[n] += FilterArPast(&in[m], order_numerator_, | 86 output[n] += |
91 numerator_coefficients_); | 87 FilterArPast(&in[m], order_numerator_, numerator_coefficients_); |
92 output[n] -= FilterArPast(&output[m], order_denominator_, | 88 output[n] -= FilterArPast(&output[m], order_denominator_, |
93 denominator_coefficients_); | 89 denominator_coefficients_); |
94 } | 90 } |
95 // Record into the past signal. | 91 // Record into the past signal. |
96 memcpy(past_input_, &in[num_input_samples - order_numerator_], | 92 memcpy(past_input_, &in[num_input_samples - order_numerator_], |
97 sizeof(in[0]) * order_numerator_); | 93 sizeof(in[0]) * order_numerator_); |
98 memcpy(past_output_, &output[num_input_samples - order_denominator_], | 94 memcpy(past_output_, &output[num_input_samples - order_denominator_], |
99 sizeof(output[0]) * order_denominator_); | 95 sizeof(output[0]) * order_denominator_); |
100 } else { | 96 } else { |
101 // Odd case that the length of the input is shorter that filter order. | 97 // Odd case that the length of the input is shorter that filter order. |
102 memmove(past_input_, &past_input_[num_input_samples], order_numerator_ * | 98 memmove(past_input_, &past_input_[num_input_samples], |
103 sizeof(past_input_[0])); | 99 order_numerator_ * sizeof(past_input_[0])); |
104 memmove(past_output_, &past_output_[num_input_samples], order_denominator_ * | 100 memmove(past_output_, &past_output_[num_input_samples], |
105 sizeof(past_output_[0])); | 101 order_denominator_ * sizeof(past_output_[0])); |
106 } | 102 } |
107 return 0; | 103 return 0; |
108 } | 104 } |
109 | 105 |
110 } // namespace webrtc | 106 } // namespace webrtc |
111 | |
OLD | NEW |