Home | History | Annotate | Download | only in intelligibility
      1 /*
      2  *  Copyright (c) 2015 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 //
     12 //  Unit tests for intelligibility enhancer.
     13 //
     14 
     15 #include <math.h>
     16 #include <stdlib.h>
     17 #include <algorithm>
     18 #include <vector>
     19 
     20 #include "testing/gtest/include/gtest/gtest.h"
     21 #include "webrtc/base/arraysize.h"
     22 #include "webrtc/base/scoped_ptr.h"
     23 #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
     24 #include "webrtc/modules/audio_processing/intelligibility/intelligibility_enhancer.h"
     25 
     26 namespace webrtc {
     27 
     28 namespace {
     29 
     30 // Target output for ERB create test. Generated with matlab.
     31 const float kTestCenterFreqs[] = {
     32     13.169f, 26.965f, 41.423f, 56.577f, 72.461f, 89.113f, 106.57f, 124.88f,
     33     144.08f, 164.21f, 185.34f, 207.5f,  230.75f, 255.16f, 280.77f, 307.66f,
     34     335.9f,  365.56f, 396.71f, 429.44f, 463.84f, 500.f};
     35 const float kTestFilterBank[][2] = {{0.055556f, 0.f},
     36                                     {0.055556f, 0.f},
     37                                     {0.055556f, 0.f},
     38                                     {0.055556f, 0.f},
     39                                     {0.055556f, 0.f},
     40                                     {0.055556f, 0.f},
     41                                     {0.055556f, 0.f},
     42                                     {0.055556f, 0.f},
     43                                     {0.055556f, 0.f},
     44                                     {0.055556f, 0.f},
     45                                     {0.055556f, 0.f},
     46                                     {0.055556f, 0.f},
     47                                     {0.055556f, 0.f},
     48                                     {0.055556f, 0.f},
     49                                     {0.055556f, 0.f},
     50                                     {0.055556f, 0.f},
     51                                     {0.055556f, 0.f},
     52                                     {0.055556f, 0.2f},
     53                                     {0, 0.2f},
     54                                     {0, 0.2f},
     55                                     {0, 0.2f},
     56                                     {0, 0.2f}};
     57 static_assert(arraysize(kTestCenterFreqs) == arraysize(kTestFilterBank),
     58               "Test filterbank badly initialized.");
     59 
     60 // Target output for gain solving test. Generated with matlab.
     61 const size_t kTestStartFreq = 12;  // Lowest integral frequency for ERBs.
     62 const float kTestZeroVar[] = {1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f,
     63                               1.f, 1.f, 1.f, 0.f, 0.f, 0.f, 0.f, 0.f,
     64                               0.f, 0.f, 0.f, 0.f, 0.f, 0.f};
     65 static_assert(arraysize(kTestCenterFreqs) == arraysize(kTestZeroVar),
     66               "Variance test data badly initialized.");
     67 const float kTestNonZeroVarLambdaTop[] = {
     68     1.f,     1.f,     1.f,     1.f,     1.f,     1.f,     1.f,     1.f,
     69     1.f,     1.f,     1.f,     0.f,     0.f,     0.0351f, 0.0636f, 0.0863f,
     70     0.1037f, 0.1162f, 0.1236f, 0.1251f, 0.1189f, 0.0993f};
     71 static_assert(arraysize(kTestCenterFreqs) ==
     72                   arraysize(kTestNonZeroVarLambdaTop),
     73               "Variance test data badly initialized.");
     74 const float kMaxTestError = 0.005f;
     75 
     76 // Enhancer initialization parameters.
     77 const int kSamples = 2000;
     78 const int kSampleRate = 1000;
     79 const int kNumChannels = 1;
     80 const int kFragmentSize = kSampleRate / 100;
     81 
     82 }  // namespace
     83 
     84 using std::vector;
     85 using intelligibility::VarianceArray;
     86 
     87 class IntelligibilityEnhancerTest : public ::testing::Test {
     88  protected:
     89   IntelligibilityEnhancerTest()
     90       : clear_data_(kSamples), noise_data_(kSamples), orig_data_(kSamples) {
     91     config_.sample_rate_hz = kSampleRate;
     92     enh_.reset(new IntelligibilityEnhancer(config_));
     93   }
     94 
     95   bool CheckUpdate(VarianceArray::StepType step_type) {
     96     config_.sample_rate_hz = kSampleRate;
     97     config_.var_type = step_type;
     98     enh_.reset(new IntelligibilityEnhancer(config_));
     99     float* clear_cursor = &clear_data_[0];
    100     float* noise_cursor = &noise_data_[0];
    101     for (int i = 0; i < kSamples; i += kFragmentSize) {
    102       enh_->AnalyzeCaptureAudio(&noise_cursor, kSampleRate, kNumChannels);
    103       enh_->ProcessRenderAudio(&clear_cursor, kSampleRate, kNumChannels);
    104       clear_cursor += kFragmentSize;
    105       noise_cursor += kFragmentSize;
    106     }
    107     for (int i = 0; i < kSamples; i++) {
    108       if (std::fabs(clear_data_[i] - orig_data_[i]) > kMaxTestError) {
    109         return true;
    110       }
    111     }
    112     return false;
    113   }
    114 
    115   IntelligibilityEnhancer::Config config_;
    116   rtc::scoped_ptr<IntelligibilityEnhancer> enh_;
    117   vector<float> clear_data_;
    118   vector<float> noise_data_;
    119   vector<float> orig_data_;
    120 };
    121 
    122 // For each class of generated data, tests that render stream is
    123 // updated when it should be for each variance update method.
    124 TEST_F(IntelligibilityEnhancerTest, TestRenderUpdate) {
    125   vector<VarianceArray::StepType> step_types;
    126   step_types.push_back(VarianceArray::kStepInfinite);
    127   step_types.push_back(VarianceArray::kStepDecaying);
    128   step_types.push_back(VarianceArray::kStepWindowed);
    129   step_types.push_back(VarianceArray::kStepBlocked);
    130   step_types.push_back(VarianceArray::kStepBlockBasedMovingAverage);
    131   std::fill(noise_data_.begin(), noise_data_.end(), 0.0f);
    132   std::fill(orig_data_.begin(), orig_data_.end(), 0.0f);
    133   for (auto step_type : step_types) {
    134     std::fill(clear_data_.begin(), clear_data_.end(), 0.0f);
    135     EXPECT_FALSE(CheckUpdate(step_type));
    136   }
    137   std::srand(1);
    138   auto float_rand = []() { return std::rand() * 2.f / RAND_MAX - 1; };
    139   std::generate(noise_data_.begin(), noise_data_.end(), float_rand);
    140   for (auto step_type : step_types) {
    141     EXPECT_FALSE(CheckUpdate(step_type));
    142   }
    143   for (auto step_type : step_types) {
    144     std::generate(clear_data_.begin(), clear_data_.end(), float_rand);
    145     orig_data_ = clear_data_;
    146     EXPECT_TRUE(CheckUpdate(step_type));
    147   }
    148 }
    149 
    150 // Tests ERB bank creation, comparing against matlab output.
    151 TEST_F(IntelligibilityEnhancerTest, TestErbCreation) {
    152   ASSERT_EQ(arraysize(kTestCenterFreqs), enh_->bank_size_);
    153   for (size_t i = 0; i < enh_->bank_size_; ++i) {
    154     EXPECT_NEAR(kTestCenterFreqs[i], enh_->center_freqs_[i], kMaxTestError);
    155     ASSERT_EQ(arraysize(kTestFilterBank[0]), enh_->freqs_);
    156     for (size_t j = 0; j < enh_->freqs_; ++j) {
    157       EXPECT_NEAR(kTestFilterBank[i][j], enh_->filter_bank_[i][j],
    158                   kMaxTestError);
    159     }
    160   }
    161 }
    162 
    163 // Tests analytic solution for optimal gains, comparing
    164 // against matlab output.
    165 TEST_F(IntelligibilityEnhancerTest, TestSolveForGains) {
    166   ASSERT_EQ(kTestStartFreq, enh_->start_freq_);
    167   vector<float> sols(enh_->bank_size_);
    168   float lambda = -0.001f;
    169   for (size_t i = 0; i < enh_->bank_size_; i++) {
    170     enh_->filtered_clear_var_[i] = 0.0f;
    171     enh_->filtered_noise_var_[i] = 0.0f;
    172     enh_->rho_[i] = 0.02f;
    173   }
    174   enh_->SolveForGainsGivenLambda(lambda, enh_->start_freq_, &sols[0]);
    175   for (size_t i = 0; i < enh_->bank_size_; i++) {
    176     EXPECT_NEAR(kTestZeroVar[i], sols[i], kMaxTestError);
    177   }
    178   for (size_t i = 0; i < enh_->bank_size_; i++) {
    179     enh_->filtered_clear_var_[i] = static_cast<float>(i + 1);
    180     enh_->filtered_noise_var_[i] = static_cast<float>(enh_->bank_size_ - i);
    181   }
    182   enh_->SolveForGainsGivenLambda(lambda, enh_->start_freq_, &sols[0]);
    183   for (size_t i = 0; i < enh_->bank_size_; i++) {
    184     EXPECT_NEAR(kTestNonZeroVarLambdaTop[i], sols[i], kMaxTestError);
    185   }
    186   lambda = -1.0;
    187   enh_->SolveForGainsGivenLambda(lambda, enh_->start_freq_, &sols[0]);
    188   for (size_t i = 0; i < enh_->bank_size_; i++) {
    189     EXPECT_NEAR(kTestZeroVar[i], sols[i], kMaxTestError);
    190   }
    191 }
    192 
    193 }  // namespace webrtc
    194