Spectrum sensing is one of the most important components of cognitive radio (CR) technology. The delays in sensing idle channels for SU data will have a direct impacet on how fast secondary user (SU) data transmissions happen. Sensing time or delay can be added via code modification to analyze the impact it has on the network performance.

Following changes are to be done to the code inorder to add sensing time/delay for spectrum sensing:

1.Open NetSim source code in Visual Studio 2015 by double clicking on the NetSim.sln file present in “<NetSim_Install_Directory>/src/Simulation” folder.

2.Go to CR project through the solution explorer and open the file SpectrumManager.c. Add the lines of code highlighted in red as shown below in the start of the file:

#include "main.h"

#include "802_22.h"

#include "SpectrumManager.h"

#define SENSING_DELAY 100000

/** This function is used  set the channel number and assign the channel characteristics for all channels */

int fn_NetSim_CR_FormChannelSet(BS_PHY* pstruBSPhy)

3. In the same file make the following changes highlighted in red inside the function fn_NetSim_CR_QuietPeriod() as shown below:

if (output->nSignalPresentVector == 0xFF)


//Generate the UCS notification packet

NetSim_PACKET* pstruPacket;

NetSim_PACKET* pstruTempPacket;

GMH* pstruGMH = (GMH*)fnpAllocateMemory(1, sizeof *pstruGMH);

pstruPacket = fn_NetSim_Packet_CreatePacket(MAC_LAYER);


add_dest_to_packet(pstruPacket, pstruCPEMAC->nBSID);

pstruPacket->nPacketPriority = Priority_High;

pstruPacket->nPacketType = PacketType_Control;

pstruPacket->nReceiverId = pstruCPEMAC->nBSID;

pstruPacket->nSourceId = pstruEventDetails->nDeviceId;

pstruPacket->nTransmitterId = pstruEventDetails->nDeviceId;

pstruPacket->pstruMacData->dArrivalTime = pstruEventDetails->dEventTime + SENSING_DELAY;

pstruPacket->pstruMacData->dOverhead = GMH_SIZE;

Delay is currently set to 100 ms (100000 micro seconds). User can modify the value of SENSING_DELAY as per the requirement to analyze the impact.

4. Open the file USFrame.c make the following changes highlighted in red inside the function fn_NetSim_CR_FormUSBurst() as shown below:

//Add an event to transmit US-Burst

pstruEventDetails->dEventTime = pstruCPEMAC->BWRequestInfo->dStartTime;

pstruEventDetails->dPacketSize = 0;

pstruEventDetails->nApplicationId = 0;

pstruEventDetails->nEventType = TIMER_EVENT;

pstruEventDetails->nPacketId = 0;

pstruEventDetails->nProtocolId = MAC_PROTOCOL_IEEE802_22;

pstruEventDetails->nSubEventType = TRANSMIT_US_BURST;

pstruEventDetails->pPacket = NULL;



// Code modification for spectrum sensing delay starts here

NetSim_PACKET* p = NULL;

NetSim_PACKET* prev = NULL;

NetSim_PACKET* temp = pstruCPEMAC->pstruQueuedPacketList[0];

while (temp)


if (temp->pstruMacData->dArrivalTime > pstruEventDetails->dEventTime)


if (prev)

prev->pstruNextPacket = temp;


pstruCPEMAC->pstruQueuedPacketList[0] = temp;

prev = temp;

temp = temp->pstruNextPacket;

prev->pstruNextPacket = NULL;

p = NULL;




p = temp;

if (pstruCPEMAC->pstruQueuedPacketList[0] == temp)

pstruCPEMAC->pstruQueuedPacketList[0] = temp->pstruNextPacket;

temp = temp->pstruNextPacket;

p->pstruNextPacket = NULL;


if (p)


//Generate physical out event

pstruEventDetails->dEventTime = t;

pstruEventDetails->dPacketSize = p->pstruMacData->dPacketSize;

pstruEventDetails->nApplicationId = 0;

pstruEventDetails->nEventType = PHYSICAL_OUT_EVENT;

pstruEventDetails->nPacketId = 0;

pstruEventDetails->nProtocolId = MAC_PROTOCOL_IEEE802_22;

pstruEventDetails->nSubEventType = TRANSMIT_US_BURST_CONTROL;

pstruEventDetails->pPacket = p;



// Code modification for spectrum sensing delay ends here


return 0;


5.Now right click on CR module in the solution explorer and select rebuild.

6.Upon successful build, you will get a new libCognitiveRadio.dll file in the “<NetSim_Install_Directory>/src/Simulation/DLL” folder.

7.Copy this newly built DLL file and replace it in the bin folder of NetSim after you rename the original libCognitiveRadio.dll file which is already existing there(as a backup).

8.Now on running any simulation in Cognitive Radio network, whenever secondary users are performing spectrum sensing, additional delay is added before UCS notification is sent to the BS.