[Positioner] Measurement: fix recalculate_average()

Adds the attribute variance_size to handle the number of elements taken
into account in the variance computation, instead of relying on
ss_list.size(), which is a mistake in the case of recalculate_average().
This commit is contained in:
Matteo Cypriani 2012-06-19 17:17:10 +02:00
parent b952dc94ab
commit 6d40ecc8c5
2 changed files with 17 additions and 7 deletions

View File

@ -117,6 +117,7 @@ void Measurement::clear()
average_mw = 0 ;
variance_mw = 0 ;
variance_mw_m2 = 0 ;
variance_size = 0 ;
ap = NULL ;
}
@ -186,6 +187,7 @@ void Measurement::recalculate_average()
average_mw = 0 ;
variance_mw = 0 ;
variance_mw_m2 = 0 ;
variance_size = 0 ;
for (map<pkt_id_t, ss_t>::const_iterator
i = ss_list.begin() ; i != ss_list.end() ; ++i)
@ -195,18 +197,21 @@ void Measurement::recalculate_average()
void Measurement::update_average(ss_t ss_dbm)
{
++variance_size ;
assert(variance_size <= ss_list.size()) ;
// Convert the new SS in mW:
float ss_mw = pow(10, static_cast<float>(ss_dbm) / 10.0) ;
// Update the average:
float delta = ss_mw - average_mw ;
average_mw += delta / ss_list.size() ;
average_mw += delta / variance_size ;
average_dbm = 10.0 * log10(average_mw) ;
// Update the variance:
variance_mw_m2 += delta * (ss_mw - average_mw) ;
if (ss_list.size() > 1)
variance_mw = variance_mw_m2 / (ss_list.size() - 1) ;
if (variance_size > 1)
variance_mw = variance_mw_m2 / (variance_size - 1) ;
}
@ -225,6 +230,7 @@ Measurement& Measurement::operator=(const Measurement &m)
average_mw = m.average_mw ;
variance_mw = m.variance_mw ;
variance_mw_m2 = m.variance_mw_m2 ;
variance_size = m.variance_size ;
return *this ;
}

View File

@ -33,8 +33,10 @@ protected:
float variance_mw ;
private:
/// Intermediate variable used to compute the variance
/// Intermediate variable used to compute the variance (mW)
float variance_mw_m2 ;
/// Number of elements taken into account in the variance computation
unsigned int variance_size ;
protected:
@ -54,12 +56,14 @@ protected:
public:
Measurement(const AccessPoint *_ap = NULL):
ap(const_cast<AccessPoint*>(_ap)), average_dbm(0),
average_mw(0), variance_mw(0), variance_mw_m2(0) {}
average_mw(0), variance_mw(0), variance_mw_m2(0),
variance_size(0) {}
Measurement(const Measurement &source):
ap(source.ap), ss_list(source.ss_list),
average_dbm(source.average_dbm), average_mw(source.average_mw),
variance_mw(source.variance_mw), variance_mw_m2(0) {}
variance_mw(source.variance_mw), variance_mw_m2(0),
variance_size(0) {}
~Measurement(void) ;
@ -73,7 +77,7 @@ public:
float get_average_dbm() const ;
/// Returns the variance of the SS list
float get_variance_mw() const ;
/// Returns the standard deviation of the SS list
/// Returns the standard deviation of the SS list (mW)
float get_std_deviation_mw() const ;
/// Returns the number of SS in the SS list
int get_nb_ss() const ;