% cdma.m
%
% This file should get you *started* in seeing how the CDMA transmitter
% works.  This file may not be complete (I think it's close, but don't
% assume everything is here).
%
% It generates length-8 Walsh codes, modulates random data
% for a number of users, and adds their DSSS chips together into a 
% waveform.  Then the signal is upsampled and the spectrum of the
% signal is viewed at a number of points.
%
% 2003/05/21  Added PrintOn variable to speed up without printing figs  (BB)
% 2003/05/13  Written  (BB)


%----- Set a few things
PrintOn     = 0;      % Prints .tiff files of figures
NumSymbols  = 1000;   % Number of symbols in waveform
NumUsers    = 7;      % Max size is Size-1
Size        = 8;      % don't change, we're using length-8 Walsh codes


%----- Build up array of Walsh codes.  Each row is an orthogonal code.
%      Do not use row 1 because it is all zeros.  Access rows with the
%      following syntax:   H8(3,:)  is the third row or second code.

H  = [0 0 ; 0 1];
H2 = H;

H  = [H H ; H (1-H)];
H4 = H;

H  = [H H ; H (1-H)];
H8 = H;

% Change codes to +/-1
Codes = (H8 - (0.5)) * 2;

%----- Build up transmit waveform
for k=0:NumSymbols-1,
   data = (round(rand(1,Size)) -0.5)*2;

   temp = zeros(1,Size);
   for l=2:1+NumUsers,
      temp = temp + data(l) * Codes(l,:);
   end

   out(k*Size +1 : (k+1)*Size) = temp;
end


figure(1); clf;
plot(out, 'x');
title('Time-domain baseband transmit waveform');
if PrintOn  print -dtiff 1.tiff;  end

figure(2); clf;
plot(abs(fft(out)));
title('FFT of baseband transmit waveform (very noisy)');
if PrintOn  print -dtiff 2.tiff;  end

figure(3); clf;
psd(out);
title('Power Spectral Density (PSD) of baseband transmit waveform');
if PrintOn  print -dtiff 3.tiff;  end


%----- Upsampled versions of tx waveform
% Upsample 2x
LengthOut = length(out);

out2 = zeros(1, 2*LengthOut);
out2(2:2:2*LengthOut) = out;

figure(4); clf;
psd(out2);
title('Power Spectral Density (PSD) of 2x upsampled transmit waveform');
if PrintOn  print -dtiff 4.tiff;  end

% Upsample 2x
LengthOut2 = length(out2);

out4 = zeros(1, 2*LengthOut2);
out4(2:2:2*LengthOut2) = out2;

figure(5); clf;
psd(out4);
title('Power Spectral Density (PSD) of 4x upsampled transmit waveform');
if PrintOn  print -dtiff 5.tiff;  end

