ipa: rpi: histogram: Add interBinMean()

Add a new helper function Histogram::interBinMean() that essentially
replaces the existing Histogram::interQuantileMean() logic but working on
bins instead.

Rework the interQuantileMean() to call into interBinMean() with the
appropriate convertion from quatiles to bins.

Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
Reviewed-by: David Plowman <david.plowman@raspberrypi.com>
Reviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
This commit is contained in:
Naushir Patuck 2023-09-15 16:58:40 +01:00 committed by Jacopo Mondi
parent e5f0846695
commit cd940f7fd3
2 changed files with 16 additions and 8 deletions

View file

@ -45,20 +45,26 @@ double Histogram::quantile(double q, int first, int last) const
return first + frac; return first + frac;
} }
double Histogram::interQuantileMean(double qLo, double qHi) const double Histogram::interBinMean(double binLo, double binHi) const
{ {
assert(qHi > qLo); assert(binHi > binLo);
double pLo = quantile(qLo);
double pHi = quantile(qHi, (int)pLo);
double sumBinFreq = 0, cumulFreq = 0; double sumBinFreq = 0, cumulFreq = 0;
for (double pNext = floor(pLo) + 1.0; pNext <= ceil(pHi); for (double binNext = floor(binLo) + 1.0; binNext <= ceil(binHi);
pLo = pNext, pNext += 1.0) { binLo = binNext, binNext += 1.0) {
int bin = floor(pLo); int bin = floor(binLo);
double freq = (cumulative_[bin + 1] - cumulative_[bin]) * double freq = (cumulative_[bin + 1] - cumulative_[bin]) *
(std::min(pNext, pHi) - pLo); (std::min(binNext, binHi) - binLo);
sumBinFreq += bin * freq; sumBinFreq += bin * freq;
cumulFreq += freq; cumulFreq += freq;
} }
/* add 0.5 to give an average for bin mid-points */ /* add 0.5 to give an average for bin mid-points */
return sumBinFreq / cumulFreq + 0.5; return sumBinFreq / cumulFreq + 0.5;
} }
double Histogram::interQuantileMean(double qLo, double qHi) const
{
assert(qHi > qLo);
double pLo = quantile(qLo);
double pHi = quantile(qHi, (int)pLo);
return interBinMean(pLo, pHi);
}

View file

@ -38,6 +38,8 @@ public:
uint64_t total() const { return cumulative_[cumulative_.size() - 1]; } uint64_t total() const { return cumulative_[cumulative_.size() - 1]; }
/* Cumulative frequency up to a (fractional) point in a bin. */ /* Cumulative frequency up to a (fractional) point in a bin. */
uint64_t cumulativeFreq(double bin) const; uint64_t cumulativeFreq(double bin) const;
/* Return the mean value between two (fractional) bins. */
double interBinMean(double binLo, double binHi) const;
/* /*
* Return the (fractional) bin of the point q (0 <= q <= 1) through the * Return the (fractional) bin of the point q (0 <= q <= 1) through the
* histogram. Optionally provide limits to help. * histogram. Optionally provide limits to help.