**How to Calculate Network Spectral Efficiency in NS2**

To calculate the Network Spectral Efficiency in ns2, this is the important parameter in wireless communication networks that evaluates the effectiveness of the network in exploiting the available spectrum to transfer data. It is usually expressed in bits per second per Hertz (bps/Hz) and supports to evaluate how efficiently the network uses its available bandwidth.

In NS2, estimate Network Spectral Efficiency contain to measure the throughput of the network and the bandwidth (or spectrum) used. Spectral efficiency is defined as:

Spectral Efficiency=Throughput (bps) Bandwidth (Hz)\text {Spectral Efficiency} = \frac{\text{Throughput (bps)}} {\text{Bandwidth (Hz)}} Spectral Efficiency=Bandwidth (Hz) Throughput (bps)

Where:

**Throughput**is the data rate achieved by the network (in bits per second, bps).**Bandwidth**is the total spectrum (in Hertz, Hz) allotted to the network.

**Steps to Calculate Network Spectral Efficiency in NS2**

**Generate the Trace File**

To estimate **spectral efficiency**, we first need to create a trace file in NS2 that logs the events like packet transmission, reception, and size.

Add the following lines to NS2 simulation script to create a trace file:

set tracefile [open out.tr w]

$ns trace-all $tracefile

The trace file will contain information such as:

**Packet size**in bytes.**Time of transmission and reception**.**Packet type**(TCP, UDP, etc.).

**Calculate Network Throughput**

The **throughput** is the total amount of data effectively routed over the network. To estimate throughput, we can sum up the sizes of all successfully received packets and divide by the total simulation time.

**AWK Script to Calculate Throughput:**

awk ‘{

if ($1 == “r” && $4 == “tcp”) { # Only consider received TCP packets

total_bytes += $6; # $6 is the packet size in bytes

}

} END {

simulation_time = 100.0; # Replace with your actual simulation time in seconds

throughput_bps = total_bytes * 8 / simulation_time; # Convert bytes to bits

print “Throughput: ” throughput_bps ” bps”;

}’ out.tr

This script:

- Summing up the total number of bytes received successfully (for TCP packets in this case).
- Converts bytes to bits by multiplying by 8.
- Divides by the total simulation time (in seconds) to calculate the
**throughput in bits per second (bps)**.

**Determine Bandwidth (Hz)**

In NS2, bandwidth is usually indicated when generating a link among nodes. For wireless networks, this is the total available spectrum for communication.

For sample, when configuring a link or wireless communication in NS2, the bandwidth is defined as part of the link properties:

# Example for a wired link

$ns duplex-link $node(0) $node(1) 10Mb 10ms DropTail

# Example for wireless setup with a specific PHY bandwidth

set val(chan) Channel/WirelessChannel ;# Wireless channel

set val(netif) Phy/WirelessPhy ;# Wireless PHY with specific bandwidth

set val(bw) 20e6 ;# Bandwidth is 20 MHz (20 MHz = 20e6 Hz)

**Wired link bandwidth**is given in Mbps like 10Mb = 10 Megabits per second.**Wireless link bandwidth**is usual defined in terms of the**PHY bandwidth**such as 20 MHz.

For example, if the total bandwidth for the wireless network is **20 MHz** (20e6 Hz), then we will utilize this value in the calculation of spectral efficiency.

**Calculate Spectral Efficiency**

Once we have the **throughput** (in bits per second) and the **bandwidth** (in Hertz), we estimate the **spectral efficiency**:

Spectral Efficiency=Throughput (bps)Bandwidth (Hz)\text{Spectral Efficiency} = \frac{\text{Throughput (bps)}}{\text{Bandwidth (Hz)}}Spectral Efficiency=Bandwidth (Hz)Throughput (bps)

**AWK Script to Calculate Spectral Efficiency:**

We can adjust the AWK script to estimate the **spectral efficiency** by dividing the throughput by the bandwidth:

awk ‘{

if ($1 == “r” && $4 == “tcp”) {

total_bytes += $6; # Sum of packet sizes (in bytes)

}

} END {

simulation_time = 100.0; # Replace with actual simulation time in seconds

bandwidth_hz = 20e6; # Bandwidth in Hz (e.g., 20 MHz = 20e6 Hz)

throughput_bps = total_bytes * 8 / simulation_time; # Convert bytes to bits

spectral_efficiency = throughput_bps / bandwidth_hz; # Spectral efficiency in bps/Hz

print “Throughput: ” throughput_bps ” bps”;

print “Spectral Efficiency: ” spectral_efficiency ” bps/Hz”;

}’ out.tr

This script:

- Computes the total
**throughput**in bps. - Divides the throughput by the bandwidth to calculate the
**spectral efficiency**in**bps/Hz**.

**Example:**

If network throughput is **4 Mbps** (4,000,000 bps) and the available bandwidth is **20 MHz** (20,000,000 Hz), the **spectral efficiency** will be:

Spectral Efficiency=4,000,00020,000,000=0.2 bps/Hz\text{Spectral Efficiency} = \frac{4,000,000}{20,000,000} = 0.2 \, \text{bps/Hz}Spectral Efficiency=20,000,0004,000,000=0.2bps/Hz

This means the network can route **0.2 bits per second for every Hertz** of bandwidth.

**Factors Affecting Spectral Efficiency**

Spectral efficiency is influenced by numerous factors that has:

**Modulation schemes**: Higher-order modulation such as 64-QAM, 256-QAM can improve spectral efficiency by routing more bits per symbol.**Multiple Access Schemes**: Technologies such as**OFDMA**(Orthogonal Frequency-Division Multiple Access) in 4G/5G can improve spectral efficiency.**Network Congestion**: Higher congestion can decrease throughput, thus dropping spectral efficiency.**Signal Quality and Interference**: Poor signal quality or meddling in wireless networks can minimize the attainable spectral efficiency.

In NS2 simulations, we can test with these metrics such as modulation schemes, interference, congestion, etc. to assess how they affect **spectral efficiency**.

**Simulating Different Bandwidths in NS2**

If we are mimic a wireless network and need to validate numerous bandwidths such as **10 MHz**, **20 MHz**, etc., that simply adapts the bandwidth parameter in simulation script:

set val(bw) 10e6 ;# Bandwidth set to 10 MHz

Then, repeated the simulation and utilize the AWK script to recalculate the spectral efficiency according to the new bandwidth.

In this replication scenario, we grasp the concepts of how the spectral efficiency will compute and estimate the effectiveness of the network in available spectrum to transfer data that were calculated in ns2 simulator. We plan to deliver more information regarding the spectral efficiency performances in other simulation tool.

To accurately calculate the Network Spectral Efficiency (NS2) using our tool, it’s recommended to seek professional assistance for achieving optimal results in your work. Please send us all the necessary parameter information, and our experienced team will be ready to assist you with the specifics of the networking comparison study.