%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 all
clf
%CDMA illustration: Barker code of length 7
Nb=4; Nc=7; Nh=Nb*Nc; Np=Nh*2;
xw=zeros(1,Np/2);
bits1=[1 -1 1 -1];
barker1=[1 1 1 -1 -1 1 -1]; 
yd=kron(bits1,barker1);
xw(1,1:2:Np)=0:Nh-1; xw(1,2:2:Np)=1:Nh;
yw(1,1:2:Np)=yd; yw(1,2:2:Np)=yd;
%for l=1:Nb;
Nch=2*Nc;
l=1;
low=1+(l-1)*2*Nch;
plot(xw(low:low+Nch),yw(low:low+Nch),'k','Linewidth',3)
axis([0 Nh-1 -2 2]); 
title('Barker Code of length M=7:  1,1,1,-1,-1,1,-1')
xlabel('Bits = 1,-1,1,-1')
hold on;
l=2;
low=1+(l-1)*Nch;
plot(xw(low:low+Nch),yw(low:low+Nch),'b','Linewidth',3)
l=3;
low=1+(l-1)*Nch;
plot(xw(low:low+Nch),yw(low:low+Nch),'r','Linewidth',3)
l=4;
low=1+(l-1)*Nch;
plot(xw(low:low+Nch-1),yw(low:low+Nch-1),'g','Linewidth',3)
hold off
pause
ycor=conv(yd,barker1(Nc:-1:1));
plot((0:length(ycor)-1),ycor,'Linewidth',3)
title('Cross-Correlation with Barker Code')
xlabel('Bits = 1,-1,1,-1')
