How to Begin Implement Network PSNR in NS3
To begin the Peak Signal-to-Noise Ratio (PSNR) is a generally used to performance of metric can the calculate amount of recreate on image or video. In this setting for ns3 has executes the PSHR contains for replicate a multimedia transmission over a network and estimate the quality of received video or image through associate the original.
Here’s how to begin implementing PSNR in ns-3:
Steps to Begin Implement Network PSNR in NS3
- Understand PSNR
- PSNR Definition:
- PSNR calculates the quality of reconstructed signal associate to the original.
- It is evaluated using the Mean Squared Error (MSE) among the original and recreate the signal.
- Formula: PSNR=10⋅log10(MAX2MSE)PSNR = 10 \cdot \log_{10}\left(\frac{MAX^2}{MSE}\right)PSNR=10⋅log10(MSEMAX2)
- MAX: Maximum possible for pixel value for instance 255 for an 8-bit image.
- MSE: Mean Squared Error among original and received signals.
- Use Cases:
- Calculation for the quality of video streaming.
- It analysis for image transmission quality.
- Set up ns-3 Environment
- Install ns-3:
git clone https://gitlab.com/nsnam/ns-3-dev.git
cd ns-3-dev
./ns3 configure –enable-examples –enable-tests
./ns3 build
- Validate the installation:
./ns3 run hello-simulator
- Design the Network Simulation
- Scenario:
- It replicates the multimedia communication for instance video streaming or image transmission.
- Evaluate the amount of recreated media at using the receiver of PSNR.
- Topology:
- Sender: Communicates the video or image data.
- Receiver: Receive the data and estimates the using quality of PSNR.
- Intermediate Nodes: Optionally enhance the network impacts for sample delay, loss, or congestion.
- Steps to Implement PSNR
(a) Create Nodes
- Describe the sender and receiver nodes:
NodeContainer sender, receiver;
sender.Create(1);
receiver.Create(1);
(b) Set up Network Links
- Use to linked the nodes such as PointToPointHelper:
PointToPointHelper p2p;
p2p.SetDeviceAttribute(“DataRate”, StringValue(“10Mbps”));
p2p.SetChannelAttribute(“Delay”, StringValue(“2ms”));
NetDeviceContainer devices = p2p.Install(NodeContainer(sender.Get(0), receiver.Get(0)));
(c) Install Internet Stack
- For all nodes are install the Internet stack:
InternetStackHelper stack;
stack.Install(sender);
stack.Install(receiver);
(d) Assign IP Addresses
- Allocate the IP addresses for devices:
Ipv4AddressHelper address;
address.SetBase(“10.1.1.0”, “255.255.255.0”);
address.Assign(devices);
- Simulate Multimedia Traffic
- Sender:
- It replicates the video or image data using an application.
- Example: Send packets by replicated the video data:
UdpEchoServerHelper echoServer(9);
ApplicationContainer serverApp = echoServer.Install(sender.Get(0));
serverApp.Start(Seconds(1.0));
serverApp.Stop(Seconds(10.0));
- Receiver:
- The recreate data are received:
UdpEchoClientHelper echoClient(Ipv4Address(“10.1.1.1”), 9);
echoClient.SetAttribute(“MaxPackets”, UintegerValue(100));
echoClient.SetAttribute(“Interval”, TimeValue(Seconds(0.1)));
echoClient.SetAttribute(“PacketSize”, UintegerValue(1024));
ApplicationContainer clientApp = echoClient.Install(receiver.Get(0));
clientApp.Start(Seconds(2.0));
clientApp.Stop(Seconds(10.0));
- Compute PSNR
(a) Measure Received Data
- Seizure the packets for the receiver:
void MeasureReceivedData(Ptr<const Packet> packet) {
NS_LOG_UNCOND(“Packet received at: ” << Simulator::Now().GetSeconds());
}
receiver.Get(0)->TraceConnectWithoutContext(“PhyRxEnd”, MakeCallback(&MeasureReceivedData));
(b) Reconstruct Received Signal
- It replicate the reconstruction through enhance the noise or loss to the original signal:
double ComputeMSE(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) {
double diff = original[i] – received[i];
mse += diff * diff;
}
return mse / original.size();
}
(c) Compute PSNR
- Use the reconstructed signal and original for calculate the PSNR:
double ComputePSNR(double mse, double maxPixelValue = 255.0) {
return 10.0 * std::log10((maxPixelValue * maxPixelValue) / mse);
}
- Complete Example Code
Following for below sample execution:
#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”
using namespace ns3;
double ComputeMSE(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) {
double diff = original[i] – received[i];
mse += diff * diff;
}
return mse / original.size();
}
double ComputePSNR(double mse, double maxPixelValue = 255.0) {
if (mse == 0) {
return std::numeric_limits<double>::infinity();
}
return 10.0 * std::log10((maxPixelValue * maxPixelValue) / mse);
}
int main(int argc, char *argv[]) {
CommandLine cmd;
cmd.Parse(argc, argv);
NodeContainer sender, receiver;
sender.Create(1);
receiver.Create(1);
PointToPointHelper p2p;
p2p.SetDeviceAttribute(“DataRate”, StringValue(“10Mbps”));
p2p.SetChannelAttribute(“Delay”, StringValue(“2ms”));
NetDeviceContainer devices = p2p.Install(NodeContainer(sender.Get(0), receiver.Get(0)));
InternetStackHelper stack;
stack.Install(sender);
stack.Install(receiver);
Ipv4AddressHelper address;
address.SetBase(“10.1.1.0”, “255.255.255.0”);
address.Assign(devices);
UdpEchoServerHelper echoServer(9);
ApplicationContainer serverApp = echoServer.Install(sender.Get(0));
serverApp.Start(Seconds(1.0));
serverApp.Stop(Seconds(10.0));
UdpEchoClientHelper echoClient(Ipv4Address(“10.1.1.1”), 9);
echoClient.SetAttribute(“MaxPackets”, UintegerValue(100));
echoClient.SetAttribute(“Interval”, TimeValue(Seconds(0.1)));
echoClient.SetAttribute(“PacketSize”, UintegerValue(1024));
ApplicationContainer clientApp = echoClient.Install(receiver.Get(0));
clientApp.Start(Seconds(2.0));
clientApp.Stop(Seconds(10.0));
// Simulate original and received data
std::vector<uint8_t> originalData(1024, 255); // Original signal (all pixels white)
std::vector<uint8_t> receivedData = originalData;
for (size_t i = 0; i < receivedData.size(); ++i) {
receivedData[i] -= rand() % 20; // Simulate degradation
}
double mse = ComputeMSE(originalData, receivedData);
double psnr = ComputePSNR(mse);
Simulator::Run();
NS_LOG_UNCOND(“Mean Squared Error (MSE): ” << mse);
NS_LOG_UNCOND(“Peak Signal-to-Noise Ratio (PSNR): ” << psnr);
Simulator::Destroy();
return 0;
}
- Validate and Extend
- Validation:
- It associates the PSNR values by expected outcomes for change the degradation stages.
- Extensions:
- It replicates the further difficult for video codecs.
- Increase the network results for instance packet loss, jitter, delay.
In this simulation, we know how to estimate the PSNR in the images and video frames over the network using the ns3 tool. We will outline the steps involved in performing the PSNR in different simulation settings.