70 lines
1.7 KiB
C++
70 lines
1.7 KiB
C++
/*
|
||
* © Copyright 2014 – Urheberrechtshinweis
|
||
* Alle Rechte vorbehalten / All Rights Reserved
|
||
*
|
||
* Programmcode ist urheberrechtlich geschuetzt.
|
||
* Das Urheberrecht liegt, soweit nicht ausdruecklich anders gekennzeichnet, bei Frank Ebner.
|
||
* Keine Verwendung ohne explizite Genehmigung.
|
||
* (vgl. § 106 ff UrhG / § 97 UrhG)
|
||
*/
|
||
|
||
#ifndef LOGDISTANCEMODEL_H
|
||
#define LOGDISTANCEMODEL_H
|
||
|
||
#include <cmath>
|
||
|
||
class LogDistanceModel {
|
||
|
||
public:
|
||
|
||
/** convert from RSSI to a distance (in meter) */
|
||
static float rssiToDistance(const float txPower, const float pathLoss, const float rssi) {
|
||
return pow(10, (txPower - rssi) / (10 * pathLoss));
|
||
}
|
||
|
||
/** convert from a distance (in meter) to the expected RSSI */
|
||
static float distanceToRssi(const float txPower, const float pathLoss, const float distance_m) {
|
||
if (distance_m <= 1) {return txPower;}
|
||
return (txPower - (10 * pathLoss * std::log10(distance_m)));
|
||
}
|
||
|
||
// static float distanceAndRssiToPathLoss(const float txPower, const float rssi, const float distance_m) {
|
||
// const float delta = txPower - rssi;
|
||
// const float log = std::log10(distance_m) * 10.0f;
|
||
// const float res = delta/log;
|
||
|
||
// // sanity check
|
||
// const float err = std::abs(rssi - distanceToRssi(txPower, res, distance_m));
|
||
// if (err > 0.01) {throw Exception("error too high!");}
|
||
|
||
// return res;
|
||
// }
|
||
|
||
};
|
||
|
||
#include <vector>
|
||
|
||
class LogDistFastModel {
|
||
|
||
private:
|
||
|
||
std::vector<float> lut;
|
||
|
||
public:
|
||
|
||
LogDistFastModel() {
|
||
lut.resize(2000);
|
||
for (int i = 0; i < 2000; ++i) {
|
||
lut[i] = std::log10(i/10.0f);
|
||
}
|
||
}
|
||
|
||
float distanceToRssi(const float txPower, const float pathLoss, const float distance) const {
|
||
if (distance <= 1) {return txPower;}
|
||
return (txPower - (10 * pathLoss * lut[ (int) (distance*10) ]));
|
||
}
|
||
|
||
};
|
||
|
||
#endif // LOGDISTANCEMODEL_H
|