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

How to Calculate Network PSNR in ns3

To calculate Peak Signal-to-Noise Ratio (PSNR) in ns3, we need to simulate the PSNR by transferring the video frames across the network then gather the transferred and received video frames subsequently compare the frames to evaluate the PSNR. The PSNR is a measure of quality of received video as reconstructed signal that compares to the original signal as transmitted video.

Do you need assistance with analyzing performance using Peak Signal-to-Noise Ratio (PSNR) in ns3 for your project? Just give us your parameters and we’ll provide you with a detailed result!

Here are the procedures to configure the ns3 by simulating the video transferring then gather the data of video framers finally it evaluate the PSNR.

Steps to Calculate PSNR in ns3

  1. Set Up ns3 Environment:
    • Make certain ns3 is installed in the computer.
  1. Create a New ns3 Script:
    • Create a new script file in the scratch directory of ns3, e.g., psnr_calculation.cc.
  1. Include Necessary Headers:
    • In the script conclude all the required ns3 headers.
  1. Define Network Topology:
    • By using multiple nodes to set up network topology.
  1. Simulate Video Transmission:
    • To simulate the transmission of video frames over the network by implementing a mechanism.
  1. Collect Received Video Frames:
    • To collect received video frames at the destination node by implementing the mechanism.
  1. Calculate PSNR:
    • To calculate PSNR by comparing the received video frames with the original frames by use of functions.

Example Code:

The given below are the sample procedure to illustrative on how to configure the network and gathers data. Based on the requirements we need simulate the actual video transmission and PSNR evaluation in ns3:

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/internet-module.h”

#include “ns3/point-to-point-module.h”

#include “ns3/applications-module.h”

#include <cmath>

#include <vector>

using namespace ns3;

NS_LOG_COMPONENT_DEFINE (“PSNRCalculationExample”);

double CalculatePSNR(const std::vector<uint8_t>& original, const std::vector<uint8_t>& received)

{

double mse = 0.0;

for (size_t i = 0; i < original.size(); ++i)

{

mse += std::pow(static_cast<double>(original[i]) – static_cast<double>(received[i]), 2);

}

mse /= original.size();

if (mse == 0) // Avoid division by zero

return 100.0;

double psnr = 10 * std::log10((255 * 255) / mse);

return psnr;

}

void ReceivePacket(Ptr<Socket> socket, std::vector<uint8_t>& receivedDataBuffer)

{

Ptr<Packet> packet;

while ((packet = socket->Recv()))

{

uint8_t* buffer = new uint8_t[packet->GetSize()];

packet->CopyData(buffer, packet->GetSize());

for (size_t i = 0; i < packet->GetSize(); ++i)

{

receivedDataBuffer.push_back(buffer[i]);

}

delete[] buffer;

}

}

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

{

uint32_t nNodes = 2; // Number of nodes

double simulationTime = 10.0; // Total simulation time in seconds

CommandLine cmd;

cmd.AddValue(“nNodes”, “Number of nodes”, nNodes);

cmd.AddValue(“simulationTime”, “Total simulation time”, simulationTime);

cmd.Parse(argc, argv);

NodeContainer nodes;

nodes.Create(nNodes);

PointToPointHelper pointToPoint;

pointToPoint.SetDeviceAttribute(“DataRate”, StringValue(“5Mbps”));

pointToPoint.SetChannelAttribute(“Delay”, StringValue(“2ms”));

NetDeviceContainer devices;

devices = pointToPoint.Install(nodes);

InternetStackHelper stack;

stack.Install(nodes);

Ipv4AddressHelper address;

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

Ipv4InterfaceContainer interfaces = address.Assign(devices);

uint16_t port = 9;

Address serverAddress(InetSocketAddress(interfaces.GetAddress(1), port));

PacketSinkHelper packetSinkHelper(“ns3::UdpSocketFactory”, serverAddress);

ApplicationContainer serverApps = packetSinkHelper.Install(nodes.Get(1));

serverApps.Start(Seconds(1.0));

serverApps.Stop(Seconds(simulationTime));

Ptr<Socket>sourceSocket=Socket::CreateSocket(nodes.Get(0),TypeId::LookupByName(“ns3::UdpSocketFactory”));

sourceSocket->Connect(serverAddress);

std::vector<uint8_t> originalData = { /* … fill with original video frame data … */ };

std::vector<uint8_t> receivedData;

for (size_t i = 0; i < originalData.size(); i += 1024)

{

Ptr<Packet> packet = Create<Packet>(&originalData[i], std::min<size_t>(1024, originalData.size() – i));

sourceSocket->Send(packet);

}  Ptr<Socket>sinkSocket=Socket::CreateSocket(nodes.Get(1),TypeId::LookupByName(“ns3::UdpSocketFactory”));

sinkSocket->Bind(serverAddress);

sinkSocket->SetRecvCallback(MakeCallback(&ReceivePacket, receivedData));

Simulator::Stop(Seconds(simulationTime));

Simulator::Run();

Simulator::Destroy();

// Calculate PSNR

double psnr = CalculatePSNR(originalData, receivedData);

std::cout << “PSNR: ” << psnr << ” dB” << std::endl;

return 0;

}

Explanation:

Below are the detailed and structures procedures for calculating the PSNR

  1. Nodes and Links:
    • Created nodes and configured a point-to-point network for communication.
    • Set up the network with nodes connected using point-to-point links.
  2. Applications:
    • Installed a packet sink on one node to receive packets.
    • Created a socket on another node to send packets.
  3. Video Transmission Simulation:
    • Implemented a basic mechanism to send packets, representing video frames, from one node to another.
  4. Receive Packet Function:
    • Implemented a ReceivePacket function to handle received packets. This is where you would collect received video frames and compare them with the original frames to calculate PSNR.
  5. PSNR Calculation:
    • Implemented a CalculatePSNR function to calculate the Peak Signal-to-Noise Ratio based on the original and received data.
    • For demonstration purposes, the example uses simple vectors to represent the original and received data.
  6. Running the Simulation:
    • The simulation runs, sending and receiving packets.
    • After the simulation, the PSNR is calculated and printed.

Finally, we had learned how to calculate the PSNR metric in ns3 tool. We also deliver additional details on how the PSNR evaluation behaves in other simulation tool.