diff --git a/src/wifi/model/interference-helper.cc b/src/wifi/model/interference-helper.cc --- a/src/wifi/model/interference-helper.cc +++ b/src/wifi/model/interference-helper.cc @@ -88,9 +88,9 @@ * short period of time. ****************************************************************/ -InterferenceHelper::NiChange::NiChange (Time time, double delta, Ptr event) +InterferenceHelper::NiChange::NiChange (Time time, double power, Ptr event) : m_time (time), - m_delta (delta), + m_power (power), m_event (event) { } @@ -102,9 +102,15 @@ } double -InterferenceHelper::NiChange::GetDelta (void) const +InterferenceHelper::NiChange::GetPower (void) const { - return m_delta; + return m_power; +} + +void +InterferenceHelper::NiChange::AddPower (double power) +{ + m_power += power; } Ptr @@ -191,19 +197,17 @@ InterferenceHelper::GetEnergyDuration (double energyW) const { Time now = Simulator::Now (); - double noiseInterferenceW = 0.0; - Time end = now; - noiseInterferenceW = m_firstPower; - for (NiChanges::const_iterator i = m_niChanges.begin (); i != m_niChanges.end (); i++) + + auto i = GetPreviousPosition (now); + double noiseInterferenceW = i->GetPower (); + Time end = i->GetTime (); + + for ( ; i != m_niChanges.end (); i++) { - noiseInterferenceW += i->GetDelta (); - end = i->GetTime (); - if (end < now) + if (i->GetPower () < energyW) { - continue; - } - if (noiseInterferenceW < energyW) - { + noiseInterferenceW = i->GetPower (); + end = i->GetTime (); break; } } @@ -213,23 +217,26 @@ void InterferenceHelper::AppendEvent (Ptr event) { - Time now = Simulator::Now (); + double previousPowerStart = 0; + auto itPreviousEvent = GetPreviousPosition (event->GetStartTime ()); + double previousPowerEnd = 0; + auto itNextEvent = GetPreviousPosition (event->GetEndTime ()); + if ( m_niChanges.size() > 0 ) { + previousPowerStart = itPreviousEvent->GetPower (); + previousPowerEnd = itNextEvent->GetPower (); + } + if (!m_rxing) { - NiChanges::const_iterator nowIterator = GetPosition (now); - for (NiChanges::const_iterator i = m_niChanges.begin (); i != nowIterator; i++) - { - m_firstPower += i->GetDelta (); - } - m_niChanges.erase (m_niChanges.begin (), nowIterator); - m_niChanges.insert (m_niChanges.begin (), NiChange (event->GetStartTime (), event->GetRxPowerW (), event)); + m_firstPower = previousPowerStart; + m_niChanges.erase (m_niChanges.begin (), GetNextPosition (event->GetStartTime ())); } - else + auto first = AddNiChangeEvent (NiChange (event->GetStartTime (), previousPowerStart, event)); + auto last = AddNiChangeEvent (NiChange (event->GetEndTime (), previousPowerEnd, event)); + for (auto i = first; i < last; i++) { - AddNiChangeEvent (NiChange (event->GetStartTime (), event->GetRxPowerW (), event)); + m_niChanges.at (i).AddPower (event->GetRxPowerW ()); } - AddNiChangeEvent (NiChange (event->GetEndTime (), -event->GetRxPowerW (), event)); - } @@ -252,40 +259,29 @@ InterferenceHelper::CalculateNoiseInterferenceW (Ptr event, NiChanges *ni) const { double noiseInterference = m_firstPower; - NiChanges::const_iterator eventIterator = m_niChanges.begin (); - while (eventIterator != m_niChanges.end ()) + + // Find this event in the NiChanges list. + // Should be multimap::find() + NiChanges::const_iterator it = GetPreviousPosition (event->GetStartTime ()); + while (it != m_niChanges.end () && it->GetEvent () != event) { - // Iterate the NI change list from the beginning to the end - // until find the position of the event in the NI change list - // The reason of using the event that causes the NI change to identify - // different NI changes is because in some special cases - // different NI changes happen at the same time with the same delta - // value. Therefore, it may be impossible to identify a NI change that belongs - // to which event based on just the NI time and NI delta value - if (eventIterator->GetEvent () != event) - { - // The NI changes which happen before the event should be considered - // as the interference. This considers the case that the receiving event - // arrives while another receiving event is going on. The SINR of - // the newly arrived event is calculated for checking the possibility of frame capture - noiseInterference += eventIterator->GetDelta (); - } - else - { - break; - } - ++eventIterator; + ++it; } - for (NiChanges::const_iterator i = eventIterator + 1; i != m_niChanges.end (); ++i) + // The existing NI power at the start of this event is in the prior + // NiChange event. + // This considers the case that the receiving event + // arrives while another receiving event is going on. The SINR of + // the newly arrived event is calculated for checking the possibility of frame capture + --it; + noiseInterference = it>GetPower (); + ni->push_back (NiChange (event->GetStartTime (), 0, event)); + + ++it; + for ( ; it->GetEvent () == event;; ++it) { - if (event->GetEndTime () == i->GetTime () && event == i->GetEvent ()) - { - break; - } - ni->push_back (*i); + ni->push_back (*it); } - ni->insert (ni->begin (), NiChange (event->GetStartTime (), noiseInterference, event)); ni->push_back (NiChange (event->GetEndTime (), 0, event)); return noiseInterference; } @@ -317,19 +313,19 @@ const WifiTxVector txVector = event->GetTxVector (); double psr = 1.0; /* Packet Success Rate */ NiChanges::const_iterator j = ni->begin (); - Time previous = (*j).GetTime (); + Time previous = j->GetTime (); WifiMode payloadMode = event->GetPayloadMode (); WifiPreamble preamble = txVector.GetPreambleType (); - Time plcpHeaderStart = (*j).GetTime () + WifiPhy::GetPlcpPreambleDuration (txVector); //packet start time + preamble + Time plcpHeaderStart = j->GetTime () + WifiPhy::GetPlcpPreambleDuration (txVector); //packet start time + preamble Time plcpHsigHeaderStart = plcpHeaderStart + WifiPhy::GetPlcpHeaderDuration (txVector); //packet start time + preamble + L-SIG Time plcpTrainingSymbolsStart = plcpHsigHeaderStart + WifiPhy::GetPlcpHtSigHeaderDuration (preamble) + WifiPhy::GetPlcpSigA1Duration (preamble) + WifiPhy::GetPlcpSigA2Duration (preamble); //packet start time + preamble + L-SIG + HT-SIG or SIG-A Time plcpPayloadStart = plcpTrainingSymbolsStart + WifiPhy::GetPlcpTrainingSymbolDuration (txVector) + WifiPhy::GetPlcpSigBDuration (preamble); //packet start time + preamble + L-SIG + HT-SIG or SIG-A + Training + SIG-B - double noiseInterferenceW = (*j).GetDelta (); + double noiseInterferenceW = m_firstPower; double powerW = event->GetRxPowerW (); j++; while (ni->end () != j) { - Time current = (*j).GetTime (); + Time current = j->GetTime (); NS_LOG_DEBUG ("previous= " << previous << ", current=" << current); NS_ASSERT (current >= previous); //Case 1: Both previous and current point to the payload @@ -354,8 +350,8 @@ NS_LOG_DEBUG ("previous is before payload and current is in the payload: mode=" << payloadMode << ", psr=" << psr); } - noiseInterferenceW += (*j).GetDelta (); - previous = (*j).GetTime (); + noiseInterferenceW = j->GetPower () - powerW; + previous = j->GetTime (); j++; } @@ -370,7 +366,7 @@ const WifiTxVector txVector = event->GetTxVector (); double psr = 1.0; /* Packet Success Rate */ NiChanges::const_iterator j = ni->begin (); - Time previous = (*j).GetTime (); + Time previous = j->GetTime (); WifiPreamble preamble = txVector.GetPreambleType (); WifiMode mcsHeaderMode; if (preamble == WIFI_PREAMBLE_HT_MF || preamble == WIFI_PREAMBLE_HT_GF) @@ -389,16 +385,16 @@ mcsHeaderMode = WifiPhy::GetHePlcpHeaderMode (); } WifiMode headerMode = WifiPhy::GetPlcpHeaderMode (txVector); - Time plcpHeaderStart = (*j).GetTime () + WifiPhy::GetPlcpPreambleDuration (txVector); //packet start time + preamble + Time plcpHeaderStart = j->GetTime () + WifiPhy::GetPlcpPreambleDuration (txVector); //packet start time + preamble Time plcpHsigHeaderStart = plcpHeaderStart + WifiPhy::GetPlcpHeaderDuration (txVector); //packet start time + preamble + L-SIG Time plcpTrainingSymbolsStart = plcpHsigHeaderStart + WifiPhy::GetPlcpHtSigHeaderDuration (preamble) + WifiPhy::GetPlcpSigA1Duration (preamble) + WifiPhy::GetPlcpSigA2Duration (preamble); //packet start time + preamble + L-SIG + HT-SIG or SIG-A Time plcpPayloadStart = plcpTrainingSymbolsStart + WifiPhy::GetPlcpTrainingSymbolDuration (txVector) + WifiPhy::GetPlcpSigBDuration (preamble); //packet start time + preamble + L-SIG + HT-SIG or SIG-A + Training + SIG-B - double noiseInterferenceW = (*j).GetDelta (); + double noiseInterferenceW = m_firstPower; double powerW = event->GetRxPowerW (); j++; while (ni->end () != j) { - Time current = (*j).GetTime (); + Time current = j->GetTime (); NS_LOG_DEBUG ("previous= " << previous << ", current=" << current); NS_ASSERT (current >= previous); //Case 1: previous and current after playload start: nothing to do @@ -812,8 +808,8 @@ } } - noiseInterferenceW += (*j).GetDelta (); - previous = (*j).GetTime (); + noiseInterferenceW = j->GetPower () - powerW; + previous = j->GetTime (); j++; } @@ -869,16 +865,25 @@ m_firstPower = 0.0; } -InterferenceHelper::NiChanges::const_iterator -InterferenceHelper::GetPosition (Time moment) +InterferenceHelper::NiChanges::iterator +InterferenceHelper::GetNextPosition (Time moment) { return std::upper_bound (m_niChanges.begin (), m_niChanges.end (), NiChange (moment, 0, NULL)); } -void +InterferenceHelper::NiChanges::iterator +InterferenceHelper::GetPreviousPosition (Time moment) +{ + InterferenceHelper::NiChanges::iterator it = GetNextPosition (moment); + --it; + return it; +} + +int64_t InterferenceHelper::AddNiChangeEvent (NiChange change) { - m_niChanges.insert (GetPosition (change.GetTime ()), change); + auto it = m_niChanges.insert (GetNextPosition (change.GetTime ()), change); + return std::distance (m_niChanges.begin (), it); } void @@ -893,6 +898,9 @@ { NS_LOG_FUNCTION (this); m_rxing = false; + //Update m_firstPower for frame capture + auto it = GetPreviousPosition (Simulator::Now ()); + m_firstPower = it->GetPower (); } } //namespace ns3 diff --git a/src/wifi/model/interference-helper.h b/src/wifi/model/interference-helper.h --- a/src/wifi/model/interference-helper.h +++ b/src/wifi/model/interference-helper.h @@ -213,10 +213,10 @@ * Create a NiChange at the given time and the amount of NI change. * * \param time time of the event - * \param delta the power + * \param power the power * \param event causes this NI change */ - NiChange (Time time, double delta, Ptr event); + NiChange (Time time, double power, Ptr event); /** * Return the event time. * @@ -228,7 +228,13 @@ * * \return the power */ - double GetDelta (void) const; + double GetPower (void) const; + /** + * Add a given amount of power. + * + * \param power the power to be added to the existing value + */ + void AddPower (double power); /** * Return the event causes the corresponding NI change * @@ -246,7 +252,7 @@ private: Time m_time; ///< time - double m_delta; ///< delta + double m_power; ///< power Ptr m_event; ///< event }; /** @@ -322,18 +328,28 @@ bool m_rxing; ///< flag whether it is in receiving state /** - * Returns a const iterator to the first nichange, which is later than moment + * Returns an iterator to the first nichange that is later than moment * * \param moment time to check from * \returns an iterator to the list of NiChanges */ - NiChanges::const_iterator GetPosition (Time moment); + NiChanges::iterator GetNextPosition (Time moment); /** - * Add NiChange to the list at the appropriate position. + * Returns an iterator to the last nichange that is before than moment + * + * \param moment time to check from + * \returns an iterator to the list of NiChanges + */ + NiChanges::iterator GetPreviousPosition (Time moment); + + /** + * Add NiChange to the list at the appropriate position and + * return the iterator of the new event. * * \param change + * \returns the iterator of the new event */ - void AddNiChangeEvent (NiChange change); + int64_t AddNiChangeEvent (NiChange change); }; } //namespace ns3