Loading...

# 7.ns3 损失模型修改

在本节中,将介绍 ns3 中 wifi 损失模型模型修改 / 新增所需的一些内容,

# 代码

信道损失模型的源码位置如下

ns-3.xx/src/propagation/model

可以将自己的定义的信道损失模型添加到该路径,更改 wscript 文件,然后重新 build 一下 ns3

./waf build

主要更改DoCalcRxPowerDoCalcRxPower 函数即可
无线默认的损失模型是P_r=\frac{P_tG_tG_r\lambda^2}

  • Gt: tx gain (unit-less)
  • Gr: rx gain (unit-less)
  • Pt: tx power (W)
  • d: distance (m)
  • L: system loss
  • lambda: wavelength (m)
    我改成了水下光学传播损耗模型中的 Pr=Ptec(λ)dηrηtP_r=P_te^{-c(\lambda)d}\eta_r\eta_t
    ηr,ηt表示发射机的发射效率和接收机的接收效率\eta_r,\eta_t表示发射机的发射效率和接收机的接收效率
    c(λ)c(\lambda) 表示海水的衰减系数

以下是我定义的损失模型
MyPropagationLossModel.h

/*************************************************************************
    > File Name: myLossModel.h
    > Author: yeyu
    > Mail: 554377661@qq.com 
    > Created Time: Sun 17 Jan 2021 01:20:27 AM PST
 ************************************************************************/
#include "ns3/propagation-loss-model.h"
#include "ns3/propagation-cache.h"
namespace ns3 {
class MyPropagationLossModel : public PropagationLossModel
{
private:
 //m_cLamada 海水的总衰减系数
 double m_cLamada;
 //m_nt 表示通信系统中发射机的发射效率,m_nr 表示通信系统中接收机的接收效率。
 double m_nt,m_nr;
public:
  /**
   * \brief Get the type ID.
   * \return the object TypeId
   */
  static TypeId GetTypeId (void);
  MyPropagationLossModel ();
  // 设置发送接收效率 (<=1)
  void SetSendReceiveEfficiency(double nt,double nr);
  //get 发送接收效率
  double GetReceiveEfficiency();
  double GetSendEfficiency();
  /**
  * set CLamada
  */
  void SetCLamada (double cLamada);
  /**
  * get CLamada
  */
  double GetCLamada ();
  /**
   * \param frequency (Hz)
   *
   * Set the carrier frequency used in the Friis model 
   * calculation.
   */
  void SetFrequency (double frequency);
  /**
   * \param systemLoss (dimension-less)
   *
   * Set the system loss used by the Friis propagation model.
   */
  void SetSystemLoss (double systemLoss);
  /**
   * \param minLoss the minimum loss (dB)
   *
   * no matter how short the distance, the total propagation loss (in
   * dB) will always be greater or equal than this value 
   */
  void SetMinLoss (double minLoss);
  /**
   * \return the minimum loss.
   */
  double GetMinLoss (void) const;
  /**
   * \returns the current frequency (Hz)
   */
  double GetFrequency (void) const;
  /**
   * \returns the current system loss (dimension-less)
   */
  double GetSystemLoss (void) const;
private:
  /**
   * \brief Copy constructor
   *
   * Defined and unimplemented to avoid misuse
   */
  MyPropagationLossModel (const MyPropagationLossModel &);
  /**
   * \brief Copy constructor
   *
   * Defined and unimplemented to avoid misuse
   * \returns
   */
  MyPropagationLossModel & operator = (const MyPropagationLossModel &);
  virtual double DoCalcRxPower (double txPowerDbm,
                                Ptr<MobilityModel> a,
                                Ptr<MobilityModel> b) const;
  virtual int64_t DoAssignStreams (int64_t stream);
  /**
   * Transforms a Dbm value to Watt
   * \param dbm the Dbm value
   * \return the Watts
   */
  double DbmToW (double dbm) const;
  /**
   * Transforms a Watt value to Dbm
   * \param w the Watt value
   * \return the Dbm
   */
  double DbmFromW (double w) const;
  double m_lambda;        //!< the carrier wavelength
  double m_frequency;     //!< the carrier frequency
  double m_systemLoss;    //!< the system loss
  double m_minLoss;       //!< the minimum loss
};
} //end of namespace ns3

MyPropagationLossModel.cc 的具体实现

