In this post we will see a simple algorithm that help us to choose the best 2.4 GHz WiFi channel for our network. This algorithm can be implemented and modified with respect to the programming language adopted.

Introduction

Before going into detail of the algorithm, let’s see some teorethical stuff.

The wifi spectrum of the 2.4GHz WiFi channels contains from 12 to 14 channels (depending on your geographical region). Every channel, except for the 14, has the central frequency increased by 5 MHz with respect to the previous one.  The band of a channel can interfere up to two adjacent channels. For example, a network that uses the channel 6 (2437 MHz) interfere with the 4,5 channels and woth the 7,8 channels.

Algorithm

The algorithm has three main steps:

Step 1: WiFi Analysis in this phase we perform the WiFi Analysis to verify if there are other networks that can interfere with our.
Step 2: Processing data in this phase we process the data (for each channel) related to the weight and we verify the status of the adjacent channels.
Step 3: Scenario assessment and best channel in this phase we can have different scenario. When we evaluate it we can surely choose the best channel.

WiFi Analysis

First of all we do the WiFi scan with a proper command (that can be iwinfo or iwlist). From the WiFi Scan we took the following information for each channel:

SSID BSSID channel RSSI[dB]

Processing data

Once the Wifi Scan is performed, we process the data generating some statistics. We evaluate a weight to associate to each channel and we see (for each channel) if the adjacent channel (left and right up to 2) are free or not.

Note: Obviously the first and the last channel of the WiFi spectrum don’t have an adjacent channel on the left and on the right. The same is for the channels right after/before, they don’t have up to two adjacent channel from left/right.

Now we create the statistics table. Every row is associated to one channel and it will contain this information:

#Channel  #apAssociated RSSIofTheBestAp Weight L R L2 R2

Note: Every channel must have an entry on this table, even if it’s not associated with any other Ap. So if a channel is free, it will have a weight and an RSSI of 100. As said before, it’s possibile that some channel they don’t have adjacent channels, so the value of L,R,L2 or R2 are set to -1. If instead the left adjacent channel is free, we set L to 0. Otherwise it will be set to 1. The same for L2, R and R2.

Weight function

The weight function is the most important part of the algorithm. To evalue the weight of a channel we divide the value of the RSSIofTheBestAp for the #apAssociated. If from the wifi scan it’s possible to check also the noise of each channel, the weight can be penalized when we reach a specific treshold.

Scenario assesment and best channel

Once we have generated the statistics, we can choose the best channel by evaluating the scenario:

Scenario 1

All the channels are occupied.

This is the worst case scenario. In this case we will choose the channel that has the higher weight associated. If we find more than one channel with the same weight, it’s up to you to choose the proper channel (by a hierarchy, the first tha we found, the last… ).

Scenario 2

One or more channels are free and they have adjacent channels free (up to two).

This is the best case scenario. If we have only one channel that has this constraints, the choice is easy. As before, if we find more than one channel with the same weight, it’s up to you to choose the proper channel (by a hierarchy, the first that we found, the last… ).

Scenario 3

One or more channels are free and they have adjacent channels free (up to two) on left or right, but they have only up to one channel free from right or left. 

As before, if we find more than one channel with the same weight, it’s up to you to choose the proper channel (by a hierarchy, the first tha we found, the last… ).

Scenario 4

One or more channels are free and they have adjacent channels free (up to one) on left and right.

As before, if we find more than one channel with the same weight, it’s up to you to choose the proper channel (by a hierarchy, the first tha we found, the last… ).

Scenario 5

One or more channels are free and they have adjacent channels free on left or right (up to one), but occupied on right or left.

As before, if we find more than one channel with the same weight, it’s up to you to choose the proper channel (by a hierarchy, the first tha we found, the last… ).

Scenario 6

One or more channels free but with adjacent channels occupied.

As before, if we find more than one channel with the same weight, it’s up to you to choose the proper channel (by a hierarchy, the first tha we found, the last… ).

This algorithm works very well and it’s very simple if we have only one channel to choose. Instead, if we have more than one best channel to choose, all depends on your choice.

Suggestion

If you don’t know which is the Best USB Wifi Adapter, check 10 Best USB Wifi Adapters for PC Gamers (2017 Edition)