10#include "metrics/hart_metric_query.hpp"
11#include "metrics/hart_metrics_common.hpp"
37 typename MetricQuery<
double>::ChannelPairMetricEvaluator evaluator =
38 [&spectrumA, &spectrumB, normaliseLevels, smoothingCents]
39 (size_t spectrumAChannel, size_t spectrumBChannel,
Slice slice,
Unit requestedUnit)
50 const std::pair<size_t, size_t> binIndices = spectrumA
.getBinIndices (slice
);
51 const size_t startBin = std::max<size_t> (1, binIndices.first);
52 const size_t stopBin = binIndices.second;
54 if (slice
.isEmpty() || stopBin - startBin == 0)
61 std::vector<
double> levelsADb;
62 std::vector<
double> levelsBDb;
67 double currentBandEndHz = currentBandStartHz * bandRatio;
69 while (currentBandEndHz <= stopFrequencyHz)
73 size_t numCurrentBandBins = 0;
75 for (size_t bin = startBin; bin < stopBin; ++bin)
79 if (frequencyHz < currentBandStartHz)
82 if (frequencyHz >= currentBandEndHz)
90 if (numCurrentBandBins > 0)
92 const double binMeanPowerA = bandPowerA
.getValue() /
static_cast<
double> (numCurrentBandBins);
93 const double binMeanPowerB = bandPowerB
.getValue() /
static_cast<
double> (numCurrentBandBins);
98 levelsADb.push_back (levelADb);
99 levelsBDb.push_back (levelBDb);
102 levelDifferenceSum
+= (levelBDb - levelADb);
105 currentBandStartHz = currentBandEndHz;
106 currentBandEndHz = currentBandStartHz * bandRatio;
109 hassert (levelsADb.size() == levelsBDb.size());
110 const size_t numPoints = levelsADb.size();
115 const double offsetDb =
118 : levelDifferenceSum
.getValue() /
static_cast<
double> (numPoints);
122 for (size_t i = 0; i < numPoints; ++i)
124 const double errorDb = levelsADb[i] - levelsBDb[i] + offsetDb;
125 squaredErrorSum
+= errorDb * errorDb;
128 return std::sqrt (squaredErrorSum
.getValue() /
static_cast<
double> (numPoints));
133 std::move (evaluator)
,
Implements Kahan algorithm for floating point accumulations.
SampleType getValue() const
AccurateSum & operator+=(SampleType value)
Adds a value to a sum, tracking the potential floating point error.
Manages the metrics calculations.
MetricQuery(ChannelPairMetricEvaluator evaluator, size_t totalNumChannelsA, size_t totalNumChannelsB, std::vector< std::pair< size_t, size_t > > &&defaultChannelPairsToProcess)
Create a metric query object for a metric that operates on pair of channels at a time.
Thrown when sample rate is mismatched or invalid.
Thrown when an unexpected container size is encountered.
Frequency-domain representation of a multi-channel audio signal.
size_t getFFTSize() const
Returns FFT size.
double getSampleRateHz() const
Returns sample rate in Hz.
std::pair< size_t, size_t > getBinIndices(const Slice &slice) const
Returns a pair of indices representing a provided slice.
double getBinFrequencyHz(size_t binIndex) const
Returns frequency corresponding to a bin index.
size_t getNumBins() const
Returns number of frequency bins per channel.
std::complex< double > getBinValue(size_t channel, size_t binIndex) const
Returns complex value of a frequency bin, by bin index.
size_t getNumChannels() const
Returns number of channels.
Thrown when some metric is requested to return a value in an unsupported unit.
Thrown when an inappropriate value is encountered.
#define hassert(condition)
Triggers a HartAssertException if the condition is false
#define HART_THROW_OR_RETURN(ExceptionType, message, returnValue)
Throws an exception if HART_DO_NOT_THROW_EXCEPTIONS is set, prints a message and returns a specified ...
MetricQuery< double > logSpectralDistance(const Spectrum &spectrumA, const Spectrum &spectrumB, Normalise normaliseLevels=Normalise::no, double smoothingCents=100.0)
Calculates difference between two spectra in log-frequency domain.
FloatType nan()
Returns a quiet NaN value for the given floating-point type.
Normalise
Helper values for something that could normalise something.
static SampleType floatsNotEqual(SampleType a, SampleType b, SampleType epsilon=(SampleType) 1e-8)
Compares two floating point numbers within a given tolerance.
static SampleType powerToDecibels(SampleType valueLinear)
Converts linear value (power) to dB.
static SampleType floatsEqual(SampleType a, SampleType b, SampleType epsilon=(SampleType) 1e-8)
Compares two floating point numbers within a given tolerance.
double centsToRatio(double cents)
Converts frequency difference in cents to frequence ratio.
Unit
Represents a physical unit.
@ dB
Value of something in decibels. Can represent voltage, power, or a domain-specific unit like "LUFS" o...
@ native
Default (native) unit of whatever returns some value.
Helpers to generate common default channel subsets.
static std::vector< std::pair< size_t, size_t > > diagonalChannelPairs(size_t numChannels)
Represents a slice of analysis data.