To simulate Cognitive Radio Networks (CRNs) using NS2 (Network Simulator 2) that needs prolonging NS2’s capabilities to model dynamic spectrum access in which cognitive radios sense obtainable spectrum and adjust their transmission according to the availability. Because NS2 doesn’t natively support Cognitive Radio (CR) functionalities, we can be mimicked CRNs in NS2 by either:
- Using an NS2 Cognitive Radio Patch: Multiple community-contributed patches and extensions are obtainable, which insert Cognitive Radio functionality to NS2.
- Modifying the Channel and Traffic Models: We can replicate spectrum sensing and channel selection by changing the default NS2 wireless channel model and traffic behaviours.
Key Components to Simulate in a Cognitive Radio Network (CRN)
- Primary Users (PUs): Licensed users who have priority access to particular spectrum bands.
- Secondary Users (SUs): Cognitive radio devices, which opportunistically access the spectrum once it is not occupied by primary users.
- Spectrum Sensing: SUs sense the spectrum for unused channels and prevent interference with PUs.
- Spectrum Mobility: If a primary user becomes active then SUs must empty the channel and move to another available spectrum band.
- Dynamic Spectrum Access (DSA): It is the core functionality of CRNs in which SUs adaptively choose channels.
The following is a step-by-step instruction on how to simulate Cognitive Radio Network (CRN) projects using NS2.
Steps for Simulating Cognitive Radio Networks (CRNs) in NS2
- Install NS2: If we haven’t already installed NS2 then we can install it using the below command (on Linux systems):
sudo apt-get install ns2
Verify the installation by typing:
ns -version
- Extend NS2 for Cognitive Radio Functionality: Since NS2 doesn’t natively support CRNs, we can expand it by:
- Utilizing patches, which insert CR capabilities (if available).
- Executing custom channel selection, spectrum sensing, and mobility models to signify primary and secondary users and their behaviour.
- Changing the wireless channel to enable for dynamic channel switching.
- Create a Cognitive Radio Network Scenario Using a Tcl Script: Below is an instance Tcl script to replicate a simple Cognitive Radio Network in which secondary users (SUs) sense the availability of channels and switch channels if a primary user (PU) becomes active.
Example Tcl Script for Simulating a Cognitive Radio Network (CRN)
# Create a new simulator object
set ns [new Simulator]
# Open trace and NAM files for output
set tracefile [open crn_out.tr w]
$ns trace-all $tracefile
set namfile [open crn_out.nam w]
$ns namtrace-all-wireless $namfile 500 500
# Define the wireless channel for CRNs
set val(chan) Channel/WirelessChannel ;# Wireless channel model
set val(prop) Propagation/TwoRayGround ;# Propagation model (you can modify this)
set val(netif) Phy/WirelessPhy ;# Wireless PHY type
set val(mac) Mac/802_11 ;# MAC type (approximating CR MAC behavior)
set val(ifq) Queue/DropTail/PriQueue ;# Interface Queue
set val(ll) LL ;# Link Layer
set val(ant) Antenna/OmniAntenna ;# Antenna model
set val(ifqlen) 50 ;# Interface Queue length
set val(nn) 5 ;# Number of nodes (SUs and PUs)
set val(x) 500 ;# X dimension of simulation area
set val(y) 500 ;# Y dimension of simulation area
# Define the topology object for cognitive radio network
set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)
# Configure simulation for cognitive radio nodes
$ns node-config -llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-channelType $val(chan) \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace ON
# Create primary users (PUs) and secondary users (SUs)
set pu_0 [$ns node] ;# Primary User (PU)
set su_0 [$ns node] ;# Secondary User (SU)
set su_1 [$ns node] ;# Another SU
# Set initial positions for PUs and SUs
$pu_0 set X_ 100.0
$pu_0 set Y_ 100.0
$su_0 set X_ 200.0
$su_0 set Y_ 200.0
$su_1 set X_ 300.0
$su_1 set Y_ 300.0
# Define channel usage: PUs occupy a certain channel; SUs sense for available channels
set channel_0 1 ;# Channel used by PU
set channel_1 2 ;# Alternative channel for SU if PU occupies channel 0
# Spectrum sensing and mobility function for SUs
proc spectrum_sensing {su_id primary_channel secondary_channel} {
global ns
# Check if the primary channel is occupied
if {[$ns now] < 5.0} { ;# Simulate PU being active in channel_0 after 5 seconds
$ns at 5.1 “$su_id switch-to $secondary_channel”
puts “Secondary User switched to secondary channel”
}
}
# Attach traffic model for SUs
set udp_su0 [new Agent/UDP]
$ns attach-agent $su_0 $udp_su0
set udp_su1 [new Agent/UDP]
$ns attach-agent $su_1 $udp_su1
# Define Null agent as a sink for the traffic
set null [new Agent/Null]
$ns attach-agent $pu_0 $null
# Connect SUs to the Null agent (representing data transfer)
$ns connect $udp_su0 $null
$ns connect $udp_su1 $null
# Define traffic pattern for SU using CBR
set cbr_su0 [new Application/Traffic/CBR]
$cbr_su0 set packetSize_ 512
$cbr_su0 set interval_ 0.05
$cbr_su0 attach-agent $udp_su0
set cbr_su1 [new Application/Traffic/CBR]
$cbr_su1 set packetSize_ 512
$cbr_su1 set interval_ 0.05
$cbr_su1 attach-agent $udp_su1
# Schedule the traffic start time for SUs
$ns at 1.0 “$cbr_su0 start”
$ns at 1.0 “$cbr_su1 start”
# Schedule spectrum sensing for SU to detect PU activity and vacate the channel
$ns at 4.0 “spectrum_sensing su_0 $channel_0 $channel_1”
$ns at 4.5 “spectrum_sensing su_1 $channel_0 $channel_1”
# Simulation ends at 10 seconds
$ns at 10.0 “finish”
# Define the finish procedure to close files and run NAM
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam crn_out.nam &
exit 0
}
# Run the simulation
$ns run
Explanation of the Tcl Script:
- Simulator Object:
- The set ns [new Simulator] command makes the simulation object for managing the NS2 simulation.
- Primary and Secondary Users:
- Two Secondary Users (SUs) (su_0 and su_1) are made together with one Primary User (PU) (pu_0).
- Channel assignment: PUs and SUs use distinct channels. If a PU becomes active on a channel then SUs switch to an available secondary channel.
- Spectrum Sensing and Dynamic Spectrum Access:
- The spectrum_sensing method replicates the process in which SUs identify PU activity and switch channels if a PU becomes active.
- In this instance, SUs are planned to sense the primary channel at 4.0 seconds, and they switch to a secondary channel at 5.1 seconds once the PU beings utilizing the primary channel.
- Traffic Flow (UDP over CBR):
- UDP agents are connected to the SUs, and traffic is generated using the CBR (Constant Bit Rate) model.
- The SUs start data transmission at 1.0 seconds and actively modify their channels according to the activity of the PU.
- Simulation End:
- The simulation stops at 10.0 seconds, and the finish procedure closes the trace files and opens NAM for visualization.
- Running the Simulation:
We can save the script as crn_simulation.tcl and run it using NS2:
ns crn_simulation.tcl
After the simulation finishes then we can open NAM to envision the network:
nam crn_out.nam
- Analysing the Results:
- The trace file (crn_out.tr) encompasses detailed records of packet transmissions, receptions, and channel switching events. We can investigate this file to extract key performance parameters like:
- Channel utilization by SUs
- Throughput before and after channel switching
- Packet loss due to PU interference
- End-to-end delay
- Example AWK script to analyse throughput:
BEGIN {
total_packets = 0;
start_time = 0;
end_time = 0;
}
{
if ($1 == “r” && $4 == “AGT”) {
total_packets++;
if (start_time == 0) {
start_time = $2;
}
end_time = $2;
}
}
END {
duration = end_time – start_time;
throughput = (total_packets * 512 * 8) / (duration * 1000); # kbps
printf(“Throughput: %.2f kbps\n”, throughput);
}
Extending the Simulation:
- Multiple Primary Users (PUs):
- Insert more Primary Users (PUs), which occupy distinct channels at several times, and mimic how SUs vacate channels or switch among distinct obtainable channels.
- Advanced Spectrum Sensing Models:
- Execute distinct sensing methods (e.g., energy detection, matched filtering) and launch sensing delays or errors to mimic more realistic CRN behaviour.
- Cognitive Radio MAC Protocols:
- Alter the MAC layer to contain cognitive radio-specific aspects such as spectrum handoff, collision avoidance, and priority access for PUs.
- Dynamic Spectrum Access (DSA):
- Execute more complex dynamic spectrum access algorithms to model intelligent decision-making by SUs when choosing channels.
- Performance Metrics:
- Estimate CRN performance parameters like spectrum efficiency, handoff latency, and interference with PUs.
Within NS2, we had comprehensively explained the key concepts and simulation procedure to replicate and analyse the Cognitive Radio Networks projects. Also we provide extension of this networks. If you asked, we can dive deeper into subject and deliver complete demonstrations.
Working with phdprime.com will help you get the best simulation results and project ideas that are suited to your interests.