Pick the clearest channel
Find the overlap between 2.4 GHz channels and choose the least crowded of 1, 6 and 11. An easy embedded C problem (Wi-Fi for embedded), run and graded in the…
easy · Wi-Fi for embedded · wifi · rf · channels
2.4 GHz Wi-Fi channels are only 5 MHz apart but each signal is about 20 MHz wide, so neighbouring channels overlap: only 1, 6 and 11 are clear of each other.
- Channel
c(1 to 13) is centred on2407 + 5cMHz; channel 14 is at 2484 MHz. - Two channels overlap if their centres are less than 20 MHz apart.
Write channel_mhz(c), overlaps(a, b), and best_channel(aps, n): given the channels of n nearby access points, return whichever of 1, 6 and 11 overlaps the fewest of them (the lowest on ties).
Starter code
int channel_mhz(int c) { return 2407 + 5 * c; }
bool overlaps(int a, int b) { return a == b; }
int best_channel(const int *aps, int n) {
int count[3] = {0};
const int choices[3] = { 1, 6, 11 };
for (int k = 0; k < 3; k++)
for (int i = 0; i < n; i++) if (overlaps(choices[k], aps[i])) count[k]++;
int best = 0;
for (int k = 1; k < 3; k++) if (count[k] < count[best]) best = k;
return choices[best];
}Example tests
Frequencies and overlap
printf("%d %d %d\n", channel_mhz(1), channel_mhz(6), channel_mhz(14));
printf("%d %d %d\n", overlaps(1, 4), overlaps(1, 5), overlaps(1, 6));Expected output:
2412 2437 2484
1 0 0Learn it first
- Wi-Fi for embedded · Station and AP modes, provisioning, power save and security.