threshold.cc

Go to the documentation of this file.
00001 /* -*-  Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
00002 
00003 /*
00004  * threshold.cc
00005  * Copyright (C) 2000 by the University of Southern California
00006  * $Id: threshold.cc,v 1.3 2005/08/25 18:58:06 johnh Exp $
00007  *
00008  * This program is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU General Public License,
00010  * version 2, as published by the Free Software Foundation.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License along
00018  * with this program; if not, write to the Free Software Foundation, Inc.,
00019  * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
00020  *
00021  *
00022  * The copyright of this module includes the following
00023  * linking-with-specific-other-licenses addition:
00024  *
00025  * In addition, as a special exception, the copyright holders of
00026  * this module give you permission to combine (via static or
00027  * dynamic linking) this module with free software programs or
00028  * libraries that are released under the GNU LGPL and with code
00029  * included in the standard release of ns-2 under the Apache 2.0
00030  * license or under otherwise-compatible licenses with advertising
00031  * requirements (or modified versions of such code, with unchanged
00032  * license).  You may copy and distribute such a system following the
00033  * terms of the GNU GPL for this module and the licenses of the
00034  * other code concerned, provided that you include the source code of
00035  * that other code when and as the GNU GPL requires distribution of
00036  * source code.
00037  *
00038  * Note that people who make modified versions of this module
00039  * are not obligated to grant this special exception for their
00040  * modified versions; it is their choice whether to do so.  The GNU
00041  * General Public License gives permission to release a modified
00042  * version without this exception; this exception also makes it
00043  * possible to release a modified version which carries forward this
00044  * exception.
00045  *
00046  */
00047 
00048 /*
00049  * Calculating the receiving threshold (RXThresh_ for Phy/Wireless)
00050  * Wei Ye, weiye@isi.edu, 2000
00051  */
00052 
00053 
00054 #include <math.h>
00055 #include <stdlib.h>
00056 #include <iostream.h>
00057 
00058 #ifndef M_PI
00059 #define M_PI 3.14159265359
00060 #endif
00061 
00062 double Friis(double Pt, double Gt, double Gr, double lambda, double L, double d)
00063 {
00064         /*
00065          * Friis free space propagation equation:
00066          *
00067          *       Pt * Gt * Gr * (lambda^2)
00068          *   P = --------------------------
00069          *       (4 *pi * d)^2 * L
00070          */
00071   double M = lambda / (4 * M_PI * d);
00072   return (Pt * Gt * Gr * (M * M)) / L;
00073 }
00074 
00075 double TwoRay(double Pt, double Gt, double Gr, double ht, double hr, double L, double d, double lambda)
00076 {
00077         /*
00078          *  if d < crossover_dist, use Friis free space model
00079          *  if d >= crossover_dist, use two ray model
00080          *
00081          *  Two-ray ground reflection model.
00082          *
00083          *       Pt * Gt * Gr * (ht^2 * hr^2)
00084          *  Pr = ----------------------------
00085          *           d^4 * L
00086          *
00087          * The original equation in Rappaport's book assumes L = 1.
00088          * To be consistant with the free space equation, L is added here.
00089          */
00090 
00091     double Pr;  // received power
00092     double crossover_dist = (4 * M_PI * ht * hr) / lambda;
00093 
00094     if (d < crossover_dist)
00095         Pr = Friis(Pt, Gt, Gr, lambda, L, d);
00096     else
00097         Pr = Pt * Gt * Gr * (hr * hr * ht * ht) / (d * d * d * d * L);
00098         
00099     return Pr;
00100 }
00101 
00102 // inverse of complementary error function
00103 // y = erfc(x) --> x = inv_erfc(y)
00104 
00105 double inv_erfc(double y)
00106 {
00107     double s, t, u, w, x, z;
00108 
00109     z = y;
00110     if (y > 1) {
00111         z = 2 - y;
00112     }
00113     w = 0.916461398268964 - log(z);
00114     u = sqrt(w);
00115     s = (log(u) + 0.488826640273108) / w;
00116     t = 1 / (u + 0.231729200323405);
00117     x = u * (1 - s * (s * 0.124610454613712 + 0.5)) -
00118         ((((-0.0728846765585675 * t + 0.269999308670029) * t +
00119         0.150689047360223) * t + 0.116065025341614) * t +
00120         0.499999303439796) * t;
00121     t = 3.97886080735226 / (x + 3.97886080735226);
00122     u = t - 0.5;
00123     s = (((((((((0.00112648096188977922 * u +
00124         1.05739299623423047e-4) * u - 0.00351287146129100025) * u -
00125         7.71708358954120939e-4) * u + 0.00685649426074558612) * u +
00126         0.00339721910367775861) * u - 0.011274916933250487) * u -
00127         0.0118598117047771104) * u + 0.0142961988697898018) * u +
00128         0.0346494207789099922) * u + 0.00220995927012179067;
00129     s = ((((((((((((s * u - 0.0743424357241784861) * u -
00130         0.105872177941595488) * u + 0.0147297938331485121) * u +
00131         0.316847638520135944) * u + 0.713657635868730364) * u +
00132         1.05375024970847138) * u + 1.21448730779995237) * u +
00133         1.16374581931560831) * u + 0.956464974744799006) * u +
00134         0.686265948274097816) * u + 0.434397492331430115) * u +
00135         0.244044510593190935) * t -
00136         z * exp(x * x - 0.120782237635245222);
00137     x += s * (x * s + 1);
00138     if (y > 1) {
00139         x = -x;
00140     }
00141     return x;
00142 }
00143 
00144 
00145 // Inverse of Q-function
00146 // y = Q(x) --> x = inv_Q(y)
00147 
00148 double inv_Q(double y)
00149 {
00150     double x;
00151     x = sqrt(2.0) * inv_erfc(2.0 * y);
00152     return x;
00153 }
00154 
00155 
00156 int main(int argc, char** argv)
00157 {
00158     
00159     // specify default values
00160     char** propModel = NULL;       // propagation model
00161     double Pt = 0.28183815;            // transmit power
00162     double Gt = 1.0;               // transmit antenna gain
00163     double Gr = 1.0;               // receive antenna
00164     double freq = 914.0e6;         // frequency
00165     double sysLoss = 1.0;          // system loss
00166     
00167     // for two-ray model
00168     double ht = 1.5;               // transmit antenna height
00169     double hr = 1.5;               // receive antenna height
00170     
00171     // for shadowing model
00172     double pathlossExp_ = 2.0;     // path loss exponent
00173     double std_db_ = 4.0;          // shadowing deviation
00174     double dist0_ = 1.0;           // reference distance
00175     double prob = 0.95;            // correct reception rate
00176     
00177     double rxThresh_;              // receiving threshold
00178 
00179     // check arguments  
00180     if (argc < 4) {
00181         cout << "USAGE: find receiving threshold for certain communication range (distance)" << endl;
00182         cout << endl;
00183         cout << "SYNOPSIS: threshold -m <propagation-model> [other-options] distance" << endl;
00184         cout << endl;
00185         cout << "<propagation-model>: FreeSpace, TwoRayGround or Shadowing" << endl;
00186         cout << "[other-options]: set parameters other than default values:" << endl;
00187         cout << endl << "Common parameters:" << endl;
00188         cout << "-Pt <transmit-power>" << endl;
00189         cout << "-fr <frequency>" << endl;
00190         cout << "-Gt <transmit-antenna-gain>" << endl;
00191         cout << "-Gr <receive-antenna-gain>" << endl;
00192         cout << "-L <system-loss>" << endl;
00193         cout << endl << "For two-ray ground model:" << endl;
00194         cout << "-ht <transmit-antenna-height>" << endl;
00195         cout << "-hr <receive-antenna-height>" << endl;
00196         cout << endl << "For shadowing model:" << endl;
00197         cout << "-pl <path-loss-exponent>" << endl;
00198         cout << "-std <shadowing-deviation>" << endl;
00199         cout << "-d0 <reference-distance>" << endl;
00200         cout << "-r <receiving-rate>" << endl;
00201         return 0;
00202     }
00203 
00204     // parse arguments  
00205     double dist = atof(argv[argc-1]);
00206     cout << "distance = " << dist << endl;
00207 
00208     int argCount = (argc - 2) / 2;   // number of parameters
00209     argv++;
00210     for (int i = 0; i < argCount; i++) {
00211         if(!strcmp(*argv,"-m")) {          // propagation model
00212             propModel = argv + 1;
00213             cout << "propagation model: " << *propModel << endl;
00214         }
00215         if(!strcmp(*argv,"-Pt")) {        // transmit power
00216             Pt = atof(*(argv + 1));
00217         }
00218         if(!strcmp(*argv,"-fr")) {        // frequency
00219             freq = atof(*(argv + 1));
00220         }
00221         if(!strcmp(*argv,"-Gt")) {        // transmit antenna gain
00222             Gt = atof(*(argv + 1));
00223         }
00224         if(!strcmp(*argv,"-Gr")) {        // receive antenna gain
00225             Gr = atof(*(argv + 1));
00226         }
00227         if(!strcmp(*argv,"-L")) {        // system loss
00228             sysLoss = atof(*(argv + 1));
00229         }
00230         if(!strcmp(*argv,"-ht")) {        // transmit antenna height (Two ray model)
00231             ht = atof(*(argv + 1));
00232         }
00233         if(!strcmp(*argv,"-hr")) {        // receive antenna height (Two ray model)
00234             hr = atof(*(argv + 1));
00235         }
00236         if(!strcmp(*argv,"-pl")) {        // path loss exponent (Shadowing model)
00237             pathlossExp_ = atof(*(argv + 1));
00238         }
00239         if(!strcmp(*argv,"-std")) {        // shadowing deviation (Shadowing model)
00240             std_db_ = atof(*(argv + 1));
00241         }
00242         if(!strcmp(*argv,"-d0")) {        // close-in reference distance (Shadowing model)
00243             dist0_ = atof(*(argv + 1));
00244         }
00245         if(!strcmp(*argv,"-r")) {        // rate of correct reception (Shadowing model)
00246             prob = atof(*(argv + 1));
00247         }
00248         argv += 2;
00249     }
00250     
00251     if (propModel == NULL) {
00252         cout << "Must specify propagation model: -m <propagation model>" << endl;
00253         return 0;
00254     }
00255     
00256     double lambda = 3.0e8/freq;
00257     
00258     // compute threshold    
00259     if (!strcmp(*propModel, "FreeSpace")) {
00260         rxThresh_ = Friis(Pt, Gt, Gr, lambda, sysLoss, dist);
00261         cout << endl << "Selected parameters:" << endl;
00262         cout << "transmit power: " << Pt << endl;
00263         cout << "frequency: " << freq << endl;
00264         cout << "transmit antenna gain: " << Gt << endl;
00265         cout << "receive antenna gain: " << Gr << endl;
00266         cout << "system loss: " << sysLoss << endl;
00267     } else if (!strcmp(*propModel, "TwoRayGround")) {
00268         rxThresh_ = TwoRay(Pt, Gt, Gr, ht, hr, sysLoss, dist, lambda);
00269         cout << endl << "Selected parameters:" << endl;
00270         cout << "transmit power: " << Pt << endl;
00271         cout << "frequency: " << freq << endl;
00272         cout << "transmit antenna gain: " << Gt << endl;
00273         cout << "receive antenna gain: " << Gr << endl;
00274         cout << "system loss: " << sysLoss << endl;
00275         cout << "transmit antenna height: " << ht << endl;
00276         cout << "receive antenna height: " << hr << endl;
00277     } else if (!strcmp(*propModel, "Shadowing")) {
00278         // calculate receiving power at reference distance
00279         double Pr0 = Friis(Pt, Gt, Gr, lambda, sysLoss, dist0_);
00280 
00281         // calculate average power loss predicted by path loss model
00282         double avg_db = -10.0 * pathlossExp_ * log10(dist/dist0_);
00283 
00284         // calculate the the threshold
00285         double invq = inv_Q(prob);
00286         double threshdb = invq * std_db_ + avg_db;
00287         rxThresh_ = Pr0 * pow(10.0, threshdb/10.0);
00288     
00289 #ifdef DEBUG
00290         cout << "Pr0 = " << Pr0 << endl;
00291         cout << "avg_db = " << avg_db << endl;
00292         cout << "invq = " << invq << endl;
00293         cout << "threshdb = " << threshdb << endl;
00294 #endif
00295         
00296         cout << endl << "Selected parameters:" << endl;
00297         cout << "transmit power: " << Pt << endl;
00298         cout << "frequency: " << freq << endl;
00299         cout << "transmit antenna gain: " << Gt << endl;
00300         cout << "receive antenna gain: " << Gr << endl;
00301         cout << "system loss: " << sysLoss << endl;
00302         cout << "path loss exp.: " << pathlossExp_ << endl;
00303         cout << "shadowing deviation: " << std_db_ << endl;
00304         cout << "close-in reference distance: " << dist0_ << endl;
00305         cout << "receiving rate: " << prob << endl;
00306     } else {
00307         cout << "Error: unknown propagation model." << endl;
00308         cout << "Available model: FreeSpace, TwoRayGround, Shadowing" << endl;
00309         return 0;
00310     }
00311 
00312     cout << endl << "Receiving threshold RXThresh_ is: " << rxThresh_ << endl;
00313 
00314 }

Generated on Tue Mar 6 16:47:52 2007 for ns2 Network Simulator 2.29 by  doxygen 1.4.6