owlps/owlps-positioner/src/measurement.hh

278 lines
7.7 KiB
C++

/*
* This file is part of the Owl Positioning System (OwlPS).
* OwlPS is a project of the University of Franche-Comte
* (Université de Franche-Comté), France.
*
* Copyright © Université de Franche-Comté 2007-2012.
*
* Corresponding author: Matteo Cypriani <mcy@lm7.fr>
*
***********************************************************************
*
* This software is governed by the CeCILL license under French law and
* abiding by the rules of distribution of free software. You can use,
* modify and/or redistribute the software under the terms of the CeCILL
* license as circulated by CEA, CNRS and INRIA at the following URL:
* http://www.cecill.info
*
* As a counterpart to the access to the source code and rights to copy,
* modify and redistribute granted by the license, users are provided
* only with a limited warranty and the software's authors, the holder
* of the economic rights, and the successive licensors have only
* limited liability.
*
* In this respect, the user's attention is drawn to the risks
* associated with loading, using, modifying and/or developing or
* reproducing the software by the user in light of its specific status
* of free software, that may mean that it is complicated to manipulate,
* and that also therefore means that it is reserved for developers and
* experienced professionals having in-depth computer knowledge. Users
* are therefore encouraged to load and test the software's suitability
* as regards their requirements in conditions enabling the security of
* their systems and/or data to be ensured and, more generally, to use
* and operate it in the same conditions as regards security.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL license and that you accept its terms.
*
***********************************************************************
*/
#ifndef _OWLPS_POSITIONING_MEASUREMENT_HH_
#define _OWLPS_POSITIONING_MEASUREMENT_HH_
#include "accesspoint.hh"
#include <map>
#include <ostream>
#include <cmath>
typedef uint_fast16_t pkt_id_t ;
typedef int_fast16_t ss_t ;
/// Represents a list of signal strengths captured by one AccessPoint
class Measurement
{
protected:
/// The AccessPoint that performed the measurement
AccessPoint *ap ;
/// List of signal strengths captured (in dBm)
std::map<pkt_id_t, ss_t> ss_list ;
/// Average of all the captured signal strengths (dBm)
float average_dbm ;
/// Average of all the captured signal strengths (mW)
float average_mw ;
/// Variance of all the captured signal strengths (mW)
float variance_mw ;
/// Variance of all the captured signal strengths (dBm)
float variance_dbm ;
private:
/// Intermediate variable used to compute the variance (mW)
float variance_mw_m2 ;
/// Intermediate variable used to compute the variance (dBm)
float variance_dbm_m2 ;
/// Number of elements taken into account in the variance computation
unsigned int variance_size ;
protected:
/** @name Operations */
//@{
/// Recalculates entirely the average and variance from #ss_list
void recalculate_average(void) ;
/// Update the average and variance with a new SS
void update_average(ss_t ss_dbm) ;
/// \brief Counts the number of SS of source that are within the
/// interval [average_dbm-bound;average_dbm+bound]
unsigned int nb_in_interval(
const Measurement &source, float bound) const ;
//@}
public:
Measurement(const AccessPoint *_ap = NULL):
ap(const_cast<AccessPoint*>(_ap)), average_dbm(0),
average_mw(0), variance_mw(0), variance_dbm(0),
variance_mw_m2(0), variance_dbm_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_dbm(source.variance_dbm),
variance_mw_m2(0), variance_dbm_m2(0), variance_size(0) {}
~Measurement(void) ;
/** @name Read accessors */
//@{
/// Returns the AccessPoint associated with the Measurement
AccessPoint* get_ap() const ;
/// Returns the packet number \em pkt_id
ss_t get_ss(pkt_id_t pkt_id) const ;
/// Returns the mean of the SS list, in dBm
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 (mW)
float get_std_deviation_mw() const ;
/// Returns the standard deviation of the SS list (dBm)
float get_std_deviation_dbm() const ;
/// Returns the number of SS in the SS list
int get_nb_ss() const ;
//@}
/** @name Write accessors */
//@{
/// Associate the Measurement with an AccessPoint
void set_ap(const AccessPoint *_ap) ;
/// Adds a signal strength (in dBm) to #ss_list
void add_ss(const pkt_id_t packet_id, const ss_t ss_dbm) ;
/// Adds several signal strengths (in dBm) to #ss_list
void add_ss_list(const std::map<pkt_id_t, ss_t> &_ss_list) ;
/// Merges a given Measurement into the current Measurement
void merge(const Measurement &source) ;
/// Reset all the attributes
void clear(void) ;
//@}
/** @name Operations */
//@{
/// Computes the similarity with another Measurement in the SS space
float similarity(const Measurement &source) const ;
/// Computes the distance to another Measurement's SS average
float ss_square_distance(const Measurement &source) const ;
/// Computes the distance to another SS value (in dBm)
float ss_square_distance(const float &ss_dbm) const ;
/// Computes the distance to another Measurement's variance (mW)
float variance_mw_square_distance(const Measurement &source) const ;
/// Computes the distance to another variance value in mW
float variance_mw_square_distance(const float &var) const ;
//@}
/** @name Operators */
//@{
Measurement& operator=(const Measurement &m) ;
bool operator==(const Measurement &m) const ;
bool operator!=(const Measurement &m) const ;
operator bool(void) const ;
//@}
/** @name Conversion accessors */
//@{
/// Converts to a CSV string
const std::string to_csv(void) const ;
//@}
/// Displays a Measurement
friend std::ostream &operator<<(std::ostream &os, const Measurement &m) ;
} ;
/* *** Read accessors *** */
inline AccessPoint* Measurement::get_ap() const
{
return ap ;
}
inline float Measurement::get_average_dbm() const
{
return average_dbm ;
}
inline float Measurement::get_variance_mw() const
{
return variance_mw ;
}
inline float Measurement::get_std_deviation_mw() const
{
return sqrt(variance_mw) ;
}
inline float Measurement::get_std_deviation_dbm() const
{
return sqrt(variance_dbm) ;
}
inline int Measurement::get_nb_ss() const
{
return ss_list.size() ;
}
/* *** Write accessors *** */
inline void Measurement::set_ap(const AccessPoint *_ap)
{
ap = const_cast<AccessPoint*>(_ap) ;
}
/* *** Operations *** */
inline float Measurement::
ss_square_distance(const Measurement &source) const
{
return ss_square_distance(source.average_dbm) ;
}
inline float Measurement::ss_square_distance(const float &ss_dbm) const
{
return ((ss_dbm - average_dbm) * (ss_dbm - average_dbm)) ;
}
inline float Measurement::
variance_mw_square_distance(const Measurement &source) const
{
return variance_mw_square_distance(source.variance_mw) ;
}
inline float Measurement::
variance_mw_square_distance(const float &var) const
{
return ((var - variance_mw) * (var - variance_mw)) ;
}
/* *** Operators *** */
inline bool Measurement::operator!=(const Measurement &m) const
{
return !(*this == m) ;
}
/**
* @return \em false if the Measurement is empty.
* @return \em true if at least one attribute is initialised.
*/
inline Measurement::operator bool() const
{
return
ap != NULL ||
! ss_list.empty() ;
}
#endif // _OWLPS_POSITIONING_MEASUREMENT_HH_