# Code-Division Multiple Access (CDMA) | Computer Networks

**Computer Networks CDMA**: In this article, we are going to learn **Code-Division Multiple Access (CDMA)** and **how to implement CDMA with Walsh table?**

Submitted by Radib Kar, on May 04, 2019

The analogy behind **code-division multiple access** says there are four people and two out of them are talking with each other in some language what other two don't know and the same goes for those two also. For example, two of them talking in Punjabi and two in Bengali. Those two talking in Punjabi has no idea about Bengali and same for Bengali guys too. So, there is no interruption in their respective communication. The same analogy has been used for **CDMA**.

We discuss the concept with the help of an example,

Say there are four stations:A, B, C, DEach station has assigned a code sayCEach of them sending their respective data say_{1}, C_{2}, C_{3}, C_{4}DAll of them throws their data multiplying with the code Thus data on the channel is_{1}, D_{2}, D_{3}, D_{4}C_{1}D_{1}+ C_{2}D_{2}+ C_{3}D_{3}+ C_{4}D_{4}

The receiver who wants to retrieve simply multiplying the data with its code and divide by the number of the station.

Now, this is possible because of the coding theory. Let's see the how codes are generated, encoding and decoding of data bit and rules of addition and multiplication

**Code generation:**

Code generation is done by Walsh table

Walsh table is a kind of recursive table which is represented by

Where W1= [1] 2N=No of stations So, for 2 stations

Code for first station is [1 1] //vector Code for second station is [1 -1] //vector Additions and multiplications are vector scalar type Say [1 0]*[-1 1]=(1*-1)+ (0*1)]=-1+0=-1

**Encoding and decoding of data**

- If a station is sending bit 0, it's encoded as -1
- If a station is sending bit 1, it's encoded as 1
- If station is idle, it's encoded as 0

**Example with 2 stations**

Say, Station A sends 1, B sends 0 Code of A: [1 1] Data of A: 1 Code of B: [1 -1] Data of B: -1 Dara on channel 1*[1 1] + -1*[1 -1] =[1 1] + [-1 1] =[0 2] //scalar addition of vector Now say A wants to retrieve its data So multiply data on channel with A’s code ([0 2] * [1 1 ] )/2 =(0*1 + 2*1)=2/2=1 For B ([0 2] * [1 -1])/2=-2/2=-1

**C++ implementation**

#include <bits/stdc++.h> using namespace std; class CDMA { public: int** wtable; int** copy; int* channel_sequence; void setUp(int data[], int num_stations) { int n=num_stations; wtable = new int* [n]; for(int i=0;i<n;i++) wtable[i]=new int[n]; copy = new int* [n]; for(int i=0;i<n;i++) copy[i]=new int[n]; buildWalshTable(num_stations, 0, num_stations - 1, 0, num_stations - 1, false); showWalshTable(num_stations); for (int i = 0; i < num_stations; i++) { for (int j = 0; j < num_stations; j++) { copy[i][j] = wtable[i][j]; wtable[i][j] *= data[i]; } } channel_sequence = new int[n]; for (int i = 0; i < num_stations; i++) { for (int j = 0; j < num_stations; j++) { channel_sequence[i] += wtable[j][i]; } } } void listenTo(int sourceStation, int num_stations) { int innerProduct = 0; for (int i = 0; i < num_stations; i++) { innerProduct += copy[sourceStation][i] * channel_sequence[i]; } int k=innerProduct/num_stations; if(k==1) cout<<"The data received from station "<<sourceStation+1<<" is: "<<k<<endl; else if(k==-1) cout<<"The data received from station "<<sourceStation+1<<" is: 0"<<endl; else cout<<"Station "<<sourceStation+1<<" is idle, it didn't send any data\n"; } //building walsh table int buildWalshTable(int len, int i1, int i2, int j1,int j2, bool isBar) { if (len == 2) { if (!isBar) { wtable[i1][j1] = 1; wtable[i1][j2] = 1; wtable[i2][j1] = 1; wtable[i2][j2] = -1; } else { wtable[i1][j1] = -1; wtable[i1][j2] = -1; wtable[i2][j1] = -1; wtable[i2][j2] = +1; } return 0; } int midi = (i1 + i2) / 2; int midj = (j1 + j2) / 2; buildWalshTable(len / 2, i1, midi, j1, midj, isBar); buildWalshTable(len / 2, i1, midi, midj + 1, j2, isBar); buildWalshTable(len / 2, midi + 1, i2, j1, midj, isBar); buildWalshTable(len / 2, midi + 1, i2, midj + 1, j2, !isBar); return 0; } void showWalshTable(int num_stations) { cout<<"................Displaying walsh table..................\n"; //cout<<endl; for (int i = 0; i < num_stations; i++) { for (int j = 0; j < num_stations; j++) { cout<<wtable[i][j]<<" "; } cout<<"\n"; } cout<<"----------------------------------------------------------\n"; } }; int main() { cout<<"-------------------------------CDMA Implementation------------------------\n"; int num_stations; cout<<"Enter no of stations\n"; cin>>num_stations; //data bits corresponding to each station cout<<"Press 1 if station is sending bit 1\n"; cout<<"Press -1 if station is sending bit 0\n"; cout<<"Press 0 if station is idle\n"; int* data=new int[num_stations]; for(int i=0;i<num_stations;i++){ cout<<"enter for station "<<i+1<<endl; cin>>data[i]; } CDMA channel; channel.setUp(data, num_stations); // station you want to listen to cout<<"Enter station no you want to listen to\n"; int sourceStation; cin>> sourceStation; channel.listenTo(sourceStation-1, num_stations); return 0; }

**Output 1**

-------------------------------CDMA Implementation------------------------ Enter no of stations 4 Press 1 if station is sending bit 1 Press -1 if station is sending bit 0 Press 0 if station is idle enter for station 1 1 enter for station 2 0 enter for station 3 -1 enter for station 4 -1 ................Displaying walsh table.................. 1 1 1 1 1 -1 1 -1 1 1 -1 -1 1 -1 -1 1 ---------------------------------------------------------- Enter station no you want to listen to 4 The data received from station 4 is: 0

**Output 2**

-------------------------------CDMA Implementation------------------------ Enter no of stations 8 Press 1 if station is sending bit 1 Press -1 if station is sending bit 0 Press 0 if station is idle enter for station 1 1 enter for station 2 1 enter for station 3 -1 enter for station 4 -1 enter for station 5 0 enter for station 6 0 enter for station 7 -1 enter for station 8 1 ................Displaying walsh table.................. 1 1 1 1 1 1 1 1 1 -1 1 -1 1 -1 1 -1 1 1 -1 -1 1 1 -1 -1 1 -1 -1 1 1 -1 -1 1 1 1 1 1 -1 -1 -1 -1 1 -1 1 -1 -1 1 -1 1 1 1 -1 -1 -1 -1 1 1 1 -1 -1 1 -1 1 1 -1 ---------------------------------------------------------- Enter station no you want to listen to 5 Station 5 is idle, it didn't send any data

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.