Thursday 7 March 2013

CDMA code for 4 Users


The text below is the final code a simulation that can extract the original data of 4 separate users from the interference pattern of 64 simultaneous users. This is code was used for the previous post.

%This script runs a simulation of a 64 simultaneous user system where 4 

%users need to be extracted by using their unique spreading code. The
%script then plots graphs of original data, the spreading code for each of
%the 4 users, the interference pattern and the recovered signal. All
%contained within a figure.


%Declares the variables
Length =20;
Users =64; 
Index1 =7;
Index2 =20;
Index3 =30;
Index4 =43;
spread = 0;
V = [1,-1];
L = Users * Length;
%Generates a random noise pattern
noise =  V(randi([1,numel(V)],L,1));

W = [1];

%A loop that generates the Walsh-Hadamard matrix.
for n = 1 : log2(Users);
    W = [ W W ; W -W];
end

%A loop that spreads user signal with spreading code unique to each user
%and then combines the spreading signals of each user into an interference
%pattern
for x=0 : (Users-1);

    %Generates spreading code for each user based on their user number.
    code = W(x+1, :);

    %Generates a random set of data for each user to transmit.
    D1 = UserData(Length);

    %This will multiply the spreading code by individual bit of User Data.
    spread1=[code*D1(1),code*D1(2),code*D1(3),code*D1(4),code*D1(5),code*D1(6),code*D1(7),code*D1(8),code*D1(9),code*D1(10),code*D1(11),code*D1(12),code*D1(13),code*D1(14),code*D1(15),code*D1(16),code*D1(17),code*D1(18),code*D1(19),code*D1(20)];

    %Checks for user one.
    if x == Index1
        UserD1 = D1; 
        SpreadC1 = code;
    end
    %Checks for user two.
      if x == Index2
                UserD2 = D1;
                SpreadC2 = code;
      end 
      %Checks for user three.
           if x == Index3
                    UserD3 = D1;  
                    SpreadC3 = code;
           end     
           %Checks for user four.
               if x == Index4
                        UserD4 = D1; 
                        SpreadC4 = code;
               end

    %Adds spread signal to the interference pattern after each loop.           
    spread=spread+spread1;     
end

%Adds noise to the interference pattern.
spread=spread+noise

%Extracts the two pieces of user data from the interference pattern.
c1=despread(SpreadC1,spread);
c2=despread(SpreadC2,spread);
c3=despread(SpreadC3,spread);
c4=despread(SpreadC4,spread);

%These all declare the axis for the graphs.
Plot1X1=size(UserD1);
X1= [1:(Plot1X1(2))];

Plot1X2=size(UserD2);
X2= [1:(Plot1X2(2))];

Plot1X3=size(UserD3);
X3= [1:(Plot1X3(2))];

Plot1X4=size(UserD4);
X4= [1:(Plot1X4(2))];

Plot3X1=size(SpreadC1);
Plot3X1=Plot3X1(1)*Plot3X1(2);
X5= [1:(Plot3X1)];

Plot3X2=size(SpreadC2);
Plot3X2=Plot3X2(1)*Plot3X2(2);
X6= [1:(Plot3X2)];

Plot3X3=size(SpreadC3);
Plot3X3=Plot3X3(1)*Plot3X3(2);
X7= [1:(Plot3X3)];

Plot3X4=size(SpreadC4);
Plot3X4=Plot3X4(1)*Plot3X4(2);
X8= [1:(Plot3X4)];

Plot5X1=size(spread);
Plot5X1=Plot5X1(1)*Plot5X1(2);
X9= [1:(Plot5X1)];

Plot7X1=length(c1);
X10=[1:Plot7X1];

Plot7X2=length(c2);
X11=[1:Plot7X2];

Plot7X3=length(c3);
X12=[1:Plot7X3];

Plot7X4=length(c4);
X13=[1:Plot7X4];


%The following code plots 13 graphs in the form of a square wave plotted in 
%a single figure.
h1=subplot(5,4,1);plot(X1,UserD1)
h1=stairs(X1,UserD1);
grid 'on';
title('Data set 1 to be transmitted.')

h2=subplot(5,4,2);plot(X2,UserD2)
h2=stairs(X2,UserD2);
grid 'on';
title('Data set 2 to be transmitted.')

h3=subplot(5,4,3);plot(X3,UserD3)
h3=stairs(X3,UserD3);
grid 'on';
title('Data set 3 to be transmitted.')

h4=subplot(5,4,4);plot(X4,UserD4)
h4=stairs(X4,UserD4);
grid 'on';
title('Data set 4 to be transmitted.')

h5=subplot(5,4,5:6);plot(X5,SpreadC1)
h5=stairs(X5,SpreadC1);
grid 'on';
title('Spread data 1.')

h6=subplot(5,4,7:8);plot(X6,SpreadC2)
h6=stairs(X6,SpreadC2);
grid 'on';
title('Spread data 2.')

h7=subplot(5,4,9:10);plot(X7,SpreadC3)
h7=stairs(X7,SpreadC3);
grid 'on';
title('Spread data 3.')

h8=subplot(5,4,11:12);plot(X8,SpreadC4)
h8=stairs(X8,SpreadC4);
grid 'on';
title('Spread data 4.')

h9=subplot(5,4,13:16);plot(X9,spread)
h9=stairs(X9,spread);
grid 'on';
title('Transmitted signal including interference')

h10=subplot(5,4,17);plot(X10,c1)
h10=stairs(X10,c1);
grid 'on';
title('Despread Signal 1')

h11=subplot(5,4,18);plot(X11,c2)
h11=stairs(X11,c2);
grid 'on';
title('Despread Signal 2')

h12=subplot(5,4,19);plot(X12,c3)
h12=stairs(X12,c3);
grid 'on';
title('Despread Signal 3')

h13=subplot(5,4,20);plot(X13,c4)
h13=stairs(X13,c4);
grid 'on';
title('Despread Signal 4')


1 comment:

  1. Hi, some functions like UseData and despread are missing. Can you please provide them of just can give me a hint how can I write them?
    Thank you

    ReplyDelete