How to Calculate PSNR in NS2

To implement the Peak Signal-to-Noise Ratio (PSNR) in ns2, we have a numerous steps to follow and it is often parameters used to measure the quality of recreated or compressed multimedia data, specifically in video and image transmission applications. It evaluates the ratio among the maximum possible power of a signal and the power of interrupting noise that impacts the signal’s quality. PSNR is uttered in decibels (dB), and higher PSNR values usually signify better quality.

Since, ns2 of PSNR is not directly estimated; we can compute it by extracting the actual and transferred signals and then using the Mean Squared Error (MSE) among the original and received data.

Here is a guide to calculate the PSNR in an NS2 simulation:

PSNR Formula:

PSNR=10⋅log⁡10(MAX2MSE)\text{PSNR}=10\cdot\log_{10}\left(\frac{MAX^2}{\text{MSE}}\right)PSNR=10⋅log10​(MSEMAX2​)

Where:

  • MAXMAXMAX is the maximum possible pixel value of the image (255 for 8-bit images).
  • MSE is the Mean Squared Error among the original and the transmitted/reconstructed signal, calculated as:

MSE=1mn∑i=1m∑j=1n[I(i,j)−K(i,j)]2\text{MSE} = \frac{1}{mn} \sum_{i=1}^{m}\sum_{j=1}^{n} \left[ I(i,j) – K(i,j) \right]^2MSE=mn1​i=1∑m​j=1∑n​[I(i,j)−K(i,j)]2

Where:

  • I(i,j)I(i,j)I(i,j) is the pixel value of the original image at position (i,j)(i,j)(i,j).
  • K(i,j)K(i,j)K(i,j) is the pixel value of the reconstructed image at position (i,j)(i,j)(i,j).
  • m×nm \times nm×n is the total number of pixels.

Steps to Calculate PSNR in NS2:

  1. Set Up NS2 to Simulate Video/Image Transmission:

In an NS2 simulation, we replicate video transmission such as using UDP or TCP across a wireless network. The main goal is to compare the original video frames with the frames received at the destination to estimate MSE and PSNR.

Example NS2 Setup for Video/Image Transmission:

# Create the simulator

set ns [new Simulator]

# Define the topology (e.g., 1000m x 1000m area)

set topo [new Topography]

$topo load_flatgrid 1000 1000

# Create two nodes (source and destination)

set node_(0) [$ns node]

set node_(1) [$ns node]

# Define the wireless channel

set chan_ [new Channel/WirelessChannel]

$node_(0) set channel_ $chan_

$node_(1) set channel_ $chan_

# Attach UDP agents for video transmission

set udp0 [new Agent/UDP]

set sink [new Agent/Null]

$ns attach-agent $node_(0) $udp0

$ns attach-agent $node_(1) $sink

$ns connect $udp0 $sink

# Set up CBR traffic (to simulate video frame transmission)

set cbr0 [new Application/Traffic/CBR]

$cbr0 attach-agent $udp0

$cbr0 set packetSize_ 1024        ;# Video frame packet size

$cbr0 set rate_ 2Mb               ;# Transmission rate (2 Mbps)

# Start and stop the traffic

$ns at 1.0 “$cbr0 start”

$ns at 10.0 “$cbr0 stop”

# Enable tracing to capture packet reception at the destination

set tracefile [open out.tr w]

$ns trace-all $tracefile

# Close the trace file at the end

proc finish {} {

global ns tracefile

$ns flush-trace

close $tracefile

exit 0

}

# End simulation at time 12 seconds

$ns at 12.0 “finish”

  1. Collect Received Data:

In the trace file (out.tr), we will see events for packet transmissions and receptions. For instance, if we are mimics a video or image transmission, each packet denotes a fragment of a video frame or an image.

Example Trace File Entries:

r 2.000000000 _1_ AGT  — 1024 cbr 0 0.0 1.0 1.0

r 3.000000000 _1_ AGT  — 1024 cbr 1 0.0 2.0 2.0

Here:

  • r: Packet reception event.
  • _1_: Node ID (the destination node).
  • 1024: Packet size in bytes (which may represent part of an image or video frame).
  1. Calculate MSE from Original and Received Data:

The MSE (Mean Squared Error) can be estimated by comparing the original signal such as original image or video frame with the reconstructed signal at the destination.

AWK Script to Calculate MSE:

We can use an AWK script to estimate the MSE among the original and received data by comparing the packet data or pixel values:

BEGIN {

original_data_size = 0;

received_data_size = 0;

total_error = 0;

}

# Track original and received data for each packet

$1 == “r” && $4 == “_1_” {

# Simulating that the packet contains part of an image or video frame

original_value = 255;  # Max pixel value for 8-bit data

received_value = $8;   # Example of received pixel value

error = (original_value – received_value) ** 2;

total_error += error;

received_data_size += $8;

}

END {

# Calculate Mean Squared Error (MSE)

mse = total_error / received_data_size;

print “MSE: ” mse;

}

This script compares the original and received pixel values (or data values) and computes the MSE.

  1. Calculate PSNR:

After estimating the MSE using the AWK script or any other techniques, we can calculate the PSNR using the formula.

Python Script to Calculate PSNR:

If we have the original and received frames or data as arrays like a video frame or image, we can estimate PSNR in Python.

import numpy as np

def calculate_psnr(original, received):

mse = np.mean((np.array(original) – np.array(received)) ** 2)

if mse == 0:  # No error

return float(‘inf’)

max_pixel_value = 255.0  # Assuming 8-bit images

psnr = 20 * np.log10(max_pixel_value / np.sqrt(mse))

return psnr

# Example original and received data

original_data = [255, 255, 255, 255]  # Original pixel values

received_data = [250, 252, 255, 240]  # Received pixel values

psnr_value = calculate_psnr(original_data, received_data)

print(f”PSNR: {psnr_value} dB”)

This script computes PSNR among the original and received pixel values.

  1. Interpreting PSNR Values:
  • PSNR > 40 dB: Excellent quality (minimal distortion).
  • 30 dB ≤ PSNR ≤ 40 dB: Good quality, acceptable for most applications.
  • 20 dB ≤ PSNR ≤ 30 dB: Fair quality, noticeable degradation.
  • PSNR < 20 dB: Poor quality, important distortion.
  1. Conclusion:

To compute PSNR in NS2:

  1. Simulate the transmission of video or image data across the network.
  2. Collect the transmitted and received data from the trace file or the actual application layer.
  3. Calculate the MSE among the original and received data (using an AWK script or Python).
  4. Compute the PSNR using the MSE and the maximum possible pixel value.

In this simulation, we know how to estimate the PSNR in the images and video frames over the network using the ns2 tool. We will outline the steps involved in performing the PSNR in different simulation settings in upcoming script. To calculate PSNR in NS2, feel free to reach out to us. We provide excellent guidance and a concise explanation to support you in your work.