% sine wave 50 kHz and 100 kHz --> periods 0.02 and 0.01 s
t=[-0.4:0.00125:0.04];
s1=[];s2=[];
for i=1:size(t,2)
s1(i)=sin(2*pi*t(i)*50);
s2(i)=sin(2*pi*t(i)*100);
end
plotyy(t,s1,t,s2);
%We transmit so we corrupt s1 and s2, and say we transmit s1
% If there is no noise and no delay
Rs1=0;
Rs12=0;
Rs2=0;
for i=1:size(t,2)
Rs1=Rs1+s1(i)*s1(i); %autocorrelation function for s1 in tau=0 (tau is time difference related to the phase difference of the two waveforms (0 is reference)
Rs12=Rs12+s1(i)*s2(i); %Cross correlation function in t=0 between s1 and s2
% it equals Rs2s1
Rs2=Rs2+s2(i)*s2(i); %autocorrelation function for s2 in tau=0
end
stem([Rs1 Rs12 Rs2]); % to see that s1 and s2 have the high correlation point and in the middle the s12 correlation is 0
% The previous calculation will actually be in a loop and autocorrelation
% and cross-correlation is done for the shifted versions of the signals
% The smallest time interval I have is 0.00125s, so that means one sample
% shift in s1 values
for i=1:size(t,2)
corr_s1(i)=0;
corr_s12(i)=0;
end
for i=1:size(t,2)
for j=i:size(t,2)
corr_s1(i)=corr_s1(i)+s1(j)*s1(j-i+1); % shift of i-1 samples, Matlab index strats at 1...
corr_s12(i)=corr_s12(i)+s1(j)*s2(j-i+1);
end
end
plot(t,corr_s1,t,corr_s12);