%Matlab file used for class demo conducted during Session 3
%on the use of cross-correlation .
%A word utterance is effectively sampled at four different
%rates and played back to hear the effects of aliasing
set(0,'defaultaxesfontsize',20);
clear
clf
%synthesize the gold codes having a Kronecker-Delta like
%autocorrelation and low mutual cross-correlation
clength=127;
alength=2*clength-1;
lag=1-clength:clength-1;
code1=gold1(1);
code2=gold2(1);
code3=gold3(1);
%Plot autocorrelation of code 1, and cross-correlation
%between code 1 and code 2
plot(lag,conv(code1,code1(end:-1:1)),'Linewidth',3)
xlabel('lag value')
hold on
plot(lag,conv(code1,code2(end:-1:1)),'r','Linewidth',3)
legend('Autocorrelation of Code 1','Cross-correlation between Code 1 and Code 2')
hold off
pause
%Plot autocorrelation of code 2, and cross-correlation
%between code 2 and code 3
plot(lag,conv(code2,code2(end:-1:1)),'Linewidth',3)
xlabel('lag value')
hold on
plot(lag,conv(code2,code3(end:-1:1)),'r','Linewidth',3)
legend('Autocorrelation of Code 2','Cross-correlation between Code 2 and Code 3')
hold off
pause
%generate length 4 bit sequences for each of
%three different users:
%represent data bit "1" as amplitude 1
%represent data bit "0" as amplitude -1
bit1=[1 1 1 1];
bit2=[1 1 -1 -1];
bit3=[1 -1 1 -1];
%synthesize CDMA signal for each user and then
%sum (superimpose) the three users' signals
%do a help on kron.m to understand what it does
D2=13; D3=23;
x1=[kron(bit1,code1) zeros(1,D2) zeros(1,D3)];
x2=[zeros(1,D2) kron(bit2,code2) zeros(1,D3)];
x3=[zeros(1,D3) kron(bit3,code3) zeros(1,D2)];
x=x1+x2+x3;
%x=kron(bit1,code1)+kron(bit2,code2)+kron(bit3,code3);
plot(x,'Linewidth',3)
xlabel('Sum signal -- three users superimposed')
pause
y1=conv(x,code1(end:-1:1));
plot(y1,'Linewidth',3)
xlabel('cross-correlation with gold code 1 -- bits: 1,1,1,1')
pause
y2=conv(x,code2(end:-1:1));
plot(y2,'Linewidth',3)
xlabel('cross-correlation with gold code 2 -- bits: 1,1,-1,-1')
pause
y3=conv(x,code3(end:-1:1));
plot(y1,'b','Linewidth',3)
hold on
plot(y2,'r','Linewidth',3)
plot(y3,'m','Linewidth',3)
xlabel('cross-correlation with gold code 3 -- bits: 1,-1,1,-1')


