User Manual, Developers Guide and API Documentation

PowerRatio.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002  * This file is part of openWNS (open Wireless Network Simulator)
00003  * _____________________________________________________________________________
00004  *
00005  * Copyright (C) 2004-2007
00006  * Chair of Communication Networks (ComNets)
00007  * Kopernikusstr. 5, D-52074 Aachen, Germany
00008  * phone: ++49-241-80-27910,
00009  * fax: ++49-241-80-22242
00010  * email: info@openwns.org
00011  * www: http://www.openwns.org
00012  * _____________________________________________________________________________
00013  *
00014  * openWNS is free software; you can redistribute it and/or modify it under the
00015  * terms of the GNU Lesser General Public License version 2 as published by the
00016  * Free Software Foundation;
00017  *
00018  * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY
00019  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
00020  * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00021  * details.
00022  *
00023  * You should have received a copy of the GNU Lesser General Public License
00024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00025  *
00026  ******************************************************************************/
00027 
00028 
00029 #ifndef _POWER_RATIO_HPP
00030 #define _POWER_RATIO_HPP
00031 
00032 
00033 #include "Exception.hpp"
00034 #include <cassert>
00035 #include <cmath>
00036 #include <iostream>
00037 #include <stdint.h>
00038 #include <WNS/Assure.hpp>
00039 
00040 namespace wns {
00041     class Ratio;
00042     class Power;
00043 
00044 // ------------------- R A T I O --------------------------------------
00045 // Declaration
00047 
00048     class Ratio
00049     {
00050         friend class Power;
00051     public:
00052 
00053         // Default constructor
00054         inline Ratio();
00055         inline Ratio(const Ratio &r);
00056 
00057         // Destructor
00058         inline ~Ratio();
00059 
00060         static inline Ratio from_factor(double aValue);
00061         static inline Ratio from_dB(double aValue);
00062 
00063         inline void   set_factor (double aValue);
00064         inline void   set_dB     (double aValue);
00065         inline double get_factor () const;
00066         inline double get_dB     () const;
00067 
00068         inline Ratio  operator - (const Ratio& r) const;
00069         inline void   operator -=(const Ratio& r);
00070         inline Ratio  operator + (const Ratio& r) const;
00071         inline void   operator +=(const Ratio& r);
00072         inline bool   operator < (const Ratio& r) const;
00073         inline bool   operator > (const Ratio& r) const;
00074         inline bool   operator >=(const Ratio& r) const;
00075         inline bool   operator <=(const Ratio& r) const;
00076         inline bool   operator ==(const Ratio& r) const;
00077         inline bool   operator !=(const Ratio& r) const;
00078         inline Ratio& operator = (const Ratio& r);
00079 
00080         inline long double
00081         convertForAveraging() const;
00082 
00083         static inline Ratio
00084         convertForAveraged(long double d);
00085 
00086         friend std::ostream& operator <<(std::ostream &str, const wns::Ratio& r)
00087         {
00088             str << r.get_dB() << " dB";
00089             return str;
00090         }
00091 
00092         friend std::istream& operator >>(std::istream &str, Ratio& r) {
00093             double tmp;
00094             str >> tmp;
00095             if(str.eof()) {
00096                 std::cout << "Warning: No unit provided (dB)!"
00097                       << "Assuming ratio to be in dB."
00098                       << std::endl;
00099                 r.set_dB(tmp);
00100                 return str;
00101             }
00102             std::string sTmp;
00103             str >> sTmp;
00104             if (sTmp=="dB") {
00105                 r.set_dB(tmp);
00106                 return str;
00107             }
00108             if (sTmp=="dBi") {
00109                 r.set_dB(tmp);
00110                 return str;
00111             }
00112             if (sTmp=="dBd") {
00113                 r.set_dB(tmp + 2.15);
00114                 return str;
00115             }
00116             throw(Exception("Warning: No valid unit provided for ratio (dB)! Got: " + sTmp));
00117         }
00118 
00119     protected:
00120         double factor;
00121 
00122     private:
00123         inline Ratio(double aValue);
00124     };
00125 
00126 //----------------------- P O W E R ----------------------------------
00127 // Declaration
00129 
00130     class Power
00131     {
00132         friend class Ratio;
00133     public:
00134 
00135         // Default constructor
00136         inline Power();
00137         inline Power(const Power& aPowerRef);
00138 
00139         // Destructor
00140         inline ~Power();
00141 
00142         static inline Power from_dBm(double aValue);
00143         static inline Power from_mW(double aValue);
00144 
00145         inline void   set_dBm    (double aValue);
00146         inline void   set_mW     (double aValue);
00147         inline double get_dBm    () const;
00148         inline double get_mW     () const;
00149 
00150         inline void   operator +=(const Power& p);
00151         inline void   operator -=(const Power& p);
00152         inline Power  operator + (const Power& p) const;
00153         inline Power  operator - (const Power& p) const;
00154         inline void   operator +=(const Ratio& r);
00155         inline void   operator -=(const Ratio& r);
00156         inline void   operator *=(double d);
00157         inline void   operator *=(float f);
00158         inline void   operator *=(long int i);
00159         inline void   operator *=(unsigned long int i);
00160         inline void   operator /=(double d);
00161         inline void   operator /=(float f);
00162         inline void   operator /=(long int i);
00163         inline void   operator /=(unsigned long int i);
00164         inline Power  operator * (double d) const;
00165         inline Power  operator * (float f) const;
00166         inline Power  operator * (long int i) const;
00167         inline Power  operator * (unsigned long int i) const;
00168         inline Power  operator * (const Ratio& r) const;
00169         inline Power  operator / (double d) const;
00170         inline Power  operator / (float f) const;
00171         inline Power  operator / (long int i) const;
00172         inline Power  operator / (unsigned long int i) const;
00173         inline Power  operator / (const Ratio& r) const;
00174         inline Ratio  operator / (const Power& p) const;
00175         inline bool   operator < (const Power& p) const;
00176         inline bool   operator > (const Power& p) const;
00177         inline bool   operator <=(const Power& p) const;
00178         inline bool   operator >=(const Power& p) const;
00179         inline bool   operator ==(const Power& p) const;
00180         inline bool   operator !=(const Power& p) const;
00181         inline Power& operator = (const Power& p);
00182         inline long double convertForAveraging() const {
00183             return mW;
00184         }
00185         static inline Power convertForAveraged(long double d) {
00186             return Power::from_mW(d);
00187         }
00188 
00189         friend std::ostream& operator <<(std::ostream &str, const wns::Power& p)
00190         {
00191             str << p.get_dBm() << " dBm";
00192             return str;
00193         }
00194 
00195         friend std::istream& operator >>(std::istream &str, Power& p) {
00196             double tmp;
00197             str >> tmp;
00198             if(str.eof()) {
00199                 std::cout << "Warning: No unit provided (dBm, mW, W)!"
00200                       << "Assuming power to be in dBm."
00201                       << std::endl;
00202                 p.set_dBm(tmp);
00203                 return str;
00204             }
00205             std::string sTmp;
00206             str >> sTmp;
00207             if (sTmp=="dBm") {
00208                 p.set_dBm(tmp);
00209                 return str;
00210             }
00211             if (sTmp=="mW") {
00212                 p.set_mW(tmp);
00213                 return str;
00214             }
00215             if (sTmp=="W") {
00216                 p.set_mW(tmp*1000.0);
00217                 return str;
00218             }
00219             throw(Exception("Warning: No valid unit provided for power (dBm, mW, W)"));
00220         }
00221 
00222     protected:
00223         double mW;
00224     private:
00225         inline Power(double aValue);
00226     };
00227 
00228 
00229 // ------------------- R A T I O --------------------------------------
00230 // Implementation
00232     inline  Ratio::Ratio()
00233     : factor(1.0) {}
00234 
00236     inline  Ratio::Ratio(const Ratio &r)
00237         : factor(r.factor) {}
00238 
00239     inline  Ratio::Ratio(double aValue)
00240     : factor(aValue) {
00241         assert(aValue>=0);
00242     }
00243 
00244     inline  Ratio::~Ratio() {}
00245 
00246     inline Ratio&
00247     Ratio::operator =(const Ratio& r)
00248     {
00249         assert(r.factor>=0);
00250         factor = r.factor;
00251         return *this;
00252     }
00253 
00254     inline Ratio Ratio::from_factor(double aValue) {
00255         assert(aValue>=0);
00256         Ratio r;
00257         r.set_factor(aValue);
00258         return r;
00259     }
00260 
00261     inline Ratio Ratio::from_dB(double aValue) {
00262         Ratio r;
00263         r.set_dB(aValue);
00264         return r;
00265     }
00266 
00267     inline void Ratio::set_factor(double aValue) {
00268         assert(aValue>=0);
00269         factor = aValue;
00270     }
00271 
00272     inline void Ratio::set_dB(double aValue) {
00273         factor = pow(10.0,aValue/10.0);
00274     }
00275 
00276     inline double Ratio::get_factor() const {
00277         assert(factor>=0);
00278         return factor;
00279     }
00280 
00281     inline double Ratio::get_dB() const {
00282         assert(factor>=0);
00283         return 10.0*log10(factor);
00284     }
00285 
00286     inline Ratio Ratio::operator -(const Ratio& r) const {
00287         assert(factor>=0);
00288         assert(r.factor>=0);
00289         Ratio re(factor/r.factor);
00290         return re;
00291     }
00292 
00293     inline void Ratio::operator -=(const Ratio& r) {
00294         assert(factor>=0);
00295         assert(r.factor>=0);
00296 
00297         factor /= r.factor;
00298     }
00299 
00300     inline Ratio Ratio::operator +(const Ratio& r) const {
00301         assert(factor>=0);
00302         assert(r.factor>=0);
00303 
00304         Ratio re(factor*r.factor);
00305         return re;
00306     }
00307 
00308     inline void Ratio::operator +=(const Ratio& r) {
00309         assert(factor>=0);
00310         assert(r.factor>=0);
00311         factor *= r.factor;
00312     }
00313 
00314     inline bool Ratio::operator <(const Ratio& r) const  {
00315         assert(factor>=0);
00316         assert(r.factor>=0);
00317         return (factor < r.factor);
00318     }
00319 
00320     inline bool Ratio::operator >(const Ratio& r) const {
00321         assert(factor>=0);
00322         assert(r.factor>=0);
00323         return (factor > r.factor);
00324     }
00325 
00326     inline bool Ratio::operator >=(const Ratio& r) const {
00327         assert(factor>=0);
00328         assert(r.factor>=0);
00329         return (factor >= r.factor);
00330     }
00331 
00332     inline bool Ratio::operator <=(const Ratio& r) const {
00333         assert(factor>=0);
00334         assert(r.factor>=0);
00335         return (factor <= r.factor);
00336     }
00337 
00338     inline bool Ratio::operator ==(const Ratio& r) const {
00339         assert(factor>=0);
00340         assert(r.factor>=0);
00341         return (factor == r.factor);
00342     }
00343 
00344     inline bool Ratio::operator !=(const Ratio& r) const {
00345         assert(factor>=0);
00346         assert(r.factor>=0);
00347         return (factor != r.factor);
00348     }
00349 
00350     inline long double Ratio::convertForAveraging() const {
00351         return get_dB();
00352     }
00353 
00354     inline Ratio Ratio::convertForAveraged(long double d) {
00355         return Ratio::from_dB(d);
00356     }
00357 
00358 //----------------------- P O W E R ----------------------------------
00359 // Implementation
00360 
00361 // Default and only constructor, sets the power to 0.0 mW
00362     inline Power::Power()
00363         : mW(0.0) {}
00364 
00365     inline Power::Power(double aValue)
00366         : mW(aValue) {}
00367 
00368     inline Power::Power(const Power& aPowerRef)
00369         : mW(aPowerRef.mW) {}
00370 
00371     inline Power::~Power() {}
00372 
00373     inline Power Power::from_dBm(double aValue) {
00374         Power p;
00375         p.set_dBm(aValue);
00376         return p;
00377     }
00378 
00379     inline Power Power::from_mW(double aValue) {
00380         assert(aValue>=0);
00381         Power p;
00382         p.set_mW(aValue);
00383         return p;
00384     }
00385 
00386     inline void  Power::set_dBm(double aValue) {
00387         mW = pow(10.0, aValue/10.0);
00388     }
00389 
00390     inline void Power::set_mW(double aValue) {
00391         assert(aValue>=0);
00392         mW = aValue;
00393     }
00394 
00395     inline double Power::get_dBm() const  {
00396         return 10.0*log10(mW);
00397     }
00398 
00399     inline double Power::get_mW() const {
00400         return mW;
00401     }
00402 
00403     inline Power&
00404     Power::operator = (const Power& p)
00405     {
00406         mW = p.mW;
00407         return *this;
00408     }
00409 
00410     inline void Power::operator += (const Power& p) {
00411         mW += p.mW;
00412     }
00413 
00414     inline void Power::operator -= (const Power& p) {
00415         mW -= p.mW;
00416         assert(mW>=0);
00417     }
00418 
00419     inline Power Power::operator +(const Power& p) const {
00420         return Power(mW+p.mW);
00421     }
00422 
00423     inline Power  Power::operator -(const Power& p) const {
00424         return Power(mW-p.mW);
00425     }
00426 
00427     inline void Power::operator += (const Ratio& r) {
00428         assert(r.factor>=0);
00429         mW *= r.factor;
00430     }
00431 
00432     inline void Power::operator -= (const Ratio& r) {
00433         assert(r.factor>=0);
00434         mW /= r.factor;
00435     }
00436 
00437     inline Power Power::operator *(double d) const {
00438         return Power(mW*d);
00439     }
00440 
00441     inline Power Power::operator *(float f) const {
00442         return Power(mW*(double)f);
00443     }
00444 
00445     inline Power Power::operator *(long int i) const {
00446         return Power(mW*(double)i);
00447     }
00448 
00449     inline Power Power::operator *(unsigned long int u) const {
00450         return Power(mW*(double)u);
00451     }
00452 
00453     inline Power Power::operator *(const Ratio& r) const {
00454         return Power(mW*r.get_factor());
00455     }
00456 
00457     inline void Power::operator *=(double d) {
00458         mW*=d;
00459         assert(mW>=0);
00460     }
00461 
00462     inline void Power::operator *=(float f) {
00463         mW*=f;
00464         assert(mW>=0);
00465     }
00466 
00467     inline void Power::operator *=(long int i) {
00468         mW*=i;
00469         assert(mW>=0);
00470     }
00471 
00472     inline void Power::operator *=(unsigned long int u) {
00473         mW*=u;
00474     }
00475 
00476     inline Power Power::operator /(double d) const {
00477         return Power(mW/d);
00478     }
00479 
00480     inline Power Power::operator /(float f) const {
00481         return Power(mW/(double)f);
00482     }
00483 
00484     inline Power Power::operator /(long int i) const {
00485         return Power(mW/(double)i);
00486     }
00487 
00488     inline Power Power::operator /(unsigned long int u) const {
00489         return Power(mW/(double)u);
00490     }
00491 
00492     inline Power Power::operator /(const Ratio& r) const {
00493         return Power(mW/r.get_factor());
00494     }
00495 
00496     inline void Power::operator /=(double d) {
00497         mW/=d;
00498         assert(mW>=0);
00499     }
00500 
00501     inline void Power::operator /=(float f) {
00502         mW/=(double)f;
00503         assert(mW>=0);
00504     }
00505 
00506     inline void Power::operator /=(long int i) {
00507         mW/=(double)i;
00508         assert(mW>=0);
00509     }
00510 
00511     inline void Power::operator /=(unsigned long int u) {
00512         mW/=(double)u;
00513     }
00514 
00515     inline Ratio Power::operator /(const Power& p) const {
00516         return Ratio(mW/p.mW);
00517     }
00518 
00519     inline bool Power::operator <(const Power& p) const {
00520         return (mW < p.mW);
00521     }
00522 
00523     inline bool Power::operator >(const Power& p) const {
00524         return (mW > p.mW);
00525     }
00526 
00527     inline bool Power::operator <=(const Power& p) const {
00528         return (mW <= p.mW);
00529     }
00530 
00531     inline bool Power::operator >=(const Power& p) const {
00532         return (mW >= p.mW);
00533     }
00534 
00535     inline bool Power::operator ==(const Power& p) const {
00536         return (mW == p.mW);
00537     }
00538 
00539     inline bool Power::operator !=(const Power& p) const {
00540         return (mW != p.mW);
00541     }
00542 
00543 }
00544 
00545 inline std::string operator +(std::string &str, const wns::Power& p)
00546 {
00547     std::ostringstream str2;
00548     str2 << str << p.get_dBm();
00549     return str2.str();
00550 }
00551 
00552 inline std::string operator +(std::string &str, const wns::Ratio& r)
00553 {
00554     std::ostringstream str2;
00555     str2 << str << r.get_dB();
00556     return str2.str();
00557 }
00558 
00559 #endif  // _POWER_RATIO_HPP
00560 

Generated on Fri May 25 03:31:49 2012 for openWNS by  doxygen 1.5.5