#include "MyPropagationLossModel.h"
#include "ns3/log.h"
#include "ns3/mobility-model.h"
#include "ns3/boolean.h"
#include "ns3/double.h"
#include "ns3/string.h"
#include "ns3/pointer.h"
#include <cmath>
namespace ns3 {
NS_LOG_COMPONENT_DEFINE ("MyLossModel");
NS_OBJECT_ENSURE_REGISTERED (MyPropagationLossModel);
TypeId
MyPropagationLossModel::GetTypeId (void)
{
  static TypeId tid = TypeId ("ns3::MyPropagationLossModel")
    .SetParent<PropagationLossModel> ()
    .SetGroupName ("Propagation")
    .AddConstructor<MyPropagationLossModel> ()
    .AddAttribute ("Frequency",
                    "The carrier frequency (in Hz) at which propagation occurs  (default is 5.15 GHz).",
                   DoubleValue (5.150e9),
                   MakeDoubleAccessor (&MyPropagationLossModel::SetFrequency,
                                       &MyPropagationLossModel::GetFrequency),
                   MakeDoubleChecker<double> ())
    .AddAttribute ("SystemLoss", "The system loss",
                   DoubleValue (1.0),
                   MakeDoubleAccessor (&MyPropagationLossModel::m_systemLoss),
                    MakeDoubleChecker<double> ())
    .AddAttribute ("MinLoss",
                   "The minimum value (dB) of the total loss, used at short ranges. Note: ",
                   DoubleValue (0.0),
                   MakeDoubleAccessor (&MyPropagationLossModel::SetMinLoss,
                                       &MyPropagationLossModel::GetMinLoss),
                    MakeDoubleChecker<double> ())
    /*
    .AddAttribute("CLamada",
                  "CLamada is a double value",
                  DoubleValue(0.056),
                  MakeDoubleAccessor (&MyPropagationLossModel::m_cLamada,
                      &MyPropagationLossModel::GetCLamada),
                  MakeDoubleChecker<double> ()
            )
            */
  ;
  return tid;
}
MyPropagationLossModel::MyPropagationLossModel ()
{
    m_cLamada=0.056;
    m_nr=1;
    m_nt=1;
}
  // 设置发送接收效率 (<=1)
void MyPropagationLossModel::SetSendReceiveEfficiency(double nt,double nr){
    m_nt=nt;
    m_nr=nr;
}
double MyPropagationLossModel::GetSendEfficiency(){
    return m_nt;
}
double MyPropagationLossModel::GetReceiveEfficiency(){
    return m_nr;
}
  /**
  * set CLamada
  */
void MyPropagationLossModel::SetCLamada (double cLamada){
    m_cLamada=cLamada;
}
  /**
  * get CLamada
  */
double MyPropagationLossModel::GetCLamada (){
    return m_cLamada;
}
void
MyPropagationLossModel::SetSystemLoss (double systemLoss)
{
  m_systemLoss = systemLoss;
}
double
MyPropagationLossModel::GetSystemLoss (void) const
{
  return m_systemLoss;
}
void
MyPropagationLossModel::SetMinLoss (double minLoss)
{
  m_minLoss = minLoss;
}
double
MyPropagationLossModel::GetMinLoss (void) const
{
  return m_minLoss;
}
void
MyPropagationLossModel::SetFrequency (double frequency)
{
  m_frequency = frequency;
  static const double C = 299792458.0; // speed of light in vacuum
  m_lambda = C / frequency;
}
double
MyPropagationLossModel::GetFrequency (void) const
{
  return m_frequency;
}
double
MyPropagationLossModel::DbmToW (double dbm) const
{
  double mw = std::pow (10.0,dbm/10.0);
  return mw / 1000.0;
}
double
MyPropagationLossModel::DbmFromW (double w) const
{
  double dbm = std::log10 (w * 1000.0) * 10.0;
  return dbm;
}
double
MyPropagationLossModel::DoCalcRxPower (double txPowerDbm,
                                          Ptr<MobilityModel> a,
                                          Ptr<MobilityModel> b) const
{
  NS_LOG_WARN("this a test model");
  double distance = a->GetDistanceFrom (b);
  if (distance <= 0)
    {
      return txPowerDbm - m_minLoss;
    }
  double myLossDb=-10 * log10 (pow(M_E,-m_cLamada*distance));
//NS_LOG_WARN ("distance=" << distance<< "m, loss=" << myLossDb <<"dB");
//return txPowerDbm - std::max (myLossDb, m_minLoss);
  double tmp=txPowerDbm - std::max (myLossDb, m_minLoss);
  NS_LOG_WARN ("发射dbm="<<txPowerDbm <<", distance=" <<distance<< "m, loss=" << myLossDb <<"dB"<<" 实际损失为:"<< tmp<<"dB");
  return tmp;
}
int64_t
MyPropagationLossModel::DoAssignStreams (int64_t stream)
{
  return 0;
}
}

使用方法
修改 wscript 文件,将 MyPropagationLossModel.hMyPropagationLossModel.cc 添加到对应的位置
然后重新编译

// 先导入已经编译好的头文件
#include "ns3/MyPropagationLossModel.h"
#include "ns3/applications-module.h"
  WifiMacHelper wifiMac;
  wifiMac.SetType ("ns3::AdhocWifiMac");
  YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
  YansWifiChannelHelper wifiChannel=YansWifiChannelHelper::Default();
  Ptr<MyPropagationLossModel> lossModel=CreateObject<MyPropagationLossModel>();
  lossModel->SetCLamada(0.398);
  printf("clamada:%f\n",lossModel->GetCLamada());
  Ptr<YansWifiChannel> myChannel=wifiChannel.Create();
  myChannel->SetPropagationLossModel(lossModel); 
  wifiPhy.SetChannel (myChannel);

结果展示
最后的结果都是 dbm,之前写错了,一直没改 ==
图片标题

更新于 阅读次数

请我喝[茶]~( ̄▽ ̄)~*

jluyeyu 微信支付

微信支付

jluyeyu 支付宝

支付宝