diff --git a/owlps-positioning/TODO b/owlps-positioning/TODO index e34957b..b4d4516 100644 --- a/owlps-positioning/TODO +++ b/owlps-positioning/TODO @@ -11,9 +11,6 @@ owlps-positioning/tests/include owlps-positioning/doc (générée par doxygen) -- Measurement - Vérifier le calcul de moyenne (a priori OK). - - Revoir le diagramme UML ° Associations : devraient êtres représentées par des attributs pointeurs. diff --git a/owlps-positioning/measurement.cc b/owlps-positioning/measurement.cc index 00b53d7..3a58280 100644 --- a/owlps-positioning/measurement.cc +++ b/owlps-positioning/measurement.cc @@ -48,31 +48,20 @@ Measurement::~Measurement() /* *** Write accessors *** */ -void Measurement::update_average_ss() -{ - average_ss = 0 ; - - for (vector::const_iterator i = ss_list.begin() ; - i != ss_list.end() ; ++i) - { - float ss_mwatts = - pow(10, (float) *i / 10.0) + - (ss_list.size() * pow(10, average_ss / 10.0)) ; - average_ss = 10 * log10(ss_mwatts / ss_list.size()) ; - } -} - - /** * #average_ss is updated to include the new value. + * @param ss_dbm The signal strength to add to #ss_list (in dBm). */ -void Measurement::add_ss(const int &ss) +void Measurement::add_ss(const int &ss_dbm) { - float ss_mwatts = - pow(10, (float) ss / 10.0) + - (ss_list.size() * pow(10, average_ss / 10.0)) ; - ss_list.push_back(ss) ; - average_ss = 10 * log10(ss_mwatts / ss_list.size()) ; + double total_ss_mwatts = + pow(10, static_cast(ss_dbm) / 10.0) + // New SS in mW + (ss_list.size() * pow(10, average_ss / 10.0)) ; // Other values in mW + + ss_list.push_back(ss_dbm) ; // Add new value (dBm) + + // Update average converting back in dBm + average_ss = 10.0 * log10(total_ss_mwatts / ss_list.size()) ; } @@ -101,6 +90,30 @@ void Measurement::clear() +/* *** Operations *** */ + + +void Measurement::update_average_ss() +{ + if (ss_list.size() == 0) + { + average_ss = 0 ; + return ; + } + + double total_ss_mwatts = 0 ; + + for (vector::const_iterator i = ss_list.begin() ; + i != ss_list.end() ; ++i) + // Add the current value in mW to the total + total_ss_mwatts += pow(10, static_cast(*i) / 10.0) ; + + // Compute the average in mW and convert it to dBm + average_ss = 10 * log10(total_ss_mwatts / ss_list.size()) ; +} + + + /* *** Operators *** */ diff --git a/owlps-positioning/measurement.hh b/owlps-positioning/measurement.hh index 61e33e7..10e700b 100644 --- a/owlps-positioning/measurement.hh +++ b/owlps-positioning/measurement.hh @@ -13,13 +13,17 @@ class Measurement protected: /// The AccessPoint that performed the measurement AccessPoint *ap ; - /// List of signal strengths captured + /// List of signal strengths captured (in dBm) std::vector ss_list ; - /// Average of all signal strength captured - float average_ss ; + /// Average of all signal strength captured (dBm) + double average_ss ; + /** @name Operations */ + //@{ /// Recalculates #average_ss from #ss_list void update_average_ss(void) ; + //@} + public: /// Constructs a Measurement from an AccessPoint (or default constructor) @@ -40,7 +44,7 @@ public: /// #ss_list read accessor const std::vector& get_ss_list() const ; /// #average_ss read accessor - float get_average_ss() const ; + double get_average_ss() const ; //float get_ss_square_distance(const float &ss) const ; //@} @@ -49,7 +53,7 @@ public: /// #ap write accessor void set_ap(const AccessPoint *_ap) ; /// Adds a signal strength to #ss_list - void add_ss(const int &ss) ; + void add_ss(const int &ss_dbm) ; /// #ss_list write accessor void set_ss_list(const std::vector &_ss_list) ; /// Reinitialises the Measurement @@ -85,7 +89,7 @@ inline const std::vector& Measurement::get_ss_list() const } -inline float Measurement::get_average_ss() const +inline double Measurement::get_average_ss() const { return average_ss ; } @@ -98,7 +102,7 @@ inline float Measurement::get_average_ss() const -/* *** write accessors *** */ +/* *** Write accessors *** */ inline void Measurement::set_ap(const AccessPoint *_ap) diff --git a/owlps-positioning/tests/measurement_test.hh b/owlps-positioning/tests/measurement_test.hh index 485934c..db1463e 100644 --- a/owlps-positioning/tests/measurement_test.hh +++ b/owlps-positioning/tests/measurement_test.hh @@ -44,14 +44,32 @@ public: vi1.push_back(-78) ; vi1.push_back(-21) ; TS_ASSERT_EQUALS(m1.get_ss_list(), vi1) ; - TS_WARN("TODO: Test average SS.") ; + /* *** Average computation *** + * + * We need to convert all dBm values into mW, compute the average + * and convert mW back to dBm. + * (1) P[mW] = 10^(P[dBm] / 10) + * (2) P[dBm] = 10 log10(P[mW]) + * + * So for the dBm values -33, -78, -21, we have: + * -33 dBm = 10^(-33/10) mW = 0.0005012 mW + * -78 dBm = 10^(-78/10) mW = 0.00000001585 mW + * -21 dBm = 10^(-21/10) mW = 0.0079433 mW + * And: + * ( 10^(-33/10) + 10^(-78/10) + 10^(-21/10) ) / 3 = 0.0028148 mW + * Back to dBm: + * 0.0028148 mW = -25.505481 dBm + */ + TS_ASSERT_DELTA(m1.get_average_ss(), -25.505481, 0.0001) ; + + m1.clear() ; std::vector vi2 ; - vi1.push_back(-54) ; - vi1.push_back(-1) ; + vi2.push_back(-54) ; + vi2.push_back(-1) ; m1.set_ss_list(vi2) ; TS_ASSERT_EQUALS(m1.get_ss_list(), vi2) ; - TS_WARN("TODO: Test average SS.") ; + TS_ASSERT_DELTA(m1.get_average_ss(), -4.0102782, 0.0001) ; m1.clear() ; Measurement m2 ;