7#include "metrics/hart_metric_query.hpp"
8#include "metrics/hart_metrics_common.hpp"
63 typename MetricQuery<
double>::SingleChannelMetricEvaluator evaluator =
65 (size_t channel,
const Slice& slice,
Unit requestedUnit)
79 const size_t startBin = binIndices.first;
80 const size_t stopBin = binIndices.second;
82 if (slice
.isEmpty() || stopBin - startBin == 0)
88 const std::complex<
double>* bins = spectrum
[channel
];
89 double maxSquaredMagnitude = 0.0;
90 size_t strongestBin = 0;
92 for (size_t currentBin = startBin; currentBin < stopBin; ++currentBin)
94 const double currentSquaredMagnitude = std::norm (bins[currentBin]);
96 if (currentSquaredMagnitude > maxSquaredMagnitude)
98 maxSquaredMagnitude = currentSquaredMagnitude;
99 strongestBin = currentBin;
104 if (strongestBin == 0 || strongestBin == spectrum
.getNumBins() - 1)
112 const double delta = 0.5 * (ym1 - yp1) / (ym1 - 2.0 * y0 + yp1 + 1e-30);
113 const double preciseBin =
static_cast<
double> (strongestBin) + delta;
115 const double fftSize =
static_cast<
double> (spectrum
.getFFTSize());
117 return preciseBin * sampleRateHz /
static_cast<
double> (fftSize);
122 std::move (evaluator)
,
Manages the metrics calculations.
MetricQuery(SingleChannelMetricEvaluator evaluator, size_t totalNumChannels, std::vector< size_t > &&defaultChannelsToProcess)
Create a metric query object for a metric that operates on one channel at a time.
Thrown when an unexpected container size is encountered.
Frequency-domain representation of a multi-channel audio signal.
const std::complex< double > * operator[](size_t channel) const
Returns pointer to read-only magnitudes of a specific channel.
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 getBinMagnitude(size_t channel, size_t binIndex) const
Returns magnitude of a frequency bin, by bin index.
size_t getNumBins() const
Returns number of frequency bins per channel.
size_t getNumChannels() const
Returns number of channels.
Thrown when some metric is requested to return a value in an unsupported unit.
#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 > interpolatedPeakFrequency(const Spectrum &spectrum)
Returns the center frequency of the loudest FFT bin.
FloatType nan()
Returns a quiet NaN value for the given floating-point type.
static SampleType floatsNotEqual(SampleType a, SampleType b, SampleType epsilon=(SampleType) 1e-8)
Compares two floating point numbers within a given tolerance.
static SampleType floatsEqual(SampleType a, SampleType b, SampleType epsilon=(SampleType) 1e-8)
Compares two floating point numbers within a given tolerance.
Unit
Represents a physical unit.
@ native
Default (native) unit of whatever returns some value.
Helpers to generate common default channel subsets.
static std::vector< size_t > allChannels(size_t numChannels)
Represents a slice of analysis data.