src/devices/wifi/jakes-propagation-loss-model.cc
changeset 2678 22aa62a108ed
child 2681 9a13f9faf4a5
equal deleted inserted replaced
2677:28dfd0c7510d 2678:22aa62a108ed
       
     1 /* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
       
     2 /*
       
     3  * Copyright (c) 2005,2006,2007 INRIA
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License version 2 as 
       
     7  * published by the Free Software Foundation;
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program; if not, write to the Free Software
       
    16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    17  *
       
    18  * Author: Federico Maguolo <maguolof@dei.unipd.it>
       
    19  */
       
    20 
       
    21 #include "ns3/simulator.h"
       
    22 #include "ns3/uinteger.h"
       
    23 #include "ns3/double.h"
       
    24 #include "ns3/random-variable.h"
       
    25 #include "ns3/default-value.h"
       
    26 #include "ns3/mobility-model.h"
       
    27 #include "ns3/log.h"
       
    28 #include "jakes-propagation-loss-model.h"
       
    29 #include <math.h>
       
    30 
       
    31 NS_LOG_COMPONENT_DEFINE ("Jakes");
       
    32 
       
    33 namespace ns3 {
       
    34 
       
    35 
       
    36 
       
    37 class JakesPropagationLossModel::PathCoefficients {
       
    38 public:
       
    39   PathCoefficients (Ptr<const JakesPropagationLossModel> jakes,
       
    40                     Ptr<MobilityModel> receiver, 
       
    41                     uint8_t nRays, 
       
    42                     uint8_t nOscillators);
       
    43   ~PathCoefficients ();
       
    44   double GetLoss (void);
       
    45   Ptr<MobilityModel> GetReceiver (void);
       
    46 private:
       
    47   void DoConstruct (void);
       
    48   Ptr<MobilityModel> m_receiver;
       
    49   uint8_t m_nOscillators;
       
    50   uint8_t m_nRays;
       
    51   double **m_phases;
       
    52   Time m_lastUpdate;
       
    53   Ptr<const JakesPropagationLossModel> m_jakes;
       
    54 };
       
    55 
       
    56 
       
    57 JakesPropagationLossModel::PathCoefficients::PathCoefficients (Ptr<const JakesPropagationLossModel> jakes, 
       
    58                                                            Ptr<MobilityModel> receiver, 
       
    59                                                            uint8_t nRays, 
       
    60 							   uint8_t nOscillators)
       
    61   : m_receiver (receiver),
       
    62     m_nOscillators (nOscillators),
       
    63     m_nRays (nRays),
       
    64     m_jakes(jakes)
       
    65 {
       
    66   DoConstruct ();
       
    67 }
       
    68 
       
    69 JakesPropagationLossModel::PathCoefficients::~PathCoefficients ()
       
    70 {
       
    71   for (uint8_t i = 0; i < m_nRays; i++) {
       
    72     delete [] m_phases[i];
       
    73   }
       
    74   delete [] m_phases;
       
    75 }
       
    76 
       
    77 void
       
    78 JakesPropagationLossModel::PathCoefficients::DoConstruct ()
       
    79 {
       
    80   m_phases = new double*[m_nRays];
       
    81   for (uint8_t i = 0; i < m_nRays; i++) {
       
    82     m_phases[i] = new double[m_nOscillators + 1];
       
    83     for (uint8_t j = 0; j <= m_nOscillators; j++) {
       
    84       m_phases[i][j] = 2.0 * JakesPropagationLossModel::PI * m_jakes->m_variable.GetValue ();
       
    85     }
       
    86   }
       
    87   m_lastUpdate = Simulator::Now ();
       
    88 }
       
    89 
       
    90 Ptr<MobilityModel>
       
    91 JakesPropagationLossModel::PathCoefficients::GetReceiver ()
       
    92 {
       
    93   return m_receiver;
       
    94 }
       
    95 
       
    96 double
       
    97 JakesPropagationLossModel::PathCoefficients::GetLoss (void)
       
    98 {
       
    99   uint16_t N = 4 * m_nOscillators + 2;
       
   100   Time interval = Simulator::Now () - m_lastUpdate;
       
   101   ComplexNumber coef= {0.0, 0.0};
       
   102   ComplexNumber fading;
       
   103   double norm = 0.0;
       
   104   for (uint8_t i = 0; i < m_nRays; i++) {
       
   105     fading.real = 0.0;
       
   106     fading.imag = 0.0;
       
   107     for (uint8_t j = 0; j <= m_nOscillators; j++) {
       
   108       m_phases[i][j] += 2.0 * JakesPropagationLossModel::PI * cos (2.0 * JakesPropagationLossModel::PI * j / N) * m_jakes->m_fd * interval.GetSeconds ();
       
   109       m_phases[i][j] -= 2.0 * JakesPropagationLossModel::PI * floor (m_phases[i][j] / 2.0 / JakesPropagationLossModel::PI);
       
   110       fading.real += m_jakes->m_amp[j].real * cos (m_phases[i][j]);
       
   111       fading.imag += m_jakes->m_amp[j].imag * cos (m_phases[i][j]);
       
   112       norm += sqrt(pow (m_jakes->m_amp[j].real, 2) + pow(m_jakes->m_amp[j].imag, 2));
       
   113     }
       
   114     coef.real += fading.real;
       
   115     coef.imag += fading.imag;
       
   116   }
       
   117   m_lastUpdate = Simulator::Now ();
       
   118   double k = sqrt (pow (coef.real, 2) + pow (coef.imag, 2)) / norm;
       
   119   NS_LOG_DEBUG ("Jakes coef "<< k << " (" << 10 * log10 (k) << "dB)");
       
   120   return 10 * log10 (k);
       
   121 }
       
   122 
       
   123 NS_OBJECT_ENSURE_REGISTERED (JakesPropagationLossModel);
       
   124 
       
   125 const double JakesPropagationLossModel::PI = 3.1415;
       
   126 
       
   127 TypeId
       
   128 JakesPropagationLossModel::GetTypeId (void)
       
   129 {
       
   130   static TypeId tid = TypeId ("ns3::JakesPropagationLossModel")
       
   131     .SetParent<PropagationLossModel> ()
       
   132     .AddConstructor<JakesPropagationLossModel> ()
       
   133     .AddAttribute ("NumberOfRaysPerPath",
       
   134                    "The number of rays to use by default for compute the fading coeficent for a given path (default is 1)",
       
   135                    Uinteger (1),
       
   136 		   MakeUintegerAccessor (&JakesPropagationLossModel::m_nRays),
       
   137 		   MakeUintegerChecker<uint8_t> ())
       
   138     .AddAttribute ("NumberOfOscillatorsPerRay",
       
   139                    "The number of oscillators to use by default for compute the coeficent for a given ray of a given path (default is 4)",
       
   140                    Uinteger (4),
       
   141 		   MakeUintegerAccessor (&JakesPropagationLossModel::m_nOscillators),
       
   142 		   MakeUintegerChecker<uint8_t> ())
       
   143     .AddAttribute ("DopplerFreq",
       
   144                    "The doppler frequency in Hz (f_d = v / lambda = v * f / c, the defualt is 0)",
       
   145                    Double(0.0),
       
   146 		   MakeDoubleAccessor (&JakesPropagationLossModel::m_fd),
       
   147 		   MakeDoubleChecker<double> ())
       
   148     .AddAttribute ("Distribution",
       
   149                    "The distribution to choose the initial phases.",
       
   150                    ConstantVariable (1.0),
       
   151                    MakeRandomVariableAccessor (&JakesPropagationLossModel::m_variable),
       
   152                    MakeRandomVariableChecker ())
       
   153     ;
       
   154   return tid;
       
   155 }
       
   156 
       
   157 JakesPropagationLossModel::JakesPropagationLossModel ()
       
   158 {
       
   159   DoConstruct ();
       
   160 }
       
   161 
       
   162 JakesPropagationLossModel::~JakesPropagationLossModel ()
       
   163 {
       
   164   delete [] m_amp;
       
   165   for (PathsList::iterator i = m_paths.end (); i != m_paths.begin (); i--) {
       
   166     PathsSet *ps = *i;
       
   167     for (DestinationList::iterator r = ps->receivers.begin (); r != ps->receivers.end (); r++) {
       
   168       PathCoefficients *pc = *r;
       
   169       delete pc;
       
   170     }
       
   171     delete ps;
       
   172   }
       
   173 }
       
   174 
       
   175 void
       
   176 JakesPropagationLossModel::DoConstruct ()
       
   177 {
       
   178   uint16_t N = 4 * m_nOscillators + 2;
       
   179   m_amp = new ComplexNumber[m_nOscillators + 1];
       
   180   m_amp[0].real = 2.0 * sqrt(2.0 / N) * cos (PI / 4.0);
       
   181   m_amp[0].imag = 2.0 * sqrt(2.0 / N) * sin (PI / 4.0);
       
   182   for (uint8_t i = 1; i <= m_nOscillators; i++) {
       
   183     double beta = PI * (double)i / m_nOscillators;
       
   184     m_amp[i].real = 4.0 * cos (beta) / sqrt(N);
       
   185     m_amp[i].imag = 4.0 * sin (beta) / sqrt(N);
       
   186   }
       
   187 }
       
   188 
       
   189 void
       
   190 JakesPropagationLossModel::SetNRays (uint8_t nRays)
       
   191 {
       
   192   m_nRays = nRays;
       
   193 }
       
   194 
       
   195 void
       
   196 JakesPropagationLossModel::SetNOscillators (uint8_t nOscillators)
       
   197 {
       
   198   m_nOscillators = nOscillators;
       
   199 }
       
   200 
       
   201 double
       
   202 JakesPropagationLossModel::GetLoss (Ptr<MobilityModel> a,
       
   203 				    Ptr<MobilityModel> b) const
       
   204 {
       
   205   PathsList::iterator i = m_paths.end ();
       
   206   while (i != m_paths.begin ()) {
       
   207     i--;
       
   208     PathsSet *ps = *i;
       
   209     if (PeekPointer (ps->sender) == PeekPointer(a)) {
       
   210       m_paths.erase (i);
       
   211       m_paths.push_back (ps);
       
   212       for (DestinationList::iterator r = ps->receivers.begin (); r != ps->receivers.end (); r++) {
       
   213         PathCoefficients *pc = *r;
       
   214       	if (PeekPointer (pc->GetReceiver ()) == PeekPointer (b)) {
       
   215           ps->receivers.erase (r);
       
   216 	  ps->receivers.push_back (pc);
       
   217 	  return pc->GetLoss ();
       
   218 	}
       
   219       }
       
   220       PathCoefficients *pc = new PathCoefficients (this, b, m_nRays, m_nOscillators);
       
   221       ps->receivers.push_back (pc);
       
   222       return pc->GetLoss ();
       
   223     }
       
   224   }
       
   225   PathsSet *ps = new PathsSet;
       
   226   ps->sender = a;
       
   227   PathCoefficients *pc = new PathCoefficients (this, b, m_nRays, m_nOscillators);
       
   228   ps->receivers.push_back (pc);
       
   229   m_paths.push_back (ps);
       
   230   return pc->GetLoss ();
       
   231 }
       
   232 
       
   233 
       
   234 } // namespace ns3
       
   235