Convert speech feature generation to use micro testing framework

PiperOrigin-RevId: 233452007
This commit is contained in:
Pete Warden 2019-02-11 12:03:10 -08:00 committed by TensorFlower Gardener
parent 4978ec3b45
commit b6d09dc1a9
8 changed files with 236 additions and 175 deletions

View File

@ -6,6 +6,11 @@ package(
licenses(["notice"]) # Apache 2.0
load(
"//tensorflow/lite/experimental/micro/testing:micro_test.bzl",
"tflite_micro_cc_test",
)
cc_library(
name = "bits",
hdrs = ["bits.h"],
@ -117,72 +122,65 @@ cc_library(
],
)
cc_test(
tflite_micro_cc_test(
name = "fft_test",
size = "small",
srcs = ["fft_test.cc"],
deps = [
":fft",
"@com_google_googletest//:gtest_main",
"//tensorflow/lite/experimental/micro/testing:micro_test",
],
)
cc_test(
tflite_micro_cc_test(
name = "filterbank_test",
size = "small",
srcs = ["filterbank_test.cc"],
deps = [
":filterbank",
"@com_google_googletest//:gtest_main",
"//tensorflow/lite/experimental/micro/testing:micro_test",
],
)
cc_test(
tflite_micro_cc_test(
name = "frontend_test",
size = "small",
srcs = ["frontend_test.cc"],
deps = [
":frontend",
"@com_google_googletest//:gtest_main",
"//tensorflow/lite/experimental/micro/testing:micro_test",
],
)
cc_test(
tflite_micro_cc_test(
name = "log_scale_test",
size = "small",
srcs = ["log_scale_test.cc"],
deps = [
":log_scale",
"@com_google_googletest//:gtest_main",
"//tensorflow/lite/experimental/micro/testing:micro_test",
],
)
cc_test(
tflite_micro_cc_test(
name = "noise_reduction_test",
size = "small",
srcs = ["noise_reduction_test.cc"],
deps = [
":noise_reduction",
"@com_google_googletest//:gtest_main",
"//tensorflow/lite/experimental/micro/testing:micro_test",
],
)
cc_test(
tflite_micro_cc_test(
name = "pcan_gain_control_test",
size = "small",
srcs = ["pcan_gain_control_test.cc"],
deps = [
":pcan_gain_control",
"@com_google_googletest//:gtest_main",
"//tensorflow/lite/experimental/micro/testing:micro_test",
],
)
cc_test(
tflite_micro_cc_test(
name = "window_test",
size = "small",
srcs = ["window_test.cc"],
deps = [
":window",
"@com_google_googletest//:gtest_main",
"//tensorflow/lite/experimental/micro/testing:micro_test",
],
)

View File

@ -15,8 +15,7 @@ limitations under the License.
#include "tensorflow/lite/experimental/microfrontend/lib/fft.h"
#include "tensorflow/lite/experimental/microfrontend/lib/fft_util.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "tensorflow/lite/experimental/micro/testing/micro_test.h"
namespace {
@ -25,9 +24,13 @@ const int16_t kFakeWindow[] = {
0, -28328, 0, 21447, 0, -13312, 0, 5943, 0, -1152, 0};
const int kScaleShift = 0;
TEST(FftTest, CheckOutputValues) {
} // namespace
TF_LITE_MICRO_TESTS_BEGIN
TF_LITE_MICRO_TEST(FftTest_CheckOutputValues) {
struct FftState state;
ASSERT_TRUE(
TF_LITE_MICRO_EXPECT(
FftPopulateState(&state, sizeof(kFakeWindow) / sizeof(kFakeWindow[0])));
FftInit(&state);
@ -37,14 +40,15 @@ TEST(FftTest, CheckOutputValues) {
{0, 0}, {-10, 9}, {-20, 0}, {-9, -10}, {0, 25}, {-119, 119},
{-887, 0}, {3000, 3000}, {0, -6401}, {-3000, 3000}, {886, 0}, {118, 119},
{0, 25}, {9, -10}, {19, 0}, {9, 9}, {0, 0}};
ASSERT_EQ(state.fft_size / 2 + 1, sizeof(expected) / sizeof(expected[0]));
TF_LITE_MICRO_EXPECT_EQ(state.fft_size / 2 + 1,
sizeof(expected) / sizeof(expected[0]));
int i;
for (i = 0; i <= state.fft_size / 2; ++i) {
EXPECT_EQ(state.output[i].real, expected[i].real);
EXPECT_EQ(state.output[i].imag, expected[i].imag);
TF_LITE_MICRO_EXPECT_EQ(state.output[i].real, expected[i].real);
TF_LITE_MICRO_EXPECT_EQ(state.output[i].imag, expected[i].imag);
}
FftFreeStateContents(&state);
}
} // namespace
TF_LITE_MICRO_TESTS_END

View File

@ -17,8 +17,7 @@ limitations under the License.
#include <cstring>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "tensorflow/lite/experimental/micro/testing/micro_test.h"
namespace {
@ -33,9 +32,9 @@ const uint64_t kWork[] = {1835887, 61162970173, 258694800000};
const int kScaleShift = 0;
// Test filterbank generation using scaled-down defaults.
class FilterbankTest : public ::testing::Test {
protected:
FilterbankTest() {
class FilterbankTestConfig {
public:
FilterbankTestConfig() {
config_.num_channels = 2;
config_.lower_band_limit = 8.0;
config_.upper_band_limit = 450.0;
@ -44,110 +43,124 @@ class FilterbankTest : public ::testing::Test {
struct FilterbankConfig config_;
};
TEST_F(FilterbankTest, CheckStartIndex) {
struct FilterbankState state;
ASSERT_TRUE(
FilterbankPopulateState(&config_, &state, kSampleRate, kSpectrumSize));
} // namespace
EXPECT_EQ(state.start_index, kStartIndex);
TF_LITE_MICRO_TESTS_BEGIN
TF_LITE_MICRO_TEST(FilterbankTest_CheckStartIndex) {
FilterbankTestConfig config;
struct FilterbankState state;
TF_LITE_MICRO_EXPECT(FilterbankPopulateState(&config.config_, &state,
kSampleRate, kSpectrumSize));
TF_LITE_MICRO_EXPECT_EQ(state.start_index, kStartIndex);
FilterbankFreeStateContents(&state);
}
TEST_F(FilterbankTest, CheckEndIndex) {
TF_LITE_MICRO_TEST(FilterbankTest_CheckEndIndex) {
FilterbankTestConfig config;
struct FilterbankState state;
ASSERT_TRUE(
FilterbankPopulateState(&config_, &state, kSampleRate, kSpectrumSize));
TF_LITE_MICRO_EXPECT(FilterbankPopulateState(&config.config_, &state,
kSampleRate, kSpectrumSize));
EXPECT_EQ(state.end_index, kEndIndex);
TF_LITE_MICRO_EXPECT_EQ(state.end_index, kEndIndex);
FilterbankFreeStateContents(&state);
}
TEST_F(FilterbankTest, CheckChannelFrequencyStarts) {
TF_LITE_MICRO_TEST(FilterbankTest_CheckChannelFrequencyStarts) {
FilterbankTestConfig config;
struct FilterbankState state;
ASSERT_TRUE(
FilterbankPopulateState(&config_, &state, kSampleRate, kSpectrumSize));
TF_LITE_MICRO_EXPECT(FilterbankPopulateState(&config.config_, &state,
kSampleRate, kSpectrumSize));
const int16_t expected[] = {0, 4, 8};
ASSERT_EQ(state.num_channels + 1, sizeof(expected) / sizeof(expected[0]));
TF_LITE_MICRO_EXPECT_EQ(state.num_channels + 1,
sizeof(expected) / sizeof(expected[0]));
int i;
for (i = 0; i <= state.num_channels; ++i) {
EXPECT_EQ(state.channel_frequency_starts[i], expected[i]);
TF_LITE_MICRO_EXPECT_EQ(state.channel_frequency_starts[i], expected[i]);
}
FilterbankFreeStateContents(&state);
}
TEST_F(FilterbankTest, CheckChannelWeightStarts) {
TF_LITE_MICRO_TEST(FilterbankTest_CheckChannelWeightStarts) {
FilterbankTestConfig config;
struct FilterbankState state;
ASSERT_TRUE(
FilterbankPopulateState(&config_, &state, kSampleRate, kSpectrumSize));
TF_LITE_MICRO_EXPECT(FilterbankPopulateState(&config.config_, &state,
kSampleRate, kSpectrumSize));
const int16_t expected[] = {0, 8, 16};
ASSERT_EQ(state.num_channels + 1, sizeof(expected) / sizeof(expected[0]));
TF_LITE_MICRO_EXPECT_EQ(state.num_channels + 1,
sizeof(expected) / sizeof(expected[0]));
int i;
for (i = 0; i <= state.num_channels; ++i) {
EXPECT_EQ(state.channel_weight_starts[i], expected[i]);
TF_LITE_MICRO_EXPECT_EQ(state.channel_weight_starts[i], expected[i]);
}
FilterbankFreeStateContents(&state);
}
TEST_F(FilterbankTest, CheckChannelWidths) {
TF_LITE_MICRO_TEST(FilterbankTest_CheckChannelWidths) {
FilterbankTestConfig config;
struct FilterbankState state;
ASSERT_TRUE(
FilterbankPopulateState(&config_, &state, kSampleRate, kSpectrumSize));
TF_LITE_MICRO_EXPECT(FilterbankPopulateState(&config.config_, &state,
kSampleRate, kSpectrumSize));
const int16_t expected[] = {8, 8, 8};
ASSERT_EQ(state.num_channels + 1, sizeof(expected) / sizeof(expected[0]));
TF_LITE_MICRO_EXPECT_EQ(state.num_channels + 1,
sizeof(expected) / sizeof(expected[0]));
int i;
for (i = 0; i <= state.num_channels; ++i) {
EXPECT_EQ(state.channel_widths[i], expected[i]);
TF_LITE_MICRO_EXPECT_EQ(state.channel_widths[i], expected[i]);
}
FilterbankFreeStateContents(&state);
}
TEST_F(FilterbankTest, CheckWeights) {
TF_LITE_MICRO_TEST(FilterbankTest_CheckWeights) {
FilterbankTestConfig config;
struct FilterbankState state;
ASSERT_TRUE(
FilterbankPopulateState(&config_, &state, kSampleRate, kSpectrumSize));
TF_LITE_MICRO_EXPECT(FilterbankPopulateState(&config.config_, &state,
kSampleRate, kSpectrumSize));
const int16_t expected[] = {0, 3277, 2217, 1200, 222, 0, 0, 0,
0, 3376, 2468, 1591, 744, 0, 0, 0,
0, 4020, 3226, 2456, 1708, 983, 277, 0};
ASSERT_EQ(state.channel_weight_starts[state.num_channels] +
TF_LITE_MICRO_EXPECT_EQ(state.channel_weight_starts[state.num_channels] +
state.channel_widths[state.num_channels],
sizeof(expected) / sizeof(expected[0]));
int i;
for (i = 0; i < sizeof(expected) / sizeof(expected[0]); ++i) {
EXPECT_EQ(state.weights[i], expected[i]);
TF_LITE_MICRO_EXPECT_EQ(state.weights[i], expected[i]);
}
FilterbankFreeStateContents(&state);
}
TEST_F(FilterbankTest, CheckUnweights) {
TF_LITE_MICRO_TEST(FilterbankTest_CheckUnweights) {
FilterbankTestConfig config;
struct FilterbankState state;
ASSERT_TRUE(
FilterbankPopulateState(&config_, &state, kSampleRate, kSpectrumSize));
TF_LITE_MICRO_EXPECT(FilterbankPopulateState(&config.config_, &state,
kSampleRate, kSpectrumSize));
const int16_t expected[] = {0, 819, 1879, 2896, 3874, 0, 0, 0,
0, 720, 1628, 2505, 3352, 0, 0, 0,
0, 76, 870, 1640, 2388, 3113, 3819, 0};
ASSERT_EQ(state.channel_weight_starts[state.num_channels] +
TF_LITE_MICRO_EXPECT_EQ(state.channel_weight_starts[state.num_channels] +
state.channel_widths[state.num_channels],
sizeof(expected) / sizeof(expected[0]));
int i;
for (i = 0; i < sizeof(expected) / sizeof(expected[0]); ++i) {
EXPECT_EQ(state.unweights[i], expected[i]);
TF_LITE_MICRO_EXPECT_EQ(state.unweights[i], expected[i]);
}
FilterbankFreeStateContents(&state);
}
TEST_F(FilterbankTest, CheckConvertFftComplexToEnergy) {
TF_LITE_MICRO_TEST(FilterbankTest_CheckConvertFftComplexToEnergy) {
struct FilterbankState state;
state.start_index = kStartIndex;
state.end_index = kEndIndex;
@ -161,42 +174,46 @@ TEST_F(FilterbankTest, CheckConvertFftComplexToEnergy) {
int i;
for (i = state.start_index; i < state.end_index; ++i) {
EXPECT_EQ(energy[i], kEnergy[i]);
TF_LITE_MICRO_EXPECT_EQ(energy[i], kEnergy[i]);
}
}
TEST_F(FilterbankTest, CheckAccumulateChannels) {
TF_LITE_MICRO_TEST(FilterbankTest_CheckAccumulateChannels) {
FilterbankTestConfig config;
struct FilterbankState state;
ASSERT_TRUE(
FilterbankPopulateState(&config_, &state, kSampleRate, kSpectrumSize));
TF_LITE_MICRO_EXPECT(FilterbankPopulateState(&config.config_, &state,
kSampleRate, kSpectrumSize));
FilterbankAccumulateChannels(&state, kEnergy);
ASSERT_EQ(state.num_channels + 1, sizeof(kWork) / sizeof(kWork[0]));
TF_LITE_MICRO_EXPECT_EQ(state.num_channels + 1,
sizeof(kWork) / sizeof(kWork[0]));
int i;
for (i = 0; i <= state.num_channels; ++i) {
EXPECT_EQ(state.work[i], kWork[i]);
TF_LITE_MICRO_EXPECT_EQ(state.work[i], kWork[i]);
}
FilterbankFreeStateContents(&state);
}
TEST_F(FilterbankTest, CheckSqrt) {
TF_LITE_MICRO_TEST(FilterbankTest_CheckSqrt) {
FilterbankTestConfig config;
struct FilterbankState state;
ASSERT_TRUE(
FilterbankPopulateState(&config_, &state, kSampleRate, kSpectrumSize));
TF_LITE_MICRO_EXPECT(FilterbankPopulateState(&config.config_, &state,
kSampleRate, kSpectrumSize));
std::memcpy(state.work, kWork, sizeof(kWork));
uint32_t* scaled_filterbank = FilterbankSqrt(&state, kScaleShift);
const uint32_t expected[] = {247311, 508620};
ASSERT_EQ(state.num_channels, sizeof(expected) / sizeof(expected[0]));
TF_LITE_MICRO_EXPECT_EQ(state.num_channels,
sizeof(expected) / sizeof(expected[0]));
int i;
for (i = 0; i < state.num_channels; ++i) {
EXPECT_EQ(scaled_filterbank[i], expected[i]);
TF_LITE_MICRO_EXPECT_EQ(scaled_filterbank[i], expected[i]);
}
FilterbankFreeStateContents(&state);
}
} // namespace
TF_LITE_MICRO_TESTS_END

View File

@ -15,8 +15,7 @@ limitations under the License.
#include "tensorflow/lite/experimental/microfrontend/lib/frontend.h"
#include "tensorflow/lite/experimental/microfrontend/lib/frontend_util.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "tensorflow/lite/experimental/micro/testing/micro_test.h"
namespace {
@ -29,9 +28,9 @@ const int16_t kFakeAudioData[] = {
0, 32767, 0, -32768, 0, 32767, 0, -32768, 0, 32767, 0, -32768};
// Test end-to-end frontend behaviors.
class FrontendTest : public ::testing::Test {
protected:
FrontendTest() {
class FrontendTestConfig {
public:
FrontendTestConfig() {
config_.window.size_ms = 25;
config_.window.step_size_ms = 10;
config_.noise_reduction.smoothing_bits = 10;
@ -53,9 +52,15 @@ class FrontendTest : public ::testing::Test {
struct FrontendConfig config_;
};
TEST_F(FrontendTest, CheckOutputValues) {
} // namespace
TF_LITE_MICRO_TESTS_BEGIN
TF_LITE_MICRO_TEST(FrontendTest_CheckOutputValues) {
FrontendTestConfig config;
struct FrontendState state;
ASSERT_TRUE(FrontendPopulateState(&config_, &state, kSampleRate));
TF_LITE_MICRO_EXPECT(
FrontendPopulateState(&config.config_, &state, kSampleRate));
size_t num_samples_read;
struct FrontendOutput output = FrontendProcessSamples(
@ -63,18 +68,20 @@ TEST_F(FrontendTest, CheckOutputValues) {
sizeof(kFakeAudioData) / sizeof(kFakeAudioData[0]), &num_samples_read);
const uint16_t expected[] = {479, 425};
ASSERT_EQ(output.size, sizeof(expected) / sizeof(expected[0]));
TF_LITE_MICRO_EXPECT_EQ(output.size, sizeof(expected) / sizeof(expected[0]));
int i;
for (i = 0; i < output.size; ++i) {
EXPECT_EQ(output.values[i], expected[i]);
TF_LITE_MICRO_EXPECT_EQ(output.values[i], expected[i]);
}
FrontendFreeStateContents(&state);
}
TEST_F(FrontendTest, CheckConsecutiveWindow) {
TF_LITE_MICRO_TEST(FrontendTest_CheckConsecutiveWindow) {
FrontendTestConfig config;
struct FrontendState state;
ASSERT_TRUE(FrontendPopulateState(&config_, &state, kSampleRate));
TF_LITE_MICRO_EXPECT(
FrontendPopulateState(&config.config_, &state, kSampleRate));
size_t num_samples_read;
FrontendProcessSamples(&state, kFakeAudioData,
@ -86,18 +93,20 @@ TEST_F(FrontendTest, CheckConsecutiveWindow) {
&num_samples_read);
const int16_t expected[] = {436, 378};
ASSERT_EQ(output.size, sizeof(expected) / sizeof(expected[0]));
TF_LITE_MICRO_EXPECT_EQ(output.size, sizeof(expected) / sizeof(expected[0]));
int i;
for (i = 0; i < output.size; ++i) {
EXPECT_EQ(output.values[i], expected[i]);
TF_LITE_MICRO_EXPECT_EQ(output.values[i], expected[i]);
}
FrontendFreeStateContents(&state);
}
TEST_F(FrontendTest, CheckNotEnoughSamples) {
TF_LITE_MICRO_TEST(FrontendTest_CheckNotEnoughSamples) {
FrontendTestConfig config;
struct FrontendState state;
ASSERT_TRUE(FrontendPopulateState(&config_, &state, kSampleRate));
TF_LITE_MICRO_EXPECT(
FrontendPopulateState(&config.config_, &state, kSampleRate));
size_t num_samples_read;
FrontendProcessSamples(&state, kFakeAudioData,
@ -113,10 +122,10 @@ TEST_F(FrontendTest, CheckNotEnoughSamples) {
kStepSamples,
&num_samples_read);
EXPECT_EQ(output.size, 0);
EXPECT_EQ(output.values, nullptr);
TF_LITE_MICRO_EXPECT_EQ(output.size, 0);
TF_LITE_MICRO_EXPECT_EQ(output.values, nullptr);
FrontendFreeStateContents(&state);
}
} // namespace
TF_LITE_MICRO_TESTS_END

View File

@ -15,15 +15,18 @@ limitations under the License.
#include "tensorflow/lite/experimental/microfrontend/lib/log_scale.h"
#include "tensorflow/lite/experimental/microfrontend/lib/log_scale_util.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "tensorflow/lite/experimental/micro/testing/micro_test.h"
namespace {
const int kScaleShift = 6;
const int kCorrectionBits = -1;
TEST(LogScaleTest, CheckOutputValues) {
} // namespace
TF_LITE_MICRO_TESTS_BEGIN
TF_LITE_MICRO_TEST(LogScaleTest_CheckOutputValues) {
struct LogScaleState state;
state.enable_log = true;
state.scale_shift = kScaleShift;
@ -36,11 +39,11 @@ TEST(LogScaleTest, CheckOutputValues) {
const uint16_t expected[] = {479, 425};
int i;
for (i = 0; i < sizeof(expected) / sizeof(expected[0]); ++i) {
EXPECT_EQ(output[i], expected[i]);
TF_LITE_MICRO_EXPECT_EQ(output[i], expected[i]);
}
}
TEST(LogScaleTest, CheckOutputValuesNoLog) {
TF_LITE_MICRO_TEST(LogScaleTest_CheckOutputValuesNoLog) {
struct LogScaleState state;
state.enable_log = false;
state.scale_shift = kScaleShift;
@ -53,8 +56,8 @@ TEST(LogScaleTest, CheckOutputValuesNoLog) {
const uint16_t expected[] = {65535, 45998};
int i;
for (i = 0; i < sizeof(expected) / sizeof(expected[0]); ++i) {
EXPECT_EQ(output[i], expected[i]);
TF_LITE_MICRO_EXPECT_EQ(output[i], expected[i]);
}
}
} // namespace
TF_LITE_MICRO_TESTS_END

View File

@ -15,17 +15,16 @@ limitations under the License.
#include "tensorflow/lite/experimental/microfrontend/lib/noise_reduction.h"
#include "tensorflow/lite/experimental/microfrontend/lib/noise_reduction_util.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "tensorflow/lite/experimental/micro/testing/micro_test.h"
namespace {
const int kNumChannels = 2;
// Test noise reduction using default config values.
class NoiseReductionTest : public ::testing::Test {
protected:
NoiseReductionTest() {
class NoiseReductionTestConfig {
public:
NoiseReductionTestConfig() {
config_.smoothing_bits = 10;
config_.even_smoothing = 0.025;
config_.odd_smoothing = 0.06;
@ -35,38 +34,48 @@ class NoiseReductionTest : public ::testing::Test {
struct NoiseReductionConfig config_;
};
TEST_F(NoiseReductionTest, TestNoiseReductionEstimate) {
} // namespace
TF_LITE_MICRO_TESTS_BEGIN
TF_LITE_MICRO_TEST(NoiseReductionTest_TestNoiseReductionEstimate) {
NoiseReductionTestConfig config;
struct NoiseReductionState state;
ASSERT_TRUE(NoiseReductionPopulateState(&config_, &state, kNumChannels));
TF_LITE_MICRO_EXPECT(
NoiseReductionPopulateState(&config.config_, &state, kNumChannels));
uint32_t signal[] = {247311, 508620};
NoiseReductionApply(&state, signal);
const uint32_t expected[] = {6321887, 31248341};
ASSERT_EQ(state.num_channels, sizeof(expected) / sizeof(expected[0]));
TF_LITE_MICRO_EXPECT_EQ(state.num_channels,
sizeof(expected) / sizeof(expected[0]));
int i;
for (i = 0; i < state.num_channels; ++i) {
EXPECT_EQ(state.estimate[i], expected[i]);
TF_LITE_MICRO_EXPECT_EQ(state.estimate[i], expected[i]);
}
NoiseReductionFreeStateContents(&state);
}
TEST_F(NoiseReductionTest, TestNoiseReduction) {
TF_LITE_MICRO_TEST(NoiseReductionTest_TestNoiseReduction) {
NoiseReductionTestConfig config;
struct NoiseReductionState state;
ASSERT_TRUE(NoiseReductionPopulateState(&config_, &state, kNumChannels));
TF_LITE_MICRO_EXPECT(
NoiseReductionPopulateState(&config.config_, &state, kNumChannels));
uint32_t signal[] = {247311, 508620};
NoiseReductionApply(&state, signal);
const uint32_t expected[] = {241137, 478104};
ASSERT_EQ(state.num_channels, sizeof(expected) / sizeof(expected[0]));
TF_LITE_MICRO_EXPECT_EQ(state.num_channels,
sizeof(expected) / sizeof(expected[0]));
int i;
for (i = 0; i < state.num_channels; ++i) {
EXPECT_EQ(signal[i], expected[i]);
TF_LITE_MICRO_EXPECT_EQ(signal[i], expected[i]);
}
NoiseReductionFreeStateContents(&state);
}
} // namespace
TF_LITE_MICRO_TESTS_END

View File

@ -15,8 +15,7 @@ limitations under the License.
#include "tensorflow/lite/experimental/microfrontend/lib/pcan_gain_control.h"
#include "tensorflow/lite/experimental/microfrontend/lib/pcan_gain_control_util.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "tensorflow/lite/experimental/micro/testing/micro_test.h"
namespace {
@ -25,9 +24,9 @@ const int kSmoothingBits = 10;
const int kCorrectionBits = -1;
// Test pcan auto gain control using default config values.
class PcanGainControlTest : public ::testing::Test {
protected:
PcanGainControlTest() {
class PcanGainControlTestConfig {
public:
PcanGainControlTestConfig() {
config_.enable_pcan = 1;
config_.strength = 0.95;
config_.offset = 80.0;
@ -37,24 +36,30 @@ class PcanGainControlTest : public ::testing::Test {
struct PcanGainControlConfig config_;
};
TEST_F(PcanGainControlTest, TestPcanGainControl) {
} // namespace
TF_LITE_MICRO_TESTS_BEGIN
TF_LITE_MICRO_TEST(PcanGainControlTest_TestPcanGainControl) {
uint32_t estimate[] = {6321887, 31248341};
PcanGainControlTestConfig config;
struct PcanGainControlState state;
ASSERT_TRUE(PcanGainControlPopulateState(&config_, &state, estimate,
kNumChannels, kSmoothingBits,
TF_LITE_MICRO_EXPECT(PcanGainControlPopulateState(
&config.config_, &state, estimate, kNumChannels, kSmoothingBits,
kCorrectionBits));
uint32_t signal[] = {241137, 478104};
PcanGainControlApply(&state, signal);
const uint32_t expected[] = {3578, 1533};
ASSERT_EQ(state.num_channels, sizeof(expected) / sizeof(expected[0]));
TF_LITE_MICRO_EXPECT_EQ(state.num_channels,
sizeof(expected) / sizeof(expected[0]));
int i;
for (i = 0; i < state.num_channels; ++i) {
EXPECT_EQ(signal[i], expected[i]);
TF_LITE_MICRO_EXPECT_EQ(signal[i], expected[i]);
}
PcanGainControlFreeStateContents(&state);
}
} // namespace
TF_LITE_MICRO_TESTS_END

View File

@ -15,8 +15,7 @@ limitations under the License.
#include "tensorflow/lite/experimental/microfrontend/lib/window.h"
#include "tensorflow/lite/experimental/microfrontend/lib/window_util.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "tensorflow/lite/experimental/micro/testing/micro_test.h"
namespace {
@ -29,9 +28,9 @@ const int16_t kFakeAudioData[] = {
0, 32767, 0, -32768, 0, 32767, 0, -32768, 0, 32767, 0, -32768};
// Test window function behaviors using default config values.
class WindowTest : public ::testing::Test {
protected:
WindowTest() {
class WindowTestConfig {
public:
WindowTestConfig() {
config_.size_ms = 25;
config_.step_size_ms = 10;
}
@ -39,84 +38,98 @@ class WindowTest : public ::testing::Test {
struct WindowConfig config_;
};
TEST_F(WindowTest, CheckCoefficients) {
} // namespace
TF_LITE_MICRO_TESTS_BEGIN
TF_LITE_MICRO_TEST(WindowState_CheckCoefficients) {
WindowTestConfig config;
struct WindowState state;
ASSERT_TRUE(WindowPopulateState(&config_, &state, kSampleRate));
TF_LITE_MICRO_EXPECT(
WindowPopulateState(&config.config_, &state, kSampleRate));
const int16_t expected[] = {16, 144, 391, 743, 1176, 1664, 2177,
2681, 3145, 3541, 3843, 4032, 4096, 4032,
3843, 3541, 3145, 2681, 2177, 1664, 1176,
743, 391, 144, 16};
ASSERT_EQ(state.size, sizeof(expected) / sizeof(expected[0]));
TF_LITE_MICRO_EXPECT_EQ(state.size, sizeof(expected) / sizeof(expected[0]));
int i;
for (i = 0; i < state.size; ++i) {
EXPECT_EQ(state.coefficients[i], expected[i]);
TF_LITE_MICRO_EXPECT_EQ(state.coefficients[i], expected[i]);
}
WindowFreeStateContents(&state);
}
TEST_F(WindowTest, CheckResidualInput) {
TF_LITE_MICRO_TEST(WindowState_CheckResidualInput) {
WindowTestConfig config;
struct WindowState state;
ASSERT_TRUE(WindowPopulateState(&config_, &state, kSampleRate));
TF_LITE_MICRO_EXPECT(
WindowPopulateState(&config.config_, &state, kSampleRate));
size_t num_samples_read;
ASSERT_TRUE(WindowProcessSamples(
TF_LITE_MICRO_EXPECT(WindowProcessSamples(
&state, kFakeAudioData,
sizeof(kFakeAudioData) / sizeof(kFakeAudioData[0]), &num_samples_read));
int i;
for (i = kStepSamples; i < kWindowSamples; ++i) {
EXPECT_EQ(state.input[i - kStepSamples], kFakeAudioData[i]);
TF_LITE_MICRO_EXPECT_EQ(state.input[i - kStepSamples], kFakeAudioData[i]);
}
WindowFreeStateContents(&state);
}
TEST_F(WindowTest, CheckOutputValues) {
TF_LITE_MICRO_TEST(WindowState_CheckOutputValues) {
WindowTestConfig config;
struct WindowState state;
ASSERT_TRUE(WindowPopulateState(&config_, &state, kSampleRate));
TF_LITE_MICRO_EXPECT(
WindowPopulateState(&config.config_, &state, kSampleRate));
size_t num_samples_read;
ASSERT_TRUE(WindowProcessSamples(
TF_LITE_MICRO_EXPECT(WindowProcessSamples(
&state, kFakeAudioData,
sizeof(kFakeAudioData) / sizeof(kFakeAudioData[0]), &num_samples_read));
const int16_t expected[] = {
0, 1151, 0, -5944, 0, 13311, 0, -21448, 0, 28327, 0, -32256, 0, 32255,
0, -28328, 0, 21447, 0, -13312, 0, 5943, 0, -1152, 0};
ASSERT_EQ(state.size, sizeof(expected) / sizeof(expected[0]));
TF_LITE_MICRO_EXPECT_EQ(state.size, sizeof(expected) / sizeof(expected[0]));
int i;
for (i = 0; i < state.size; ++i) {
EXPECT_EQ(state.output[i], expected[i]);
TF_LITE_MICRO_EXPECT_EQ(state.output[i], expected[i]);
}
WindowFreeStateContents(&state);
}
TEST_F(WindowTest, CheckMaxAbsValue) {
TF_LITE_MICRO_TEST(WindowState_CheckMaxAbsValue) {
WindowTestConfig config;
struct WindowState state;
ASSERT_TRUE(WindowPopulateState(&config_, &state, kSampleRate));
TF_LITE_MICRO_EXPECT(
WindowPopulateState(&config.config_, &state, kSampleRate));
size_t num_samples_read;
ASSERT_TRUE(WindowProcessSamples(
TF_LITE_MICRO_EXPECT(WindowProcessSamples(
&state, kFakeAudioData,
sizeof(kFakeAudioData) / sizeof(kFakeAudioData[0]), &num_samples_read));
EXPECT_EQ(state.max_abs_output_value, 32256);
TF_LITE_MICRO_EXPECT_EQ(state.max_abs_output_value, 32256);
WindowFreeStateContents(&state);
}
TEST_F(WindowTest, CheckConsecutiveWindow) {
TF_LITE_MICRO_TEST(WindowState_CheckConsecutiveWindow) {
WindowTestConfig config;
struct WindowState state;
ASSERT_TRUE(WindowPopulateState(&config_, &state, kSampleRate));
TF_LITE_MICRO_EXPECT(
WindowPopulateState(&config.config_, &state, kSampleRate));
size_t num_samples_read;
ASSERT_TRUE(WindowProcessSamples(
TF_LITE_MICRO_EXPECT(WindowProcessSamples(
&state, kFakeAudioData,
sizeof(kFakeAudioData) / sizeof(kFakeAudioData[0]), &num_samples_read));
ASSERT_TRUE(WindowProcessSamples(
TF_LITE_MICRO_EXPECT(WindowProcessSamples(
&state, kFakeAudioData + kWindowSamples,
sizeof(kFakeAudioData) / sizeof(kFakeAudioData[0]) - kWindowSamples,
&num_samples_read));
@ -124,38 +137,41 @@ TEST_F(WindowTest, CheckConsecutiveWindow) {
const int16_t expected[] = {
0, -1152, 0, 5943, 0, -13312, 0, 21447, 0, -28328, 0, 32255, 0, -32256,
0, 28327, 0, -21448, 0, 13311, 0, -5944, 0, 1151, 0};
ASSERT_EQ(state.size, sizeof(expected) / sizeof(expected[0]));
TF_LITE_MICRO_EXPECT_EQ(state.size, sizeof(expected) / sizeof(expected[0]));
int i;
for (i = 0; i < state.size; ++i) {
EXPECT_EQ(state.output[i], expected[i]);
TF_LITE_MICRO_EXPECT_EQ(state.output[i], expected[i]);
}
WindowFreeStateContents(&state);
}
TEST_F(WindowTest, CheckNotEnoughSamples) {
TF_LITE_MICRO_TEST(WindowState_CheckNotEnoughSamples) {
WindowTestConfig config;
struct WindowState state;
ASSERT_TRUE(WindowPopulateState(&config_, &state, kSampleRate));
TF_LITE_MICRO_EXPECT(
WindowPopulateState(&config.config_, &state, kSampleRate));
size_t num_samples_read;
ASSERT_TRUE(WindowProcessSamples(
TF_LITE_MICRO_EXPECT(WindowProcessSamples(
&state, kFakeAudioData,
sizeof(kFakeAudioData) / sizeof(kFakeAudioData[0]), &num_samples_read));
ASSERT_TRUE(WindowProcessSamples(
TF_LITE_MICRO_EXPECT(WindowProcessSamples(
&state, kFakeAudioData + kWindowSamples,
sizeof(kFakeAudioData) / sizeof(kFakeAudioData[0]) - kWindowSamples,
&num_samples_read));
ASSERT_FALSE(WindowProcessSamples(
TF_LITE_MICRO_EXPECT_EQ(
false, WindowProcessSamples(
&state, kFakeAudioData + kWindowSamples + kStepSamples,
sizeof(kFakeAudioData) / sizeof(kFakeAudioData[0]) - kWindowSamples -
kStepSamples,
sizeof(kFakeAudioData) / sizeof(kFakeAudioData[0]) -
kWindowSamples - kStepSamples,
&num_samples_read));
EXPECT_EQ(
TF_LITE_MICRO_EXPECT_EQ(
state.input_used,
sizeof(kFakeAudioData) / sizeof(kFakeAudioData[0]) - 2 * kStepSamples);
WindowFreeStateContents(&state);
}
} // namespace
TF_LITE_MICRO_TESTS_END