## Sunday, October 15, 2006

### CDMA

I developed this simulation just for fun and only in a few minutes:

#include
#include

#define NUMBER_OF_SEQUENCES 8
#define IS_ORTHOGONAL 0
#define IS_NOT_ORTHOGONAL -1

typedef struct {
int chip[NUMBER_OF_SEQUENCES];
} ChipSeq;

int Add(ChipSeq *X, ChipSeq* Y, ChipSeq *R)
{
int i;

for (i=0; i
chip[i] = X->chip[i] + Y->chip[i];

return 0;
}

int DotProduct(ChipSeq *X, ChipSeq *Y, ChipSeq *Res)
{
ChipSeq temp;
int i;

if (!X || !Y)
return -1;

for(i=0; i
chip[i] = X->chip[i] * Y->chip[i];
return 0;
}

void InitChip(ChipSeq *A, int initVal)
{
int i;

for(i=0; i
chip[i] = initVal;
}

int CheckOrthogonality(ChipSeq *X, ChipSeq *Y)
{
int i;
int sum = 0;

for (i=0; i
chip[i] * Y->chip[i];

if (sum/NUMBER_OF_SEQUENCES == 0)
return IS_ORTHOGONAL; // it is orthogonal
else
return IS_NOT_ORTHOGONAL;
}

int RecoverBit(ChipSeq *S, ChipSeq *SenderSeq)
{
int i;
int sum=0;

for(i=0; i
chip[i] * SenderSeq->chip[i];

return sum/NUMBER_OF_SEQUENCES;
}

int Negative(ChipSeq *A, ChipSeq *Comp)
{
int i;
for(i=0; i
chip[i] = -(A->chip[i]);
}

void PrintChipSeq(ChipSeq *X)
{
int i;

printf("[ ");
for (i=0; i
chip[i]);
printf("]");
}

void CopySeq(ChipSeq* src, ChipSeq* dest)
{
memcpy(&dest, &src, sizeof(ChipSeq));
}

void ConvertToBipolar(ChipSeq *A, ChipSeq *Bip)
{
int i;

for(i=0; i
chip[i] == 1)
Bip->chip[i] = 1;
if (A->chip[i] == 0)
Bip->chip[i] = -1;
}
}

int main(int argc, char *argv[])
{
ChipSeq A = { {0, 0, 0, 1, 1, 0, 1, 1}};
ChipSeq B = { {0, 0, 1, 0, 1, 1, 1, 0}};
ChipSeq C = { {0, 1, 0, 1, 1, 1, 0, 0}};
ChipSeq D = { {0, 1, 0, 0, 0, 0, 1, 0}};
ChipSeq temp, ba, bb, bc, bd, ca, cb, cc, cd, sum;

printf("A = ");
PrintChipSeq(&A);
printf("\n");
ConvertToBipolar(&A, &ba);
printf("Bipolar(A) = ");
PrintChipSeq(&ba);
printf("\n");

printf("B = ");
PrintChipSeq(&B);
printf("\n");
ConvertToBipolar(&B, &bb);

printf("C = ");
PrintChipSeq(&C);
printf("\n");
ConvertToBipolar(&C, &bc);

printf("D = ");
PrintChipSeq(&D);
printf("\n");
ConvertToBipolar(&C, &bd);

// A + B + C sends 0 bits
Negative(&ba, &ca);
printf("!A = "); PrintChipSeq(&ca); printf("\n");
Negative(&bb, &cb);
printf("!B = "); PrintChipSeq(&cb); printf("\n");
Negative(&bc, &cc);
printf("!C = "); PrintChipSeq(&cc); printf("\n");

printf("S = !A + !B + !C = ");
PrintChipSeq(∑);
printf("\n");

printf("Recover bit from A: S*A\n");
printf("Bit sent from A was: %d\n", RecoverBit(∑, &A));
printf("\n");

printf("Recover bit from B: S*B\n");
printf("Bit sent from B was: %d\n", RecoverBit(∑, &B));
printf("\n");

printf("Recover bit from C: S*C\n");
printf("Bit sent from C was: %d\n", RecoverBit(∑, &C));
printf("\n");

}