File size: 3,568 Bytes
5f923cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2025 The ODML Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "runtime/components/preprocessor/signal_vector_util.h"

#include <cmath>
#include <numeric>
#include <vector>

#include <gtest/gtest.h>
#include "runtime/util/test_utils.h"  // NOLINT

namespace litert::lm {
namespace {

TEST(VectorTest, SmootherCoefficientFromScale) {
  float scale1, coefficient;
  // scale1 is the smoothing scale of one forward/backward pass.  The net
  // smoothing scale is scale1 * sqrt(2.0).  Using scale1 makes the
  // expected coefficient values a little simpler.
  scale1 = 2;
  coefficient = SmootherCoefficientFromScale(scale1 * M_SQRT2);
  // This is one coincidentally simple value that we know:
  EXPECT_NEAR(coefficient, 1 / scale1, 1e-6);
  // Other values we can bound:
  scale1 = 1;
  coefficient = SmootherCoefficientFromScale(scale1 * M_SQRT2);
  EXPECT_LT(coefficient, 1 / scale1);
  scale1 = 4;
  coefficient = SmootherCoefficientFromScale(scale1 * M_SQRT2);
  EXPECT_GT(coefficient, 1 / scale1);
  // Small-scale approximation.
  scale1 = 0.1;
  coefficient = SmootherCoefficientFromScale(scale1 * M_SQRT2);
  EXPECT_NEAR(coefficient, 1.0 - 0.5 * scale1 * scale1, 1e-4);
  // Large-scale approximation.
  scale1 = 1000;
  coefficient = SmootherCoefficientFromScale(scale1 * M_SQRT2);
  EXPECT_NEAR(coefficient, sqrt(2.0) / scale1, 1e-4);
}

TEST(VectorTest, ForwardSmoothVector) {
  std::vector<float> v1({0, 0, 1, 0, 0});  // Impulse input.
  const float initial_state = 1;
  float state = initial_state;
  ForwardSmoothVector(0.5, &state, &v1);
  EXPECT_EQ(v1[0], 0.5);          // initial_state * coefficient.
  EXPECT_EQ(v1[1], 0.25);         // Decay by factor 0.5.
  EXPECT_EQ(v1[2], 0.5 + 0.125);  // The impulse comes in here.
  EXPECT_EQ(v1[3], v1[2] / 2);
  EXPECT_EQ(v1[4], v1[3] / 2);
  EXPECT_EQ(state, v1[4]);  // Final state is last value stored.
}

TEST(VectorTest, BackwardSmoothVector) {
  std::vector<float> v1({0, 0, 1, 0, 0});  // Impulse input.
  const float initial_state = 1;
  float state = initial_state;
  BackwardSmoothVector(0.5, &state, &v1);
  // Just reversed indices from the ForwardSmoother test.
  EXPECT_EQ(v1[4], 0.5);
  EXPECT_EQ(v1[3], 0.25);
  EXPECT_EQ(v1[2], 0.5 + 0.125);
  EXPECT_EQ(v1[1], v1[2] / 2);
  EXPECT_EQ(v1[0], v1[1] / 2);
  EXPECT_EQ(state, v1[0]);
}

TEST(VectorTest, SmoothVector) {
  std::vector<float> v1({0, 0, 0, 1, 0, 0, 0});
  SmoothVector(0.5, &v1);
  std::vector<float> expected_half({0.07, 0.105, 0.15, 0.19});
  for (int i = 0; i < expected_half.size(); ++i) {
    // Expect the result to be pretty nearly symmetric.
    EXPECT_NEAR(expected_half[i], v1[i], 5e-3);
    EXPECT_NEAR(v1[i], v1[v1.size() - 1 - i], 3e-3);
  }
  EXPECT_NEAR(std::accumulate(v1.begin(), v1.end(), 0.0), 0.83, 5e-3);
  // Try more room to allow sum to be closer to 1.
  std::vector<float> v2({0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0});
  SmoothVector(0.5, &v2);
  EXPECT_NEAR(std::accumulate(v2.begin(), v2.end(), 0.0), 0.97, 5e-3);
}

}  // namespace
}  // namespace litert::lm