Sunday, October 15, 2006

CDMA (2)

Here is an improvement of the previous code. To simulate bit stream, I add a function to generate random number generator for each node.



#include <stdio.h>
#include <stdlib.h>

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

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


void PrintChipSeq(ChipSeq *X);

int Add(ChipSeq *X, ChipSeq* Y, ChipSeq *R)
{
int i;
for (i=0; ichip[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; ichip[i] = X->chip[i] * Y->chip[i];
return 0;
}

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

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


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

for (i=0; ichip[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; ichip[i] * SenderSeq->chip[i];

return (sum/NUMBER_OF_SEQUENCES <= 0 ? 0 : 1); } int Negative(ChipSeq *A, ChipSeq *Comp) { int i; for(i=0; ichip[i] = -(A->chip[i]);
}


void PrintChipSeq(ChipSeq *X)
{
int i;

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

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


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

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


void Send(int n, int bit[], ChipSeq node[], ChipSeq *S)
{
ChipSeq bp[N_NODES], temp;
int i,j;

if (n>N_NODES) return;
for(i=0; i if (bit[i]==1)
CopySeq(&node[i], &bp[i]);
else
Negative(&node[i], &bp[i]);
}
memset(S, 0, sizeof(ChipSeq));
for(i=0; i Add(S, &bp[i], S);
}
}


int GenerateRandom(void)
{
return random() % 2;
}


int main(int argc, char *argv[])
{
int i,j;
ChipSeq node[4] = { {{0, 0, 0, 1, 1, 0, 1, 1}},
{{0, 0, 1, 0, 1, 1, 1, 0}},
{{0, 1, 0, 1, 1, 1, 0, 0}},
{{0, 1, 0, 0, 0, 0, 1, 0}}
};
ChipSeq temp, b[N_NODES], s;
int bit[N_NODES];
time_t t;

for(i=0; i printf("Node[%u] = ", i);
PrintChipSeq(&node[i]);
printf("\n");
ConvertToBipolar(&node[i], &b[i]);
printf("Bipolar(node[%u]) = ", i);
PrintChipSeq(&b[i]);
printf("\n");
}

for (i=0; i for(j=i+1; j if (CheckOrthogonality(&b[i], &b[j])==IS_ORTHOGONAL)
printf("Node[%u] & node[%u] is orthogonal\n", i, j);
else {
printf("Node[%u] & node[%u] is NOT orthogonal\n", i, j);
exit(0);
}
}
}

time(&t);
srandom(t);
for (i=0; i<100; i++) {
bit[0] = GenerateRandom();
bit[1] = GenerateRandom();
bit[2] = GenerateRandom();
bit[3] = GenerateRandom();
printf("Send: A=%u B=%u C=%u D=%u\n", bit[0], bit[1], bit[2], bit[3]);
Send(N_NODES, bit, b, &s);
printf("S = !A + !B + !C + !D = ");
PrintChipSeq(&s);
printf("\n");

printf("Recover bit from A: S*A\n");
printf("Bit sent from A was: %d\n", RecoverBit(&s, &b[0]));
printf("\n");

printf("Recover bit from B: S*B\n");
printf("Bit sent from B was: %d\n", RecoverBit(&s, &b[1]));
printf("\n");

printf("Recover bit from C: S*C\n");
printf("Bit sent from C was: %d\n", RecoverBit(&s, &b[2]));
printf("\n");

printf("Recover bit from D: S*D\n");
printf("Bit sent from D was: %d\n", RecoverBit(&s, &b[3]));
printf("\n");
}
}

No comments:

Post a Comment