Skip to content

Commit

Permalink
Add a function to compute the log-spectral distance between two sets …
Browse files Browse the repository at this point in the history
…of audio samples

The function also checks that the resulting distance is below a given threshold.

PiperOrigin-RevId: 678820628
  • Loading branch information
Googler authored and jwcullen committed Sep 26, 2024
1 parent 74b970d commit 77135ab
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
4 changes: 4 additions & 0 deletions iamf/cli/tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ cc_library(
"//iamf/obu/decoder_config:lpcm_decoder_config",
"//iamf/obu/decoder_config:opus_decoder_config",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/log",
"@com_google_absl//absl/status:status_matchers",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:span",
"@com_google_googletest//:gtest",
],
)
Expand Down Expand Up @@ -96,6 +99,7 @@ cc_test(
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:status_matchers",
"@com_google_absl//absl/types:span",
"@com_google_googletest//:gtest_main",
"@com_google_protobuf//:protobuf",
],
Expand Down
21 changes: 21 additions & 0 deletions iamf/cli/tests/cli_test_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "iamf/cli/tests/cli_test_utils.h"

#include <algorithm>
#include <cmath>
#include <cstdint>
#include <filesystem>
#include <list>
Expand All @@ -23,9 +24,12 @@
#include <vector>

#include "absl/container/flat_hash_map.h"
#include "absl/log/log.h"
#include "absl/status/status_matchers.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "iamf/cli/audio_element_with_data.h"
Expand Down Expand Up @@ -343,4 +347,21 @@ std::string GetAndCreateOutputDirectory(absl::string_view suffix) {
return output_directory;
}

bool IsLogSpectralDistanceBelowThreshold(
const absl::Span<const InternalSampleType>& first_log_spectrum,
const absl::Span<const InternalSampleType>& second_log_spectrum,
double threshold_db) {
const int num_samples = first_log_spectrum.size();
if (num_samples != second_log_spectrum.size()) {
LOG(ERROR) << "Spectrum sizes are not equal.";
return false;
}
double log_spectral_distance = 0.0;
for (int i = 0; i < num_samples; ++i) {
log_spectral_distance += (first_log_spectrum[i] - second_log_spectrum[i]) *
(first_log_spectrum[i] - second_log_spectrum[i]);
}
return (10 * std::sqrt(log_spectral_distance / num_samples)) <= threshold_db;
}

} // namespace iamf_tools
18 changes: 18 additions & 0 deletions iamf/cli/tests/cli_test_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
#include <vector>

#include "absl/container/flat_hash_map.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "iamf/cli/audio_element_with_data.h"
#include "iamf/cli/demixing_module.h"
#include "iamf/cli/renderer/audio_element_renderer_base.h"
Expand Down Expand Up @@ -190,6 +192,22 @@ std::string GetAndCleanupOutputFileName(absl::string_view suffix);
*/
std::string GetAndCreateOutputDirectory(absl::string_view suffix);

/*!\brief Computes the log-spectral distance (LSD) between two spectra.
*
* The log-spectral distance (LSD) is a distance measure (expressed in dB)
* between two spectra.
*
* \param first_log_spectrum First log-spectrum to compare.
* \param second_log_spectrum Second log-spectrum to compare.
* \param threshold_db LSD threshold to compare against, in db.
* \return true if the log-spectral distance
* between the two spectra is below the specified threshold, false otherwise.
*/
bool IsLogSpectralDistanceBelowThreshold(
const absl::Span<const InternalSampleType>& first_log_spectrum,
const absl::Span<const InternalSampleType>& second_log_spectrum,
double threshold_db);

} // namespace iamf_tools

#endif // CLI_TESTS_CLI_TEST_UTILS_H_
32 changes: 32 additions & 0 deletions iamf/cli/tests/cli_util_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
#include <cstdint>
#include <list>
#include <memory>
#include <numeric>
#include <utility>
#include <vector>

#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "absl/status/status.h"
#include "absl/status/status_matchers.h"
#include "absl/types/span.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "iamf/cli/audio_element_with_data.h"
Expand Down Expand Up @@ -738,5 +740,35 @@ TEST(GenerateParamIdToMetadataMapTest,
EXPECT_FALSE(param_id_to_metadata_map.ok());
}

TEST(IsLogSpectralDistanceBelowThreshold, BelowThresholdSucceeds) {
std::vector<double> first_log_spectrum(10);
std::iota(first_log_spectrum.begin(), first_log_spectrum.end(), 0);
std::vector<double> second_log_spectrum(10);
std::iota(second_log_spectrum.begin(), second_log_spectrum.end(), 1);
EXPECT_TRUE(IsLogSpectralDistanceBelowThreshold(
absl::MakeConstSpan(first_log_spectrum),
absl::MakeConstSpan(second_log_spectrum), 11.0));
}

TEST(IsLogSpectralDistanceBelowThreshold, AtThresholdSucceeds) {
std::vector<double> first_log_spectrum(10);
std::iota(first_log_spectrum.begin(), first_log_spectrum.end(), 0);
std::vector<double> second_log_spectrum(10);
std::iota(second_log_spectrum.begin(), second_log_spectrum.end(), 1);
EXPECT_TRUE(IsLogSpectralDistanceBelowThreshold(
absl::MakeConstSpan(first_log_spectrum),
absl::MakeConstSpan(second_log_spectrum), 10.0));
}

TEST(ExpectLogSpectralDistanceBelowThreshold, AboveThresholdFails) {
std::vector<double> first_log_spectrum(10);
std::iota(first_log_spectrum.begin(), first_log_spectrum.end(), 0);
std::vector<double> second_log_spectrum(10);
std::iota(second_log_spectrum.begin(), second_log_spectrum.end(), 1);
EXPECT_FALSE(IsLogSpectralDistanceBelowThreshold(
absl::MakeConstSpan(first_log_spectrum),
absl::MakeConstSpan(second_log_spectrum), 9.0));
}

} // namespace
} // namespace iamf_tools

0 comments on commit 77135ab

Please sign in to comment.