# Java | CDMA (Code Division Multiple Access)

CDMA is a channelization protocol for Multiple Access, where information can be sent simultaneously through several transmitters over a single communication channel.

It is achieved in below steps:

- A signal is generated which extends over a wide bandwidth.
- The code which performs this action is called spreading code.
- Later on, a specific signal can be selected with a given code even in the presence of many other signals.

It is mainly used in mobile networks like 2G and 3G.

**How does CDMA work?**

To see how CDMA works, we have to understand orthogonal sequences (also known as chips).

Let N be the number of stations establishing multiple access over a common channel.

Then the properties of orthogonal sequences can be stated as follows:

- An orthogonal sequence can be thought of as a 1xN matrix.
**Eg:**[+1 -1 +1 -1] for N = 4. - Scalar multiplication and matrix addition rules follow as usual.
**Eg:**3**.**[+1 -1 +1 -1] = [+3 -3 +3 -3]**Eg:**[+1 -1 +1 -1] + [-1 -1 -1 -1] = [0 -2 0 -2] **Inner Product:**It is evaluated by multiplying two sequences element by element and then adding all elements of the resulting list.- Inner Product of a sequence with itself is equal to N

[+1 -1 +1 -1]**.**[+1 -1 +1 -1] = 1 + 1 + 1 + 1 = 4 - Inner Product of two distinct sequences is zero

[+1 -1 +1 -1]**.**[+1 +1 +1 +1] = 1-1+1-1 = 0

- Inner Product of a sequence with itself is equal to N

To generate valid orthogonal sequences, use a **Walsh Table** as follows:

**Rule 1:****Rule 2:**Where = Complement of (Replace +1 by -1 and -1 by +1)

**Example:**Each row of the matrix represents an orthogonal sequence.Hence we can construct sequences for N = . Now let’s take a look at how CDMA works by using orthogonal sequences.

**Procedure:**

- The station encodes its data bit as follows.
- +1 if bit = 1
- -1 if bit = 0
- no signal(interpreted as 0) if station is idle

- Each station is assigned a unique orthogonal sequence (code) which is N bit long for N stations
- Each station does a scalar multiplication of its encoded data bit and code sequence.
- The resulting sequence is then placed on the channel.
- Since the channel is common, amplitudes add up and hence resultant channel sequence is sum of sequences from all channels.
- If station 1 wants to listen to station 2, it multiplies (inner product) the channel sequence with code of station S2.
- The inner product is then divided by N to get data bit transmitted from station 2.

**Example: **Assume 4 stations S1, S2, S3, S4. We’ll use 4×4 Walsh Table to assign codes to them.

C1 = [+1 +1 +1 +1] C2 = [+1 -1 +1 -1] C3 = [+1 +1 -1 -1] C4 = [+1 -1 -1 +1]Let their data bits currently be: D1 = -1 D2 = -1 D3 = 0 (Silent) D4 = +1Resultant channel sequence= C1.D1 + C2.D2 + C3.D3 + C4.D4 = [-1 -1 -1 -1] + [-1 +1 -1 +1] + [0 0 0 0] + [+1 -1 -1 +1] = [-1 -1 -3 +1] Now suppose station 1 wants to listen to station 2. Inner Product = [-1 -1 -3 +1] x C2 = -1 + 1 - 3 - 1 = -4 Data bit that was sent = -4/4 = -1.

Below program illustrate implementation of a simple CDMA channel:

`// Java code illustrating a simple implementation of CDMA` ` ` `import` `java.util.*;` ` ` `public` `class` `CDMA {` ` ` ` ` `private` `int` `[][] wtable;` ` ` `private` `int` `[][] copy;` ` ` `private` `int` `[] channel_sequence;` ` ` ` ` `public` `void` `setUp(` `int` `[] data, ` `int` `num_stations)` ` ` `{` ` ` ` ` `wtable = ` `new` `int` `[num_stations][num_stations];` ` ` `copy = ` `new` `int` `[num_stations][num_stations];` ` ` ` ` `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++) {` ` ` ` ` `// Making a copy of walsh table` ` ` `// to be used later` ` ` `copy[i][j] = wtable[i][j]; ` ` ` ` ` `// each row in table is code for one station. ` ` ` `// So we multiply each row with station data` ` ` `wtable[i][j] *= data[i];` ` ` `}` ` ` `}` ` ` ` ` `channel_sequence = ` `new` `int` `[num_stations];` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < num_stations; i++) {` ` ` ` ` `for` `(` `int` `j = ` `0` `; j < num_stations; j++) {` ` ` `// Adding all sequences to get channel sequence` ` ` `channel_sequence[i] += wtable[j][i]; ` ` ` `}` ` ` `}` ` ` `}` ` ` ` ` `public` `void` `listenTo(` `int` `sourceStation, ` `int` `num_stations)` ` ` `{` ` ` `int` `innerProduct = ` `0` `;` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < num_stations; i++) {` ` ` ` ` `// multiply channel sequence and source station code` ` ` `innerProduct += copy[sourceStation][i] * channel_sequence[i];` ` ` `}` ` ` ` ` `System.out.println(` `"The data received is: "` `+ ` ` ` `(innerProduct / num_stations));` ` ` `}` ` ` ` ` `public` `int` `buildWalshTable(` `int` `len, ` `int` `i1, ` `int` `i2, ` `int` `j1, ` ` ` `int` `j2, ` `boolean` `isBar)` ` ` `{` ` ` `// len = size of matrix. (i1, j1), (i2, j2) are` ` ` `// starting and ending indices of wtable.` ` ` ` ` `// isBar represents whether we want to add simple entry` ` ` `// or complement(southeast submatrix) to wtable.` ` ` ` ` `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` `;` ` ` `}` ` ` ` ` `public` `void` `showWalshTable(` `int` `num_stations)` ` ` `{` ` ` ` ` `System.out.print(` `"\n"` `);` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < num_stations; i++) {` ` ` `for` `(` `int` `j = ` `0` `; j < num_stations; j++) {` ` ` `System.out.print(wtable[i][j] + ` `" "` `);` ` ` `}` ` ` `System.out.print(` `"\n"` `);` ` ` `}` ` ` `System.out.println(` `"-------------------------"` `);` ` ` `System.out.print(` `"\n"` `);` ` ` `}` ` ` ` ` `// Driver Code` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `int` `num_stations = ` `4` `;` ` ` ` ` `int` `[] data = ` `new` `int` `[num_stations];` ` ` ` ` `//data bits corresponding to each station` ` ` `data[` `0` `] = -` `1` `;` ` ` `data[` `1` `] = -` `1` `;` ` ` `data[` `2` `] = ` `0` `;` ` ` `data[` `3` `] = ` `1` `;` ` ` ` ` `CDMA channel = ` `new` `CDMA();` ` ` ` ` `channel.setUp(data, num_stations);` ` ` ` ` `// station you want to listen to` ` ` `int` `sourceStation = ` `3` `;` ` ` ` ` `channel.listenTo(sourceStation, num_stations);` ` ` `}` `}` |

**Output**:

1 1 1 1 1 -1 1 -1 1 1 -1 -1 1 -1 -1 1 The data received is: 1

**Advantages of CDMA**: Unlike other channelization schemes like FDMA or TDMA which divide the channel based on frequency or time slots, CDMA allows all stations to have access to the full bandwidth of the channel for the entire duration.