frame_in = 20000;
frame_out = 160000;
SNR2 = 50;

a = out.URe.data;
b = out.VRe.data;
c = out.UIm.data;
d = out.VIm.data;

a = squeeze(a(1,:,:));
b = squeeze(b(1,:,:));
c = squeeze(c(1,:,:));
d = squeeze(d(1,:,:));

a = awgn(a(frame_in:frame_out),SNR2,'measured');
b = awgn(b(frame_in:frame_out),SNR2,'measured');
c = awgn(c(frame_in:frame_out),SNR2,'measured');
d = awgn(d(frame_in:frame_out),SNR2,'measured');                %period 20000 with shift 2500 


fi_re = acos(dot(a,b)/(norm(a)*norm(b)));
fi_deg_re = rad2deg(fi_re)

fi_im = acos(dot(c,d)/(norm(c)*norm(d)));
fi_deg_im = rad2deg(fi_im)

fi_deg = (fi_deg_re + fi_deg_im)/2

delta_t_lf = (1/5000 )*(fi_deg/360)
TDoA = (1/5000000)*(fi_deg/360)

figure(1)

plot([1:1:length(a)],b,[1:1:length(a)],a)
xlim([0,length(a)])
xlabel('n')
ylabel('S(n)')
title("LF signal output SNR 50dB/input SNR 50dB lowpass filter")
legend('diferrence signal shifted 90°','diferrence signal')

%error angle for 45 and 90 deg
%output noise
% a_45db_out = [50 20 15 10 5 -0.5 -5 -10 -20 -50];
% a_45_out = [45 45.55 46.73 49.95 57.59 70.32 79.9 86.23 89.49 89.7];
% a_90db_out = [50 20 15 10 5 -0.5 -5 -10 -20 -50];
% a_90_out = [90 89.97 90 90.03 90.08 89.97 90 89.98 90.02 90.1];
% 
% a_45db_in = [50 20 15 10 5 -0.5 -5 -10 -20 -50];
% a_45_in = [45 45.02 44.98 45 45.01 45.03 44.99 45.01 44.98 45.02];
% a_90db_in = [50 20 15 10 5 -0.5 -5 -10 -20 -50];
% a_90_in = [90.0013 89.97 90.01 90.04 90.05 89.98 90 89.96 90.01 90.009];

%error angle for 45 and 90 deg
%input noise and outpur noise
% figure(2)
% subplot(2,1,1)
% plot(a_45db_out,a_45_out,a_90db_out,a_90_out)
% xlim([-50,50])
% ylim([0,100])
% xlabel('db')
% ylabel('alpha_m°')
% title("Output noise angle deviation")
% legend('45°','90°')
% subplot(2,1,2)
% plot(a_45db_in,a_45_in,a_90db_in,a_90_in)
% xlim([-50,50])
% ylim([0,100])
% xlabel('db')
% ylabel('alpha_m°')
% title("Input noise angle deviation")
% legend('45°','90°')


