27#include "ns3/acoustic-modem-energy-model.h"
28#include "ns3/double.h"
29#include "ns3/energy-source-container.h"
33#include "ns3/simulator.h"
34#include "ns3/string.h"
35#include "ns3/trace-source-accessor.h"
36#include "ns3/traced-callback.h"
37#include "ns3/uan-tx-mode.h"
38#include "ns3/uinteger.h"
82 NS_LOG_WARN(
"Calculating SINR for unsupported modulation type");
85 double intKp = -
DbToKp(rxPowerDb);
86 UanTransducer::ArrivalList::const_iterator it = arrivalList.begin();
87 for (; it != arrivalList.end(); it++)
89 intKp +=
DbToKp(it->GetRxPowerDb());
95 << rxPowerDb <<
" dB. Number of interferers = " << arrivalList.size()
96 <<
" Interference + noise power = " << totalIntDb
97 <<
" dB. SINR = " << rxPowerDb - totalIntDb <<
" dB.");
98 return rxPowerDb - totalIntDb;
117 .AddAttribute(
"NumberOfHops",
118 "Number of frequencies in hopping pattern.",
121 MakeUintegerChecker<uint32_t>());
147 for (; pit != pdp.
GetEnd(); pit++)
149 if (std::abs(pit->GetAmp()) > maxAmp)
151 maxAmp = std::abs(pit->GetAmp());
158 double effRxPowerDb = rxPowerDb +
KpToDb(csp);
164 UanTransducer::ArrivalList::const_iterator it = arrivalList.begin();
165 double intKp = -
DbToKp(effRxPowerDb);
166 for (; it != arrivalList.end(); it++)
168 UanPdp intPdp = it->GetPdp();
169 Time tDelta =
Abs(arrTime + maxTapDelay - it->GetArrivalTime());
174 tDelta =
Rem(tDelta, ts + clearingTime);
177 if (arrTime + maxTapDelay > it->GetArrivalTime())
179 tDelta = ts + clearingTime - tDelta;
182 double intPower = 0.0;
189 intPdp.
SumTapsNc(ts - tDelta + clearingTime, 2 * ts - tDelta + clearingTime);
195 Time start = ts + clearingTime - tDelta;
198 intPower += intPdp.
SumTapsNc(start, end);
200 start = start + ts + clearingTime;
202 end = end + ts + clearingTime;
203 intPower += intPdp.
SumTapsNc(start, end);
205 intKp +=
DbToKp(it->GetRxPowerDb()) * intPower;
208 double totalIntDb =
KpToDb(isiUpa + intKp +
DbToKp(ambNoiseDb));
211 << rxPowerDb <<
" dB. Effective Rx power " << effRxPowerDb
212 <<
" dB. Number of interferers = " << arrivalList.size()
213 <<
" Interference + noise power = " << totalIntDb
214 <<
" dB. SINR = " << effRxPowerDb - totalIntDb <<
" dB.");
215 return effRxPowerDb - totalIntDb;
234 .AddAttribute(
"Threshold",
235 "SINR cutoff for good packet reception.",
238 MakeDoubleChecker<double>());
283 double EbNo = std::pow(10.0, sinrDb / 10.0);
294 BER = 0.5 * erfc(sqrt(EbNo));
299 BER = 0.5 * erfc(sqrt(0.5 * EbNo));
318 int log2sqrtM = (int)::std::log2(sqrt(M));
320 double log2M = ::std::log2(M);
327 double sqrtM = ::std::sqrt(M);
329 NS_LOG_DEBUG(
"M=" << M <<
"; log2sqrtM=" << log2sqrtM <<
"; log2M=" << log2M
330 <<
"; sqrtM=" << sqrtM);
335 for (
int k = 0; k < log2sqrtM; k++)
338 (int)((1.0 - ::std::pow(2.0, (-1.0) * (
double)k)) * ::std::sqrt(M) - 1.0);
339 double pow2k = ::std::pow(2.0, (
double)k - 1.0);
341 NS_LOG_DEBUG(
"k=" << k <<
"; sum_items=" << sum_items <<
"; pow2k=" << pow2k);
346 for (
int j = 0; j < sum_items; ++j)
348 PbK += ::std::pow(-1.0, (
double)j * pow2k / sqrtM) *
349 (pow2k - ::std::floor((
double)(j * pow2k / sqrtM) - 0.5)) *
350 erfc((2.0 * (
double)j + 1.0) *
351 ::std::sqrt(3.0 * (log2M * EbNo) / (2.0 * (M - 1.0))));
359 NS_LOG_DEBUG(
"k=" << k <<
"; PbK=" << PbK <<
"; BER=" << BER);
362 BER *= 1.0 / (
double)log2sqrtM;
371 BER = 0.5 * erfc(sqrt(0.5 * EbNo));
385 PER = (1.0 - pow(1.0 - BER, (
double)pkt->GetSize() * 8.0));
418 for (
uint32_t i = std::max(k, n - k) + 1; i <= n; ++i)
423 for (
uint32_t i = 2; i <= std::min(k, n - k); ++i)
434 uint32_t d[] = {12, 14, 16, 18, 20, 22, 24, 26, 28};
436 {33, 281, 2179, 15035LLU, 105166LLU, 692330LLU, 4580007LLU, 29692894LLU, 190453145LLU};
439 double ebno = std::pow(10.0, sinr / 10.0);
440 double perror = 1.0 / (2.0 + ebno);
461 sumd = sumd +
NChooseK(d[r] - 1 + k, k) * std::pow(1 - perror, (
double)k);
463 P[r] = std::pow(perror, (
double)d[r]) * sumd;
469 Pb = Pb + Bd[r] * P[r];
477 temp *= std::pow((1 - Pb), (
double)bits);
479 temp =
NChooseK(288, 1) * Pb * std::pow((1 - Pb), bits - 1.0);
497 m_transducer(nullptr),
507 m_pg = CreateObject<UniformRandomVariable>();
588 .AddAttribute(
"CcaThreshold",
589 "Aggregate energy of incoming signals to move to CCA Busy state dB.",
592 MakeDoubleChecker<double>())
593 .AddAttribute(
"RxThreshold",
594 "Required SNR for signal acquisition in dB.",
597 MakeDoubleChecker<double>())
598 .AddAttribute(
"TxPower",
599 "Transmission output power in dB.",
602 MakeDoubleChecker<double>())
603 .AddAttribute(
"SupportedModes",
604 "List of modes supported by this PHY.",
607 MakeUanModesListChecker())
608 .AddAttribute(
"PerModel",
609 "Functor to calculate PER based on SINR and TxMode.",
612 MakePointerChecker<UanPhyPer>())
613 .AddAttribute(
"SinrModel",
614 "Functor to calculate SINR based on pkt arrivals and modes.",
617 MakePointerChecker<UanPhyCalcSinr>())
618 .AddTraceSource(
"RxOk",
619 "A packet was received successfully.",
621 "ns3::UanPhy::TracedCallback")
622 .AddTraceSource(
"RxError",
623 "A packet was received unsuccessfuly.",
625 "ns3::UanPhy::TracedCallback")
626 .AddTraceSource(
"Tx",
627 "Packet transmission beginning.",
629 "ns3::UanPhy::TracedCallback");
656 <<
", stopping rx/tx activities");
678 <<
", restoring rx/tx activities");
689 NS_LOG_DEBUG(
"Energy depleted, node cannot transmit any packet. Dropping.");
695 NS_LOG_DEBUG(
"PHY requested to TX while already Transmitting. Dropping packet.");
700 NS_LOG_DEBUG(
"PHY requested to TX while sleeping. Dropping packet.");
728 NS_LOG_DEBUG(
"Transmission ended but node sleeping or dead");
756 NS_LOG_DEBUG(
"PHY " <<
m_mac->GetAddress() <<
": rx power after RX gain = " << rxPowerDb
762 NS_LOG_DEBUG(
"Energy depleted, node cannot receive any packet. Dropping.");
775 <<
": Starting RX in RX mode. SINR of pktRx = " <<
m_minRxSinrDb);
783 bool hasmode =
false;
799 <<
": Starting RX in IDLE mode. SINR = " << newsinr);
1081 ->CalcSinrDb(pkt, arrTime, rxPowerDb, noiseDb, mode, pdp,
m_transducer->GetArrivalList());
1089 UanTransducer::ArrivalList::const_iterator it = arrivalList.begin();
1091 double interfPower = 0;
1093 for (; it != arrivalList.end(); it++)
1095 if (pkt != it->GetPacket())
1097 interfPower +=
DbToKp(it->GetRxPowerDb());
1101 return KpToDb(interfPower);
1107 return std::pow(10, db / 10.0);
1113 return 10 * std::log10(kp);
1119 ListenerList::const_iterator it =
m_listeners.begin();
1122 (*it)->NotifyRxStart();
1129 ListenerList::const_iterator it =
m_listeners.begin();
1132 (*it)->NotifyRxEndOk();
1139 ListenerList::const_iterator it =
m_listeners.begin();
1142 (*it)->NotifyRxEndError();
1149 ListenerList::const_iterator it =
m_listeners.begin();
1152 (*it)->NotifyCcaStart();
1159 ListenerList::const_iterator it =
m_listeners.begin();
1162 (*it)->NotifyCcaEnd();
1169 ListenerList::const_iterator it =
m_listeners.begin();
1172 (*it)->NotifyTxStart(duration);
1179 ListenerList::const_iterator it =
m_listeners.begin();
1182 (*it)->NotifyTxEnd();
void Nullify()
Discard the implementation, set it to null.
bool IsNull() const
Check for null implementation.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
bool IsRunning() const
This method is syntactic sugar for !IsExpired().
virtual void DoDispose()
Destructor implementation.
Smart pointer class similar to boost::intrusive_ptr.
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
static Time Now()
Return the current simulation virtual time.
Hold variables of type string.
Time GetDelay() const
Get the delay time, usually from first arrival of signal.
Simulation virtual time values and global simulation resolution.
a unique identifier for an interface.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Container for UanTxModes.
uint32_t GetNModes() const
Get the number of modes in this list.
void AppendMode(UanTxMode mode)
Add mode to this list.
AttributeValue implementation for UanModesList.
The power delay profile returned by propagation models.
Iterator GetEnd() const
Get the end of the tap list (one beyond the last entry).
double SumTapsFromMaxNc(Time delay, Time duration) const
Compute the non-coherent sum of tap amplitudes starting after a delay from the maximum amplitude for ...
std::vector< Tap >::const_iterator Iterator
Convenience iterator typedef.
double SumTapsNc(Time begin, Time end) const
Compute the non-coherent sum of tap amplitudes between a start and end time.
const Tap & GetTap(uint32_t i) const
Get the Tap at the specified delay index.
Iterator GetBegin() const
Get the beginning of the tap vector.
Default SINR calculator for UanPhyGen.
double CalcSinrDb(Ptr< Packet > pkt, Time arrTime, double rxPowerDb, double ambNoiseDb, UanTxMode mode, UanPdp pdp, const UanTransducer::ArrivalList &arrivalList) const override
Calculate the SINR value for a packet.
UanPhyCalcSinrDefault()
Constructor.
static TypeId GetTypeId()
Register this type.
~UanPhyCalcSinrDefault() override
Destructor.
WHOI Micromodem like FH-FSK model.
UanPhyCalcSinrFhFsk()
Constructor.
double CalcSinrDb(Ptr< Packet > pkt, Time arrTime, double rxPowerDb, double ambNoiseDb, UanTxMode mode, UanPdp pdp, const UanTransducer::ArrivalList &arrivalList) const override
Calculate the SINR value for a packet.
static TypeId GetTypeId()
Register this type.
~UanPhyCalcSinrFhFsk() override
Destructor.
uint32_t m_hops
Number of hops.
Class used for calculating SINR of packet in UanPhy.
double DbToKp(double db) const
Convert dB re 1 uPa to kilopascals.
double KpToDb(double kp) const
Convert kilopascals to dB re 1 uPa.
Ptr< UanChannel > GetChannel() const override
Get the attached channel.
bool IsStateSleep() override
void SetTxPowerDb(double txpwr) override
Set the transmit power.
double KpToDb(double kp)
Convert kilopascals to dB.
void NotifyTransStartTx(Ptr< Packet > packet, double txPowerDb, UanTxMode txMode) override
Called when a transmission is beginning on the attached transducer.
int64_t AssignStreams(int64_t stream) override
Assign a fixed random variable stream number to the random variables used by this model.
Ptr< UanPhyPer > m_per
Error model.
void SetRxThresholdDb(double thresh) override
Set the minimum SINR threshold to receive a packet without errors.
void EnergyDepletionHandler() override
Handle the energy depletion event.
void NotifyListenersTxStart(Time duration)
Call UanListener::NotifyTxStart on all listeners.
Time m_pktRxArrTime
Packet arrival time.
UanTxMode GetMode(uint32_t n) override
Get a specific transmission mode.
void UpdatePowerConsumption(const State state)
Update energy source with new state.
double GetRxThresholdDb() override
Get the minimum received signal strength required to receive a packet without errors.
ListenerList m_listeners
List of listeners.
void NotifyListenersCcaStart()
Call UanListener::NotifyCcaStart on all listeners.
void SetSleepMode(bool sleep) override
Set the Phy SLEEP mode.
~UanPhyGen() override
Dummy destructor, see DoDispose.
double m_rxRecvPwrDb
Receiver power.
bool IsStateBusy() override
void NotifyListenersRxGood()
Call UanListener::NotifyRxEndOk on all listeners.
uint32_t GetNModes() override
Get the number of transmission modes supported by this Phy.
Ptr< UanPhyCalcSinr > m_sinr
SINR calculator.
void NotifyListenersRxBad()
Call UanListener::NotifyRxEndError on all listeners.
double GetCcaThresholdDb() override
Get the CCA threshold signal strength required to detect channel busy.
Ptr< Packet > m_pktRx
Received packet.
void SetReceiveErrorCallback(RxErrCallback cb) override
Set the callback to be used when a packet is received with errors.
Ptr< UanTransducer > GetTransducer() override
Get the attached transducer.
DeviceEnergyModel::ChangeStateCallback m_energyCallback
Energy model callback.
Ptr< UanTransducer > m_transducer
Associated transducer.
double m_rxThreshDb
Receive SINR threshold.
EventId m_rxEndEvent
Rx event.
void DoDispose() override
Destructor implementation.
double m_ccaThreshDb
CCA busy threshold.
void SetTransducer(Ptr< UanTransducer > trans) override
Attach a transducer to this Phy.
RxOkCallback m_recOkCb
Callback for packets received without error.
void NotifyListenersTxEnd()
Call UanListener::NotifyTxEnd on all listeners.
Ptr< UanMac > m_mac
MAC layer.
Ptr< UanNetDevice > GetDevice() const override
Get the device hosting this Phy.
double GetInterferenceDb(Ptr< Packet > pkt)
Calculate interference power from overlapping packet arrivals, in dB.
double m_txPwrDb
Transmit power.
double CalculateSinrDb(Ptr< Packet > pkt, Time arrTime, double rxPowerDb, UanTxMode mode, UanPdp pdp)
Calculate the SINR value for a packet.
void SetReceiveOkCallback(RxOkCallback cb) override
Set the callback to be used when a packet is received without error.
static UanModesList GetDefaultModes()
Get the default transmission modes.
bool IsStateTx() override
ns3::TracedCallback< Ptr< const Packet >, double, UanTxMode > m_rxOkLogger
A packet destined for this Phy was received without error.
Ptr< UniformRandomVariable > m_pg
Provides uniform random variables.
EventId m_txEndEvent
Tx event.
void SendPacket(Ptr< Packet > pkt, uint32_t modeNum) override
Send a packet using a specific transmission mode.
void RegisterListener(UanPhyListener *listener) override
Register a UanPhyListener to be notified of common UanPhy events.
bool m_cleared
Flag when we've been cleared.
bool IsStateRx() override
void SetMac(Ptr< UanMac > mac) override
Set the MAC forwarding messages to this Phy.
double m_minRxSinrDb
Minimum receive SINR during packet reception.
static TypeId GetTypeId()
Register this type.
void SetCcaThresholdDb(double thresh) override
Set the threshold for detecting channel busy.
Ptr< UanChannel > m_channel
Attached channel.
bool IsStateCcaBusy() override
void SetEnergyModelCallback(DeviceEnergyModel::ChangeStateCallback cb) override
Set the DeviceEnergyModel callback for UanPhy device.
void SetDevice(Ptr< UanNetDevice > device) override
Set the device hosting this Phy.
double DbToKp(double db)
Convert dB to kilopascals.
UanTxMode m_pktRxMode
Packet transmission mode at receiver.
void TxEndEvent()
Event to process end of packet transmission.
void Clear() override
Clear all pointer references.
Ptr< Packet > GetPacketRx() const override
Get the packet currently being received.
RxErrCallback m_recErrCb
Callback for packets received with errors.
void RxEndEvent(Ptr< Packet > pkt, double rxPowerDb, UanTxMode txMode)
Event to process end of packet reception.
Ptr< UanNetDevice > m_device
Device hosting this Phy.
void StartRxPacket(Ptr< Packet > pkt, double rxPowerDb, UanTxMode txMode, UanPdp pdp) override
Packet arriving from channel: i.e.
Ptr< Packet > m_pktTx
Sent packet.
void SetChannel(Ptr< UanChannel > channel) override
Attach to a channel.
ns3::TracedCallback< Ptr< const Packet >, double, UanTxMode > m_txLogger
A packet was sent from this Phy.
void NotifyIntChange() override
Called when there has been a change in the amount of interference this node is experiencing from othe...
double GetTxPowerDb() override
Get the current transmit power, in dB.
bool IsStateIdle() override
UanPdp m_pktRxPdp
Power delay profile of packet.
void NotifyListenersCcaEnd()
Call UanListener::NotifyCcaEnd on all listeners.
void EnergyRechargeHandler() override
Handle the energy recharge event.
void NotifyListenersRxStart()
Call UanListener::NotifyRxStart on all listeners.
ns3::TracedCallback< Ptr< const Packet >, double, UanTxMode > m_rxErrLogger
A packet destined for this Phy was received with error.
UanModesList m_modes
List of modes supported by this PHY.
Base class for UAN Phy models.
void NotifyTxDrop(Ptr< const Packet > packet)
Called when the transducer attempts to transmit a new packet while already transmitting a prior packe...
void NotifyRxDrop(Ptr< const Packet > packet)
Called when the Phy drops a packet.
void NotifyRxBegin(Ptr< const Packet > packet)
Called when the Phy begins to receive a packet.
void NotifyRxEnd(Ptr< const Packet > packet)
Called when a packet is received without error.
State
Enum defining possible Phy states.
Interface for PHY event listener.
Packet error rate calculation for common tx modes based on UanPhyPerUmodem.
~UanPhyPerCommonModes() override
Destructor.
double CalcPer(Ptr< Packet > pkt, double sinrDb, UanTxMode mode) override
Calculate the Packet ERror probability based on SINR at the receiver and a tx mode.
UanPhyPerCommonModes()
Constructor.
static TypeId GetTypeId()
Register this type.
Default Packet Error Rate calculator for UanPhyGen.
UanPhyPerGenDefault()
Constructor.
~UanPhyPerGenDefault() override
Destructor.
double CalcPer(Ptr< Packet > pkt, double sinrDb, UanTxMode mode) override
Calculate the packet error probability based on SINR at the receiver and a tx mode.
static TypeId GetTypeId()
Register this type.
double m_thresh
SINR threshold.
Calculate packet error probability, based on received SINR and modulation (mode).
Packet error rate calculation assuming WHOI Micromodem-like PHY (FH-FSK)
double CalcPer(Ptr< Packet > pkt, double sinrDb, UanTxMode mode) override
Calculate the packet error probability based on SINR at the receiver and a tx mode.
static TypeId GetTypeId()
Register this type.
double NChooseK(uint32_t n, uint32_t k)
Binomial coefficient.
UanPhyPerUmodem()
Constructor.
~UanPhyPerUmodem() override
Destructor.
std::list< UanPacketArrival > ArrivalList
List of arriving packets overlapping in time.
static UanTxMode CreateMode(UanTxMode::ModulationType type, uint32_t dataRateBps, uint32_t phyRateSps, uint32_t cfHz, uint32_t bwHz, uint32_t constSize, std::string name)
Abstraction of packet modulation information.
uint32_t GetUid() const
Get a unique id for the mode.
@ QAM
Quadrature amplitude modulation.
@ OTHER
Unspecified/undefined.
@ FSK
Frequency shift keying.
uint32_t GetConstellationSize() const
Get the number of constellation points in the modulation scheme.
uint32_t GetPhyRateSps() const
Get the physical signaling rate.
uint32_t GetDataRateBps() const
Get the data rate of the transmit mode.
uint32_t GetBandwidthHz() const
Get the transmission signal bandwidth.
ModulationType GetModType() const
Get the modulation type of the mode.
uint32_t GetCenterFreqHz() const
Get the transmission center frequency.
Hold an unsigned integer type.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
int64x64_t Abs(const int64x64_t &value)
Absolute value.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Time Seconds(double value)
Construct a Time in the indicated unit.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Time Rem(const Time &lhs, const Time &rhs)
Remainder (modulus) from the quotient of two Times.
@ IDLE
Channel is IDLE, no packet is being transmitted.