Ns3 Projects for B.E/B.Tech M.E/M.Tech PhD Scholars.  Phone-Number:9790238391   E-mail: ns3simulation@gmail.com

How to Implement Dynamic Spectrum Access in ns3

To implement Dynamic Spectrum Access (DSA) in ns3, we need to set-up a cognitive radio network (CRN). In this cognitive radio network, based on the availability of channels the secondary users (SUs) will access the spectrum dynamically. For this we need to simulate the behavior of primary users (PUs) and secondary users (SUs), as well as the mechanisms for spectrum sensing and access. The following steps will guide on how to implement Dynamic spectrum access in ns3.

Step-by-step guide to set up Dynamic Spectrum Access in ns3:

Step 1: Install ns3

Make sure that ns3 is installed on the system.

Step 2: Set Up the Simulation Environment

Create a new simulation script or modify an existing one. This script will define the network topology, nodes, mobility models, and communication protocols.

Step 3: Define Network Topology

Create nodes and define the network topology, including primary users (PUs) and secondary users (SUs).

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/internet-module.h”

#include “ns3/wifi-module.h”

#include “ns3/mobility-module.h”

#include “ns3/applications-module.h”

#include “ns3/flow-monitor-module.h”

using namespace ns3;

NS_LOG_COMPONENT_DEFINE (“DynamicSpectrumAccessExample”);

void SpectrumSensing (Ptr<NetDevice> device, Ptr<WifiPhy> wifiPhy)

{

double noiseDbm = -100; // Noise level in dBm

double thresholdDbm = -80; // Sensing threshold in dBm

SpectrumValue noisePower = SpectrumValue (wifiPhy->GetRxSpectrumModel ());

for (uint32_t i = 0; i < noisePower.GetSpectrumModel ()->GetNumBands (); ++i)

{

noisePower[i] = noiseDbm;

}

Ptr<SpectrumChannel> channel = wifiPhy->GetSpectrumChannel ();

SpectrumValue receivedPower = channel->GetTxSpectrumValue ();

double totalPowerDbm = 10 * std::log10 (receivedPower.CalculatePower () + noisePower.CalculatePower ());

if (totalPowerDbm < thresholdDbm)

{

NS_LOG_UNCOND (“Spectrum is available for secondary user.”);

}

else

{

NS_LOG_UNCOND (“Spectrum is occupied by primary user.”);

}

Simulator::Schedule (Seconds (1.0), &SpectrumSensing, device, wifiPhy);

}

int main (int argc, char *argv[])

{

// Enable logging

LogComponentEnable (“DynamicSpectrumAccessExample”, LOG_LEVEL_INFO);

// Create nodes

NodeContainer primaryNodes;

primaryNodes.Create (1); // Primary user

NodeContainer secondaryNodes;

secondaryNodes.Create (2); // Secondary users

// Set up Wi-Fi for secondary users

WifiHelper wifi;

wifi.SetStandard (WIFI_PHY_STANDARD_80211a);

YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();

YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();

wifiPhy.SetChannel (wifiChannel.Create ());

WifiMacHelper wifiMac;

wifiMac.SetType (“ns3::AdhocWifiMac”);

NetDeviceContainer secondaryDevices;

secondaryDevices = wifi.Install (wifiPhy, wifiMac, secondaryNodes);

// Install Internet stack for secondary users

InternetStackHelper internet;

internet.Install (secondaryNodes);

// Assign IP addresses to secondary users

Ipv4AddressHelper address;

address.SetBase (“10.1.1.0”, “255.255.255.0”);

Ipv4InterfaceContainer secondaryInterfaces = address.Assign (secondaryDevices);

// Set up mobility model for secondary users

MobilityHelper mobility;

mobility.SetPositionAllocator (“ns3::GridPositionAllocator”,

“MinX”, DoubleValue (0.0),

“MinY”, DoubleValue (0.0),

“DeltaX”, DoubleValue (10.0),

“DeltaY”, DoubleValue (10.0),

“GridWidth”, UintegerValue (3),

“LayoutType”, StringValue (“RowFirst”));

mobility.SetMobilityModel (“ns3::RandomWalk2dMobilityModel”,

“Bounds”, RectangleValue (Rectangle (0, 50, 0, 50)));

mobility.Install (secondaryNodes);

// Set up Wi-Fi for primary users

WifiHelper wifiPrimary;

wifiPrimary.SetStandard (WIFI_PHY_STANDARD_80211b);

YansWifiPhyHelper wifiPhyPrimary = YansWifiPhyHelper::Default ();

YansWifiChannelHelper wifiChannelPrimary = YansWifiChannelHelper::Default ();

wifiPhyPrimary.SetChannel (wifiChannelPrimary.Create ());

WifiMacHelper wifiMacPrimary;

wifiMacPrimary.SetType (“ns3::AdhocWifiMac”);

NetDeviceContainer primaryDevices;

primaryDevices = wifiPrimary.Install (wifiPhyPrimary, wifiMacPrimary, primaryNodes);

// Install Internet stack for primary users

internet.Install (primaryNodes);

// Assign IP addresses to primary users

address.SetBase (“10.1.2.0”, “255.255.255.0”);

Ipv4InterfaceContainer primaryInterfaces = address.Assign (primaryDevices);

// Set up mobility model for primary users

mobility.Install (primaryNodes);

// Install and start applications on secondary nodes

uint16_t port = 8080;

OnOffHelper onOff (“ns3::UdpSocketFactory”, InetSocketAddress (secondaryInterfaces.GetAddress (1), port));

onOff.SetAttribute (“DataRate”, DataRateValue (DataRate (“1Mbps”)));

onOff.SetAttribute (“PacketSize”, UintegerValue (1024));

onOff.SetAttribute(“OnTime”,StringValue (“ns3::ConstantRandomVariable[Constant=1]”));

onOff.SetAttribute(“OffTime”,StringValue (“ns3::ConstantRandomVariable[Constant=0]”));

ApplicationContainer app = onOff.Install (secondaryNodes.Get (0));

app.Start (Seconds (1.0));

app.Stop (Seconds (10.0));

PacketSinkHelper sink (“ns3::UdpSocketFactory”, InetSocketAddress (Ipv4Address::GetAny (), port));

app = sink.Install (secondaryNodes.Get (1));

app.Start (Seconds (1.0));

app.Stop (Seconds (10.0));

// Spectrum sensing for secondary users

Simulator::Schedule (Seconds (1.0), &SpectrumSensing, secondaryDevices.Get (0), wifiPhy.Get (0));

// Set up FlowMonitor

FlowMonitorHelper flowmon;

Ptr<FlowMonitor> monitor = flowmon.InstallAll ();

// Run the simulation

Simulator::Stop (Seconds (10.0));

Simulator::Run ();

// Print statistics

monitor->CheckForLostPackets ();

Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmon.GetClassifier ());

