%--------------- Correlation -----------------
%
% Technical University of Sofia - MUSSPI lab course
% Created by Viktor Stoev
% October 2016
%----------------------------------------------------------




%% 3. 
close all;
clear all;

f1=200;
f2=500;
N=256;
Fs=10000;
Ts=1/Fs;
t=(0:Ts:(N-1)*Ts);

x1=sin(2*pi*f1*t);
x2=sin(2*pi*f2*t);

figure(2);
subplot(3,1,1), plot(t,x1), 
xlabel('t'), ylabel('x1(t)'), title('time discrete signal 200 Hz');
subplot(3,1,2), plot(t,x2), 
xlabel('t'), ylabel('x2(t)'), title('time discrete signal 500 Hz');   
y=xcorr(x1,x2);
subplot(3,1,3), plot(t,y(N:2*N-1)), 
ylabel('y(t)'), title('correlation signal y(t)');

%% 4. Radar simulation - transmit a signal with certain shape and receive the echo 

%%close all;
%%clear all;

f=2e06;
N=51;
Fs=100e06;
Ts=1/Fs;
t=(0:Ts:(N-1)*Ts);

x=sin(2*pi*f*t); % transmit signal 
x = [x, zeros(1,4*256)];

x1 = [zeros(1,128),1*x(1,1:end-128)] + 0.35*randn(size(x)); % received signal with noise

y = xcorr(x1,x,'coeff');

t1=(0:Ts:(size(x,2)-1)*Ts);

figure(3);
subplot(3,1,1),plot(t1,x), 
xlabel('t'), ylabel('x1(t)'), title('transmitted signal');
subplot(3,1,2),plot(t1,x1), 
xlabel('t'), ylabel('x1(t)'), title('received signal');
subplot(3,1,3),plot(t1,y(end/2:end)), 
xlabel('t'), ylabel('x1(t)'), title('correlation result');

