29#include "ns3/packet.h"
30#include "ns3/simulator.h"
31#include "ns3/wifi-phy-operating-channel.h"
54 m_endTime(m_startTime + duration),
55 m_rxPowerW(
std::move(rxPower))
97 [](
const auto& p1,
const auto& p2) { return p1.second < p2.second; });
128 auto band = currentRxPowerW.first;
129 auto it = rxPower.find(band);
130 if (it != rxPower.end())
132 currentRxPowerW.second += it->second;
140 os <<
"start=" <<
event.GetStartTime() <<
", end=" <<
event.GetEndTime()
141 <<
", TXVECTOR=" <<
event.GetTxVector() <<
", power=" <<
event.GetRxPowerW() <<
"W"
142 <<
", PPDU=" <<
event.GetPpdu();
202 .SetGroupName(
"Wifi")
225 bool isStartOfdmaRxing)
227 Ptr<Event> event = Create<Event>(ppdu, txVector, duration, std::move(rxPowerW));
240 Ptr<WifiPpdu> fakePpdu = Create<WifiPpdu>(Create<WifiPsdu>(Create<Packet>(0), hdr),
265 auto result =
m_niChanges.insert({band, niChanges});
284 const auto frequencies = it->first.frequencies;
286 std::find_if(bands.cbegin(), bands.cend(), [frequencies](
const auto& item) {
287 return frequencies == item.frequencies;
288 }) != std::end(bands);
301 for (
const auto& band : bands)
344 for (; i != niIt->second.end(); ++i)
346 double noiseInterferenceW = i->second.GetPower();
348 if (noiseInterferenceW < energyW)
360 for (
const auto& [band, power] : event->GetRxPowerWPerBand())
364 double previousPowerStart = 0;
365 double previousPowerEnd = 0;
367 previousPowerStart = previousPowerPosition->second.GetPower();
373 niIt->second.erase(++(niIt->second.begin()), ++previousPowerPosition);
375 else if (isStartOfdmaRxing)
385 for (
auto i =
first; i != last; ++i)
387 i->second.AddPower(power);
397 for (
const auto& [band, power] : rxPower)
403 for (
auto i =
first; i != last; ++i)
405 i->second.AddPower(power);
408 event->UpdateRxPowerW(rxPower);
413 double noiseInterference,
414 uint16_t channelWidth,
417 NS_LOG_FUNCTION(
this << signal << noiseInterference << channelWidth << +nss);
419 static const double BOLTZMANN = 1.3803e-23;
421 double Nt = BOLTZMANN * 290 * channelWidth * 1e6;
424 double noise = noiseFloor + noiseInterference;
425 double snr = signal / noise;
426 NS_LOG_DEBUG(
"bandwidth(MHz)=" << channelWidth <<
", signal(W)= " << signal <<
", noise(W)="
427 << noiseFloor <<
", interference(W)=" << noiseInterference
437 NS_LOG_DEBUG(
"SNR improvement thanks to diversity: " << 10 * std::log10(gain) <<
"dB");
451 double noiseInterferenceW = firstPower_it->second;
454 auto it = niIt->second.find(event->GetStartTime());
455 for (; it != niIt->second.end() && it->first <
Simulator::Now(); ++it)
457 noiseInterferenceW = it->second.GetPower() -
event->GetRxPowerW(band);
459 it = niIt->second.find(event->GetStartTime());
461 for (; it != niIt->second.end() && it->second.GetEvent() != event; ++it)
466 ni.emplace(event->GetStartTime(),
NiChange(0, event));
467 while (++it != niIt->second.end() && it->second.GetEvent() != event)
471 ni.emplace(event->GetEndTime(),
NiChange(0, event));
472 nis->insert({band, ni});
474 "CalculateNoiseInterferenceW returns negative value " << noiseInterferenceW);
475 return noiseInterferenceW;
490 uint64_t nbits =
static_cast<uint64_t
>(rate * duration.
GetSeconds());
500 uint16_t staId)
const
508 uint64_t nbits =
static_cast<uint64_t
>(rate * duration.
GetSeconds());
509 nbits /= txVector.
GetNss(staId);
523 uint16_t channelWidth,
527 std::pair<Time, Time> window)
const
529 NS_LOG_FUNCTION(
this << channelWidth << band << staId << window.first << window.second);
531 const auto& niIt = nis->find(band)->second;
532 auto j = niIt.cbegin();
533 Time previous = j->first;
534 WifiMode payloadMode =
event->GetTxVector().GetMode(staId);
535 Time phyPayloadStart = j->first;
537 event->GetPpdu()->GetType() !=
543 Time windowStart = phyPayloadStart + window.first;
544 Time windowEnd = phyPayloadStart + window.second;
547 double powerW =
event->GetRxPowerW(band);
548 while (++j != niIt.cend())
550 Time current = j->first;
551 NS_LOG_DEBUG(
"previous= " << previous <<
", current=" << current);
556 event->GetTxVector().GetNss(staId));
558 if (previous >= windowStart)
561 Min(windowEnd, current) - previous,
562 event->GetTxVector(),
564 NS_LOG_DEBUG(
"Both previous and current point to the windowed payload: mode="
565 << payloadMode <<
", psr=" << psr);
568 else if (current >= windowStart)
571 Min(windowEnd, current) - windowStart,
572 event->GetTxVector(),
575 "previous is before windowed payload and current is in the windowed payload: mode="
576 << payloadMode <<
", psr=" << psr);
578 noiseInterferenceW = j->second.GetPower() - powerW;
580 if (previous > windowEnd)
583 <<
" after time window end=" << windowEnd);
587 double per = 1 - psr;
595 uint16_t channelWidth,
601 auto niIt = nis->find(band)->second;
602 auto j = niIt.begin();
606 for (
const auto& section : phyHeaderSections)
608 stopLastSection =
Max(stopLastSection, section.second.first.second);
611 Time previous = j->first;
614 double powerW =
event->GetRxPowerW(band);
615 while (++j != niIt.end())
617 Time current = j->first;
618 NS_LOG_DEBUG(
"previous= " << previous <<
", current=" << current);
620 double snr =
CalculateSnr(powerW, noiseInterferenceW, channelWidth, 1);
621 for (
const auto& section : phyHeaderSections)
623 Time start = section.second.first.first;
624 Time stop = section.second.first.second;
626 if (previous <= stop || current >= start)
628 Time duration =
Min(stop, current) -
Max(start, previous);
633 section.second.second,
634 event->GetTxVector(),
637 << section.first <<
" [" << start <<
", " << stop <<
"] for "
638 << duration.
As(
Time::NS) <<
": mode=" << section.second.second
643 noiseInterferenceW = j->second.GetPower() - powerW;
645 if (previous > stopLastSection)
647 NS_LOG_DEBUG(
"Stop: new previous=" << previous <<
" after stop of last section="
658 uint16_t channelWidth,
663 auto niIt = nis->find(band)->second;
667 for (
const auto& section :
668 phyEntity->GetPhyHeaderSections(event->GetTxVector(), niIt.begin()->first))
670 if (section.first == header)
672 sections[header] = section.second;
677 if (!sections.empty())
686 uint16_t channelWidth,
689 std::pair<Time, Time> relativeMpduStartStop)
const
691 NS_LOG_FUNCTION(
this << channelWidth << band << staId << relativeMpduStartStop.first
692 << relativeMpduStartStop.second);
698 event->GetTxVector().GetNss(staId));
703 double per =
CalculatePayloadPer(event, channelWidth, &ni, band, staId, relativeMpduStartStop);
710 uint16_t channelWidth,
716 double snr =
CalculateSnr(event->GetRxPowerW(band), noiseInterferenceW, channelWidth, nss);
722 uint16_t channelWidth,
729 double snr =
CalculateSnr(event->GetRxPowerW(band), noiseInterferenceW, channelWidth, 1);
739InterferenceHelper::NiChanges::iterator
742 return niIt->second.upper_bound(moment);
745InterferenceHelper::NiChanges::iterator
755InterferenceHelper::NiChanges::iterator
758 return niIt->second.insert(
GetNextPosition(moment, niIt), std::make_pair(moment, change));
783 m_firstPowers.find(niIt->first)->second = it->second.GetPower();
handles interference calculations
WifiTxVector m_txVector
TXVECTOR.
Time m_startTime
start time
Ptr< const WifiPpdu > GetPpdu() const
Return the PPDU.
Ptr< const WifiPpdu > m_ppdu
PPDU.
void UpdateRxPowerW(const RxPowerWattPerChannelBand &rxPower)
Update the received power (W) for all bands, i.e.
Event(Ptr< const WifiPpdu > ppdu, const WifiTxVector &txVector, Time duration, RxPowerWattPerChannelBand &&rxPower)
Create an Event with the given parameters.
Time GetEndTime() const
Return the end time of the signal.
Time GetDuration() const
Return the duration of the signal.
double GetRxPowerW() const
Return the total received power (W).
const WifiTxVector & GetTxVector() const
Return the TXVECTOR of the PPDU.
RxPowerWattPerChannelBand m_rxPowerW
received power in watts per band
const RxPowerWattPerChannelBand & GetRxPowerWPerBand() const
Return the received power (W) for all bands.
Time GetStartTime() const
Return the start time of the signal.
Noise and Interference (thus Ni) event.
void AddPower(double power)
Add a given amount of power.
NiChange(double power, Ptr< Event > event)
Create a NiChange at the given time and the amount of NI change.
double GetPower() const
Return the power.
Ptr< Event > GetEvent() const
Return the event causes the corresponding NI change.
handles interference calculations
void SetNoiseFigure(double value)
Set the noise figure.
double m_noiseFigure
noise figure (linear)
Ptr< ErrorRateModel > GetErrorRateModel() const
Return the error rate model.
void AddForeignSignal(Time duration, RxPowerWattPerChannelBand &rxPower)
Add a non-Wifi signal to interference helper.
bool m_rxing
flag whether it is in receiving state
NiChanges::iterator AddNiChangeEvent(Time moment, NiChange change, NiChangesPerBand::iterator niIt)
Add NiChange to the list at the appropriate position and return the iterator of the new event.
std::map< WifiSpectrumBandInfo, NiChanges > NiChangesPerBand
Map of NiChanges per band.
uint8_t m_numRxAntennas
the number of RX antennas in the corresponding receiver
bool IsBandInFrequencyRange(const WifiSpectrumBandInfo &band, const FrequencyRange &freqRange) const
Check whether a given band belongs to a given frequency range.
void DoDispose() override
Destructor implementation.
std::multimap< Time, NiChange > NiChanges
typedef for a multimap of NiChange
NiChangesPerBand m_niChanges
NI Changes for each band.
void UpdateBands(const std::vector< WifiSpectrumBandInfo > &bands, const FrequencyRange &freqRange)
Update the frequency bands that belongs to a given frequency range when the spectrum model is changed...
void SetErrorRateModel(const Ptr< ErrorRateModel > rate)
Set the error rate model for this interference helper.
bool HasBands() const
Check whether bands are already tracked by this interference helper.
double CalculatePayloadChunkSuccessRate(double snir, Time duration, const WifiTxVector &txVector, uint16_t staId=SU_STA_ID) const
Calculate the success rate of the payload chunk given the SINR, duration, and TXVECTOR.
Ptr< ErrorRateModel > m_errorRateModel
error rate model
void AppendEvent(Ptr< Event > event, bool isStartOfdmaRxing)
Append the given Event.
void NotifyRxStart()
Notify that RX has started.
NiChanges::iterator GetNextPosition(Time moment, NiChangesPerBand::iterator niIt)
Returns an iterator to the first NiChange that is later than moment.
FirstPowerPerBand m_firstPowers
first power of each band in watts
Time GetEnergyDuration(double energyW, const WifiSpectrumBandInfo &band)
double CalculateNoiseInterferenceW(Ptr< Event > event, NiChangesPerBand *nis, const WifiSpectrumBandInfo &band) const
Calculate noise and interference power in W.
double CalculateChunkSuccessRate(double snir, Time duration, WifiMode mode, const WifiTxVector &txVector, WifiPpduField field) const
Calculate the success rate of the chunk given the SINR, duration, and TXVECTOR.
double CalculatePayloadPer(Ptr< const Event > event, uint16_t channelWidth, NiChangesPerBand *nis, const WifiSpectrumBandInfo &band, uint16_t staId, std::pair< Time, Time > window) const
Calculate the error rate of the given PHY payload only in the provided time window (thus enabling per...
bool HasBand(const WifiSpectrumBandInfo &band) const
Check whether a given band is tracked by this interference helper.
void AddBand(const WifiSpectrumBandInfo &band)
Add a frequency band.
double CalculatePhyHeaderPer(Ptr< const Event > event, NiChangesPerBand *nis, uint16_t channelWidth, const WifiSpectrumBandInfo &band, WifiPpduField header) const
Calculate the error rate of the PHY header.
NiChanges::iterator GetPreviousPosition(Time moment, NiChangesPerBand::iterator niIt)
Returns an iterator to the last NiChange that is before than moment.
double CalculateSnr(Ptr< Event > event, uint16_t channelWidth, uint8_t nss, const WifiSpectrumBandInfo &band) const
Calculate the SNIR for the event (starting from now until the event end).
PhyEntity::SnrPer CalculatePhyHeaderSnrPer(Ptr< Event > event, uint16_t channelWidth, const WifiSpectrumBandInfo &band, WifiPpduField header) const
Calculate the SNIR at the start of the PHY header and accumulate all SNIR changes in the SNIR vector.
Ptr< Event > Add(Ptr< const WifiPpdu > ppdu, const WifiTxVector &txVector, Time duration, RxPowerWattPerChannelBand &rxPower, bool isStartOfdmaRxing=false)
Add the PPDU-related signal to interference helper.
static TypeId GetTypeId()
Get the type ID.
void UpdateEvent(Ptr< Event > event, const RxPowerWattPerChannelBand &rxPower)
Update event to scale its received power (W) per band.
void NotifyRxEnd(Time endTime, const FrequencyRange &freqRange)
Notify that RX has ended.
double CalculatePhyHeaderSectionPsr(Ptr< const Event > event, NiChangesPerBand *nis, uint16_t channelWidth, const WifiSpectrumBandInfo &band, PhyEntity::PhyHeaderSections phyHeaderSections) const
Calculate the success rate of the PHY header sections for the provided event.
void SetNumberOfReceiveAntennas(uint8_t rx)
Set the number of RX antennas in the receiver corresponding to this interference helper.
PhyEntity::SnrPer CalculatePayloadSnrPer(Ptr< Event > event, uint16_t channelWidth, const WifiSpectrumBandInfo &band, uint16_t staId, std::pair< Time, Time > relativeMpduStartStop) const
Calculate the SNIR at the start of the payload and accumulate all SNIR changes in the SNIR vector for...
~InterferenceHelper() override
A base class which provides memory management and object aggregation.
std::map< WifiPpduField, PhyHeaderChunkInfo > PhyHeaderSections
A map of PhyHeaderChunkInfo elements per PPDU field.
Smart pointer class similar to boost::intrusive_ptr.
Control the scheduling of simulation events.
static Time Now()
Return the current simulation virtual time.
Simulation virtual time values and global simulation resolution.
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
bool IsStrictlyPositive() const
Exactly equivalent to t > 0.
bool IsZero() const
Exactly equivalent to t == 0.
a unique identifier for an interface.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
represent a single transmission mode
uint64_t GetDataRate(uint16_t channelWidth, uint16_t guardInterval, uint8_t nss) const
static const Ptr< const PhyEntity > GetStaticPhyEntity(WifiModulationClass modulation)
Get the implemented PHY entity corresponding to the modulation class.
static Time CalculatePhyPreambleAndHeaderDuration(const WifiTxVector &txVector)
Class that keeps track of all information about the current PHY operating channel.
This class mimics the TXVECTOR which is to be passed to the PHY in order to define the parameters whi...
WifiMode GetMode(uint16_t staId=SU_STA_ID) const
If this TX vector is associated with an SU PPDU, return the selected payload transmission mode.
uint8_t GetNss(uint16_t staId=SU_STA_ID) const
If this TX vector is associated with an SU PPDU, return the number of spatial streams.
uint16_t GetChannelWidth() const
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
#define NS_ABORT_IF(cond)
Abnormal program termination if a condition is true.
#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_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Time Now()
create an ns3::Time instance which contains the current simulation time.
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Time Seconds(double value)
Construct a Time in the indicated unit.
WifiPpduField
The type of PPDU field (grouped for convenience)
@ WIFI_PPDU_FIELD_DATA
data field
Every class exported by the ns3 library is enclosed in the ns3 namespace.
double RatioToDb(double ratio)
Convert from ratio to dB.
std::ostream & operator<<(std::ostream &os, const Angles &a)
std::map< WifiSpectrumBandInfo, double > RxPowerWattPerChannelBand
A map of the received power (Watts) for each band.
Struct defining a frequency range between minFrequency (MHz) and maxFrequency (MHz).
uint16_t maxFrequency
the maximum frequency in MHz
uint16_t minFrequency
the minimum frequency in MHz
A struct for both SNR and PER.
WifiSpectrumBandInfo structure containing info about a spectrum band.
WifiSpectrumBandFrequencies frequencies
the start and stop frequencies of the band