Chromium Code Reviews| Index: webrtc/modules/audio_processing/intelligibility/intelligibility_utils.h |
| diff --git a/webrtc/modules/audio_processing/intelligibility/intelligibility_utils.h b/webrtc/modules/audio_processing/intelligibility/intelligibility_utils.h |
| index 4ac11671474dcde823379be25990532684a499ea..f459d391d421a09878178bb0e42566e63a8f9889 100644 |
| --- a/webrtc/modules/audio_processing/intelligibility/intelligibility_utils.h |
| +++ b/webrtc/modules/audio_processing/intelligibility/intelligibility_utils.h |
| @@ -8,14 +8,11 @@ |
| * be found in the AUTHORS file in the root of the source tree. |
| */ |
| -// |
| -// Specifies helper classes for intelligibility enhancement. |
| -// |
| - |
| #ifndef WEBRTC_MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_UTILS_H_ |
| #define WEBRTC_MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_UTILS_H_ |
| #include <complex> |
| +#include <vector> |
| #include "webrtc/base/scoped_ptr.h" |
| @@ -23,120 +20,40 @@ namespace webrtc { |
| namespace intelligibility { |
| -// Return |current| changed towards |target|, with the change being at most |
| -// |limit|. |
| -float UpdateFactor(float target, float current, float limit); |
| - |
| -// Apply a small fudge to degenerate complex values. The numbers in the array |
| -// were chosen randomly, so that even a series of all zeroes has some small |
| -// variability. |
| -std::complex<float> zerofudge(std::complex<float> c); |
| - |
| -// Incremental mean computation. Return the mean of the series with the |
| -// mean |mean| with added |data|. |
| -std::complex<float> NewMean(std::complex<float> mean, |
| - std::complex<float> data, |
| - size_t count); |
| - |
| -// Updates |mean| with added |data|; |
| -void AddToMean(std::complex<float> data, |
| - size_t count, |
| - std::complex<float>* mean); |
| - |
| -// Internal helper for computing the variances of a stream of arrays. |
| -// The result is an array of variances per position: the i-th variance |
| -// is the variance of the stream of data on the i-th positions in the |
| -// input arrays. |
| -// There are four methods of computation: |
| -// * kStepInfinite computes variances from the beginning onwards |
| -// * kStepDecaying uses a recursive exponential decay formula with a |
| -// settable forgetting factor |
| -// * kStepWindowed computes variances within a moving window |
| -// * kStepBlocked is similar to kStepWindowed, but history is kept |
| -// as a rolling window of blocks: multiple input elements are used for |
| -// one block and the history then consists of the variances of these blocks |
| -// with the same effect as kStepWindowed, but less storage, so the window |
| -// can be longer |
| -class VarianceArray { |
| +// Internal helper for computing the power of a stream of arrays. |
| +// The result is an array of power per position: the i-th power is the power of |
| +// the stream of data on the i-th positions in the input arrays. |
| +class PowerEstimator { |
| public: |
| - enum StepType { |
| - kStepInfinite = 0, |
| - kStepDecaying, |
| - kStepWindowed, |
| - kStepBlocked, |
| - kStepBlockBasedMovingAverage |
| - }; |
| - |
| - // Construct an instance for the given input array length (|freqs|) and |
| - // computation algorithm (|type|), with the appropriate parameters. |
| - // |window_size| is the number of samples for kStepWindowed and |
| - // the number of blocks for kStepBlocked. |decay| is the forgetting factor |
| - // for kStepDecaying. |
| - VarianceArray(size_t freqs, StepType type, size_t window_size, float decay); |
| - |
| - // Add a new data point to the series and compute the new variances. |
| - // TODO(bercic) |skip_fudge| is a flag for kStepWindowed and kStepDecaying, |
| - // whether they should skip adding some small dummy values to the input |
| - // to prevent problems with all-zero inputs. Can probably be removed. |
| - void Step(const std::complex<float>* data, bool skip_fudge = false) { |
| - (this->*step_func_)(data, skip_fudge); |
| + // Construct an instance for the given input array length (|freqs|), with the |
| + // appropriate parameters. |decay| is the forgetting factor. |
| + PowerEstimator(size_t freqs, float decay); |
| + |
| + // Add a new data point to the series. |
| + template <typename T> |
|
turaj
2016/02/19 16:48:47
I'm sure have compiled this code, just out of my c
aluebs-webrtc
2016/02/19 19:30:48
I am not experienced with templates either, but I
turaj
2016/02/22 17:36:53
Like you, I really don't know what is preferred wi
aluebs-webrtc
2016/02/22 20:41:03
That is an excellent point! Changed it to a templa
|
| + void Step(const T* data) { |
| + for (size_t i = 0; i < power_.size(); ++i) { |
| + power_[i] = decay_ * power_[i] + |
| + (1.f - decay_) * std::abs(data[i]) * std::abs(data[i]); |
| + } |
| } |
| - // Reset variances to zero and forget all history. |
| - void Clear(); |
| - // Scale the input data by |scale|. Effectively multiply variances |
| - // by |scale^2|. |
| - void ApplyScale(float scale); |
| - |
| - // The current set of variances. |
| - const float* variance() const { return variance_.get(); } |
| - // The mean value of the current set of variances. |
| - float array_mean() const { return array_mean_; } |
| + // The current power array. |
| + const std::vector<float>& power() { return power_; }; |
| private: |
| - void InfiniteStep(const std::complex<float>* data, bool dummy); |
| - void DecayStep(const std::complex<float>* data, bool dummy); |
| - void WindowedStep(const std::complex<float>* data, bool dummy); |
| - void BlockedStep(const std::complex<float>* data, bool dummy); |
| - void BlockBasedMovingAverage(const std::complex<float>* data, bool dummy); |
| - |
| - // TODO(ekmeyerson): Switch the following running means |
| - // and histories from rtc::scoped_ptr to std::vector. |
| - |
| - // The current average X and X^2. |
| - rtc::scoped_ptr<std::complex<float>[]> running_mean_; |
| - rtc::scoped_ptr<std::complex<float>[]> running_mean_sq_; |
| + // The current power array. |
| + std::vector<float> power_; |
| - // Average X and X^2 for the current block in kStepBlocked. |
| - rtc::scoped_ptr<std::complex<float>[]> sub_running_mean_; |
| - rtc::scoped_ptr<std::complex<float>[]> sub_running_mean_sq_; |
| - |
| - // Sample history for the rolling window in kStepWindowed and block-wise |
| - // histories for kStepBlocked. |
| - rtc::scoped_ptr<rtc::scoped_ptr<std::complex<float>[]>[]> history_; |
| - rtc::scoped_ptr<rtc::scoped_ptr<std::complex<float>[]>[]> subhistory_; |
| - rtc::scoped_ptr<rtc::scoped_ptr<std::complex<float>[]>[]> subhistory_sq_; |
| - |
| - // The current set of variances and sums for Welford's algorithm. |
| - rtc::scoped_ptr<float[]> variance_; |
| - rtc::scoped_ptr<float[]> conj_sum_; |
| - |
| - const size_t num_freqs_; |
| - const size_t window_size_; |
| const float decay_; |
| - size_t history_cursor_; |
| - size_t count_; |
| - float array_mean_; |
| - bool buffer_full_; |
| - void (VarianceArray::*step_func_)(const std::complex<float>*, bool); |
| }; |
| -// Helper class for smoothing gain changes. On each applicatiion step, the |
| +// Helper class for smoothing gain changes. On each application step, the |
| // currently used gains are changed towards a set of settable target gains, |
| -// constrained by a limit on the magnitude of the changes. |
| +// constrained by a limit on the relative changes. |
| class GainApplier { |
| public: |
| - GainApplier(size_t freqs, float change_limit); |
| + GainApplier(size_t freqs, float relative_change_limit); |
| // Copy |in_block| to |out_block|, multiplied by the current set of gains, |
| // and step the current set of gains towards the target set. |
| @@ -148,7 +65,7 @@ class GainApplier { |
| private: |
| const size_t num_freqs_; |
| - const float change_limit_; |
| + const float relative_change_limit_; |
| rtc::scoped_ptr<float[]> target_; |
| rtc::scoped_ptr<float[]> current_; |
| }; |