xref: /aosp_15_r20/external/webrtc/modules/audio_processing/ns/histograms.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #include "modules/audio_processing/ns/histograms.h"
12*d9f75844SAndroid Build Coastguard Worker 
13*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
14*d9f75844SAndroid Build Coastguard Worker 
Histograms()15*d9f75844SAndroid Build Coastguard Worker Histograms::Histograms() {
16*d9f75844SAndroid Build Coastguard Worker   Clear();
17*d9f75844SAndroid Build Coastguard Worker }
18*d9f75844SAndroid Build Coastguard Worker 
Clear()19*d9f75844SAndroid Build Coastguard Worker void Histograms::Clear() {
20*d9f75844SAndroid Build Coastguard Worker   lrt_.fill(0);
21*d9f75844SAndroid Build Coastguard Worker   spectral_flatness_.fill(0);
22*d9f75844SAndroid Build Coastguard Worker   spectral_diff_.fill(0);
23*d9f75844SAndroid Build Coastguard Worker }
24*d9f75844SAndroid Build Coastguard Worker 
Update(const SignalModel & features_)25*d9f75844SAndroid Build Coastguard Worker void Histograms::Update(const SignalModel& features_) {
26*d9f75844SAndroid Build Coastguard Worker   // Update the histogram for the LRT.
27*d9f75844SAndroid Build Coastguard Worker   constexpr float kOneByBinSizeLrt = 1.f / kBinSizeLrt;
28*d9f75844SAndroid Build Coastguard Worker   if (features_.lrt < kHistogramSize * kBinSizeLrt && features_.lrt >= 0.f) {
29*d9f75844SAndroid Build Coastguard Worker     ++lrt_[kOneByBinSizeLrt * features_.lrt];
30*d9f75844SAndroid Build Coastguard Worker   }
31*d9f75844SAndroid Build Coastguard Worker 
32*d9f75844SAndroid Build Coastguard Worker   // Update histogram for the spectral flatness.
33*d9f75844SAndroid Build Coastguard Worker   constexpr float kOneByBinSizeSpecFlat = 1.f / kBinSizeSpecFlat;
34*d9f75844SAndroid Build Coastguard Worker   if (features_.spectral_flatness < kHistogramSize * kBinSizeSpecFlat &&
35*d9f75844SAndroid Build Coastguard Worker       features_.spectral_flatness >= 0.f) {
36*d9f75844SAndroid Build Coastguard Worker     ++spectral_flatness_[features_.spectral_flatness * kOneByBinSizeSpecFlat];
37*d9f75844SAndroid Build Coastguard Worker   }
38*d9f75844SAndroid Build Coastguard Worker 
39*d9f75844SAndroid Build Coastguard Worker   // Update histogram for the spectral difference.
40*d9f75844SAndroid Build Coastguard Worker   constexpr float kOneByBinSizeSpecDiff = 1.f / kBinSizeSpecDiff;
41*d9f75844SAndroid Build Coastguard Worker   if (features_.spectral_diff < kHistogramSize * kBinSizeSpecDiff &&
42*d9f75844SAndroid Build Coastguard Worker       features_.spectral_diff >= 0.f) {
43*d9f75844SAndroid Build Coastguard Worker     ++spectral_diff_[features_.spectral_diff * kOneByBinSizeSpecDiff];
44*d9f75844SAndroid Build Coastguard Worker   }
45*d9f75844SAndroid Build Coastguard Worker }
46*d9f75844SAndroid Build Coastguard Worker 
47*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
48