OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 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 // | 11 // |
12 // Specifies helper classes for intelligibility enhancement. | 12 // Specifies helper classes for intelligibility enhancement. |
13 // | 13 // |
14 | 14 |
15 #ifndef WEBRTC_MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_UTILS_H_ | 15 #ifndef WEBRTC_MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_UTILS_H_ |
16 #define WEBRTC_MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_UTILS_H_ | 16 #define WEBRTC_MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_UTILS_H_ |
17 | 17 |
18 #include <complex> | 18 #include <complex> |
19 | 19 |
20 #include "webrtc/base/scoped_ptr.h" | 20 #include "webrtc/base/scoped_ptr.h" |
21 | 21 |
22 namespace webrtc { | 22 namespace webrtc { |
23 | 23 |
24 namespace intelligibility { | 24 namespace intelligibility { |
25 | 25 |
| 26 // Return |current| changed towards |target|, with the change being at most |
| 27 // |limit|. |
| 28 float UpdateFactor(float target, float current, float limit); |
| 29 |
| 30 // std::isfinite for complex numbers. |
| 31 bool cplxfinite(std::complex<float> c); |
| 32 |
| 33 // std::isnormal for complex numbers. |
| 34 bool cplxnormal(std::complex<float> c); |
| 35 |
| 36 // Apply a small fudge to degenerate complex values. The numbers in the array |
| 37 // were chosen randomly, so that even a series of all zeroes has some small |
| 38 // variability. |
| 39 std::complex<float> zerofudge(std::complex<float> c); |
| 40 |
| 41 // Incremental mean computation. Return the mean of the series with the |
| 42 // mean |mean| with added |data|. |
| 43 std::complex<float> NewMean(std::complex<float> mean, |
| 44 std::complex<float> data, |
| 45 int count); |
| 46 |
| 47 // Updates |mean| with added |data|; |
| 48 void AddToMean(std::complex<float> data, int count, std::complex<float>* mean); |
| 49 |
26 // Internal helper for computing the variances of a stream of arrays. | 50 // Internal helper for computing the variances of a stream of arrays. |
27 // The result is an array of variances per position: the i-th variance | 51 // The result is an array of variances per position: the i-th variance |
28 // is the variance of the stream of data on the i-th positions in the | 52 // is the variance of the stream of data on the i-th positions in the |
29 // input arrays. | 53 // input arrays. |
30 // There are four methods of computation: | 54 // There are four methods of computation: |
31 // * kStepInfinite computes variances from the beginning onwards | 55 // * kStepInfinite computes variances from the beginning onwards |
32 // * kStepDecaying uses a recursive exponential decay formula with a | 56 // * kStepDecaying uses a recursive exponential decay formula with a |
33 // settable forgetting factor | 57 // settable forgetting factor |
34 // * kStepWindowed computes variances within a moving window | 58 // * kStepWindowed computes variances within a moving window |
35 // * kStepBlocked is similar to kStepWindowed, but history is kept | 59 // * kStepBlocked is similar to kStepWindowed, but history is kept |
36 // as a rolling window of blocks: multiple input elements are used for | 60 // as a rolling window of blocks: multiple input elements are used for |
37 // one block and the history then consists of the variances of these blocks | 61 // one block and the history then consists of the variances of these blocks |
38 // with the same effect as kStepWindowed, but less storage, so the window | 62 // with the same effect as kStepWindowed, but less storage, so the window |
39 // can be longer | 63 // can be longer |
40 class VarianceArray { | 64 class VarianceArray { |
41 public: | 65 public: |
42 enum StepType { | 66 enum StepType { |
43 kStepInfinite = 0, | 67 kStepInfinite = 0, |
44 kStepDecaying, | 68 kStepDecaying, |
45 kStepWindowed, | 69 kStepWindowed, |
46 kStepBlocked | 70 kStepBlocked, |
| 71 kStepBlockBasedMovingAverage |
47 }; | 72 }; |
48 | 73 |
49 // Construct an instance for the given input array length (|freqs|) and | 74 // Construct an instance for the given input array length (|freqs|) and |
50 // computation algorithm (|type|), with the appropriate parameters. | 75 // computation algorithm (|type|), with the appropriate parameters. |
51 // |window_size| is the number of samples for kStepWindowed and | 76 // |window_size| is the number of samples for kStepWindowed and |
52 // the number of blocks for kStepBlocked. |decay| is the forgetting factor | 77 // the number of blocks for kStepBlocked. |decay| is the forgetting factor |
53 // for kStepDecaying. | 78 // for kStepDecaying. |
54 VarianceArray(int freqs, StepType type, int window_size, float decay); | 79 VarianceArray(int freqs, StepType type, int window_size, float decay); |
55 | 80 |
56 // Add a new data point to the series and compute the new variances. | 81 // Add a new data point to the series and compute the new variances. |
(...skipping 13 matching lines...) Expand all Loading... |
70 const float* variance() const { return variance_.get(); } | 95 const float* variance() const { return variance_.get(); } |
71 | 96 |
72 // The mean value of the current set of variances. | 97 // The mean value of the current set of variances. |
73 float array_mean() const { return array_mean_; } | 98 float array_mean() const { return array_mean_; } |
74 | 99 |
75 private: | 100 private: |
76 void InfiniteStep(const std::complex<float>* data, bool dummy); | 101 void InfiniteStep(const std::complex<float>* data, bool dummy); |
77 void DecayStep(const std::complex<float>* data, bool dummy); | 102 void DecayStep(const std::complex<float>* data, bool dummy); |
78 void WindowedStep(const std::complex<float>* data, bool dummy); | 103 void WindowedStep(const std::complex<float>* data, bool dummy); |
79 void BlockedStep(const std::complex<float>* data, bool dummy); | 104 void BlockedStep(const std::complex<float>* data, bool dummy); |
| 105 void BlockBasedMovingAverage(const std::complex<float>* data, bool dummy); |
80 | 106 |
81 // TODO(ekmeyerson): Switch the following running means | 107 // TODO(ekmeyerson): Switch the following running means |
82 // and histories from rtc::scoped_ptr to std::vector. | 108 // and histories from rtc::scoped_ptr to std::vector. |
83 | 109 |
84 // The current average X and X^2. | 110 // The current average X and X^2. |
85 rtc::scoped_ptr<std::complex<float>[]> running_mean_; | 111 rtc::scoped_ptr<std::complex<float>[]> running_mean_; |
86 rtc::scoped_ptr<std::complex<float>[]> running_mean_sq_; | 112 rtc::scoped_ptr<std::complex<float>[]> running_mean_sq_; |
87 | 113 |
88 // Average X and X^2 for the current block in kStepBlocked. | 114 // Average X and X^2 for the current block in kStepBlocked. |
89 rtc::scoped_ptr<std::complex<float>[]> sub_running_mean_; | 115 rtc::scoped_ptr<std::complex<float>[]> sub_running_mean_; |
90 rtc::scoped_ptr<std::complex<float>[]> sub_running_mean_sq_; | 116 rtc::scoped_ptr<std::complex<float>[]> sub_running_mean_sq_; |
91 | 117 |
92 // Sample history for the rolling window in kStepWindowed and block-wise | 118 // Sample history for the rolling window in kStepWindowed and block-wise |
93 // histories for kStepBlocked. | 119 // histories for kStepBlocked. |
94 rtc::scoped_ptr<rtc::scoped_ptr<std::complex<float>[]>[]> history_; | 120 rtc::scoped_ptr<rtc::scoped_ptr<std::complex<float>[]>[]> history_; |
95 rtc::scoped_ptr<rtc::scoped_ptr<std::complex<float>[]>[]> subhistory_; | 121 rtc::scoped_ptr<rtc::scoped_ptr<std::complex<float>[]>[]> subhistory_; |
96 rtc::scoped_ptr<rtc::scoped_ptr<std::complex<float>[]>[]> subhistory_sq_; | 122 rtc::scoped_ptr<rtc::scoped_ptr<std::complex<float>[]>[]> subhistory_sq_; |
97 | 123 |
98 // The current set of variances and sums for Welford's algorithm. | 124 // The current set of variances and sums for Welford's algorithm. |
99 rtc::scoped_ptr<float[]> variance_; | 125 rtc::scoped_ptr<float[]> variance_; |
100 rtc::scoped_ptr<float[]> conj_sum_; | 126 rtc::scoped_ptr<float[]> conj_sum_; |
101 | 127 |
102 const int freqs_; | 128 const int freqs_; |
103 const int window_size_; | 129 const int window_size_; |
104 const float decay_; | 130 const float decay_; |
105 int history_cursor_; | 131 int history_cursor_; |
106 int count_; | 132 int count_; |
107 float array_mean_; | 133 float array_mean_; |
| 134 bool buffer_full_; |
108 void (VarianceArray::*step_func_)(const std::complex<float>*, bool); | 135 void (VarianceArray::*step_func_)(const std::complex<float>*, bool); |
109 }; | 136 }; |
110 | 137 |
111 // Helper class for smoothing gain changes. On each applicatiion step, the | 138 // Helper class for smoothing gain changes. On each applicatiion step, the |
112 // currently used gains are changed towards a set of settable target gains, | 139 // currently used gains are changed towards a set of settable target gains, |
113 // constrained by a limit on the magnitude of the changes. | 140 // constrained by a limit on the magnitude of the changes. |
114 class GainApplier { | 141 class GainApplier { |
115 public: | 142 public: |
116 GainApplier(int freqs, float change_limit); | 143 GainApplier(int freqs, float change_limit); |
117 | 144 |
(...skipping 10 matching lines...) Expand all Loading... |
128 const float change_limit_; | 155 const float change_limit_; |
129 rtc::scoped_ptr<float[]> target_; | 156 rtc::scoped_ptr<float[]> target_; |
130 rtc::scoped_ptr<float[]> current_; | 157 rtc::scoped_ptr<float[]> current_; |
131 }; | 158 }; |
132 | 159 |
133 } // namespace intelligibility | 160 } // namespace intelligibility |
134 | 161 |
135 } // namespace webrtc | 162 } // namespace webrtc |
136 | 163 |
137 #endif // WEBRTC_MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_UTILS
_H_ | 164 #endif // WEBRTC_MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_UTILS
_H_ |
OLD | NEW |