std::map<FlowId, FlowMonitor::FlowStats> stats = monitor->GetFlowStats ();

for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i = stats.begin (); i != stats.end (); ++i)

{

Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i->first);

NS_LOG_UNCOND (“Flow ” << i->first << ” (” << t.sourceAddress << ” -> ” << t.destinationAddress << “)”);

NS_LOG_UNCOND (”  Tx Packets: ” << i->second.txPackets);

NS_LOG_UNCOND (”  Tx Bytes:   ” << i->second.txBytes);

NS_LOG_UNCOND (”  Rx Packets: ” << i->second.rxPackets);

NS_LOG_UNCOND (”  Rx Bytes:   ” << i->second.rxBytes);

NS_LOG_UNCOND (”  Throughput: ” << i->second.rxBytes * 8.0 / (i->second.timeLastRxPacket.GetSeconds () – i->second.timeFirstTxPacket.GetSeconds ()) / 1024 / 1024  << ” Mbps”);

}

// Clean up

Simulator::Destroy ();

return 0;

}

Step 4: Implement Spectrum Sensing

The example includes a simple spectrum sensing function that checks if the spectrum is available for secondary users based on a threshold. Replace this logic with your actual spectrum sensing algorithm.

void SpectrumSensing (Ptr<NetDevice> device, Ptr<WifiPhy> wifiPhy)

{

double noiseDbm = -100; // Noise level in dBm

double thresholdDbm = -80; // Sensing threshold in dBm

SpectrumValue noisePower = SpectrumValue (wifiPhy->GetRxSpectrumModel ());

for (uint32_t i = 0; i < noisePower.GetSpectrumModel ()->GetNumBands (); ++i)

{

noisePower[i] = noiseDbm;

}

Ptr<SpectrumChannel> channel = wifiPhy->GetSpectrumChannel ();

SpectrumValue receivedPower = channel->GetTxSpectrumValue ();

double totalPowerDbm = 10 * std::log10 (receivedPower.CalculatePower () + noisePower.CalculatePower ());

if (totalPowerDbm < thresholdDbm

{

NS_LOG_UNCOND (“Spectrum is available for secondary user.”);

}

else

{

NS_LOG_UNCOND (“Spectrum is occupied by primary user.”);

}

Simulator::Schedule (Seconds (1.0), &SpectrumSensing, device, wifiPhy);

}

Step 5: Set Up Mobility Models

In the example, the RandomWalk2dMobilityModel is used for secondary users, and the ConstantPositionMobilityModel is used for primary users. Adjust the mobility models and parameters as needed.

Step 6: Install and Start Applications

The example sets up an OnOff application to simulate traffic from a secondary user to another secondary user. The server receives packets sent by the client.

Step 7: Run the Simulation

Compile and run your simulation script to see the effect of dynamic spectrum access on network performance. The output will include statistics such as the number of packets transmitted and received, throughput, and any packet loss.

Finally, we had successfully implemented the Dynamic spectrum access in ns3 by using the cognitive radio network which access the secondary users under analyzing the channel availability and simulating it in ns3 environment.

Make sure to approach us  for top-notch outcomes and concise insights on various project concepts and performance evaluation linked to Dynamic Spectrum Access in the ns3 tool.